简体   繁体   中英

how to set background image to pseudo elements using percent instead of pixel?

demo

the key 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;
}

I wanted to set background-image inside gray-colored box with 100% width and 100% height right after using pseudo :before

edit

I want to do like this

在此处输入图片说明

but not like this

在此处输入图片说明

Or not like this

在此处输入图片说明

Now you can write this 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 With Background-size:cover

please use this code

http://jsfiddle.net/YUj5W/4/

.tab-contents{
    background: #2b2a26;
    padding: 0px 8px;
    clear: both;
}


#tabs {
    border-collapse: separate;
    border-spacing: 4px 0;
    float: right;
    list-style: none outside none;
    margin: 0 -4px 0 0;
    padding: 0;
}
#tabs li {
    background: none repeat scroll 0 0 #000000;
    border-radius: 19px 19px 0 0;
    display: table-cell;
    height: 47px;
    margin: 0 4px;
    text-align: center;
    vertical-align: middle;
    width: 145px;
}
#tab-1 {position:relative;}

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

    top: 10px;
    left:0;
    background-size:cover;
    position: relative;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM