Goal of this article:
1. Master several usages of attribute selectors in CSS3
Question:
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
Now let’s do the specific operation
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
Summary:
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!

You can make a garden variety anchor link () open up a new email. Let's take a little journey into this feature. It's pretty easy to use, but as with anything

Little confession here: when I first saw Netlify CMS at a glance, I thought: cool, maybe I'll try that someday when I'm exploring CMSs for a new project. Then

In December 2018, Microsoft announced that Edge would adopt Chromium, the open source project that powers Google Chrome. Many within the industry reacted with

I like Gutenberg, the new WordPress editor. I'm not oblivious to all the conversation around accessibility, UX, and readiness, but I know how hard it is to

Using for a menu may be an interesting idea, but perhaps not something to actually ship in production. See "More Details on "

With visual regression testing, we can update a page, take screenshots before and after the fact, and compare the results for unintended changes. In this article, learn how to set up visual regression testing using Playwright.

CSS Houdini may be the most exciting development in CSS. Houdini is comprised of a number of separate APIs, each shipping to browsers separately, and some


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment