Home > Article > Web Front-end > Detailed explanation of HTML5+CSS3 application
This article mainly introduces the detailed explanation of HTML5 CSS3 application, which has certain reference value. Now I share it with you. Friends in need can refer to it.
Now, HTML5 and CSS3 are eagerly waiting for everyone. Let’s see if they can really take our designs to the next level
Web designers can do some cool stuff using HTML4 and CSS2.1. 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 filled with flashes, scrolls, and flashes.
Although we have now commonly used HTML4 and CSS2.1, 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 software based on 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
Copy code
The code is as follows:
<div class="section"> <div class="article"> <div class="header"> <h1>Div Soup Demonstration</h1> <p>Posted on July 11th, 2009</p> </div> <div 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> </div> <div class="footer"> <p>Tags: HMTL, code, demo</p> </div> </div> <div class="aside"> <div class="header"> <h1>Tangential Information</h1> </div> <div 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> </div> <div class="footer"> <p>Tags: HMTL, code, demo</p> </div> </div> </div> </div>
Although this is a bit reluctant, the above example can still illustrate that using HTML4 to code a complex design is still too bloated (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 those 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
Copy the code
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 replace a large number of meaningless e388a4556c0f65e1904146cc1a846bee tags with many more semantic structured code 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 become 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 {}
Copy code
The code is as follows:
div.section {} div.article {} div.header {} div.content {} div.footer {} div.aside {}
Let’s take a look at an example based on HTML5: section {}
Copy code
The code is as follows:
article {} header {} footer {} aside {}
This is progress, but there are still some issues that need to be resolved. 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 few advanced CSS selectors to target 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:
后代选择器:[CSS 2.1]: EF
兄弟选择器:[CSS 2.1]: E + F
子元素选择器:[CSS 2.1]: E > F
下面让我们来看看如何不使用class和id而完成对文档中的那些section元素的定位吧:
定位最外层的2f8332c8dcfd5c7dec030a070bf652c3元素
考虑到我们的例子并不是一套完整的HTML5代码,所以我们假定在6c04bd5ca3fcae76e30b72ad730ca86d元素下有个c787b9a589a3ece771e842a6176cf8e9元素与2f8332c8dcfd5c7dec030a070bf652c3元素是兄弟元素。这样的话,我们就可以向下面代码那样定位最外层的
2f8332c8dcfd5c7dec030a070bf652c3了:
复制代码
代码如下:
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属性的页面元素。
总结
我相信随着时间的推进和更多浏览器的支持,HTML5和CSS3将越来越受欢迎,它们将为web设计师们带来更无穷的能量,让我们的web前端更上一个台阶。
相关推荐:
The above is the detailed content of Detailed explanation of HTML5+CSS3 application. For more information, please follow other related articles on the PHP Chinese website!