简体   繁体   English

CSS,背景重复距离

[英]CSS, background-repeat distance

I want to set a background-image but with an specific distance between the repetitions. 我想设置background-image但重复之间有一个特定的距离。

For example, I have this image to become a background-image : 例如,我将此图像变为background-image

在此输入图像描述

And I want to fix the repetitions in a pattern like this: 我希望以这样的模式修复重复:

在此输入图像描述

Check the JSFiddle playground 检查JSFiddle游乐场

I'm looking for a CSS3 clean solution, not JS, not extra elements, and so forth. 我正在寻找一个CSS3清洁解决方案,而不是JS,而不是额外的元素,等等。 If I have to use very modern CSS3 (un-supported) tricks is ok. 如果我必须使用非常现代的CSS3(不支持)技巧是可以的。

I guess the ship has pretty much sailed, but still there is a solution based on data-URI. 我想这艘船几乎已经航行了,但仍有一种基于data-URI的解决方案。

You can generate an SVG containing your image which has a size greater than the image (eg to make the margin 60px just make a width equal to the width of the image (40px) + the desired margin (60px) = 100px): 您可以生成包含尺寸大于图像的图像的SVG(例如,使边距60px的宽度等于图像的宽度(40px)+所需的边距(60px)= 100px):

The next step is embedding your image inside of the SVG: 下一步是将您的图像嵌入SVG中:

<image width="40" height="40" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=" />

And finally add this SVG as a background-image with the data URI: 最后将此SVG添加为带有数据URI的背景图像:

#container {
    width: 300px;
    height: 100px;
    border: 1px solid #ccc;

    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="40"><image width="40" height="40" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=" /></svg>');
    background-position: left center;
    background-repeat: repeat-x;
}

See the updated Fiddle . 查看更新的小提琴

One can say this is quite a big and dirty workaround. 可以说这是一个非常大而且肮脏的解决方法。 However this is a pure CSS solution which does not require creating of additional HTML elements / JavaScript. 但是,这是一个纯CSS解决方案,不需要创建其他HTML元素/ JavaScript。 It has some disadvantages: 它有一些缺点:

  1. You need to embed the image with the data-URI. 您需要使用data-URI嵌入图像。 This is probably not the thing you would like to do often. 这可能不是你想经常做的事情。
  2. The solution looks a bit heavy. 解决方案看起来有点沉重。
  3. For IE9 you would need to encode the SVG as a URL. 对于IE9,您需要将SVG编码为URL。

Possible solution to all these problems is using CSS preprocessors, eg Sass. 所有这些问题的可能解决方案是使用CSS预处理器,例如Sass。 You can make a mixin, eg: 你可以制作混音,例如:

@mixin background-image-spaced($width, $height, $margin-right, $image) {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="#{$width + $margin-right}" height="#{$height}"><image width="#{$width}" height="#{$height}" xlink:href="data:image/png;base64,#{$image}" /></svg>');
}

body {
  @include background-image-spaced(40px, 40px, 60px, 'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=');
}

You can add here many things, eg all other margin values, you can encode the image to inject it instead of writing it manually using Compass, see eg this article to learn more about this process. 您可以在这里添加许多内容,例如所有其他边距值,您可以对图像进行编码以注入它,而不是使用Compass手动编写它,请参阅本文以了解有关此过程的更多信息。 This already looks way better and more comfortable to use. 这已经看起来更好,更舒适。

You need to use the space attribute in the background-repeat (CSS3) 你需要在background-repeat(CSS3)中使用space属性

http://www.impressivewebs.com/space-round-css3-background/ http://www.impressivewebs.com/space-round-css3-background/

Not all browsers support it, and it looks like its only for vertical space by the sample pictures. 并非所有浏览器都支持它,并且它看起来像样本图片仅用于垂直空间。

The better chose would be to modify the background image itself to have the needed space around the actual image (padding the image with blank area) 更好的选择是修改背景图像本身以获得实际图像周围所需的空间(用空白区域填充图像)

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

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