html - 如何使用百分比而不是像素将背景图像设置为伪元素?

标签 html css pseudo-element

demo

关键CSS

#tab-1:before {
    content:"";
    display: block;
    width: 100%;
    height: 100%;
    background: url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
    top: 10px;
    position: relative;
}

我想在使用伪 :before 之后立即在 100% 宽度和 100% 高度的灰色框中设置背景图像

编辑

我想这样做

enter image description here

但不是这样的

enter image description here

或者不是这样

enter image description here

最佳答案

现在你可以写这个css了

#tab-1{

position: relative;   z-index:1; 
}
#tab-1:before{
    content: "";
    background:  url("http://t3.gstatic.com/images?q=tbn:ANd9GcQQxJ4VT26y0vXV4ea0BVugIdFEJ3BhnZByh13xvD-LbWPocNCHHw") no-repeat;
    top: 10px;
    position:absolute;
    left:0;right:0;top:0;bottom:0;z-index:-1;

}

Demo

======================

Demo 2 使用 Background-size:cover

关于html - 如何使用百分比而不是像素将背景图像设置为伪元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939749/

相关文章:

html - Angular ng-repeat 到 PDF 或打印机 CSS 假定对齐问题

java - 从Processing3.4插入HTML(java)

html - YouTube 缩略图破坏了 maxres 使用

html - 我可以使用 html 或 css 进行图像方向检测吗?

css - 父级 :before pseudoelement with child alt attribute as content

使用 :after psuedo element displaying as a rectangle instead in IE8 的 CSS 三 Angular 形

css - 使用 :before pseudo element 插入非 ASCII 字符

html - 当 ng-view 内容大于窗口高度时出现边框问题

javascript - 下拉菜单打乱其他标签

html - 基于 CSS 内容的布局