search
HomeWeb Front-endCSS TutorialSeveral uses of CSS3 attribute selectors (code examples)

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

Several uses of CSS3 attribute selectors (code examples)

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:

Several uses of CSS3 attribute selectors (code examples)

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
All About mailto: LinksAll About mailto: LinksApr 22, 2025 am 11:04 AM

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

It's pretty cool how Netlify CMS works with any flat file site generatorIt's pretty cool how Netlify CMS works with any flat file site generatorApr 22, 2025 am 11:03 AM

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

Testing for Visual Regressions with PercyTesting for Visual Regressions with PercyApr 22, 2025 am 11:02 AM

It’s a Herculean task to test

Edge Goes Chromium: What Does it Mean for Front-End Developers?Edge Goes Chromium: What Does it Mean for Front-End Developers?Apr 22, 2025 am 10:58 AM

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

A Gutenburg-Powered NewsletterA Gutenburg-Powered NewsletterApr 22, 2025 am 10:57 AM

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 Menus and Dialogs is an Interesting IdeaUsing for Menus and Dialogs is an Interesting IdeaApr 22, 2025 am 10:56 AM

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

Automated Visual Regression Testing With PlaywrightAutomated Visual Regression Testing With PlaywrightApr 22, 2025 am 10:54 AM

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 Could Change the Way We Write and Manage CSSCSS Houdini Could Change the Way We Write and Manage CSSApr 22, 2025 am 10:45 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

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

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

Dreamweaver CS6

Visual web development tools

DVWA

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment