带有十字图案的CSS网格

xwbd5t1u  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(119)

我在试着做一个十字形的格子。
到目前为止一切都很好,但我正在努力让十字架在外面的部门。
在这个例子中,你可以看到外面的十字架是“平的”,我想让他们完全一样的内部(=一个完整的十字架)。

编辑:我说的平是指它们不是一个完整的十字架。它们要么是一个角,要么是半个十字架。

有人有主意吗?
我的笔:
https://codepen.io/twohundred/pen/OJoObXw

<div class="container">
<div class="grid">
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
    <div><div></div></div>
</div>
* { box-sizing: border-box; }

body { 
    background-color: red; 
    color: white; 
    font-size: 1rem; 
} 

.container { 
    height: 100%;
    margin: 0 auto; 
    padding: 1.5em;
}

.grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-auto-rows: 101px;
}

.grid > div {
    position: relative;
}

/* Bottom right */
.grid > div:after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    display: block;
    border: 1px solid white;
    border-top: 0;
    border-left: 0;
    width: 1rem;
    height: 1rem;
}

.grid > div:before {
    content: '';
    position: absolute;
    top: -1px;
    left: -1px;
    display: block;
    border: 1px solid white;
    border-bottom: 0;
    border-right: 0;
    width: 1rem;
    height: 1rem;
}

.grid > div > div {
    height: calc(100% - 1rem);
    margin: 0.5rem;
}

.grid > div:nth-child(5n - 4) > div:before {
    content: '';
    position: absolute;
    bottom: 0;
    left: -1px;
    display: block;
    border: 1px solid white;
    border-top: 0;
    border-right: 0;
    width: 1rem;
    height: 1rem;
}

.grid > div:nth-child(5n - 5) > div:after {
    content: '';
    position: absolute;
    top: -1px;
    right: 0;
    display: block;
    border: 1px solid white;
    border-bottom: 0;
    border-left: 0;
    width: 1rem;
    height: 1rem;
}

/* Top row top right */
.grid > div:nth-child(-n + 5) >div:after {
    content: '';
    position: absolute;
    top: -1px;
    right: 0;
    display: block;
    border: 1px solid white;
    border-bottom: 0;
    border-left: 0;
    width: 1rem;
    height: 1rem;
}

.grid > div:nth-last-child(-n + 4) > div:before {
    content: '';
    position: absolute;
    bottom: 0;
    left: -1px;
    display: block;
    border: 1px solid white;
    border-top: 0;
    border-right: 0;
    width: 1rem;
    height: 1rem;
}
e7arh2l6

e7arh2l61#

我会用一个伪元素和渐变来做,诀窍是在所有伪元素之间有一个重叠,不要忘记考虑网格上差距,使计算变得容易。

body {
  background-color: red;
}

.container {
  padding: 2em;
}

.grid {
  --w: 25px; /* width of the line */
  --s: 1px;  /* thickness of the line */
  --c: #fff;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-auto-rows: 100px;
  gap: var(--s);
}

.grid > * {
  position: relative;
}
.grid > *:before {
  content:"";
  position: absolute;
  pointer-events: none;
  inset: calc((var(--s) + var(--w))/-2);
  background:
    conic-gradient(at var(--s) var(--w),#0000 75%,var(--c) 0)
     calc((var(--w) - var(--s))/2) 0,
    conic-gradient(at var(--w) var(--s),#0000 75%,var(--c) 0)
     0 calc((var(--w) - var(--s))/2);
  background-size: calc(100% - var(--w)) calc(100% - var(--w));
}
<div class="container">
  <div class="grid">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

相关问题