完全なリファレンスガイド🚀


皆さん、こんにちは!🚀
今日はCSSセレクタを深くカバーします.

目次
  • What is a selector in CSS?

  • Types of selectors
  • Universal selector
  • Type selector
  • Class selector
  • ID selector
  • Attribute selector

  • Grouping selectors
  • Group selector

  • Combining selectors
  • Descendant combinator
  • Child combinator
  • General sibling combinator
  • Adjacent sibling combinator
  • Pseudo-classes
  • Pseudo-elements

  • CSSのセレクターとは?

    A CSS selector is a pattern used to choose the HTML elements that we want to style.

    Speaking more technically, CSS selectors are able to select those HTML elements to which a style ruleset will be applied.


    セレクタの種類
    ユニバーサルセレクタ

    Syntax: * { style properties }

    This selector is represented by an asterisk (*) and matches all the elements in the HTML document.

    <ul>
      <li>Computer Science Eng.</li>
      <li>Mathematics</li>
      <li>Physics</li>
    </ul>
    
    * {
      border: 1px solid coral;
    }
    

    This will apply a 1px solid coral border to all elements in the document.

    Output:

    borderプロパティは、このセレクタの動作を非常によく示します.上記のイメージで観察することができますように、あらゆる要素は現在、境界線によって囲まれます<body> 要素と<html> 要素.
    ユニバーサルセレクタも混乱を避けるために、あなたのコードを読みやすくするために使用されます.
    以下の例を見てみましょう.
    div :first-child {
      background-color: maroon;
    }
    
    div:first-child {
      background-color: maroon;
    }
    
    これらの2つのコードスニペットは、両方ともかなり同じように見えます?
    さて、タイプセレクタと擬似クラスの間の空白は、まったく同じではないことを認識する必要があります<div> 要素と2番目の<div> 別の要素の最初の子要素です.
    まだパニックにならないでください、我々はより詳細に疑似クラスに関して詳細に話しています、そして、セレクターが後で働く方法🙂
    今のところ、この例では、スタイル規則を適用するために2つの異なる要素セレクタが一緒に動作していることに注意してください.すべては後で意味をなす.約束.
    いずれにしても、他のものと区別して、空白のスペースをより明確に見ることができるようにするには、次のようにユニバーサルセレクタを使用できます.
    div *:first-child{
      background-color: maroon;
    }
    
    機能の面で言えば、アスタリスクシンボルを追加することは何もしませんが、ターゲットになる要素をすばやく識別し、コードをきちんとしてきちんと保つことができます.

    タイプセレクタ

    Syntax: elemType { style properties }

    This selector matches all elements that belong to the specified type.

    <p>A paragraph</p>
    <p>Another paragraph</p>
    <p>One more paragraph</p>
    
    p {
      font-size: 1.5rem;
      color: deeppink;
    }
    

    This will apply a font size of 1.5rem and a deep pink color to all <p> elements. Easy and direct.

    Output:


    クラスセレクタ

    Syntax: .classname { style properties }

    This selector is represented by a period (.) and matches all elements that contain the specified class.

    <div>
      <p class="deeppink">This is a deep pink paragraph</p>
      <p class="hotpink">This is a hot pink paragraph</p>
      <p class="pink">This is a regular pink paragraph</p>
      <p class="deeppink-bg">This is a paragraph with a deep pink background</p>
      <p class="hotpink-bg">This is a paragraph with a hot pink background</p>
      <p class="pink-bg">This is a paragraph with a regular pink background</p>
    </div>
    
    .deeppink {
      color: deeppink;
    }
    .hotpink {
      color: hotpink;
    }
    .pink {
      color: pink;
    }
    .deeppink-bg {
      background-color: deeppink;
    }
    .hotpink-bg {
      background-color: hotpink;
    }
    .pink-bg {
      background-color: pink;
    }
    
    /* Group selector - Stay until the end to understand its
     behavior 😉 */
    .deeppink-bg, .hotpink-bg, .pink-bg {
      color: white;
    }
    
    

    This will apply a different pink color variant to every <p> element depending on the class they contain, styling either their text or their background.

    Output:


    クラスセレクタを指定する別の方法は、通常のクラスセレクタの前にクラス名を含む要素の型を追加することです.
    この例を見てください.
    <ul>
      <li class="list-item">...</li>
      <li class="list-item">...</li>
      <li class="list-item">...</li>
    </ul>
    
    以下のように、すべてのリスト項目をスタイル化できます.
    .list-item{
      padding: 5px;
    }
    
    または以下のようになります:
    li.list-item{
      padding: 5px;
    }
    
    この場合、リスト項目クラスを含むすべての要素が<li> 要素を追加するので、要素の型を追加すると少しも違いません.
    以下の例を見てみましょう.
    <section>
     <h1 class="big">List of animals</h1>
     <ul>
       <li><p class="big">Cat</p></li>
       <li><p class="big">Dog</p></li>
       <li><p class="big">Monkey</p></li>
       <li><p class="big">Dolphin</p></li>
       <li><p class="big">Frog</p></li>
     </ul>
    </section>
    
    この例では、同じクラスを参照している要素の種類が異なるので、それぞれのスタイルに異なるスタイル規則を適用するには、そのクラスを含む要素の型を指定する必要があります.さもなければ、要素は適切に目標とされません、そして、したがって、適用しようとしているスタイルは予想通りに表示されません.
    ですから、以下のようにして、要素が見出しか段落かどうかによって異なる規則を適用すべきです.
    h1.big {
      font-size: 3rem;
      color: coral;
    }
    
    p.big {
      font-size: 2rem;
      color: orange;
    }
    
    出力:


    IDセレクタ

    Syntax: #idname { style properties }

    This selector is represented by a hash symbol (#) and matches the unique element that contains the specified id.

    <div>
      <table id="users-table">
        <th>Users</th>
        <tr>
          <td>John Doe</td>
        </tr>
        <tr>
          <td>Jane Doe</td>
        </tr>
      </table>
      <table id="staff-table">
        <th>Staff</th>
        <tr>
          <td>Hannah Legend</td>
        </tr>
        <tr>
          <td>Daniel Oaks</td>
        </tr>
      </table>
    </div>
    
    /* Type selector */
    table {
      padding: 20px;
    }
    /* ID selectors */
    #users-table {
      background-color: black;
      color: white;
    }
    #staff-table {
      border: 1px solid black;
    }
    

    This will apply a black background color and a white text color to the table that matches the id users-table, and a 1px solid black border to the table that matches the id staff-table. Both of them receive a padding of 20px based on their type.

    Output:


    Important: Even though HTML allows you to assign the same id value to several elements, you shouldn't ever do it.

    If you need to apply the same style to a bunch of different elements, always use the class attribute. You'll keep your code clean and will get rid of a possible confusion.



    属性セレクタ

    The attribute selector is more complex than the rest of the selectors and has several syntaxes that can be applied to our HTML elements based on which condition should be satisfied by them.

    In other words, it matches all the HTML elements which contain a specified attribute and whose value for that attribute satisfies a given condition.


    属性セレクタ構文

    構文1
    [スタイル]プロパティ
    指定した属性の要素にマッチします.
    <nav>
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/blog">Blog</a></li>
        <li><a href="/contact">Contact</a></li>
        <li><a href="/about">About</a></li>
      </ul>
    </nav>
    
    /* Type selector */
    nav {
      background-color: darkslategray;
      padding: 10px;
    }
    
    /* Attribute selector */
    a[href] {
      color: white;
      text-decoration: none;
    }
    
    
    これはあらゆる色に適用されます<a> を含む要素href 属性の値にかかわらずアンダーラインを削除します.
    我々はまた、スタイリング<nav> 背景色と要素セレクタを使用していくつかのパディングを持つ要素.
    出力:


    構文2
    [ attr = value ] {スタイルプロパティ}
    attrの値が正確に値を持つ要素にマッチします.
    <form>
      <label for="username">Username</label>
      <input id="username" type="text" placeholder="Username" />
      <label for="password">Password</label>
      <input id="password" type="password" placeholder="Password" />
    </form>
    
    input[type="text"] {
      color: chocolate;
    }
    
    これはすべてのチョコレートテキストの色を適用されます<input> を持つ要素type テキストの正確な値を持つ属性.
    出力:

    この構文を使えばIDセレクタをエミュレートできます(セクション4の例を思い出してください).
    [id="users-table"] {
      background-color: black;
      color: white;
    }
    
    [id="staff-table"] {
      border: 1px solid black;
    }
    

    Explanation: By using this attribute selector syntax, we are targeting elements that contain an id attribute whose value is exactly users-table or staff-table. We're using this one syntax because an element can only have one idname, so the value needs to be exact (=).



    構文3
    [ attr ~= value ] {スタイルプロパティ}
    attrの値が空白で区切られた単語のリストである要素にマッチし、そのうちの1つがまさに値です.
    <p lang="en-gb en-us en-ca en-au en-nz">Welcome!</p>
    <p lang="fr-fr fr-ca fr-be">Bienvenue!</p>
    
    p[lang~="en-us"] {
      color: navy;
    }
    p[lang~="fr-fr"] {
      color: red;
    }
    
    これはすべての海軍の色を適用されます<p> 要素の値lang は正確な文字列en - usを含むリストです.すべて同じ<p> 値の要素lang この場合、赤い色を適用します.
    出力:

    IDセレクタの動作をエミュレートする前の構文と同様に、この1つの属性構文を使用してクラスセレクタの動作をエミュレートできます.
    [class~="list-item"]{
      padding: 5px;
    }
    

    Explanation: By using this attribute selector syntax, we are targeting elements that have a class attribute whose value is a list that contains the string list-item.

    We're using this specific syntax because an element can contain several classes and we're aiming at just one of them (~=). But even though the element had just one class, the value for the class attribute always acts like a list of values.



    構文4
    [ attr chen = value ] {スタイルプロパティ}
    attrの値が正確に値を持つ要素にマッチします.
    <p lang="en-us">Hello</p>
    <p lang="en-gb">Hello</p>
    
    p[lang|="en"] {
      background-color: cornflowerblue;
      color: antiquewhite;
    }
    
    これはコーンフラワーブルーの背景色とすべてのアンティークの白いテキストの色を適用されます<p> を持つ要素lang 属性が正確にenであるか、en -から始まる属性.
    出力:


    構文5
    [ attr ^= value ] {スタイルプロパティ}
    attrの値が値から始まる要素にマッチします.
    <a href="#list1">Go to list 1</a>
    <a href="#list2">Go to list 2</a>
    <section>
      <h1>Lists</h1>
      <div id="list1">
        <h1>List 1</h1>
        <ul>
          <li>Milk</li>
          <li>Butter</li>
          <li>Eggs</li>
          <li>Sugar</li>
        </ul>
      </div>
      <div id="list2">
        <h1>List 2</h1>
        <ul>
          <li>Oranges</li>
          <li>Lemons</li>
          <li>Strawberries</li>
          <li>Apples</li>
        </ul>
      </div>
    </section>
    
    a[href^="#"] {
      color: crimson;
      font-size: 2rem;
    }
    
    これは、すべての2 remの深紅色とフォントサイズを適用します<a> を持つ要素href 属性の値.
    出力:


    構文6
    [ attr $= value ] {スタイルプロパティ}
    attrの値が値を持つ要素にマッチします.
    <a href="css-selectors-guide.pdf">CSS Selectors Guide</a>
    
    a[href$=".pdf"] {
      color: slateblue;
    }
    
    これはすべてにスレートブルー色を適用されます<a> を持つ要素href 値が終了する属性.PDFファイル.
    出力:


    構文7
    [ attr *= value ] {スタイルプロパティ}
    attrの値が少なくとも1つの値の出現を含む要素にマッチします.
    <div class="small-box">
      <p>This is a small box</p>
    </div>
    <div class="big-box">
      <p>This is a big box</p>
    </div>
    
    
    div[class*="box"] {
      background-color: burlywood;
    }
    
    これはBurlywood背景色をすべてに適用されます<div> を含む要素class 属性には、文字列ボックスの少なくとも1つの値があります.
    出力:


    グループセレクタ

    Grouping selectors in CSS is basically used to put together those elements of different type or with different id/classes that we want to apply the same style properties to.

    Additionally, by using this technique, we'll get rid of redundancy and our code will be clean, concise and organized.


    グループセレクタ

    Syntax: elem1, ..., elemN { style properties }

    This selector is represented by a comma (,) and matches all the elements stated in the list and applies the same ruleset to all of them.

    <h1>Computer Science Engineering</h1>
    <h2>List of courses</h2>
    <ul>
      <li>Algebra</li>
      <li>Calculus</li>
      <li>Physics</li>
      <li>Discrete Mathematics</li>
      <li>Introduction to Programming</li>
    </ul>
    
    
    h1, h2, li {
      color: darkred;
    }
    

    This will apply a dark red color to every <h1> , <h2> and <li> element.

    Output:


    結合セレクタ

    CSS selectors can also be combined. By combining selectors, then we can define CSS combinators.

    CSS combinators are used to establish a relationship between different selectors and are very useful to make your element selection more targeted.


    子孫コンビナータ

    Syntax: elem1 elem2 { style properties }

    This combinator is represented by a single space ( ) and matches all elem2 that are descendants of elem1.

    Consider the following navigation:

    <nav>
      <ul>
        <li><a>Home</a></li>
        <li>
          <a>People</a>
          <ul>
            <li><a>Students</a></li>
            <li>
              <a>Faculty members</a>
              <ul>
                <a>Discrete Mathematics</a>
                <a>Programming</a>
                <a>Physics</a>
                <a>Algorithms</a>
              </ul>
            </li>
            <li><a>Staff members</a></li>
          </ul>
        </li>
        <li><a>About</a></li>
        <li><a>Contact</a></li>
      </ul>
    </nav>
    
    nav a {
      border: 1px solid crimson;
      color: darkslateblue;
      font-size: 1.5rem;
      font-weight: bold;
    }
    

    This will apply a 1px solid crimson border, a dark slate blue color, a font size of 1.5rem and a bold font weight to every <a> element that is descendant of a <nav> element, regardless of how nested they are.

    Output:



    子組子

    Syntax: elem1 > elem2 { style properties }

    This combinator is represented by a prompt (>) and matches all elem2 that are direct children of elem1.

    <div class="box">
      <p>This is a direct child of .box</p>
      <div>
        <p>This is not a direct child of .box</p>
      </div>
      <p>This is another direct child of .box</p>
    </div>
    
    .box > p {
      color: darkgoldenrod;
    }
    

    This will apply a dark golden color to every <p> element that is a direct child of any element that has the class box, so, in this HTML example, the first and last <p> elements will be selected, but not the one in the middle.

    Output:



    一般的な兄弟結合器

    Syntax: elem1 ~ elem2 { style properties }

    This combinator is represented by a tilde (~) and matches all elem2 that are siblings to elem1 and come after it.

    <img src="blue-mug.jpeg" alt="a regular blue coffee mug" />
    <p>Blue mug</p>
    <p>Price: $15</p>
    
    img ~ p {
      color: darkblue;
    }
    

    This will apply a dark blue color to every <p> element which is a sibling of any <img> element and comes after it. In this example, both <p> elements will be selected.

    Output:


    隣接する兄弟結合器

    Syntax: elem1 + elem2 { style properties }

    This combinator is represented by a plus symbol (+) and matches all elem2 that are siblings to elem1 and appear immediately after it.

    <img src="blue-mug.jpeg" alt="a regular blue coffee mug" />
    <p>Blue mug</p>
    <p>Price: $15</p>
    
    img + p {
      color: darkblue;
    }
    

    In this case, only the first <p> element will be selected, since the second one doesn't appear immediately after the <img> element.

    Output:


    擬似クラス

    A CSS pseudo-class is a keyword that is added to a selector and defines a special state of the selected elements.

    Syntax: elem:pseudo-class { style properties }

    This selector is represented by a colon (:).

    <h1>Shopping list</h1>
    <ul>
      <li>Milk</li>
      <li>Butter</li>
      <li>Eggs</li>
      <li>Sugar</li>
    </ul>
    
    li:hover {
      background-color: black;
      color: white;
    }
    

    In this example, we're applying a black background color and a white text color to every <li> element when the cursor hovers over it.

    Take a look at what happens when we hover over the Butter element:

    Output:

    最も一般的なCSS擬似クラスのいくつかは以下の通りです.:active , :hover , :focus , :disabled , :checked , :first-child , :nth-child , :first-of-type .

    擬似要素

    A CSS pseudo-element is a keyword that is added to a selector to style a specific part of the selected elements.

    Syntax: elem:pseudo-element { style properties }

    This selector is represented by a double colon (::).

    <p>CODE</p>
    
    p::before{
      content: "_";
    }
    

    In this example, we're appending an underscore symbol in front of every <p> element.

    Output:

    最も一般的なcss擬似要素のいくつかは以下の通りです.::after (:after ), ::before (:before ), ::marker , ::placeholder , ::first-letter .
    そしてそれはかなりの🙂
    私はこのリファレンスガイドはあなたのために有用であることを願っています.
    また、InstramアカウントのCSSセレクタに関する以下の関連記事をチェックすることもできます.
    < div >
    < htv class "Instagramの位置
    Instagramの液体タグを指定しますhttps://www.instagram.com/p/CQErX0PMe1I/embed/captioned"\\-\\-\\-\\-\\-\\- c "0\\fPを指定する.
    < iframe >
    < tt/async =""defer ="defer "src ="https://platform.instagram.com/en_US/embeds.js//>
    < div >
    < htv class "Instagramの位置
    Instagramの液体タグを指定しますhttps://www.instagram.com/p/CQWagoQsEdV/embed/captioned"\\-\\-\\-\\-\\-\\- c "0\\fPを指定する.
    < iframe >
    < tt/async =""defer ="defer "src ="https://platform.instagram.com/en_US/embeds.js//>
    < div >
    < hr/>
    < P >🎉 フォローすることを忘れないで
    そして、より毎日のWebdevコンテンツ🖥🖤