如何在使用背景图片时平移偶数行。

huangapple go评论45阅读模式
英文:

How can even rows will be shifted while using background-image

问题

我正在尝试使用`background-image`来创建一个 div使用以下代码
.square {
        width: 1000px;
        height: 1000px;
        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-repeat: repeat;
      }

但我得到了一排垂直线我想将偶数行向右移动一些像素而不是让所有线都垂直对齐我尝试了这段代码
.square:nth-child(even) {
        background-size: 10px right;
      }

但它不起作用有什么解决方法吗

我尝试创建类似棋盘格的图案使用了以下代码

.container {
        width: 1000px;
        height: 1000px;
        margin:0;
        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>'), 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-repeat: no-repeat;
        background-position:  left 40px top, left 40px
    }
英文:

I'm trying to use background-image to create a div using this code:

square {
        width: 1000px;
        height: 1000px;
        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-repeat: repeat;
      }

But I got a block of vertical lines aligned. I want to shift the even rows by some pixels to the right and not having all the lines aligned vertically.
I tried this code:

 .square:nth-child(even) {
        background-size: 10px right;
      }

But it does not works. Is there any solution to fix that?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  .square {
        width: 1000px;
        height: 1000px;
        background-image: url(&#39;data:image/svg+xml;utf8,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;100&quot; height=&quot;40&quot;&gt;&lt;image width=&quot;40&quot; height=&quot;40&quot; xlink:href=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=&quot; /&gt;&lt;/svg&gt;&#39;);
        background-repeat: repeat;
      }
      
      .square:nth-child(even) {
        background-size: 10px right;
      }

<!-- language: lang-html -->

 &lt;div class=&quot;square&quot;&gt;
    &lt;/div&gt;

<!-- end snippet -->

Updated:

I tried to create a pattern similar to checkboard pattern using this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
    width: 1000px;
    height: 1000px;
   margin:0;

    background-image: url(&#39;data:image/svg+xml;utf8,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;100&quot; height=&quot;40&quot;&gt;&lt;image width=&quot;40&quot; height=&quot;40&quot; xlink:href=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=&quot; /&gt;&lt;/svg&gt;&#39;), url(&#39;data:image/svg+xml;utf8,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;100&quot; height=&quot;40&quot;&gt;&lt;image width=&quot;40&quot; height=&quot;40&quot; xlink:href=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=&quot; /&gt;&lt;/svg&gt;&#39;);
    background-repeat: no-repeat;
    background-position:  left 40px top, left 40px
}

<!-- language: lang-html -->

&lt;div class=&#39;container&#39;&gt;
&lt;/div&gt;

<!-- end snippet -->

However, when I set the background-repeat: repeat the result is not what I'm looking for:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
    width: 1000px;
    height: 1000px;
   margin:0;

    background-image: url(&#39;data:image/svg+xml;utf8,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;100&quot; height=&quot;40&quot;&gt;&lt;image width=&quot;40&quot; height=&quot;40&quot; xlink:href=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=&quot; /&gt;&lt;/svg&gt;&#39;), url(&#39;data:image/svg+xml;utf8,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;100&quot; height=&quot;40&quot;&gt;&lt;image width=&quot;40&quot; height=&quot;40&quot; xlink:href=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAFVJREFUeNrs2EENgEAQBMGBoOOsgLQTgaB1dV9Cggf2Ua2g3r2te5xJrvSsjg83mwLnnuYBAgICAgICAgICAgICAgICAgICAgIC/tV7+St9L389AgwA9YgGfH7VrXwAAAAASUVORK5CYII=&quot; /&gt;&lt;/svg&gt;&#39;);
    background-repeat: repeat;
    background-position:  left 40px top, left 40px
}

<!-- language: lang-html -->

&lt;div class=&#39;container&#39;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

使用蒙版来实现这个:

.container {
  --size: 50px; /* 图案的尺寸 */

  height: 100vh;
  background: 
   url(https://picsum.photos/id/1069/200/300) /* 在此添加你自定义的 SVG */
   0 0/var(--size) var(--size);
  -webkit-mask: 
   repeating-conic-gradient(#0000 0 25%, #000 0 50%)
   0 0/calc(2*var(--size)) calc(2*var(--size));
}

body {
 margin: 0;
}
<div class='container'></div>
英文:

Use mask to do this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  --size: 50px; /* the size of the pattern*/

  height: 100vh;
  background: 
   url(https://picsum.photos/id/1069/200/300) /* your custom SVG here */
   0 0/var(--size) var(--size);
  -webkit-mask: 
   repeating-conic-gradient(#0000 0 25%,#000 0 50%)
   0 0/calc(2*var(--size)) calc(2*var(--size));
}


body {
 margin: 0;
}

<!-- language: lang-html -->

&lt;div class=&#39;container&#39;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 21:48:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定