Home Web Front-end HTML Tutorial CSS3 selectors (basic selectors, attribute selectors, pseudo-class selectors, selector strategies)_html/css_WEB-ITnose

CSS3 selectors (basic selectors, attribute selectors, pseudo-class selectors, selector strategies)_html/css_WEB-ITnose

Jun 24, 2016 am 11:59 AM
css3 Base Attributes Strategy Selector

《CSS3 Basic Selector》

                      <p class="sycode">                          <p class="sycode">        一、通配符选择器(*)       </p>                          <p class="sycode">        *{       </p>                          <p class="sycode">        marigin: 0;       </p>                          <p class="sycode">        padding: 0;       </p>                          <p class="sycode">        }       </p>                          <p class="sycode">       </p>                          <p class="sycode">        二、元素选择器(E)       </p>                          <p class="sycode">        li {background-color: grey;color: orange;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        三、类选择器(.className)       </p>                          <p class="sycode">       </p>                          <p class="sycode">        四、id选择器(#ID)       </p>                          <p class="sycode">        #first {background: lime;color: #000;}       </p>                          <p class="sycode">        #last {background: #000;color: lime;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        五、后代选择器(E F)       </p>                          <p class="sycode">        .demo li {color: blue;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        六、子元素选择器(E>F)       </p>                          <p class="sycode">        ul > li {background: green;color: yellow;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        七、相邻兄弟元素选择器(E + F)       </p>                          <p class="sycode">        li + li {background: green;color: yellow; border: 1px solid #ccc;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        八、通用兄弟选择器(E ? F)       </p>                          <p class="sycode">        .active ~ li {background: green;color: yellow; border: 1px solid #ccc;}       </p>                          <p class="sycode">       </p>                          <p class="sycode">        九、群组选择器(selector1,selector2,...,selectorN)       </p>                          <p class="sycode">        .first, .last {background: green;color: yellow; border: 1px solid #ccc;}       </p>                      </p>
Copy after login

《CSS3 Attribute Selector》

CSS3 attribute selectors mainly include the following types:

  1. E[attr]: only uses the attribute name, but does not determine any attribute value;
  2. E[attr="value"]: Specifies the attribute name, and specifies the attribute value of the attribute;
  3. E[attr~="value"]: Specifies the attribute name, and has the attribute value, This attribute value is a word list separated by spaces. The word list contains a value word, and the "?" in front of the equal sign must be written;
  4. E[attr^="value" ]: The attribute name is specified, and there is an attribute value, and the attribute value starts with value;
  5. E[attr$="value"]: The attribute name is specified, and there is an attribute value, and the attribute value is Ending with value;
  6. E[attr*="value"]: The attribute name is specified, and there is an attribute value, and the attribute value contains value;
  7. E[attr|= "value"]: The attribute name is specified, and the attribute value is value or a value starting with "value-" (for example, zh-cn);
  8.                               <p class="sycode">                                  <p class="sycode">          .demo a[href][title] {background: yellow; color:green;}//存在href 和title 被选中         </p>                                  <p class="sycode">          .demo a[id="first"] {background: blue; color:yellow;font-weight:bold;}//id="first"被选中         </p>                              </p>
    Copy after login

《CSS3 Pseudo-Class Selector》

1. This is the most commonly used dynamic pseudo-class

                      <p class="sycode">                          <p class="sycode">        .demo a:link {color:gray;}/*链接没有被访问时前景色为灰色*/       </p>                          <p class="sycode">        .demo a:visited{color:yellow;}/*链接被访问过后前景色为黄色*/       </p>                          <p class="sycode">        .demo a:hover{color:green;}/*鼠标悬浮在链接上时前景色为绿色*/       </p>                          <p class="sycode">        .demo a:active{color:blue;}/*鼠标点中激活链接那一下前景色为蓝色*/       </p>                      </p>
Copy after login

2. UI element status pseudo-class

":enabled",":disabled",":checked "Pseudo-classes are called UI element state pseudo-classes. These are mainly used for Form element operations in HTML. The most common one is that our "type="text" has two states: enabled and disabled. The former is a writable state and the latter is Not available; in addition, "type="radio" and "type="checkbox"" have two states: "checked" and "unchecked". Let's look at two examples. For example, if you want to distinguish the "disabled" text box from other text boxes, you can apply it like this;

IE6-8 does not support ":checked", ":enabled" ",":disabled" these three selectors

3. :nth selector

  • :fist-childSelect the first child element of an element;
  • :last-childSelect the last child element of an element;
  • :nth- child()Select one or more specific child elements of an element;
  • :nth-last-child()Select one or more specific child elements of an element child elements, counting from the last child element of this element;
  • :nth-of-type() selects the specified element;
  • : nth-last-of-type()Select the specified element, counting from the last element;
  • :first-of-typeSelect the first element under a superior A child element of the same type;
  • :last-of-type Selects the last child element of the same type of a parent element;
  • :only-childThe selected element is the only element of its parent element;
  • :only-of-typeThe selected element is the only child element of the same type of its parent element;
  • :emptyThe selected element has no content.
  • IE6-8 and FF3 - browsers do not support ":nth-child, :nth-last-child(), :nth-of-type" selectors

    4 , Negative selector (:not)

    The negative selector is exactly the same as the :not selector in jq. Just use the elements in the form to illustrate the usage of this selector. For example, if you want to Add borders to all inputs, but you don’t want the submit to change. You can use :not to implement

                      <p class="sycode">                      <p class="sycode">       input:not([type="submit"]) {border: 1px solid red;}      </p>                  </p>
    Copy after login

    5. Pseudo element

    Everyone has seen pseudo elements in CSS before: :first-line, :first-letter, :before, :after; then in CSS3, he has made certain changes to pseudo elements. Based on the previous adjustment, a ":" was added, which now becomes "::first-letter,::first-line,::before,::after". In addition, he also added a "::" selection", two "::" and one ":" are mainly used to distinguish pseudo classes and pseudo elements in CSS3. So far, both methods are accepted, which means that no matter which writing method is used, The functions are the same, but the writing format is different.

    Then let’s briefly understand their functions

    ::first-line selects the first line of the element. For example, to change the style of the first line of text in each paragraph, we can use this.

    ::before和::after这两个主要用来给元素的前面或后面插入内容,这两个常用"content"配合使用, 见过最多的就是清除浮动

                      <p class="sycode">                      <p class="sycode">       .clearfix:before,      </p>                      <p class="sycode">       .clearfix:after {      </p>                      <p class="sycode">       content: ".";      </p>                      <p class="sycode">       display: block;      </p>                      <p class="sycode">       height: 0;      </p>                      <p class="sycode">       visibility: hidden;      </p>                      <p class="sycode">       }      </p>                      <p class="sycode">       .clearfix:after {clear: both;}      </p>                      <p class="sycode">       .clearfix {zoom: 1;}      </p>                  </p>
    Copy after login

    《CSS选择器优化》

    固有效率:

  • id选择器(#myid)
  • 类选择器(.myclassname)
  • 标签选择器(div,h1,p)
  • 相邻选择器(h1+p)
  • 子选择器(ul > li)
  • 后代选择器(li a)
  • 通配符选择器(*)
  • 属性选择器(a[rel="external"])
  • 伪类选择器(a:hover,li:nth-child)
  • 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)

    exe to php: an effective strategy to achieve function expansion exe to php: an effective strategy to achieve function expansion Mar 04, 2024 pm 09:36 PM

    EXE to PHP: An effective strategy to achieve function expansion. With the development of the Internet, more and more applications have begun to migrate to the web to achieve wider user access and more convenient operations. In this process, the demand for converting functions originally run as EXE (executable files) into PHP scripts is also gradually increasing. This article will discuss how to convert EXE to PHP to achieve functional expansion, and give specific code examples. Why Convert EXE to PHP Cross-Platformness: PHP is a cross-platform language

    bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

    Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

    Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

    Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

    Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

    Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

    How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

    How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

    'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' 'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' Feb 25, 2024 pm 09:04 PM

    What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

    MyBatis cache strategy analysis: best practices for first-level cache and second-level cache MyBatis cache strategy analysis: best practices for first-level cache and second-level cache Feb 21, 2024 pm 05:51 PM

    MyBatis cache strategy analysis: best practices for first-level cache and second-level cache When developing using MyBatis, we often need to consider the choice of cache strategy. The cache in MyBatis is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache, while the second-level cache is a Mapper-level cache. In practical applications, rational use of these two caches is an important means to improve system performance. This article will use specific code examples to analyze a MyBatis

    How to improve attributes in Hero Maze Adventure How to improve attributes in Hero Maze Adventure Mar 20, 2024 pm 03:56 PM

    Hero Maze Adventure is a stand-alone development game designed with a very fun operation mode. It provides excellent business simulation characters and excellent gameplay design of business and RPG, bringing players the excitement of in-depth operation adventure. Regarding the attributes, How to improve the content is also of interest to many players. This issue brings you a guide to improve the attributes of the Hero's Maze Adventure! A guide to quickly improve the attributes of the Hero's Maze Adventure. What should you do if you want to improve a certain attribute? A: Find equipment with the same growth, such as fists Sheaths and spears have speed growth, single and two-handed swords have strength growth, axes and hammers have physical strength growth. Note: In addition to wooden sticks, only shields can increase health. Luck can only increase with accessories. How to increase attributes quickly? A: The higher the map level, the more monsters, and the greater the equipment LV growth.

    See all articles