带边框的透明三角形

时间:2016-04-30 21:13:35

标签: html css css-shapes

尝试创建带有彩色边框的透明三角形div。

CSS

#down {
 display: block;
 position: fixed;
 left: 0; right: 0;
 width: 0; height: 0;
 margin: 0 auto;
 border-left: 55px solid transparent;
 border-right: 55px solid transparent;
 z-index: 20; 
 bottom: 0; 
 border-bottom: 55px solid rgba(250,250,250,0.75); 
}

将div放在顶部另一个div会破坏透明度

4 个答案:

答案 0 :(得分:1)

您还可以使用渐变和/或变换:

  • 在左边:square + border-top / left + transform + gradient来绘制底部边框:
  • on middle:你的
  • 右侧的
  • :三角形顶部边框的边框底部+渐变

both extra example can hold content,例如font icone / text / image。

body {
  background:tomato;
}

#rotate {
  position:fixed;
  border:solid turquoise;
  border-bottom:none;
  border-right:none;
  bottom:7px;
  left:calc(50% - 180px);
  height:75px;
  width:75px;
  transform-origin: bottom left;
  transform:rotate(45deg);
  background:linear-gradient(to bottom right, transparent calc(50% - 3px), turquoise calc(50% - 3px), turquoise 50%, transparent 50% ); 
}

#bg-gradient {
  position:fixed;
  bottom:5px;
  left: calc(50% + 70px)  ;
  border-bottom:solid turquoise;
  background:linear-gradient(to bottom right, transparent 50%, turquoise 50%, turquoise calc(50% + 3px), transparent calc(50% + 3px) ),linear-gradient(to bottom left, transparent 50%, turquoise 50%, turquoise calc(50% + 3px), transparent calc(50% + 3px) ) right
    ;
  background-repeat:no-repeat;
  background-size:50% 100%;
  height:55px;
  width:110px;
  
}

#down {
 display: block;
 position: fixed;
 left: 0; right: 0;
 width: 0; height: 0;
 margin: 0 auto;
 border-left: 55px solid transparent;
 border-right: 55px solid transparent;
 z-index: 20; 
 bottom: 5px; 
 border-bottom: 55px solid rgba(250,250,250,0.75); 
}
<div id="down"></div>
<div id="rotate"></div>
<div id="bg-gradient"></div>

请注意,底部的旋转方块可以隐藏一半

答案 1 :(得分:0)

这通常是通过边框技巧完成的,而这些对这个

并没有多大帮助

你需要其他技术。

例如,请参阅此CSS

body {
    background: linear-gradient(90deg, lightblue, yellow)
}

.trapezoid {
    left: 50px;
    top: 50px;
    position: absolute;
    height: 100px;
    width: 500px;
    background-color: transparent;
}

.trapezoid:before {
    content: '';
    width: 57%;
    height: 100%;
    left: -4%;
    position: absolute;
    border-color: red;
    border-style: solid;
    border-width: 3px 0px 3px 3px;
    -webkit-transform: skewX(-20deg);
}

.trapezoid:after {
    content: '';
    width: 59%;
    height: 100%;
    right: -4%;
    position: absolute;
    border-color: red;
    border-style: solid;
    border-width: 3px 3px 3px 0px;
    -webkit-transform: skewX(20deg);
}

答案 2 :(得分:0)

这是一个非常简单的解决方案,但它使用的CSS transform不受IE低于9.0的支持。

请记住,此三角形位于页面的最底部,因此可以使用旋转的方块。

#down {
    display: block;
    position: fixed;
    left: 0; right: 0;
    bottom: -47px;
    width: 80px;
    height: 80px;
    margin: 0 auto;
    border: 0;
    z-index: 20;
    background-color: rgba(250,250,250,0.75);
    -ms-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    border: 3px solid #ffaa33;
}

#down-inner { /* Must be rotated back */
    width: 100%;
    height: 100%;
    -ms-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
}

body {
    background-color: #e7e7e7;
    color: #444444;
    font-family: Arial, sans-serif;
}
<div id="down">
    <div id="down-inner">A rotated square</div>
</div>

答案 3 :(得分:0)

我能够用更少的代码来解决这个问题。

./manage.py test --enable-wiki MyApp.tests
body { 
  background: url(https://images.unsplash.com/photo-1500390365106-166bb67248d6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2434&q=80) no-repeat top; background-size: cover;
  min-height: 300px;
}

.triangle { 
  position: absolute;
  top: 25%;
  left: 30%;
  height: 158px; 
  width: 182px; 
  background: white; 
  clip-path: polygon(50% 0%, 0% 100%, 1.75% 100%, 50% 3%, 97.5% 98.35%, 1.75% 98.35%, 1.75% 100%, 100% 100%);
}

六角形版本也显示在这里: https://codepen.io/smeyer/pen/gOPmxqm

您可以玩数字游戏来更改边框宽度。

相关问题