简体   繁体   English

CSS:重复的背景位置?

[英]CSS: background position with repeat?

I currently have an image repeating on y, but I want it to start 20 px down from where the div starts.... 我目前有一个重复y的图像,但我希望它从div开始的地方开始下降20 px ....

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. 也许你应该使用:after:before ,因为没有必要在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/ 检查这个http://jsfiddle.net/h6K9z/

It works in IE8 & above. 它适用于IE8及以上版本。 For IE7 or IE6 you can give extra DIV for this. 对于IE7或IE6,您可以为此提供额外的DIV

I prefer the shorthand method: 我更喜欢速记方法:

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

I don't think simply combining background-position and background-repeat eg repeat-y 0 20px will work. 我不认为简单地结合background-positionbackground-repeat例如repeat-y 0 20px将起作用。 Assuming this jsFiddle sample represents OP's case, the solution should make the green background starts at (vertical) 20px; 假设这个jsFiddle样本代表OP的情况,解决方案应该使绿色背景从(垂直)20px开始; the "Hello there!" “你好!” text shouldn't sit on top of the green background. 文字不应该位于绿色背景之上。

My answer is this cannot be done plainly by CSS; 我的答案是CSS无法做到这一点; meaning, you might want to change your HTML structure to accomplish your goal. 意思是,您可能希望更改HTML结构以实现目标。 However, I am starting a bounty hoping others to come up with better answers. 但是,我正在开始一个赏金,希望其他人能够提出更好的答案。

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

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

You just need the background-position property to be set: 您只需要设置background-position属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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