【问题标题】:What is wrong with this simple CSS and html chess layout? (Short code)这个简单的 CSS 和 html 国际象棋布局有什么问题? (短代码)
【发布时间】:2021-05-26 05:01:48
【问题描述】:

我正在制作一个棋盘,我的主要问题是在我设置了棋盘的前两行之后,典当的插入会改变深色方块的颜色。

所以棋盘具有“main”类并且颜色为白色,正方形 (class="box) 的颜色与棋盘相同(白色)或棕色。组件(棋子)是 SVG。添加后将棋子 ID 设置为正方形,它会使正方形颜色无效。这对于白色正方形很好,但对于棕色正方形则有问题。

这里是代码

body {
  background-color: grey;
}

.main {
  height: 50%;
  width: 70%;
  margin: 10% 15%;
  background: white;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.box {
  height: 100%;
  width: 100%;
  border: 1px solid black;
}

.box:nth-child(even) {
  background: burlywood;
}

#pawn {
  background: url(https://svgsilh.com/svg/3413417.svg) center;
  background-size: cover;
}
<html lang="en">

<head>
  <link rel="stylesheet" href="styles.css">
  <title>Document</title>
</head>

<body>

  <div class="main">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
  </div>
</body>

</html>

【问题讨论】:

  • #ids 必须是唯一的。所以为每个棋子重做#id。 (例如pawn1pawn2 等)。此外,background-size: cover 将使图像拉伸到其容器的所有侧面,从而覆盖它。
  • 顺便说一句,你真的看到你的例子的结果了吗?它与描述的完全不同。
  • 在可视化代码或 jsfiddle 上试试。它不适用于 sn-p。
  • 将链接发布到小提琴。
  • grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr; 将组成 9 列 -- 棋盘有 8 列。

标签: html css chess


【解决方案1】:

如果你想让 height 和 width % 起作用,你应该给它的 parent 一个 height 和 width ,因为这就是他们将作为 100% 的值。在这种情况下,您在 main 中有 % 但在 body 标记中没有说明宽度或高度。

在这种情况下,您不能使用那些 %,所以我使用静态 px 以便更快地执行此操作并且不重新构建 html。

您在使用背景时遇到的问题是,当您将图像添加为背景时,它会覆盖以前的背景,因此为了获得具有样式背景的透明图像,您必须将其添加为具有该背景的容器。

我还更改了主标签的主类并替换了id pawn,因为你不应该有多个具有相同id的标签,换句话说,它应该是唯一的,如果你想使用更多,使用一个类.

body {
  background-color: grey;
  width: 100vw;
  height: 100vh;
}

main {
  height: 100px;
  width: 450px;
  margin: 10% 15%;
  background: white;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.box {
  height: 48px;
  width: 48px;
  border: 1px solid black;
}

.box:nth-child(even) {
  background: burlywood;
}

img{
width:100%;
height:100%;
}
<html lang="en">

<head>
  <link rel="stylesheet" href="styles.css">
  <title>Document</title>
</head>

<body>

  <main>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
  </main>
</body>

</html>

【讨论】:

  • 另外,你不应该为多个标签使用相同的id,而不是使用名为“main”的类,你可以使用标签
  • 这仍然会使正方形的背景颜色无效,因此棋子似乎都在白色正方形上。
  • 谢谢。你当然可以有多个背景图片,所以你不需要在 HTML 中添加 img 元素。
  • 谢谢,非常感谢。我正在尝试制作国际象棋游戏,而 CSS 棋盘设置似乎是一场噩梦。我已经准备好了国际象棋游戏算法,但是 css 似乎很麻烦。
【解决方案2】:

您的错误是您将 svg 作为背景图片导入。所以它不再是一个真正的svg了。此 svg 图片覆盖 box 属性。 您可以通过将 svg 一张一张放在 div 中来实现您想要的。祝你好运!

    <head>
      <link rel="stylesheet" href="styles.css">
      <title>Document</title>
    </head>
    <style>
        body {
      background-color: grey;
    }
    
    .main {
      height: 50%;
      width: 70%;
      margin: 10% 15%;
      background-color: white;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
    }
    
    .box {
      height: 100%;
      width: 100%;
      border: 1px solid black;
    }
    
    .box:nth-child(even) {
      background: burlywood;
    }
    /*
    #pawn {
      background: url(https://svgsilh.com/svg/3413417.svg) center;
      background-size: cover;
    }*/
    </style>
    <body>
    
      <div class="main">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
        <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
   
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0"width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
   
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%" viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
        <div class="box" id="pawn"><svg xmlns="http://www.w3.org/2000/svg" version="1.0" width="100%" height="100%"  viewBox="0 0 1280.000000 1280.000000" preserveAspectRatio="xMidYMid meet">
    
    <g transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
    <path d="M6240 9660 c-210 -37 -369 -113 -507 -244 -142 -134 -246 -294 -278 -427 -19 -83 -19 -215 1 -319 42 -221 151 -394 326 -517 84 -59 128 -104 128 -132 0 -27 -37 -69 -78 -90 -21 -10 -93 -37 -162 -61 -141 -48 -277 -111 -315 -146 -19 -17 -25 -33 -25 -63 0 -66 17 -88 124 -159 273 -179 387 -297 461 -481 60 -148 91 -444 75 -725 -15 -251 -48 -401 -126 -561 -76 -159 -177 -271 -394 -438 -98 -75 -103 -83 -125 -182 -10 -47 -72 -98 -146 -120 -199 -58 -350 -173 -472 -358 -65 -97 -100 -178 -127 -290 -37 -154 -25 -237 46 -316 26 -29 44 -58 42 -68 -2 -10 -37 -36 -78 -58 -95 -53 -129 -95 -136 -166 -14 -146 92 -208 445 -259 805 -116 2540 -100 3129 29 214 47 282 98 282 211 0 83 -41 135 -150 190 -49 25 -70 45 -70 66 0 2 18 24 40 48 53 58 70 102 70 183 0 321 -278 690 -586 778 -128 37 -166 72 -185 167 -13 66 -7 59 -208 219 -181 144 -292 299 -360 504 -47 144 -62 256 -68 503 -10 422 37 637 182 830 63 84 138 148 313 267 131 89 148 104 161 141 13 37 12 44 -5 80 -16 33 -32 46 -113 85 -51 26 -150 66 -220 89 -194 66 -234 92 -234 150 0 38 26 66 124 135 218 153 340 398 340 680 0 161 -45 287 -152 428 -181 239 -398 365 -702 406 -103 14 -142 13 -267 -9z"/>
    </g>
    </svg></div>
    
    
      </div>
    </body>
    
    </html>

【讨论】:

  • 嗨,AFAIK 通常使用 svg 作为背景很好 - 你能解释一下为什么它不再是 svg 了吗?
【解决方案3】:

在将 pawn SVG 设置为背景时,您将摆脱其他背景设置 - 在本例中是交替方块的背景。

更明确的背景颜色和背景图像设置将确保颜色不会被覆盖。 SVG 本身具有透明背景,因此正方形的背景颜色会显示出来。

为了确保无论片段的 SVG 的纵横比如何,它都在正方形中,使用包含而不是覆盖,并将背景图像设置为不重复并居中。

body {
  background-color: grey;
}

.main {
  height: 50vmin;
  width: 50vmin;
  margin: 10% 15%;
  background: white;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.box {
  height: 100%;
  width: 100%;
  border: 1px solid black;
}

.box:nth-child(even) {
  background-color: burlywood;
}

#pawn {
  background-image: url(https://svgsilh.com/svg/3413417.svg);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
}
<div class="main">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
    <div class="box" id="pawn"></div>
  </div>

body {
  background-color: grey;
  width: 100vw;
  height: 100vh;
}

main {
  height: 100px;
  width: 450px;
  margin: 10% 15%;
  background: white;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.box {
  height: 48px;
  width: 48px;
  border: 1px solid black;
}

.box:nth-child(even) {
  background: burlywood;
}

img{
width:100%;
height:100%;
}
<html lang="en">

<head>
  <link rel="stylesheet" href="styles.css">
  <title>Document</title>
</head>

<body>

  <main>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
    <div class="box"><img class="pawn" src="https://svgsilh.com/svg/3413417.svg" alt="pawn"></div>
  </main>
</body>

</html>

给定代码中还有其他问题。您可能想研究使用 vmin 作为一个单位来定义一个正方形(因为这将确保响应性,无论视口的纵横比如何)以及网格的使用有点奇怪(需要 9 个文件)。通过删除网格并将 8 个正方形包含在具有类等级的 div 中,将布局与棋盘相关联可能更容易。这将使着色更简单,并确保布局是方形的,并且在将来添加更多等级时会有所帮助。但是,所有这些都是不同问题的主题。

【讨论】:

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