如何用css制作网格(如图纸网格)?

Ami*_*deh 45 html css

如何用css制作网格(如图纸网格)?我只想制作一个只使用CSS的虚拟网格纸.在此先感谢您的帮助.

shu*_*111 84

在现代浏览器中,以下内容应该足够..

body {
  background-size: 40px 40px;
  background-image:
    linear-gradient(to right, grey 1px, transparent 1px),
    linear-gradient(to bottom, grey 1px, transparent 1px);
}
Run Code Online (Sandbox Code Playgroud)

http://codepen.io/anon/pen/VvPWvv

这将产生一个虚线网格..

body {
  background-size: 40px 40px;
  background-image: radial-gradient(circle, #000000 1px, rgba(0, 0, 0, 0) 1px);
}
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/anon/pen/xdqjRZ

http://caniuse.com/#search=linear-gradient

  • 如果某人的代码仍然无法正常工作,请尝试添加:background-repeat:repeat; (3认同)
  • 清晰又聪明,赞! (2认同)
  • 这对本地开发非常有用,我只是用它来确保我的 CSS 实现坚持设计网格,所以我不关心跨浏览器支持。我对我的用例做了一点修改,通过使用百分比而不是像素,无论视口宽度如何,我都会得到一个 16 列的网格:background-size: 6.25% 16pt; (2认同)

小智 35

body {
    background:
        linear-gradient(-90deg, rgba(0,0,0,.05) 1px, transparent 1px),
        linear-gradient(rgba(0,0,0,.05) 1px, transparent 1px), 
        linear-gradient(-90deg, rgba(0, 0, 0, .04) 1px, transparent 1px),
        linear-gradient(rgba(0,0,0,.04) 1px, transparent 1px),
        linear-gradient(transparent 3px, #f2f2f2 3px, #f2f2f2 78px, transparent 78px),
        linear-gradient(-90deg, #aaa 1px, transparent 1px),
        linear-gradient(-90deg, transparent 3px, #f2f2f2 3px, #f2f2f2 78px, transparent 78px),
        linear-gradient(#aaa 1px, transparent 1px),
        #f2f2f2;
    background-size:
        4px 4px,
        4px 4px,
        80px 80px,
        80px 80px,
        80px 80px,
        80px 80px,
        80px 80px,
        80px 80px;
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨,欢迎来到 SO!请尝试添加几行来解释为什么您认为您的答案最适合原始问题,谢谢。 (3认同)
  • 其他答案都很好,但这是很多人在搜索“方格纸”时想到的那种网格,谢谢。 (2认同)

Tem*_*fif 21

一个人conic-gradient()就能完成这项工作

html {
  background:
    conic-gradient(from 90deg at 1px 1px,#0000 90deg,blue 0) 
    0 0/50px 50px;
}
Run Code Online (Sandbox Code Playgroud)

另一个概念:

html {
  --s: 100px; /* control the size */
  
  --_g: #0000 90deg,#366 0;
  background: 
    conic-gradient(from 90deg at 2px 2px,var(--_g))
     0 0/var(--s) var(--s),
    conic-gradient(from 90deg at 1px 1px,var(--_g))
     0 0/calc(var(--s)/5) calc(var(--s)/5);
}
Run Code Online (Sandbox Code Playgroud)


cha*_*m15 16

既然你提到了衬纸:


background-color: #fff;
background-size: 100% 1.2em;
background-image: -webkit-linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px),
                  -webkit-linear-gradient(#eee .05em, transparent .05em);
background-image: -moz-linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px),
                  -moz-linear-gradient(#eee .05em, transparent .05em);
background-image: -ms-linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px),
                  -ms-linear-gradient(#eee .05em, transparent .05em);
background-image: -o-linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px),
                  -o-linear-gradient(#eee .05em, transparent .05em);
background-image: linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px),
                  linear-gradient(#eee .05em, transparent .05em);
-pie-background: linear-gradient(0deg, transparent 79px, #abced4 79px, #abced4 81px, transparent 81px) 0 0 / 100% 1.2em,
                 linear-gradient(#eee .05em, transparent .05em) 0 0 / 100% 1.2em #fff;
behavior: url(/PIE.htc);

Run Code Online (Sandbox Code Playgroud)

最后一行:behavior: url(/PIE.htc);是一个名为css3pie的插件,它增加了对6-9的支持,我相信.事实上,这个例子来自他们的网站,其中有更多有趣的例子:http://css3pie.com/demos/gradient-patterns/


Yi *_*ang 10

你能做的就是抓住像这样的网格图像

替代文字

然后用CSS拼贴它

#background {
  background: url('path/to/grid-image.png');
}
Run Code Online (Sandbox Code Playgroud)

所以是的,不仅是 CSS - 你还需要图像,但解决方案应该非常干净.在这里看到它:http://jsfiddle.net/bhVhV/

  • 从技术上讲,您只能使用以下方法来制作此CSS: (2认同)

Tim*_*ass 5

使用 svg 和 base64 完成。可以通过更改 svg 中的宽度、高度和颜色参数来修改比例和颜色。这是两个具有不同大小的蓝色和白色方形网格的示例。

.blue-square-grid-20px {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect width='100%' height='100%' fill='%230000ff' /%3E%3Crect x='50%' width='2' height='100%' fill='%231ff' /%3E%3Crect y='50%' width='100%' height='2' fill='%231ff' /%3E%3C/svg%3E%0A");
}

.white-square-grid-40px {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Crect width='40' height='40' fill='%23fff' /%3E%3Crect x='50%' width='1' height='100%' fill='%23ddd' /%3E%3Crect y='50%' width='100%' height='1' fill='%23ddd' /%3E%3C/svg%3E%0A");
}

.example-box {
  width: 100vw;
  height: 100px;
}
Run Code Online (Sandbox Code Playgroud)
<p>blue grid, 20px grid size and 2px line width</p>
<div class="blue-square-grid-20px example-box"></div>
<p>white grid, 40px grid size and 1px line width</p>
<div class="white-square-grid-40px example-box"></div>
Run Code Online (Sandbox Code Playgroud)