如何在不填充内容的情况下向重复的 CSS 背景添加填充?
有没有什么方法可以填充重复的背景图像,而不会同时向内容添加填充?
我有一个使用浮动创建的 3 列布局,以及一个清除元素以确保容器的高度正确。下面是代码:
HTML
<div id="container">
<div id="col1" class="col">col 1</div>
<div id="col2" class="col">col 2</div>
<div id="col3" class="col">col 3</div>
<div class="clearFix"></div>
</div>
CSS
div#container {
width:340px;
border:1px solid #0f0;
margin:30px;
background:url(https://i.sstatic.net/PEE4K.png) 0 0 repeat-y;
}
div.col {
float:left;
width:100px;
height:60px;
margin:0px 10px;
background-color:#f00;
}
div#col1 {
margin-left:0;
}
div#col3 {
margin-right:0;
}
div.clearFix {
clear:both;
}
当与我的重复图像一起使用时:
它看起来像这样:
< img src="https://i.sstatic.net/MBjio.png" alt="在此处输入图像描述">
但我想调整重复图像的起点和点,以便出现填充,如下所示:
<图像src="https://i.sstatic.net/vEVwd.png" alt="在此处输入图像描述">
重要的是,当列缩放时,填充也会移动:
有谁知道如何实现这一点,最好没有额外的标记,并且绝对没有 JavaScript?
Is there any way to pad a repeating background image that will not also add padding to the content?
I have a 3 column layout created using floats, and a clearing element to ensure the container is the right height. Here is the code for that:
HTML
<div id="container">
<div id="col1" class="col">col 1</div>
<div id="col2" class="col">col 2</div>
<div id="col3" class="col">col 3</div>
<div class="clearFix"></div>
</div>
CSS
div#container {
width:340px;
border:1px solid #0f0;
margin:30px;
background:url(https://i.sstatic.net/PEE4K.png) 0 0 repeat-y;
}
div.col {
float:left;
width:100px;
height:60px;
margin:0px 10px;
background-color:#f00;
}
div#col1 {
margin-left:0;
}
div#col3 {
margin-right:0;
}
div.clearFix {
clear:both;
}
When used with my repeating image:
It looks like this:
But I would like to adjust the start and points of the repeating image so that padding appears, like this:
It is important that when the columns scale the padding moves too:
Does anyone have an idea how to achieve this, preferably without additional markup and definitely without JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 div.col css 定义中更改此设置:
正如 @BoltClock 所说,如果您还需要绿色边框,则这不起作用。如果你不需要边框,即使在 ie6 中也应该可以正常工作。
change this in your div.col css definition:
as @BoltClock remarked, this doesn't work if you need the green border as well. if you don't need the border this should work fine even in ie6.