Detailed explanation of examples of only-child and only-of-type in CSS3 selectors

黄舟
Release: 2017-07-26 14:30:48
Original
3153 people have browsed it

only-child selector

The ":only-child" selector selects only one child element in the parent element, and only one child element. In other words, there is only one child element in the parent element of the matching element, and it is a only child element.

Example Demonstration

Use the ":only-child" selector to control the background style of only one child element. For a better understanding, we use this example Demonstrate to everyone through comparison.

HTML code:


<p class="post">
  <p>我是一个段落</p>
  <p>我是一个段落</p></p><p class="post">
  <p>我是一个段落</p></p>
Copy after login

CSS code:


.post p {
  background: green;
  color: #fff;
  padding: 10px;
}.post p:only-child {
  background: orange;
}
Copy after login

Demo results:

##only-of-type selector

":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. Selects the only type child element in this element.

Example demonstration

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

HTML code:


<p class="wrapper">
  <p>我是一个段落</p>
  <p>我是一个段落</p>
  <p>我是一个段落</p>
  <p>我是一个p元素</p></p><p class="wrapper">
  <p>我是一个p</p>
  <ul>
    <li>我是一个列表项</li>
  </ul>
  <p>我是一个段落</p></p>
Copy after login

CSS code:


.wrapper > p:only-of-type {
  background: orange;
}
Copy after login

Demo results:

The above is the detailed content of Detailed explanation of examples of only-child and only-of-type in CSS3 selectors. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template