简体   繁体   English

如何使用 CSS 制作弯曲的边缘六边形

[英]How to make a curved edge hexagon by using CSS

This is my CSS.这是我的 CSS。

CSS CSS

#hexagon-circle { 
    width: 100px; 
    height: 55px; 
    background: red; 
    position: relative;
    border-radius: 10px;} 
#hexagon-circle:before { 
    content: ""; 
    position: absolute; 
    top: -25px; 
    left: 0; 
    width: 0; 
    height: 0; 
    border-left: 50px solid transparent; 
    border-right: 50px solid transparent; 
    border-bottom: 29px solid red;
    border-radius: 10px;} 
#hexagon-circle:after { 
    content: ""; 
    position: absolute; 
    bottom: -25px; 
    left: 0; 
    width: 0; 
    height: 0; 
    border-left: 50px solid transparent; 
    border-right: 50px solid transparent; 
    border-top: 29px solid red;
    border-radius: 10px;}

The output is 4 edges of hexagon is curved, but top and the bottom is not.输出是六边形的 4 个边是弯曲的,但顶部和底部不是。 I want to make all edge of hexagon curved.我想让六边形的所有边缘弯曲。 How to make top and bottom edge to be curved?如何使顶部和底部边缘弯曲? or How to make the top edge of triangle to be curved?或如何使三角形的顶部边缘弯曲?

http://jsfiddle.net/yR7zt/ 1 http://jsfiddle.net/yR7zt/ 1

I think you are looking for this.我想你正在寻找这个。

 .hex { position: relative; margin: 1em auto; width: 10em; height: 17.32em; border-radius: 1em/.5em; background: orange; transition: opacity .5s; } .hex:before, .hex:after { position: absolute; width: inherit; height: inherit; border-radius: inherit; background: inherit; content: ''; } .hex:before { -webkit-transform: rotate(60deg); transform: rotate(60deg); } .hex:after { -webkit-transform: rotate(-60deg); transform: rotate(-60deg); }
 <div class="hex"></div>

I understand this is a fairly old question, but I thought I'd add an answer to show more about how it works.我知道这是一个相当古老的问题,但我想我会添加一个答案来展示更多关于它是如何工作的。

So, first off, we need to create a single element on the page.所以,首先,我们需要在页面上创建一个元素。 I have gone for a size of height:300px; width:180px;我已经选择了height:300px; width:180px; height:300px; width:180px; and a border radius of 10px.和 10px 的边框半径。 Just because I like the roundness of the number (forgive the pun).只是因为我喜欢数字的圆度(原谅双关语)。 Giving this element a position:relative;给这个元素一个position:relative; means that we can herein position everything absolute ly in relative terms to this div.意味着我们可以在这里absolute定位所有相对于该 div 的内容。

We then need to create two pseudo elements, with the same height and width as the parent.然后我们需要创建两个伪元素,其高度和宽度与父元素相同。

Because the pseudo elements are exactly that, pseudo elements, we need to add a content: to them.因为伪元素就是这样,伪元素,我们需要添加一个content:给他们。 And since because we can put stuff within the parent, we don't really need these, so set them to "";因为我们可以把东西放在父级中,所以我们并不需要这些,所以将它们设置为"";

this leads us onto how to create the hexagon, rather than the rectangle we currently have.这引导我们如何创建六边形,而不是我们目前拥有的矩形。 To do that, we're going to have to include a rotation in order to generate the other sides of the hexagon.为此,我们将不得不包含一个旋转以生成六边形的另一边。 With there being 6 sides, and the angles needing to add to 360, we can rotate one of the pseudo element by 60 degrees.由于有 6 条边,并且需要添加 360 的角度,我们可以将伪元素之一旋转 60 度。 The other we'll rotate by -60 degrees (or 300degrees, if you'd prefer).另一个我们将旋转 -60 度(或 300 度,如果您愿意的话)。

This leaves us with our 'hexagon' in which we can add some nice styling and hover animations as need be:这给我们留下了我们的“六边形”,我们可以根据需要添加一些漂亮的样式和悬停动画:

 .roundHex { position: relative; margin: 0 auto; background: rgba(0, 0, 0, 0.2); border-radius: 10px; height: 300px; width: 180px; transition: all 1s; line-height:300px; text-align:center; color:white; font-size:20px; } .roundHex:before, .roundHex:after { content: ""; position: absolute; top: 0; left: 0; background: inherit; border-radius: inherit; height: 100%; width: 100%; z-index:-1; } .roundHex:before { -webkit-transform: rotate(60deg); -moz-transform: rotate(60deg); transform: rotate(60deg); transition: all 1s 0.5s; } .roundHex:after { -webkit-transform: rotate(-60deg); -moz-transform: rotate(-60deg); transform: rotate(-60deg); transition: all 1s 1s; } .roundHex:hover { background: tomato; }
 <div class="roundHex">HOVER ME</div>

Jsfiddle demo also available Jsfiddle 演示也可用

I will consider the same trick I used in the previous answer Where I will build the hexagon using clip-path我将考虑我在上一个答案中使用的相同技巧在哪里我将使用clip-path构建六边形

 .hex { width: 200px; display: inline-block; color:orange; } .hex::before { content: ""; display: block; background:currentColor; padding-top: 90%; clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%); }
 <div class="hex"></div>

Then I will apply an SVG filter:然后我将应用一个 SVG 过滤器:

 .hex { width: 200px; display: inline-block; color:orange; filter: url('#goo'); } .hex::before { content: ""; display: block; background:currentColor; padding-top: 86.6%; /* 100%*cos(30) */ clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%); }
 <div class="hex"></div> <div class="hex" style="color:blue;width:150px;"></div> <div class="hex" style="color:red;width:100px;"></div> <svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" /> <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" /> <feComposite in="SourceGraphic" in2="goo" operator="atop"/> </filter> </defs> </svg>

CSS曲线六边形

And in the opposite direction并且在相反的方向

 .hex { width: 200px; display: inline-block; margin:0 5px; color:orange; filter: url('#goo'); } .hex::before { content: ""; display: block; background:currentColor; padding-top: 115%; /* 100%/cos(30) */ clip-path: polygon(0% 25%,0% 75%,50% 100%,100% 75%,100% 25%,50% 0%); }
 <div class="hex"></div> <div class="hex" style="color:blue;width:150px;"></div> <div class="hex" style="color:red;width:100px;"></div> <svg style="visibility: hidden; position: absolute;" width="0" height="0" xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="goo"><feGaussianBlur in="SourceGraphic" stdDeviation="8" result="blur" /> <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9" result="goo" /> <feComposite in="SourceGraphic" in2="goo" operator="atop"/> </filter> </defs> </svg>

圆形边框六边形

Try this way :(works in chrome and in ie 10)试试这种方式:(适用于 chrome 和 ie 10)

<br><br><br>
<div id="hexagon-circle"></div>
<style>
 #hexagon-circle {
    position: relative;
    margin: 1em auto;
    width: 10em; height: 17.32em;
    border-radius: 1em/.5em;
    opacity: .25;
    background: orange;
    transition: opacity .5s;
    cursor: pointer;
 }
 #hexagon-circle:before, #hexagon-circle:after {
    position: absolute;
    width: inherit; height: inherit;
    border-radius: inherit;
    background: inherit;
    content: '';
  }
  #hexagon-circle:before {
   transform: rotate(60deg);
   -ms-transform:rotate(60deg); /* IE 9 */
   -webkit-transform:rotate(60deg); /* Opera, Chrome, and Safari */
  }
  #hexagon-circle:after {
    transform: rotate(-60deg);
    -ms-transform:rotate(-60deg); /* IE 9 */
    -webkit-transform:rotate(-60deg); /* Opera, Chrome, and Safari */
  }

  </style>

You can try this approach:您可以尝试这种方法:

CSS CSS

#hexagon-circle { 
position: relative;
margin: 1em auto;
width: 10em;
height: 17.32em;
border-radius: 1em/.5em;
background: red;
transition: opacity .5s;
cursor: pointer;} 

#hexagon-circle:before { 
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(60deg);  /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(60deg);  /* IE 9 */
transform: rotate(60deg);} /* Firefox 16+, IE 10+, Opera */

#hexagon-circle:after { 
position: absolute;
width: inherit;
height: inherit;
border-radius: inherit;
background: inherit;
content: '';
-webkit-transform: rotate(-60deg);  /* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(-60deg);  /* IE 9 */
transform: rotate(-60deg);} /* Firefox 16+, IE 10+, Opera */

DEMO演示

http://jsfiddle.net/yR7zt/4/ http://jsfiddle.net/yR7zt/4/

With your current code, using the triangle top and bottom, you can modify them slightly to give it a curved look.使用您当前的代码,使用三角形顶部和底部,您可以稍微修改它们以使其具有弯曲的外观。 Add a width of 4px to #hexagon-circle:before and #hexagon-circle:after and reduce border-left and border-right by 2px each.#hexagon-circle:before#hexagon-circle:after添加4px的宽度,并将border-leftborder-right分别减少2px

Js Fiddle here Js Fiddle在这里

#hexagon-circle { 
  width: 100px; 
  height: 55px; 
  background: red; 
  position: relative;
  border-radius: 10px;
}

#hexagon-circle:before { 
  content: ""; 
  position: absolute; 
  top: -25px; 
  left: 0; 
  width: 4px; 
  height: 0; 
  border-left: 48px solid transparent; 
  border-right: 48px solid transparent; 
  border-bottom: 29px solid red;
  border-radius: 10px;
}

#hexagon-circle:after { 
  content: ""; 
  position: absolute; 
  bottom: -25px; 
  left: 0; 
  width: 4px; 
  height: 0; 
  border-left: 48px solid transparent; 
  border-right: 48px solid transparent; 
  border-top: 29px solid red;
  border-radius: 10px;
}

It's not a true curve as it creates a straight line, however it gives the impression it might be curved when viewed in the context of the shape.它不是一条真正的曲线,因为它创建了一条直线,但是当从形状的上下文中查看时,它给人的印象可能是弯曲的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM