Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Before pseudo element not showing

Tags:

html

css

div {
    width: 100px;
    height: 100px;
    background: red;
}

div:before {
    content: "";
    width: 100px;
    height:30px;
    background: yellow;
}

Why isn't the before pseudo element showed above the div element when the position values (relative and absolute respectively) are not set?

like image 681
LearningMath Avatar asked Jun 03 '26 22:06

LearningMath


1 Answers

The ::before and ::after pseudo-elements are display:inline by default, which width and height will not affect.

You can set the pseudo-element to display:block.

div {
  width: 100px;
  height: 100px;
  background: red;
}

div:before {
  content: "";
  display: block;
  width: 100px;
  height: 30px;
  background: yellow;
}
<div></div>

View on JSFiddle

Also see What is the default display property of the :before and :after pseudo-elements?

like image 125
showdev Avatar answered Jun 05 '26 13:06

showdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!