Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS pseudo-element size issue

Please tell me why :before pseudo-element doesn't behave like a regular img in this case:

enter image description here

Left one is div with an img inside and img's width and height are equals 100% . Right one is div with :before and :before's width and height are also 100% , but effect is different!

(I know i can use a background-image workaround , but what is wrong with :pseudo when its content property is url() ?)

Fiddle: http://jsfiddle.net/Tp9JG/4/

like image 536
Ivan Chernykh Avatar asked Aug 28 '13 07:08

Ivan Chernykh


People also ask

Does Z Index work on pseudo elements?

Does Z Index working on pseudo-element? Pseudo-elements are treated as descendants of their associated element. That means by default (with no positioning, z-index or opacity applied) they sit above their parent in the stacking order.

How do you align pseudo elements?

You need to position the :before pseudo-element absolutely by using the well known centering technique (use the top, left, and transform properties). Here, -25px will offset the text above the circle. Note that for the <span> element, we set the position property to its "relative" value and the display to "block".

What is the difference between pseudo-class and pseudo-element?

Pseudo-classes enable you to target an element when it's in a particular state, as if you had added a class for that state to the DOM. Pseudo-elements act as if you had added a whole new element to the DOM, and enable you to style that.

Is () CSS pseudo-class?

The :is() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. This is useful for writing large selectors in a more compact form.


1 Answers

Unfortunately you cannot control the size of the image when specifying it through content, But you can if you're using it as background:

.with_before:before{
    content:'';
    background-image: url('http://i.stack.imgur.com/CAAFj.jpg');
    background-size: 100% 100%;
    width: inherit;
    height: inherit;
    display: inline-block;
}

check this jsFiddle

And for your question why we can't style generated content: We can't because generated content is rendered into a generated box, and you can style that box, but not the content.

references:

  1. check this post
  2. another lengthy discussion on this subject

please notice that different browsers show very different behaviors.

like image 153
Tomzan Avatar answered Sep 17 '22 11:09

Tomzan