search
HomeWeb Front-endCSS TutorialHow to use selectors to insert required content into the page in css3 (code attached)

The content of this article is about how to use CSS3 selectors to insert the required content into the page. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1 Use a selector to insert content

Use the after or before selector to define the content to be inserted in the content attribute of the selector. When the inserted content is text When inserting text, you must add single or double quotes on both sides of the inserted text.

<style type="text/css">
h2:before{  content:‘COLUMN’;}
</style>

2 Specify individual elements not to be inserted

Use the none attribute value of the content attribute

<style type="text/css">
h2.sample:before{  content:none;}
</style>

3 Insert image files

<style type="text/css">
h2:before{  content:url(mark.png);}
</style>

4 Display the value of the alt attribute as the title of the image

By specifying the form of "attr (attribute name)" in the content attribute, you can The attribute values ​​of each attribute are displayed.

img:after{
content:attr(alt);
display:block;
text-align:center;
}

5 Use the content attribute to insert the item number

(1) Add consecutive numbers before multiple titles

1. In the content attribute Use the counter attribute value to append consecutive numbers to multiple items

: before{ content: counter (counter name);} (Use a counter to calculate the number, and the counter can be named arbitrarily)

2. You also need to add the specification of the counter-increment attribute of the element in the element's style. In order to use continuous numbering, you need to set the attribute value of the counter-increment attribute to the before selector or after selector. The counter name specified in the counter attribute value.

{ counter-increment: the counter name specified in the before selector or after selector}

h1:before{content:counter(mycounter);}
h1{counter-increment:mycounter;}//1、2、3.......

(2) Append text to the item number

h1:before{content:‘第’counter(mycounter)‘章’;}//第1章大标题、第2章大标题.........

(3) Specify the type of number

1. Using the content attribute of the before selector or after selector, you can add not only numeric numbers, but also alphabetical numbers or Roman numeral numbers.

content: counter (counter name, numbering type)

2. You can use the value of the list-style-type attribute to specify the numbering type. For example, when numbering in uppercase letters, use "upper- alpha" attribute, use the "upper-roman" attribute when specifying uppercase Roman letters.

h1:before{content:counter(mycounter,upper-alpha)‘.’;}// A.、B.、C.、

(4) Number nesting

h1:before{content:counter(mycounter);}//1(1、2、3...)、2(1、2、3...)、........
h1{counter-increment:mycounter;counter-reset:subcounter;}(将中编号进行重置)
h2:before{content:counter(subcounter);}
h2{counter-increment:subcounter;margin-left:40px}

(5) Embed large numbers in middle numbers

h2:before{content:counter(mycounter)‘-’ counter(subcounter)‘.’;}//1-1、1-2

(6) Add nested text symbols on both sides of the string

1. You can use the open-quote attribute value and close-quote attribute value of the content attribute to add nested text symbols such as brackets, single quotes, and double quotes on both sides of the string.

2. The open-quote attribute value is used to add the beginning of the nested text symbol, and the close-quote attribute value is used to add the end of the nested text symbol.

3. Use the quotes attribute in the style of the element to specify what nested text symbols to use

h1:before{  content:open-quote; }
h1:after { content:close-quote; }
h1{   quotes:"(" ")"   }//形如:   (标题)

Related recommendations:

CSS3 Series 1 (Overview, Selection , use selectors to insert content)_html/css_WEB-ITnose

How to insert content into the page in css3

The above is the detailed content of How to use selectors to insert required content into the page in css3 (code attached). 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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools