如何使用带有边框的CSS制作半边形六边形,边框位于半六边形中间的边框处

时间:2016-03-04 21:34:04

标签: css html5 css3 css-shapes

如何制作带边框的半边形六角形,并使用CSS和HTML5制作带边框的矩形形状和半六边形内的图像

我没有这方面的代码,因为我已经尝试但无法弄清楚如何做到这一点

我添加了一张我希望能够做到的图像。

enter image description here

4 个答案:

答案 0 :(得分:5)

您可以使用:before:after使用矩形和2个带有透明边框的CSS三角形相当轻松地创建梯形。

工作示例:



body {
    background: black;
}

.rectangle {
    background: #ECECEC;
    height: 20px;
}

.trapezoid {
    width: 50px;
    height: 50px;
    position: relative;
    margin: 0 auto;
    background: #ECECEC;
}
.trapezoid:before,
.trapezoid:after {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    border: 25px solid transparent;
    border-top-color: #ECECEC;
}
.trapezoid:before {
    right: 100%;
    border-right-color: #ECECEC;
}
.trapezoid:after {
    left: 100%;
    border-left-color: #ECECEC;
}

<div class="rectangle">
    <div class="trapezoid"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

使用形状和边框颜色进行了更新

div {
  margin-top:1em;;
  text-align: center;
  padding: 0.5em;
  border-top:1px solid lightgray;
  background: linear-gradient(to bottom, #ECECEC 50%, lightgray 50%, lightgray 51%, transparent 52%);
}

img {
  position: relative;
  display: block;
  margin: 10px auto;
  z-index: 1;
}

span {
  text-align: center;
  display: inline-block;
  width:320px;
  position: relative;
  overflow: hidden;
  border-top:1px solid lightgray;
  background: linear-gradient(to left, lightgray, lightgray) bottom center, linear-gradient(40deg, transparent 50px, lightgray, 50px, lightgray 52px, #ECECEC 52px)bottom left, linear-gradient(-40deg,  transparent 50px, lightgray, 50px, lightgray 52px, #ECECEC 52px)bottom right;
  background-repeat: no-repeat;
  background-size: 50% 2px, 50% 100%, 50% 100%;
}
<div>
  <span>
    <img src="http://lorempixel.com/55/46/technics/1" alt="ico"/>
  </span>
</div>

旧代码

单个伪和溢出:隐藏,也可以这样做:

div {
  text-align: center;
  padding: 0.5em;
  background: linear-gradient(to bottom, gray 50%, black 50%);
}
img {
  position: relative;
  display: block;
  padding: 0.5em 0;
  z-index: 1;
}
span {
  text-align: center;
  display: inline-block;
  padding: 0 3em;
  position: relative;
  overflow: hidden;
}
span:before {
  position: absolute;
  content: '';
  bottom: 0;
  left: 50%;
  margin-left: -75px;
  height: 150px;
  width: 150px;
  background: gray;
  transform: rotate(45deg);
}
<div>
  <span>
    <img src="http://lorempixel.com/40/50/nature/3" alt="ico"/>
  </span>
</div>

或渐变(如果需要,可能更容易to draw borders或阴影)

div {
  text-align: center;
  padding: 0.5em;
  background: linear-gradient(to bottom, gray 50%, black 50%);
}
img {
  position: relative;
  display: block;
  padding: 0.5em 0;
  z-index: 1;
}
span {
  text-align: center;
  display: inline-block;
  padding: 0 3em;
  position: relative;
  overflow: hidden;
  background: linear-gradient(40deg, transparent 1.5em, gray 1.5em)bottom left, linear-gradient(-40deg, transparent 1.5em, gray 1.5em)bottom right;
  background-repeat: no-repeat;
  background-size: 50% 100%;
}
<div>
  <span>
    <img src="http://lorempixel.com/40/50/nature/3" alt="ico"/>
  </span>
</div>

答案 2 :(得分:2)

这是使用具有偏斜的伪元素的解决方案。可以毫无问题地叠加图像

.rect {
  width: 100%;
  height: 20px;
  background-color: lightgrey;
  border-bottom: 1px solid grey;
  position: relative;
}

.hex {
  width: 200px;
  height:  40px;
  position: absolute;
   left: 50%;
  transform: translateX(-50%);
}

.hex:before, .hex:after {
   content: "";
  position: absolute;
  width: 200px;
  height: 40px;
  border-style: solid;
  border-color: grey;
  border-width: 0px 0px 1px 0px;
  transform-origin: bottom center;
  background-color: lightgrey;
}

.hex:before {
   transform: skew(10deg); 
  border-left-width: 1px;
}
.hex:after {
   transform: skew(-10deg);  
  border-right-width: 1px;
}
<div class="rect">
  <div class="hex"></div>
  </div>

答案 3 :(得分:0)

你可以使用:after。

创建半八边形
.halfOctagon {
    width: 100px;
    height: 50px;
    background: #f35916;
    position: relative;
    top:25px;
    left:50px;
}

.halfOctagon:after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 29px solid #f35916;
    border-left: 29px solid #eee;
    border-right: 29px solid #eee;
    width: 42px;
    height: 0;
}

您可以在https://jsfiddle.net/kb2tzxq4/

中试用实例

要移动半个八边形,请在css中调整顶部和左侧的.halfOctagon

相关问题