开发者

CSS: background position with repeat?

I currently ha开发者_开发百科ve an image repeating on y, but I want it to start 20 px down from where the div starts....

how do I do that?


Maybe you should use :after or :before for this, because there's no need to add extra markup in your HTML. Like this:

.container{
    height:400px;
    background:red;
    position:relative;
    width:400px;
}
.container:after{
    content:"";
    position:absolute;
    top:20px;
    left:0;
    right:0;
    bottom:0;
    background:url(http://lorempixel.com/400/200) repeat-y;
}

Check this http://jsfiddle.net/h6K9z/

It works in IE8 & above. For IE7 or IE6 you can give extra DIV for this.


I don't think simply combining background-position and background-repeat e.g. repeat-y 0 20px will work. Assuming this jsFiddle sample represents OP's case, the solution should make the green background starts at (vertical) 20px; the "Hello there!" text shouldn't sit on top of the green background.

My answer is this cannot be done plainly by CSS; meaning, you might want to change your HTML structure to accomplish your goal. However, I am starting a bounty hoping others to come up with better answers.


I prefer the shorthand method:

.yourClass {
    background: url(../images/yourImage.png) repeat-y 0 20px;
}


.yourClass{
    background: url(images/yourImage.png) repeat-y;
    background-position: 0px 20px;
}

http://www.w3schools.com/css/pr_background-position.asp


You just need the background-position property to be set:

background-position: 0px 20px;
background-repeat: repeat-y;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜