【问题标题】:How to create a responsive checkerboard background in CSS?如何在 CSS 中创建响应式棋盘背景?
【发布时间】:2019-04-01 12:30:15
【问题描述】:

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

我尝试了什么:

.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>

问题:

  • 我的结果不是响应式的。 (我有固定的像素大小)
  • 如果背景较大或元素较小,则需要大量代码。
  • 我没有实现居中模式或使用background-repeat: round

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

不是这样的(右下截断):

【问题讨论】:

  • 如果您使用固定像素值,它肯定不会响应...我会从那里开始。
  • 你想让元素居中吗?
  • 如果可能的话,我希望它们的尺寸调整为父尺寸,这样它们就不会被切断。但是将棋盘背景居中的想法听起来也不错!
  • 你试过在上面使用 flex 吗?显示:弹性;证明内容:中心;

标签: css background linear-gradients


【解决方案1】:

这里有一个想法,依赖conic-gradient

.chess {
  background:
    repeating-conic-gradient(
        #fff 0deg  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>

如果你想拥有相同的尺寸并且不被切断,你也可以考虑roundbackground-repeat

.chess {
  background:
    repeating-conic-gradient(
        #fff 0deg 90deg,
        grey 0deg 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>


为了获得更好的支持,您可以将渐变替换为 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>

【讨论】:

    【解决方案2】:

    使用这个:

    .chess {
      background-image:
        linear-gradient(45deg, #ccc 25%, transparent 25%),
        linear-gradient(-45deg, #ccc 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #ccc 75%),
        linear-gradient(-45deg, transparent 75%, #ccc 75%);
      background-size: 60px 60px;
      background-position: 0 0, 0 29px, 29px -29px, -29px 0px;
    }
    .btn {}
    
    .fas {
      color: black;
      text-align: center;
      font-size: 10em;
    }
    
    
    .fas.f5em {
      font-size: 5em;
    }
    
    .fas.f35em {
      font-size: 35em;
    }
    <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>
    
    <div>
      <div class="btn btn-outline-secondary chess">
      <i class="fas fa-car f5em"></i>
      </div>
    <div>
    
    <div>
      <div class="btn btn-outline-secondary chess">
      <i class="fas fa-car f35em"></i>
      </div>
    <div>



    或者你可以使用像这样的图案图像:

    https://mdn.mozillademos.org/files/5705/alpha.png
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2015-08-04
      相关资源
      最近更新 更多