Table of Contents
Goal of this article:
Question:
Now let’s do the specific operation
Home Web Front-end CSS Tutorial Several uses of CSS3 attribute selectors (code examples)

Several uses of CSS3 attribute selectors (code examples)

Jun 09, 2020 pm 06:05 PM
css3

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>
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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>
Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

How to enlarge the image by clicking the mouse in css3 How to enlarge the image by clicking the mouse in css3 Apr 25, 2022 pm 04:52 PM

Implementation method: 1. Use the ":active" selector to select the state of the mouse click on the picture; 2. Use the transform attribute and scale() function to achieve the picture magnification effect, the syntax "img:active {transform: scale(x-axis magnification, y Axis magnification);}".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

How to set animation rotation speed in css3 How to set animation rotation speed in css3 Apr 28, 2022 pm 04:32 PM

In CSS3, you can use the "animation-timing-function" attribute to set the animation rotation speed. This attribute is used to specify how the animation will complete a cycle and set the speed curve of the animation. The syntax is "element {animation-timing-function: speed attribute value;}".

Does css3 animation effect have deformation? Does css3 animation effect have deformation? Apr 28, 2022 pm 02:20 PM

The animation effect in css3 has deformation; you can use "animation: animation attribute @keyframes ..{..{transform: transformation attribute}}" to achieve deformation animation effect. The animation attribute is used to set the animation style, and the transform attribute is used to set the deformation style. .

See all articles