如何使用HTML和CSS创建一个三角形网格

huangapple go评论74阅读模式
英文:

How to create a triangular grid using html and css

问题

如何创建像下面的图片一样的网格?我正在尝试制作一个可以用于我的项目的网格,但我真的不确定应该如何解决这个问题。

有人可以帮帮我吗?

图片

<div class="grid">
  <div class="triangle"></div>
  
  <div class="row2">
     <div class="triangle2"></div>
    <div class="triangle3"></div>
    <div class="triangle2"></div>
  </div>
  <!-- 根据需要添加更多三角形 -->
</div>
.grid {
  display: flex;
  flex-wrap: wrap;
}

.triangle {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  margin: 10px;
}

.triangle2 {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  margin: 10px;
}
英文:

How can I create a grid like the image below? I am trying to make a grid that can be used for my project but I am really unsure of how I should go about this problem.

Can somebody please help me?

PICTURE

<div class="grid">
  <div class="triangle"></div>
  
  <div class="row2">
     <div class="triangle2"></div>
    <div class="triangle3"></div>
    <div class="triangle2"></div>
  </div>
  <!-- Add more triangles as needed -->
</div>
.grid {
  display: flex;
  flex-wrap: wrap;
}

.triangle {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  margin: 10px;
}

.triangle2 {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  margin: 10px;
}

答案1

得分: 4

使用 clip-path 可以创建所需的三角形,并使用负的左右 margin 将它们并排放置。

  • 指向上的三角形:clip-path: polygon(50% 0%, 0% 100%, 100% 100%)
  • 指向下的三角形:clip-path: polygon(50% 100%, 0% 0%, 100% 0%)
  • 负边距:-1 x 三角形宽度的1/4

请注意:当需要边框时,之前建议的 SVG 解决方案可能是更好的选择,因为 clip-path 也会裁剪边框。

注意:确保在代码段中将鼠标悬停在三角形上。

英文:

With clip-path you can the create triangles you need and use a negative left and right margin to put them side by side.

  • triangle pointing up: clip-path: polygon(50% 0%, 0% 100%, 100% 100%)
  • triangle pointing down: clip-path: polygon(50% 100%, 0% 0%, 100% 0%)
  • negative margin: -1 x 1/4th of the width of a triangle

Beware: when you need borders, the previously adviced SVG solutions may be a better choice as clip-path clips borders as well.

Note: make sure to hover the triangles in the snippet.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-css -->

/* Grid setup */
.grid    { display: flex; flex-direction: column; justify-content: center; }
.row     { display: flex; justify-content: center; }
.row &gt; * { display: grid; place-items: center }

/* Triangle setup */
.grid { --triangle-size: calc(12.5vw + 1rem); /*  y=mx+b for points p1(160,36) p2(1920,256) */ }

.row &gt; :nth-child(odd)  { clip-path: polygon(50%   0%, 0% 100%, 100% 100%); background-color: LemonChiffon } /* up   */
.row &gt; :nth-child(even) { clip-path: polygon(50% 100%, 0%   0%, 100%   0%); background-color: LightBlue    } /* Down */

.row &gt; * {
    width : var(--triangle-size);
    height: var(--triangle-size);
    margin: 0 calc(-1 * var(--triangle-size)/4);
}

/* Action */
.row &gt; :hover { background-color: tomato }

<!-- language: lang-html -->

&lt;div class=&quot;grid&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div&gt;1&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div&gt;1&lt;/div&gt;
        &lt;div&gt;2&lt;/div&gt;
        &lt;div&gt;3&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div&gt;1&lt;/div&gt;
        &lt;div&gt;2&lt;/div&gt;
        &lt;div&gt;3&lt;/div&gt;
        &lt;div&gt;4&lt;/div&gt;
        &lt;div&gt;5&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div&gt;1&lt;/div&gt;
        &lt;div&gt;2&lt;/div&gt;
        &lt;div&gt;3&lt;/div&gt;
        &lt;div&gt;4&lt;/div&gt;
        &lt;div&gt;5&lt;/div&gt;
        &lt;div&gt;6&lt;/div&gt;
        &lt;div&gt;7&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 08:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定