CSS擬似要素の美しさを1つのタグで説明する
beforeと::beforeの違い
具体的な使い方に入る前に、擬似クラスと擬似要素について簡単に紹介します。擬似クラスについてはよく聞きますが、擬似要素についてはあまり聞いたことがないと思います。
擬似クラス要素にコロンが1つではなく2つ使われていることがありますが、これは擬似クラスと擬似要素を区別するためのCSS3仕様の要求の一部で、ほとんどのブラウザは両方の表現をサポートしています。
#id:after{
...
}
#id::after{
...
}
CSS3 の擬似クラスにはシングルコロン(:)、CSS3 の擬似要素にはダブルコロン(::)を使用します。CSS2にすでにある擬似要素、例えば:beforeについては、シングルコロンとダブルコロンが同じように動作するように::beforeと表記されます。
そのため、webkitやfirefox、operaなどとの互換性だけが求められるサイトでは、疑似要素にダブルコロンのスタイルを使用することが推奨されますが、IEとの互換性が求められる場合は、CSS2のシングルコロンのスタイルを使用した方が無難でしょう。
より具体的な情報は、MDN の擬似クラスと擬似要素についての理解をご覧ください。
今回の主役は前後の擬似要素ですが、この2つの擬似要素の魅力について詳しくお話しします。
擬似要素に対応していないタグは?
I also just learned about this pose. Just hasten to add it so as not to mislead readers.
Although pseudo-elements are powerful, there are still some specific tags that do not support pseudo-elements before and after.
tags such as , , and
The reason for this is that for a tag to support a pseudo-element, the element needs to be insertable, i.e., it needs to be a container. The input, img, iframe, etc. elements can't contain other elements, so they can't insert content via pseudo elements.
Clear floats with after
This one is probably known to the front-end, using the after pseudo-element to clear the page float, without too much explanation.
.clearfix:after {content:". "; display:block; height:0; visibility:hidden; clear:both; }
.clearfix { *zoom:1; }
Pseudo elements with css sprites Sprite image
This is also an old pose. Sprites should be no stranger to the need for Sprites on many sites by combining multiple image icons into one image, thus reducing http requests.
However, in the process of creating Sprite images, or Sprite images automatically generated by many packaging tools nowadays, there is a problem of how many margins need to be reserved for each icon. Take a look at the following image.
For example, in the case above (assuming the icon in the button is a Sprite image), if the product suddenly requires the button to change from state left to state right one day, the original margin of the Sprite image will be insufficient and other graphics will appear in the button.
And we don't usually add an extra label for a small icon (it's not semantic).
So usually, if we need to use a Sprite image in this case, we set a pseudo-element in the button, set the height and width of the pseudo-element to the size of the original icon, and use absolute positioning to position it where it needs to be, so that it fits perfectly no matter what the margins of each Sprite icon are. The
Single color for hover and active buttons
Recently, there was a requirement in a project where the operation needed to configure a button with different background color values depending on the business scenario. But as we know, a button usually has 3 color values, normal, hover and active, usually hover is a little brighter than the original color and active is a little darker.
It looks like this (below).
To ease the burden of the operations students, how can we configure only one background color without hover and active colors so that the buttons can follow the changes adaptively? Yes, you can do that with the before and after pseudo elements.
Color trivia
Here's a little bit of color value trivia. In addition to #fff, rgb(255,255,255), and hsl(0, 100%, 100%) (HSV), we are familiar with the color representation.
HSL, for example, is a representation of the points in the RGB color model in a cylindrical coordinate system. hue, saturation, lightness (English: Hue, Saturation, Lightness).
For a color represented in HSL, we only need to change the value of L (luminance) to get a color that is a little brighter or a little darker.
Of course, changing the brightness can also be done by overlaying a transparent layer, as in the case of changing the background color of the button using the pseudo-element by overlaying a translucent layer.
Simply put, a white translucent layer rgba(255,255,255,.2) overlaid on top of the background color will give you a brighter color. (This is not very strict, assuming that an element with a pure white background will not be brighter with a white translucent layer)
Conversely, a black translucent layer rgba(0,0,0,.2) overlaid on top of the background color gives a darker color.
So, we use the before pseudo-element to generate a black translucent layer rgba(0,0,0,.2) that matches the size of the button to be displayed at .btn:hover:before, and the after pseudo-element to generate a white translucent layer rgba(255,255,255,. 2) that matches the size of the button to be displayed at .btn:active :before, so that only one background color can be configured to achieve the change of light and dark when hover and active.
.pesudo:before{
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
z-index:-1;
background:rgba(0,0,0,.1);
}
.pesudo:hover:before{
content:"";
}
.pesudo:after{
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
z-index:-1;
background:rgba(255,255,255,.2);
}
.pesudo:active:after{
content:"";
}
Transformation recovery
There are times when designers want to express different meanings through some more specific geometric shapes.
関連
-
HTML DOMのsetIntervalとclearIntervalのメソッドのケーススタディ
-
HTML-Canvasの優れた性能とその実用化について
-
CSSカスタムスクロールバースタイルの事例詳細
-
見栄えの良いテーブル表cssスタイルコードを推奨 詳細
-
HTML clearfix clear floatの説明
-
CSSの失敗術を解説
-
cssのテキストを垂直方向に中央寄せにする8つの方法
-
[解決済み】Uncaught TypeError。プロパティ 'onclick' を null に設定できない [重複]。
-
[解決済み】CSS3 トランジションが機能しない
-
CSSで曲線の描画とアニメーションを表示する方法
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン