CSS
 Computer >> コンピューター >  >> プログラミング >> CSS

疑似要素とCSSクラス


CSS疑似要素は、要素だけでなくCSSクラスにも適用できます。

CSSクラスでCSSPseudo要素を使用する例を見てみましょう-

<!DOCTYPE html>
<html>
<head>
<title>CSS Pseudo Elements and CSS Classes</title>
<style>
form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
   box-sizing: border-box;
}
input[type="button"] {
   border-radius: 10px;
}
.child{
   line-height: .1em;
   border: 4px solid black;
}
::selection{
   color:white;
}
.child:nth-child(1), h1::selection{
   background-color: #FF8A00;
}
.child:nth-child(2), div::selection{
   background-color: #F44336;
}
.child:nth-child(3), p::selection{
   background-color: #C303C3;
}
.child:nth-child(4), article::selection{
   background-color: #4CAF50;
}
.child:nth-child(5), code::selection{
   background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS Pseudo Elements and CSS Classes</legend>
<div id="container">
<h1>A Heading</h1>
<div>Welcome to DIV World</div>
<p>Every one is welcome in this town. Make yourself comfertable</p>
<article>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</article>
<code>function helloWorld(){ document.write("Hello World");}</code>
<div><span class="child">h1</span><span class="child">div</span><span class="child">p</span><span class="child">article</span><span class="child">code</span></div>
</div>
</body></html>

出力

これにより、次の出力が生成されます-

初期レイアウト-

疑似要素とCSSクラス

要素と
要素-

を選択します

疑似要素とCSSクラス

疑似要素とCSSクラス

要素と

要素-

を選択します

疑似要素とCSSクラス

疑似要素とCSSクラス

要素-

を選択します

疑似要素とCSSクラス


  1. CSSの疑似クラスと疑似要素の違い

    疑似クラス 疑似クラスは、:hover、:active、:last-childなどのセレクターの状態を表します。これらは単一のコロン(:)で始まります。 CSS疑似クラスの構文は次のとおりです- :pseudo-class{ attribute: /*value*/ } 疑似要素 同様に、疑似要素は、::after、::before、::first-lineなどの仮想要素を選択するために使用されます。 これらは二重コロン(::)で始まります。 CSS疑似要素の構文は次のとおりです- ::pseudo-element{ attribute: /*value*/ } 例 次

  2. CSSを使用した子要素の選択

    CSS子コンビネータは、親要素のすべての子要素を選択するために使用されます。 CSS子コンビネータの構文は次のとおりです。 セレクター{属性:/*値*/} CSS子孫コンビネータは、親要素のすべての子孫を選択するために使用されます CSS子孫コンビネータの構文は次のとおりです セレクターセレクター{属性:/*値*/} 例 次の例は、CSSの子と子孫のコンビネータを示しています。 * {text-align:center;ボーダー:10pxグルーブトマト;} ::first-line {box-shadow:inset 0 0 7px cornflowerblue;}