必要なのは、次のとおりです。
@mixin horizontal-line-styles-1 ($color1, $color2) {
position: relative;
border: none;
&:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 1px;
background: radial-gradient(ellipse at center, $color1 0%, $color2 75%);
}
}
.fancy-line-1 {
@include horizontal-line-styles-1(blue, red);
}
Codepen Demo:
@mixin horizontal-line-styles ($color1, $color2) {
height: 1px;
border: none;
background: radial-gradient(ellipse at center, $color1 0%, $color2 75%);
}
.fancy-line {
@include horizontal-line-styles(blue, red);
}
それとも、本当に擬似要素を使用する場合。
元のコードで何が問題になっていますか?
まず、フォーマットが正しくありません。固定:あなたがあなたのミックスインを呼び出す
@mixin horizontal-line-styles ($color1, $color2) {
hr {
border: 0;
height: 1px;
&:after {
content: '';
height: 0.5em;
top: 1px;
}
&:before,
hr:after {
content: '';
position: absolute;
width: 100%;
}
hr,
hr:before {
background: radial-gradient(ellipse at center, $color1 0%, $color2 75%);
}
}
}
:
.fancy-line {
@include horizontal-line-styles(#e0afaf, #e0afaf);
}
CSSコードが生成され、以下:
.fancy-line hr {
border: 0;
height: 1px;
}
.fancy-line hr:after {
content: '';
height: 0.5em;
top: 1px;
}
.fancy-line hr:before,
.fancy-line hr hr:after {
content: '';
position: absolute;
width: 100%;
}
.fancy-line hr hr,
.fancy-line hr hr:before {
background: radial-gradient(ellipse at center, #ff0000 0%, #008000 75%);
}
最初の行:.fancy-line hr
はその要素hr
がfancy-line
クラスを持つ要素の内側になければならないことを意味名。しかし、hr
とこのクラス名:<hr class="fancy-line">
があります。したがって、これらのCSSルールは適用されません。
Css background
は、.fancy-line hr hr:before
に適用されます。この要素はありません。あなたのコードがうまくいかない理由。あなたは、生成されるいくつかの奇妙なルールを見ることができます:.fancy-line hr hr:after
、.fancy-line hr hr:before
、.fancy-line hr hr
。
私の考えは.fancy-line
要素(コード例#1)に直接background
を設定すると:before
と:after
要素を使用していないです。
質問した質問に間違いがありましたか? – Muzzleeeehso
@Muzzleeeehsoは私の答えを編集しました。 – 3rdthemagical
答えをありがとう、私はまだmixinを使用して私の時間のための望ましい外観を達成しない繰り返しコードに頼らざるを得なかった。 – Muzzleeeehso