「:before」「:after」が効かなかった…~解決~

12月に入って5日目。

インフルエンザがどんどん流行している中、体調管理はしっかりしてくださいね。

いろんなところにクリスマスシーズン!

今年のクリスマスは賑やかになれることを祈ってます。

さてと、話題を取り戻して…

「:before」「:after」が効かなかった…

:before」、「:after」の疑似要素の解消する方法を伝授しますね。

div {
	margin: 50px auto;
	width: 50%;
}
div h1:before {
	border-bottom: 2px solid #191970;
	bottom: -2px;
	content: '';
	left: 0;
	margin: auto;
	padding: 0;
	position: absolute;
	width: 100px;
}

div h1:first-of-type:before {
	border-bottom: 2px solid #556b2f;
}

div h1{
	border-bottom: 2px solid #d3d3d3;
	margin: 0 auto 10px;
	padding: 0 0 3px;
	position: relative;
}
div h2:before{
	background: #ffa500;
	content: '';
	height: 100%;
	left: 0;
	margin: auto;
	padding: 0;
	position: absolute;
	top: 0;
	width: 5px;
}

div h2:last-of-type:before{
	background: #00bfff;
}
div h2{
	margin: 0 auto 10px;
	padding: 0 0 0 10px;
	position: relative;
}
div p {
	margin: 0 0 30px;
}

実行結果

:before」、「:after」の要素を付けるなら一番最後に配置しないとだめなんですね。

これで、すっきりしました。

では、また!

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください