search
HomeWeb Front-endHTML Tutorialcss3 selector (Part 1)

1、给导航加分割线,左右

 .nav li::before,.nav li::after{

            content:"";

            position:absolute;

            top:14px;

            height:25px;

            width:1px; 

        }

     

        .nav li::before{

            left:0;

            background:-webkit-linear-gradient(to bottom,#f06254,#ffffff,#f06254);

            background:-moz-linear-gradient(to bottom,#f06254,#ffffff,#f06254);

            background:-o-linear-gradient(to bottom,#f06254,#ffffff,#f06254);

            background:-ms-linear-gradient(to bottom,#f06254,#ffffff,#f06254);

            background:linear-gradient(to bottom,#f06254,#ffffff,#f06254);

        }

        .nav li::after{

            right:0;

            background:-webkit-linear-gradient(to bottom,#f06254,#bf554c,#f06254);

            background:-moz-linear-gradient(to bottom,#f06254,#bf554c,#f06254);

            background:-o-linear-gradient(to bottom,#f06254,#bf554c,#f06254);

            background:-ms-linear-gradient(to bottom,#f06254,#bf554c,#f06254);

            background:linear-gradient(to bottom,#f06254,#bf554c,#f06254);

        }

        

        

         .nav li:first-child::before{ background:none;}

         .nav li:last-child::after{ background:none;}

 

2

 

html代码:

 

我链接的是PDF文件

我类名是icon

我的titlemore">我的titlemore

 

css代码  

 

a[class^=icon]{

  background: green;

  color:#fff;//定义以icon开头的任何字符串

}

a[href$=pdf]{

background: orange;

color: #fff;define hrefany string ending with pdf

}

a[title*=more]{

background: blue;

color: #fff;Any string defined with title

}

For example:

a[class^=column]{

background:#fc0001;

       }

a[href$=doc]{

background:#007d02;

}

a[title*=box]{

background:#0000fe;

}

I want to make my background red

I want to make the background red

I want to make my background red

I want to make the background green

I want to make the background green

I want to make the background blue

I want to make the background blue

I want to make the background blue

3

Structural pseudo-class selectorroot

:rootselector, literally we can clearly understand it as the root selector,

He means to match the root element of the document where the element E is located. In a HTML document, the root element is always

The

(":root" selector is equivalent to the element, to put it simply:

:root{background:orange}

html {background:orange;}

The effect obtained is the same.

It is recommended to use the :root method.

In addition, under IE9, you can also use “:root” to implement the hack function. )

4

Structural pseudo-class selector—not

:not选择器称为否定选择器,和jQuery中的:not选择器一模一样,可以选择除某个元素之外的所有元素。就拿form元素来说,比如说你想给表单中submit按钮之外input元素添加红色边框,CSS代码可以写成:form {

input:not([type="submit"]){

  border:1px solid red;

}//意思是除了type=submit意外的input边框为红色

 

 

 

5结构性伪类选择器—empty

:empty选择器表示的就是空。用来选择没有任何内容的元素,这里没有内容指的是一点内容都没有,哪怕是一个空格。

比如说,你的文档中有三个段落p元素,你想把没有任何内容的P元素隐藏起来。我们就可以使用“:empty”选择器来控制。

HTML代码:

我是一个段落

 

CSS代码:

p{

 background: orange;

 min-height: 30px;

}

p:empty {

  display: none;

}​

6结构性伪类选择器—target

:target选择器称为目标选择器,用来匹配文档(页面)url的某个标志符的目标元素

:

Brand

  content for Brand

Brand

 content for jake

Brand

    content for aron

cssCode:

#brand:target {

background: orange;

color: #fff;

}

#jake:target {

background: blue;

color: #fff;

}

#aron:target{

background: red;

color: #fff;

}

7, structural pseudo-class selector—first-child

":first-child"The selector means selecting the element E that is the first child element of the parent element. A simple understanding is to select the first child element in the element. Remember that it is a child element, not a descendant element.

HTMLCode:

  1. Link1
  2. Link2
  3. link3

CSSCode:

ol > li:first-child{

color: red;

}//The first sequence number of html turns red. If it is an unordered list, the sequence icon on the front end changes color

First-child is exactly the opposite of last-child

8, structural pseudo-class selector—nth-child(n)

The ":nth-child(n)" selector is used to locate one or more specific child elements of a parent element. Where "n" is its parameter, and it can be an integer value (1,2,3,4), or an expression (2n+1, -n+5 ) and keywords (odd, even), but the starting value of parameter n is always 1, not 0. In other words, when the value of parameter n is 0, the selector will not select any matching elements.

HTMLCode:

  1. item1
  2. item2
  3. item3
  4. item4

CSSCode:

ol > li:nth-child(2n){

background: orange;

} //Pass the ":nth-child(n)" selector, and the parameter uses the expression "2n" to set the even row list background color to orange.

9, structural pseudo-class selector—nth-last-child(n)

The

":nth-last-child(n)"selector is very similar to the previous ":nth-child(n)"selector, except that there is one more "last", The function is different from the ":nth-child(n)"selector. It starts from the last child element of a parent element to select a specific element

ol > li:nth-last-child(5){

background: orange;

}//Select the fifth to last list item in the list and set its background to orange.

10, first-of-typeselector

":first-of-type"selector is similar to ":first-child"selector, the difference is that specifies the type of element, it is mainly used to locate a certain element under a parent element The first child element of a type.

Position the first p element in the div container via the ":first-of-type" selector (p is not necessarily the first child in the container element) and set its background color to orange.

.wrapper > p:first-of-type {

background: orange;

//last-of-typeselector

The

":last-of-type"selector has the same function as the ":first-of-type"selector. The difference is that it selects the last child element of a certain type under the parent element. .

11, nth-of-type(n)selector

The

":nth-of-type(n)"selector is very similar to the ":nth-child(n)"selector, except that it only counts children of a certain type specified in the parent element element. When the child elements in an element are not only child elements of the same type, use the ":nth-of-type(n)"selector to locate a certain type of child elements in the parent element. Very convenient and useful. "n" in ":nth-of-type(n)"selector and ":nth-child(n)""n" in selector The same goes for parameters, which can be specific integers, expressions, or keywords.

Example: .wrapper > p:nth-of-type(2n){

background: orange;

} Set the background of the even number of segments in the container "div.wrapper" to orange through the ":nth-of-type(2n)"selector.

18, nth-last-of-type(n)selector

“:nth-last-of-type(n)”选择器和“:nth-of-type(n)”选择器是一样的,选择父元素中指定的某种子元素类型,但它的起始方向是从最后一个子元素开始,而且它的使用方法类似于上节中介绍的“:nth-last-child(n)”选择器一样。

通过“:nth-last-of-type(n)”选择器将容器“div.wrapper”中的倒数第三个段落背景设置为橙色。

.wrapper > p:nth-last-of-type(3){

  background: orange;

}

 

12only-child选择器

“:only-child”选择器选择的是父元素中只有一个子元素,而且只有唯一的一个子元素。也就是说,匹配的元素的父元素中仅有一个子元素,而且是一个唯一的子元素。

示例演示

通过“:only-child”选择器,来控制仅有一个子元素的背景样式,为了更好的理解,我们这个示例通过对比的方式来向大家演示。

HTML代码:

  

我是一个段落

  

我是一个段落

  

我是一个段落

CSSCode:

.post p {

background: green;

color: #fff;

padding: 10px;

}

.post p:only-child {

background: orange;

}

13, only-of-typeselector

":only-of-type"The selector is used to select an element that is the only child element of the same type as its parent element. This may not be easy to understand, let’s put it another way. ":only-of-type" means that an element has many sub-elements, and only one type of sub-element is unique. Use the ":only-of-type" selector You can select the only type sub-element in this element.

Example Demo

Use the ":only-of-type" selector to modify the background color of only one div element in the container to orange.

HTMLCode:

I am a paragraph

I am a paragraph

I am a paragraph

I am a Divelement

I am a Div

          

  • I am a list item

I am a paragraph

CSSCode:

.wrapper > div:only-of-type {

background: orange;

}

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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