Home Web Front-end HTML Tutorial css3 selector (Part 1)

css3 selector (Part 1)

Aug 30, 2016 am 09:21 AM

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:

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

Brand

Brand

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

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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 &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

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 &lt;datalist&gt; 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 &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles