search
HomeWeb Front-endHTML TutorialCSS3 series one (overview, selectors, using selectors to insert content)_html/css_WEB-ITnose

CSS3 modular structure

Historical development of CSS

CSS (Cascading Style Sheet), cascading style sheet , a markup language used to control the style of web pages and allow the separation of style information from web content.

CSS3 attribute selector

  1. E[attr=val] means having attribute att and its value is equal to val
  2. E[attr*=val] means it has attribute attr and its value contains val
  3. E[attr^=val] means it has attribute attr and its value starts with val
  4. E[ attr$=val] means it has attribute attr and its value ends with val

In fact, in addition to the above four, there are two relatively less used attribute selectors, that is

E[attr|=val] is used to select elements that have attribute attr and the value of the attribute is val or starts with val- (where - is indispensable).

a[lang|=zh]{do something} //将会选择具有lang属性值为zh或属性值以zh-开头的a标签

E[attr~=val] When an attribute of an element has multiple attribute values ​​separated by spaces, use this E[attr~=val] As long as one of the multiple attribute values ​​​​of the attr attribute is equal to val, the matching element will be selected.

<a href="" id="test" title="test first"></a> a[title~=test]{do something} //将会选择具有title属性且其中一个属性值为test的a标签

Universal pseudo-class selector

  1. E: first-line: used for the first line of text in an element Style
  2. E:first-letter is used to use style for the first letter (European and American text) or the first character (Chinese or Japanese) of the text in an element
  3. E:before Used to insert some content before an element
  4. E:after is used to insert some content after an element

CSS3 structural pseudo-class selector

  1. : The root selector binds the style to the root element of the page. The so-called root element refers to the element located at the top level of the document tree. In an HTML page, it refers to the entire page. The part
  2. E:not want to exclude the substructure elements below this structural element
  3. E:empty specifies the style used when the element content is blank
  4. E:target specifies a style for a target element on the page. This style only takes effect after the user clicks a hyperlink on the page and jumps to the target element
  5. E:first-child is used to select a specific The first child element of the element
  6. E:last-child is used to select the last child element of a specific element.
  7. E:nth-child is used to select one or more specific child elements of a parent element, where n can be a numerical value (starting from 1), or an expression containing n, or It can be odd (odd number) or even (even number).
  8. E:nth-last-child Its usage is the same as nth-child, but the element selected by nth-last-child() starts from the last child element of the parent element.
  9. E:nth-of-type only counts child elements of a certain type specified in the parent element (when an element has more than one child element type, using nth-of-type to select will More useful)
  10. E:nth-last-of-type Its usage is the same as nth-of-type, but the difference is: nth-last-of-type() also starts from the last element of the parent element Counting begins with child elements.
  11. E:only-child matches the element whose parent element has only one child element

CSS3 UI element status pseudo-class selector

  1. E:hover{do something} //Select the matching E element where the mouse is hovering
  2. E:active{do something} //Select the matching E element and the element is activated , commonly used on anchors and buttons
  3. E:link{do something} //Select an element that has a hyperlink defined but the link has not been visited yet
  4. E:visited{do something} //Select the element with a hyperlink defined and the link has been visited
  5. E:focus{do something} //Select the matching E element, and the element gets focus
  6. E:enabled{ do something} //Select the matching E element, and the element is in the available state
  7. E:disabled{do something} //Select the matching E element, and the element is in the disabled state
  8. E:read-only{do something} //Used to specify the style when the element is in the read-only state
  9. E:read-write{do something} //Used to specify the style when the element is in the non-read-only state Style when in state
  10. E:checked{do something} //Used to specify the style when the radio radio button or checkbox in the form is in the selected state
  11. E:default {do something} //Used to specify the style of the radio button or check control that is in the selected state by default when the page is opened (even if the user sets the selected state of the check box or check box control to the non-selected state E :The style specified in the default selector is still valid)
  12. E:indeterminate{do something} //Used to specify that when the page is opened, if any radio button in a group of radio button boxes is not set The style of the entire set of radio button boxes when it is set to the selected state (if the user selects any one of the radio button boxes, the style is unspecified)
  13. E::selection is used to specify when the element is in the selected state Style

CSS3 hierarchical relationship selector

Descendant selector "E F" selects matching E All elements within the element that match F.

子选择器「E > F」 选择配配E的元素的匹配F的直系子元素。

相邻兄弟元素选择器「E + F」E和F是同辈元素,具有相同的父元素,并且F元素紧邻在E元素的后面,此时可以使用相邻兄弟选择器。(也就是说只会选择紧接着E元素的第一个F元素)

通用兄弟选择器「E ~ F」E和F是同辈元素,具有相同的父元素,并且F元素在E元素之后,E ~ F将选中E元素后面的所有F元素。

使用选择器来插入内容

        h1:before {            content: 'MYTEXT';        }        h2::after {            content: none;        }        h2::before {            content: normal;            /*            虽然normal和none属性值都表示不插入任何内容            但normal比none使用更广泛,            换句话的意思就是说none属性值只能应用到这两个选择器中,而normal属性值还可以应用到其它用来插入内容的选择器中            */        }        h3:before {           content:url(logo.png);        }        h3 {            background-image:url(logo.png);            /*            虽然两种方法都可以插入图像,但是使用background-image方法时,如果在打印的时候设定了不打印背景就不能正常打印图像了            但使用before选择器追加的图像文件却可以正常打印            */        }        img::after {            content:attr(alt);/*将alt属性的值作为图像的标题来显示*/        }

上面的几个用法可能都会,但下面的用法相信用的相对会比较少,但却很实用哦!

    <style type="text/css">        h1:before {            content: counter(mycounter,upper-alpha)'.';            /*后面的.可以为其指定样式 比如说color:blue                 upper-alpha为大写罗马字母 这里面的值可以是list-style-type中的任意一种             */            color: blue;            font-size: 42px;        }        h1 {            counter-increment: mycounter;        }    </style>    <h1 id="大标题">大标题</h1>    <p>示例文字</p>    <h1 id="大标题">大标题</h1>    <p>示例文字</p>    <h1 id="大标题">大标题</h1>    <p>示例文字</p>

界面显示如下图所示:(也就是说使用counter属性值来针对多个项目追加连续编号)

再来看一个相对上面稍微复杂一点的,也就是编号多层嵌套的例子

 <style type="text/css">        h1:before {            content: counter(mycounter)'.';            color: blue;        }        h1 {            counter-increment: mycounter;            counter-reset: subcounter;        }        h2:before {            content: counter(mycounter) '-' counter(subcounter) '.';        }        h2 {            counter-increment: subcounter;            counter-reset: subsubcounter;            margin-left: 40px;        }        h3:before {            content: counter(mycounter) '-' counter(subcounter) '-' counter(subsubcounter) '.';        }        h3 {            counter-increment: subsubcounter;            margin-left: 80px;        }    </style>    <h1 id="大标题">大标题</h1>    <h2 id="中标题">中标题</h2>    <h3 id="小标题">小标题</h3>    <h3 id="小标题">小标题</h3>    <h3 id="小标题">小标题</h3>    <h2 id="中标题">中标题</h2>    <h3 id="小标题">小标题</h3>    <h3 id="小标题">小标题</h3>    <h2 id="中标题">中标题</h2>    <h3 id="小标题">小标题</h3>    <h3 id="小标题">小标题</h3>    <h1 id="大标题">大标题</h1>    <h2 id="中标题">中标题</h2>    <h3 id="小标题">小标题</h3>

 

界面效果图如下:(相信你看了代码一看便明白了)

还有一个使用content的小技巧,那就是在字符串两边添加嵌套文字符号

        h1:before {            content:open-quote;        }        h1:after {            content:close-quote;        }        h1 {            quotes:"(" ")";            /*当如果要使用双引号的时候 需要用\转义字符*/            quotes:"\"" "\"";        }

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
HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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),