css - 响应棋盘图案背景

标签 css background linear-gradients

我想要一个响应式棋盘背景,看起来应该像颜色选择器中的背景:

Wanted result

我希望图案始终显示 N 个每宽度的正方形(动态)。

我尝试了什么:

.chess {
  background-image: 
    linear-gradient(90deg, #999 30px, white 30px), 
    linear-gradient(90deg, white 30px, #999 30px), 
    linear-gradient(90deg, #999 30px, white 30px), 
    linear-gradient(90deg, white 30px, #999 30px), 
    linear-gradient(90deg, #999 30px, white 30px), 
    linear-gradient(90deg, white 30px, #999 30px), 
    linear-gradient(90deg, #999 30px, white 30px);
  background-position: 0 0, 0 30px, 0 60px, 0 90px, 0 120px;
  background-repeat: repeat-x;
  background-size: 60px 30px;
  height: 150px;
  margin: 15px auto;
  width: 30%;
}


.fas {
  text-align: center;
  font-size: 10em
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div>
  <div class="btn btn-outline-secondary chess">
  <i class="fas fa-car"></i>
  </div>
<div>

如果容器不是 1:1 的长宽比,则正方形不是 1:1 的长宽比,这不是响应式的:

div {
  width: 300px;
  height: 100px;
  resize: auto;
  overflow: hidden;

  aspect-ratio: 1/2;
  border: 1px solid red;
  background: repeating-conic-gradient(transparent 0 90deg, grey 0 180deg) 
              0 0 / 25% 25% round;
}
<div></div>

问题:

  • 我的结果没有响应。 (我有固定的像素大小)
  • 如果我有更大的背景或更小的元素,我将需要大量代码。
  • 我无法将图案居中或使用 background-repeat: round

如果可能的话,我不希望在棋盘背景中有任何截断。

不是这样的(从右边和底部切掉):

enter image description here

最佳答案

这是一个依赖conic-gradient的想法:

.chess {
  background:
    repeating-conic-gradient(
        #fff 0 90deg,
        grey 0 180deg) 
    0 0/25% 25%;
  margin: 15px;
  padding:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet"/>

<div class="chess fas fa-7x fa-car"></div>
<div class="chess fas fa-5x fa-car"></div>
<div class="chess fas fa-10x fa-user"></div>
<div class="chess fas fa-3x fa-phone"></div>

CSS responsive checkerboard

你也可以考虑 roundbackground-repeat 如果你想有相同的大小并且没有被切断:

.chess {
  background:
    repeating-conic-gradient(
        #fff 0 90deg,
        grey 0 180deg) 
    0 0/40px 40px round;
  margin: 15px;
  padding:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet"/>

<div class="chess fas fa-7x fa-car"></div>
<div class="chess fas fa-5x fa-car"></div>
<div class="chess fas fa-10x fa-user"></div>
<div class="chess fas fa-3x fa-phone"></div>

CSS chessboard reasponsive


为了更好的支持,您可以用 SVG 替换渐变以获得相同的结果:

.chess {
  background:
    url('data:image/svg+xml;utf8,<svg preserveAspectRatio="none"  viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"><rect x="0" y="0" width="5" height="5" fill="grey" /><rect x="5" y="5" width="5" height="5" fill="grey" /><rect x="5" y="0" width="5" height="5" fill="white" /><rect x="0" y="5" width="5" height="5" fill="white" /></svg>') 
    0 0/25% 25%;
  margin: 15px;
  padding:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet"/>

<div class="chess fas fa-7x fa-car"></div>
<div class="chess fas fa-5x fa-car"></div>
<div class="chess fas fa-10x fa-user"></div>
<div class="chess fas fa-3x fa-phone"></div>

固定尺寸​​:

.chess {
  background:
    url('data:image/svg+xml;utf8,<svg preserveAspectRatio="none"  viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg"><rect x="0" y="0" width="5" height="5" fill="grey" /><rect x="5" y="5" width="5" height="5" fill="grey" /><rect x="5" y="0" width="5" height="5" fill="white" /><rect x="0" y="5" width="5" height="5" fill="white" /></svg>') 
    0 0/40px 40px round;
  margin: 15px;
  padding:10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet"/>

<div class="chess fas fa-7x fa-car"></div>
<div class="chess fas fa-5x fa-car"></div>
<div class="chess fas fa-10x fa-user"></div>
<div class="chess fas fa-3x fa-phone"></div>

关于css - 响应棋盘图案背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55455177/

相关文章:

php - 如何使用 php-url 路径在 Magento CMS 页面中加载图像背景?

ios - Swift:如何创建带有彩色 block 矩形背景的文本

swift - 如何在单击时突出显示 UIButton 的背景和 "unhighlight"呢?

css - Web 标记,背景颜色设置为基础

css - &lt;style&gt; 在 <head> 之外在页面上呈现

html - Wave 可访问性工具显示对齐文本的警报

css - 如何防止背景和渐变之间出现 1 像素线?

Android:如何制作具有多色渐变的可绘制对象?

css - 线性渐变重叠时渲染的规则是什么?

javascript - 当从 css 方法中的属性中删除引号时,jQuery 停止工作