Home >Web Front-end >H5 Tutorial >Example tutorial on the combined use of H5 and CSS3
Now, HTML5 and CSS3 are eagerly waiting for everyone. Let’s see if they can really bring our design to the next level.
Web designers can use HTML4 and CSS2. 1 Finished something cool. We can complete the logical structure of documents and create content-rich websites without using the old table-based layout. We can add beautiful and subtle styling to our website without using inline 240cb830ca84ebaabbd07850110b414d and 0c6dc11e160d3b678d68754cc175188a tags. In fact, our current design capabilities have taken us far away from that terrible era of browser wars, proprietary protocols, and those ugly web pages full of flashes, scrolls, and flashes.
Although we have commonly used HTML4 and CSS2.1 now, we can do better! We can restructure our code and make our page code more semantic. We can reduce the amount of styling code that gives pages a beautiful look and make them more scalable. Now, HTML5 and CSS3 are eagerly waiting for everyone. Let’s see if they can really bring our design to the next level...
In the past, designers often used the The table has no semantic layout. But in the end, thanks to innovators like Jeffrey Zeldman and Eric Meyer, smart designers slowly accepted the relatively more semantic e388a4556c0f65e1904146cc1a846bee layout instead of the table layout, and began to call external style sheets. But unfortunately, complex web design requires a lot of different tag structure code, we call it "e388a4556c0f65e1904146cc1a846bee-soup" syndrome. Maybe you are familiar with the following code: 1e9cef3dee283b0faa4bd5e876889dab
The code is as follows:
<p class="section"> <p class="article"> <p class="header"> <h1>p Soup Demonstration</h1> <p>Posted on July 11th, 2009</p> </p> <p class="content"> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> </p> <p class="footer"> <p>Tags: HMTL, code, demo</p> </p> </p> <p class="aside"> <p class="header"> <h1>Tangential Information</h1> </p> <p class="content"> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> </p> <p class="footer"> <p>Tags: HMTL, code, demo</p> </p> </p> </p> </p>
Although this is a bit reluctant, the above example can still illustrate the use of HTML4 for a Complex designs are still too bloated after being coded (in fact, xHTML1.1 is nothing more than that). But what’s exciting is that HTML5 solves the “e388a4556c0f65e1904146cc1a846bee-soup” syndrome and gives us a new set of structural elements. These new HTML5 elements have more detailed semantics to replace the meaningless e388a4556c0f65e1904146cc1a846bee tags, and at the same time provide "natural" CSS hooks for CSS calls.
The following is an example of an HTML5 solution: 2f8332c8dcfd5c7dec030a070bf652c3
The code is as follows:
<section> <article> <header> <h1>p Soup Demonstration</h1> <p>Posted on July 11th, 2009</p> </header> <section> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> </section> <footer> <p>Tags: HMTL, code, demo</p> </footer> </article> <aside> <header> <h1>Tangential Information</h1> </header> <section> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> <p>Lorem ipsum text blah blah blah.</p> </section> <footer> <p>Tags: HMTL, code, demo</p> </footer> </aside> </section> </section>
As we can see, HTML5 allows us to use many more semantic structures Use code tags to replace a large number of meaningless e388a4556c0f65e1904146cc1a846bee tags. This semantic feature not only improves the quality and semantics of our web pages, but also greatly reduces the class and id attributes that must be called for CSS in the code. In fact, CSS3 also allows us to ignore all classes and ids.
Say goodbye to class attributes and welcome neat tags
Combined with HTML5 rich in new semantic tags, CSS3 provides web designers with magic for their web pages. General strength. With the power of HTML5, we will get more control over the document code. With the power of CSS3, our control will be infinite!
Even without those advanced CSS selectors, we can still call different containers through powerful HTML5 clauses without bothering with attributes such as class and id. Like the previous p layout, we may have to call it like this in css: p#news {}
The code is as follows:
p.section {} p.article {} p.header {} p.content {} p.footer {} p.aside {}
Let’s take a look at an example based on HTML5: section {}
The code is as follows:
article {} header {} footer {} aside {}
This is progress, but there are still some problems that need to be solved. In the e388a4556c0f65e1904146cc1a846bee example, we need to call the element in the page through the class or id attribute. This logic will allow us to apply styles to any element in the document, whether as a whole or individually. For example, in the e388a4556c0f65e1904146cc1a846bee example, the .section and .content elements are easy to locate. But in the HTML5 example, there will be many section elements in the actual document. Actually we could add some specific attribute selectors to call those different section elements, but thankfully I don't have a handful of advanced CSS selectors to target the different section elements now.
Locating HTML-5 elements without using class and id
Let’s take a look at how to locate an instance of HTML5 page elements without using class and id. We can use three A CSS selector to locate and identify elements within an instance. As follows:
Descendant selector: [CSS 2.1]: EF
Sibling selector: [CSS 2.1]: E + F
Child element selector: [CSS 2.1]: E > F
Let’s take a look at how to locate the section elements in the document without using class and id:
Locate the outermost 2f8332c8dcfd5c7dec030a070bf652c3 element
Considering that our example is not a complete set of HTML5 code, we assume that there is a c787b9a589a3ece771e842a6176cf8e9 element under the 6c04bd5ca3fcae76e30b72ad730ca86d element and the 2f8332c8dcfd5c7dec030a070bf652c3 element is a sibling element. In this case, we can locate the outermost
2f8332c8dcfd5c7dec030a070bf652c3 as in the following code:
The code is as follows:
body nav+section {}
定位下一个2f8332c8dcfd5c7dec030a070bf652c3元素
作为最外层2f8332c8dcfd5c7dec030a070bf652c3元素下的唯一直属子集元素,这个2f8332c8dcfd5c7dec030a070bf652c3元素也许可以这样定位:
代码如下:
section>section {}
定位23c3de37f2f9ebcb477c4a90aac6fffd元素
可以定位23c3de37f2f9ebcb477c4a90aac6fffd元素的方法有很多,不过最简单的方法当然就是后代选择器了:
代码如下:
section section article {}
定位1aa9e5d373740b65a0cc8f0a02150c53、2f8332c8dcfd5c7dec030a070bf652c3 和c37f8231a37e88427e62669260f0074d元素
这三个元素分别在两个地方都出现过,一是在23c3de37f2f9ebcb477c4a90aac6fffd元素中出现,另一是在15221ee8cba27fc1d7a26c47a001eb9b元素中出现。这种差别能让我们轻松定位每个元素。
代码如下:
article header {} article section {} article footer {}
或者一起定义:
代码如下:
section section header {} section section section {} section section footer {}
到目前为止,我们已经使用CSS2.1选择器排除掉了所有的class和id。那么我们为什么还需要使用CSS3 呢?我很高兴你能这么问…
使用CSS3对HTML5元素进行高级定位
虽然我们已经使用CSS2.1选择器排除掉了所有的class和id,显然还会有很多更复杂的情况需要CSS3的高级选择器来解决。让我们通过完成一下的实例来了解一下如何在不使用无用的class和id属性的情况下利用CSS3定位页面元素。
使用一个唯一的日志 (post)ID定位所有日志
wordpress提供给我们一种包含了ID的每篇日志的源代码输出。这种信息通常用于导航和/或了解资料的意图,不过CSS3可以利用这些唯一的ID来定义这些日志的样式。当然,你还可以像往常那样为每篇日志添加class=”post”这样的属性,但这就与我们练习的意图相冲突了(再加上它没有一点乐趣所在)。使用”子字符串匹配选择器”,我们就可以像下面这样定位所有日志和它们的不同元素了。
代码如下:
article[id*=post-] {} /* 定位所有日志 */ article[id*=post-] header h1 {} /* 定位所有日志中的h1标签 */ article[id*=post-] section p {} /* 定位所有日志中的p标签 */
我们仍然可以使用同样的方式定位评论的元素和它们的子元素。
代码如下:
article[id*=comment-] {} /* 定位所有评论 */ article[id*=comment-] header h1 {} /* 定位所有评论中的h1标签 */ article[id*=comment-] section p {} /* 定位所有评论中的p标签 */
定位一些指定的区域(section)或文章(article)
有很多博客的日志量和评论量都相当大,HTML 5 会将它们由2f8332c8dcfd5c7dec030a070bf652c3或23c3de37f2f9ebcb477c4a90aac6fffd元素组成。为了定位哪些指定的2f8332c8dcfd5c7dec030a070bf652c3 或23c3de37f2f9ebcb477c4a90aac6fffd元素,我们就要转而使用强大的“:nth-child”选择器了:
代码如下:
section:nth-child(1) {} /* 选择第一个 <section> */ article:nth-child(1) {} /* 选择第一个 <article> */ section:nth-child(2) {} /* 选择第二个 <section> */ article:nth-child(2) {} /* 选择第二个 <article> */
同样,我们可以使用“:nth-last-child”选择器定位反序的一些元素。
代码如下:
section:nth-last-child(1) {} /* 选择最后一个 <section> */ article:nth-last-child(1) {} /* 选择最后一个 <article> */ </p> <p>section:nth-last-child(2) {} /* 选择倒数第二个 <section> */ article:nth-last-child(2) {} /* 选择倒数第二个 <article> */
使用更多的方式选择指定元素
另一种选择HTML5中指定元素(如 header、section和footer)的方法就是利用”:only-of-type”选择器的优势。由于这些HTML5元素通常会在很多地方出现不止一次,所以当我们想定位那种在父元素下仅出现过一次的标签时这种方法很方便。例如,我们要选择的是在某元素中有切仅有的唯一一个元素,如以下代码:
代码如下:
<section> <section></section> <section> <section>定位这个section元素</section> </section> <section> <section>定位这个section元素</section> </section> <section> <section>但不定位这个section元素</section> <section>和这个section元素</section> </section> <section></section> </section>
我们可以仅使用以下一行选择器:
代码如下:
section>section:only-of-type {}
再次唠叨,你可以固执的为每个元素添加ID属性,但你会失去代码的可扩展性、维护性和绝对简洁的结构与表现相分离。 CSS3的确能让我们可快速更方便的定位几乎所有没有ID和class属性的页面元素。
总结
【相关推荐】
1. HTML5免费视频教程
5. 关于H5的事件属性详解
The above is the detailed content of Example tutorial on the combined use of H5 and CSS3. For more information, please follow other related articles on the PHP Chinese website!