创建一个分成4个三角形的全宽网页。

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

Create a fullwidth web page that split into 4 triangles

问题

我正在使用HTML和CSS,尝试将网页分为4个三角形,它们在屏幕中央相交。

到目前为止,这是我得到的最佳结果。

我的Codepen

每次我尝试将宽度减半时,一切都乱了。

我的想法是在中间显示4个相交的三角形。

我该如何实现这样的结果?

非常感谢。

英文:

I'm using HTML and CSS and trying to split a webpage into 4 triangles that join in the middle of the screen.

So far there is the best result I got.

My Codepen

body {
    margin: 0;
    padding: 0;
  }
  
  .container {
    position: relative;
    height: 100vh;
    overflow: hidden;
  }
  
  .triangle {
    position: absolute;
    width: 0;
    height: 0;
    z-index: 1;
  }
  
  .top {
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    border-left: 50vw solid transparent;
    border-right: 50vw solid transparent;
    border-bottom: 100vh solid #FF5733;
  }
  
  .left {
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    border-top: 50vh solid transparent;
    border-bottom: 50vh solid transparent;
    border-right: 100vw solid #C70039;
  }
  
  .bottom {
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    border-left: 50vw solid transparent;
    border-right: 50vw solid transparent;
    border-top: 100vh solid #581845;
  }
  
  .right {
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    border-top: 50vh solid transparent;
    border-bottom: 50vh solid transparent;
    border-left: 100vw solid #900C3F;
  }
  
  @media (max-width: 767px) {
    .triangle {
      width: 100vw;
      height: 0;
    }
    
    .top, .left, .bottom, .right {
      border-style: solid;
      border-width: 0;
    }
    
    .top {
      border-color: #FF5733 transparent transparent transparent;
      border-bottom-width: 50vh;
      transform: none;
    }
    
    .left {
      border-color: transparent #C70039 transparent transparent;
      border-right-width: 50vw;
      transform: none;
    }
    
    .bottom {
      border-color: transparent transparent #581845 transparent;
      border-top-width: 50vh;
      transform: none;
    }
    
    .right {
      border-color: transparent transparent transparent #900C3F;
      border-left-width: 50vw;
      transform: none;
    }
  }
  

Every time i try to get those half width everything got messed up.

The idea is the display 4 triangle that join in the middle

How can i achieve such resluts ?

Thank you very much

答案1

得分: 2

我认为使用 clip-path 是实现这个效果的方法,而不是纠结于边框。

body {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.triangle {
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.top {
  background: #FF5733;
  clip-path: polygon(0 0, 100% 0, 50% 50%);
}

.left {
  background: #C70039;
  clip-path: polygon(0 0, 0 100%, 50% 50%);
}

.bottom {
  background: #581845;
  clip-path: polygon(0 100%, 100% 100%, 50% 50%);
}

.right {
  background: #900C3F;
  clip-path: polygon(100% 0, 100% 100%, 50% 50%);
}
<div class="container">
  <div class="triangle top"></div>
  <div class="triangle left"></div>
  <div class="triangle bottom"></div>
  <div class="triangle right"></div>
</div>
英文:

I think clip-path is how I would do it, rather than fussing around with borders.

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

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

body {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}
  
.triangle {
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
  
.top {
  background: #FF5733;
  clip-path: polygon(0 0, 100% 0, 50% 50%);
}
  
.left {
  background: #C70039;
  clip-path: polygon(0 0, 0 100%, 50% 50%);
}
  
.bottom {
  background: #581845;
  clip-path: polygon(0 100%, 100% 100%, 50% 50%);
}
  
.right {
  background: #900C3F;
  clip-path: polygon(100% 0, 100% 100%, 50% 50%);
}

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

&lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;triangle top&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;triangle left&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;triangle bottom&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;triangle right&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的内容:

You almost had it. For each side, set that side position to 0 (i.e., for top, set top: 0, etc.). Then just give each side's corresponding border width to 50% of the viewport height for the top and bottom triangles, and 50% of the viewport width for the left and right triangles.

The best part is that since you're using viewport lengths, there's no media query needed. It will always fill the viewport.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.triangle {
  position: absolute;
  width: 0;
  height: 0;
  z-index: 1;
}

.top {
  top: 0;
  border-left: 50vw solid transparent;
  border-right: 50vw solid transparent;
  border-top: 50vh solid #FF5733;
}

.bottom {
  bottom: 0;
  border-left: 50vw solid transparent;
  border-right: 50vw solid transparent;
  border-bottom: 50vh solid #581845;
}

.left {
  left: 0;
  border-top: 50vh solid transparent;
  border-bottom: 50vh solid transparent;
  border-left: 50vw solid #C70039;
}

.right {
  right: 0;
  border-top: 50vh solid transparent;
  border-bottom: 50vh solid transparent;
  border-right: 50vw solid #900C3F;
}
<div class="container">
  <div class="triangle top"></div>
  <div class="triangle right"></div>
  <div class="triangle bottom"></div>
  <div class="triangle left"></div>
</div>

Of course this can also be done with just one element too...

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.triangles {
  position: absolute;
  inset: 0;
  border-left: 50vw solid #C70039;
  border-right: 50vw solid #581845;
  border-bottom: 50vh solid #900C3F;
  border-top: 50vh solid #FF5733;
}
<div class="container">
  <div class="triangles"></div>
</div>
英文:

You almost had it. For each side, set that side position to 0 (i.e., for top, set top: 0, etc.). Then just give each side's corresponding border width to 50% of the viewport height for the top and bottom triangles, and 50% of the viewport width for the left and right triangles.

The best part is that since you're using viewport lengths, there's no media query needed. It will always fill the viewport.

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

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

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}
  
.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}
  
.triangle {
  position: absolute;
  width: 0;
  height: 0;
  z-index: 1;
}
  
.top {
  top: 0;
  border-left: 50vw solid transparent;
  border-right: 50vw solid transparent;
  border-top: 50vh solid #FF5733;
}

.bottom {
  bottom: 0;
  border-left: 50vw solid transparent;
  border-right: 50vw solid transparent;
  border-bottom: 50vh solid #581845;
}

.left {
  left: 0;
  border-top: 50vh solid transparent;
  border-bottom: 50vh solid transparent;
  border-left: 50vw solid #C70039;
}
  
  
.right {
  right: 0;
  border-top: 50vh solid transparent;
  border-bottom: 50vh solid transparent;
  border-right: 50vw solid #900C3F;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;triangle top&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;triangle right&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;triangle bottom&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;triangle left&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Of course this can also be done with just one element too...

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

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

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.triangles {
  position: absolute;
  inset: 0;
  border-left: 50vw solid #C70039;
  border-right: 50vw solid #581845;
  border-bottom: 50vh solid #900C3F;
  border-top: 50vh solid #FF5733;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;triangles&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 06:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656235.html
匿名

发表评论

匿名网友

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

确定