1. Master several usages of attribute selectors in CSS3
1. It is required to use div css to achieve the following effects, but css is required All attribute selectors are used in the file to set element styles
Additional notes:
1. The entire div width is 850, and it is required to be displayed in the center of the page
2. Title size is 28, bold display
3. Other font size is 14px
4. Click on celebrity gossip to jump to tobagua.html, click on military news to jump Go to toaffairs.html
1. Because this case does not require some additional materials, the step of preparing the materials can be omitted
2. Create index.html and write the architecture. How to analyze the architecture?
Idea analysis:
1. The goal is divided into two parts: left and right. Each section displays a list of news, but the category of each news is different, and some styles are also different
2. The color of news containing Lin Xinru must be set separately
3. The color of news containing f15 The color of the news must also be set separately
4. The titles of the two parts must also be set separately, so some attributes must be set separately for them, so that they can be matched through the attribute selector
5 . Because each part is a list, we can use ul, but both uls are flush, so we need to use float. Since it is float, in order to ensure that the outer container can still wrap the floating elements , so finally you need to add a clear element to clear the float
According to the analysis, we get the following code
<!doctype html> <html> <head> <meta charset="utf-8"> <title>属性选择器</title> </head> <body> <div container="true"> <div left="true"> <span title="true"><a href="tobagua.html">明星八卦</a></span> <ul> <li class="news lxr">->林心如女儿小海豚正面照被公开颜值撞脸年轻时的霍建华,太美了 </li> <li class="news">->倪萍还是胖点好,瘦下来后太显老了,一点精神没有穿衣也不好看! </li> <li class="news">->汪小菲晒儿女沙滩上顽皮玩耍,儿子长高不少,女儿一身泥被指酷似奶奶张兰 </li> <li class="news">->伊能静给婆婆安排了个小次卧, 走进秦妈妈的表情变化让人心疼! </li> </ul> </div> <div right="true"> <span title="true"><a href="toaffairs.html">军事新闻</a></span> <ul> <li class="affairs news f15">->F15E成为首型接收B61-12核弹的战斗机 </li> <li class="affairs news">->越南的骑兵警队,马略矮了点啊 </li> <li class="affairs news">->美军运输机降落时冲出跑道后撞墙起火 4人受伤 </li> <li class="affairs news">->不到一个月又出事:美军F-35降落时起落架折断 </li> </ul> </div> <div class="clear"> </div> </div> </body> </html>
3. Write the style, create a css folder, and create a new index.css in it , how to write the styles inside, the following is the analysis idea
Idea analysis:
all sub-elements of the container container
1. Because each style must use an attribute selector to match the setting, our idea is to first obtain the element with the attribute container=true, and then set some common styles for it, such as the most common padding and margin, because this is not the case When setting, you may not know the default padding of some elements. In order to allow you to concentrate on writing logic, we have unified their padding and margin to 0
So add the following code to index.css:
div[container =true] *{ padding:0; margin:0; }
container container
1. According to the requirements, the width of the outermost container is 850, it should be centered, there is padding on the top, bottom, left and right, with a gray border, and the default font size is 14px. Which is the container container? It is the element with container attribute = true.
So add the following code to index.css:
div[container =true] { width: 850px; margin: 0 auto; font-size: 14px; border: 1px solid lightgray; padding: 10px; }
2 title public style settings
1. Because the titles are required to be centered, with a font size of 28px and bold display, which ones are titles, that is, all elements with the attribute title=true
So add the following code to index.css:
span[title=true] { text-align: center; font-size: 28px; font-weight: bold; }
Separate settings for 2 title connections
1. The color of the gossip title is red
2. The color of the military title is blue
3. The href attribute containing bagua string is the title connection on the left. The href attribute starting with toaffairs is the title connection on the right.
*= means inclusion, ^= means the beginning
So add the following code to index.css:
a[href *=bagua] { color: rgb(234, 84, 23); } a[href ^=toaffairs] { color: green; }
Left and right floating div settings
1. The left div needs to float to the left, so which one is the left div? Actually It is the div with the left attribute as true
2. The right div needs to float to the right, so which div is the right div? In fact, it is the div with the right attribute as true
3. The function is to clear the float The div is actually the one whose class ends with clear
Note: $= indicates what it ends with
So add the following code to index.css:
div[left =true] { float: left; } div[right =true] { float: right; } div[class $= clear] { clear: both; float: none; width: 0; height: 0; }
li settings
1. li does not contain black dots, so list-style:none
2. The element whose attribute class contains the news string is li
3. According to the effect, there is a certain spacing between the top, bottom, left and right
So add the following code to index.css:
li[class =news] { list-style: none; height: 42px; line-height: 42px; padding:3px 10px; }
Contains Ruby Lin’s title settings
1. The color of this title is earthy yellow and the font is bold
2. The class attribute ending with lxr is Ruby Lin’s title, so use $=
[class $=lxr]{ color:peru; font-weight: bold; }
Title setting containing f15
1. The title color is blue and the font is bold
2. The title whose class attribute contains the f15 string is the title for the target. So use *=
[class*=f15]{ color:blue; font-weight: bold; }
So far, the entire content of index.css is as follows:
div[container =true] *{ padding:0; margin:0; } div[container =true] { width: 850px; margin: 0 auto; font-size: 14px; border: 1px solid lightgray; padding: 10px; } span[title=true] { text-align: center; font-size: 28px; font-weight: bold; } a[href *=bagua] { color: rgb(234, 84, 23); } a[href ^=toaffairs] { color: green; } div[left =true] { float: left; } div[right =true] { float: right; } div[class $= clear] { clear: both; float: none; width: 0; height: 0; } li[class *=news] { list-style: none; height: 42px; line-height: 42px; padding:3px 10px; } [class $=lxr]{ color:peru; font-weight: bold; } [class*=f15]{ color:blue; font-weight: bold; }
Then introduce index.css into index.html
<!doctype html> <html> <head> <meta charset="utf-8"> <title>属性选择器</title> <link rel="stylesheet" type="text/css" href="css/index.css" /> </head> <body> <div container="true"> <div left="true"> <span title="true"><a href="tobagua.html">明星八卦</a></span> <ul> <li class="news lxr">->林心如女儿小海豚正面照被公开颜值撞脸年轻时的霍建华,太美了 </li> <li class="news">->倪萍还是胖点好,瘦下来后太显老了,一点精神没有穿衣也不好看! </li> <li class="news">->汪小菲晒儿女沙滩上顽皮玩耍,儿子长高不少,女儿一身泥被指酷似奶奶张兰 </li> <li class="news">->伊能静给婆婆安排了个小次卧, 走进秦妈妈的表情变化让人心疼! </li> </ul> </div> <div right="true"> <span title="true"><a href="toaffairs.html">军事新闻</a></span> <ul> <li class="affairs news f15">->F15E成为首型接收B61-12核弹的战斗机 </li> <li class="affairs news">->越南的骑兵警队,马略矮了点啊 </li> <li class="affairs news">->美军运输机降落时冲出跑道后撞墙起火 4人受伤 </li> <li class="affairs news">->不到一个月又出事:美军F-35降落时起落架折断 </li> </ul> </div> <div class="clear"> </div> </div> </body> </html>
and run The result is:
So far, the required results have been achieved
1. Explained several uses of attribute selectors , for example
A=B means that the A attribute value is equal to the B string
A*=B means that the A attribute value contains the B string
A$=B Indicates that the A attribute value ends with the B string
A^=B indicates that the A attribute value starts with the B string
The above is the detailed content of Several uses of CSS3 attribute selectors (code examples). For more information, please follow other related articles on the PHP Chinese website!