0

There must be something that I am missing, but I am trying to use ::after in my css, but unfortunately it isn't working.

My css code is below.

.test {
  height: 40px;
  width: 40px;
  background: #444;
}
.test::after {
  position: relative;
  top: 15px;
  height: 240px;
  width: 240px;
  background: red;
}
<div class="test"></div>

6
  • 1
    It's not working is not s sufficient problem description. Commented Oct 4, 2016 at 16:46
  • after requires always content:'' ... and something to give dimensions like display or absolute/fixed position Commented Oct 4, 2016 at 16:46
  • @PeeHaa you are correct, but i didn't know that it always requires content: '' and I'm sure others don't either, but you are correct, it is a bad question and is sure to be downvoted Commented Oct 4, 2016 at 16:49
  • @DaniP does it also always need to be set to position: absolute? Commented Oct 4, 2016 at 16:50
  • 1
    NO .. as I mention you need any property that gives the element box behavior like display. Commented Oct 4, 2016 at 16:51

1 Answer 1

2

You just need add content: '' to pseudo-class :after or :before and set the position to absolute.

.test {
  height: 40px;
  width: 40px;
  background: #444;
  position:relative;
}
.test:after {
  position: absolute;
  top: 15px;
  height: 240px;
  width: 240px;
  background: red;
  content: ''
}
<div class="test"></div>

but if you want you can use it without absolute, just add some float to it, because pseudo-classes generates like inside the parent node.

.test {
  height: 40px;
  width: 40px;
  background: #444;
  position:relative;
}
.test:after {
  content: '';
  background: red;
  width: 10px;
  height: 10px;
  float: right;
  
}
<div class="test"></div>

But if you need use it like icon, inside the block better way use it with absolute.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! does position always need to be set at absolute?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.