CSS pseudo-element size issue

两盒软妹~` 提交于 2020-01-03 08:41:37

问题


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

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/


回答1:


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.



来源:https://stackoverflow.com/questions/18482219/css-pseudo-element-size-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!