html/css:如何创建六角形图像占位符

编程入门 行业动态 更新时间:2024-10-23 20:24:53
本文介绍了html/css:如何创建六角形图像占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在上面的图片中,您可以看到我是如何找到这些数字的;)如果您对此还有其他疑问,请随时提出.我会尽力解释.

This is (almost) what I want to create:

HTML

<div class="hexagon1"> <div class="hexagon-in1"> <div class="hexagon-in2"> </div> </div> </div>

CSS

.hexagon1 { overflow: hidden; visibility: hidden; -webkit-transform: rotate(120deg); -moz-transform: rotate(120deg); -ms-transform: rotate(120deg); -o-transform: rotate(120deg); transform: rotate(120deg); width: 400px; height: 200px; margin: 0 0 0 -80px; } .hexagon-in1 { overflow: hidden; width: 100%; height: 100%; -webkit-transform: rotate(-60deg); -moz-transform: rotate(-60deg); -ms-transform: rotate(-60deg); -o-transform: rotate(-60deg); transform: rotate(-60deg); } .hexagon-in2 { width: 100%; height: 100%; background-repeat: no-repeat; background-position: 50%; background-image: url(placekitten/240/240); visibility: visible; -webkit-transform: rotate(-60deg); -moz-transform: rotate(-60deg); -ms-transform: rotate(-60deg); -o-transform: rotate(-60deg); transform: rotate(-60deg); }

The problem is, that I need a border on the hexagon and if possible I would like to place the picture inside an img-tag. I tried adding the border on either div but I only got a border on top and bottom of the hexagon because of the visibility-hidden or the overflow-hidden attribute.

This is what I've found so far while googling:

csshexagon/

www.queness/post/13901/create-beautiful-hexagon-shapes-with-pure-css3

github/web-tiki/responsive-grid-of-hexagons

I've also found some questions concerning this matter here on Stackoverflow but neither of them explained how exactly you could create a hexagon. Also the hexagons in the examples are all standing on an edge, which is not what I want (as demonstrated in the code). I only need one hexagon and not a grid as well.

When I tried to change the styles of the examples it always ended in a desastrous chaos. This is why I would like to know how to create and to calculate the divs which are used to create a hexagon with border and a picture inside.

Which rate does the width have to the height?

How can I create a border that has the same width on each side?

Where do I have to place the picture as background-image?

How big should the picture be (in percentage to the divs)?

What transformations do you really need to create the hexagon? (I saw an example which used scale, rotate and translate to get a picture inside)

How can the rotation be calculated?

How do I calculate the margin needed?

As I am quite the novice in web-designing can someone explain this as simple as possible? It would suffice if someone can show me according to the example-code above how the numbers are calculated. I know that a hexagon has an inner angle of 120° and that's about it.

Thanks for your help in anticipation. :)

EDIT

Another page I found about hexagons but only to create the shape and not really putting either an image in it nor having a border around it:

jtauber.github.io/articles/css-hexagon.html

解决方案

Important Note

Be informed that this solution does not work for those who want to create something similar supported by all browsers as for the time being IE does not support the clip-path-property used in this example!!

I've found a way to do it thanks to @SahilDhir although it's more of a workaround:

HTML

<div class="poligon"> <img src="lorempixel/g/600/400/"> </div>

CSS

.poligon { display: inline-block; position: relative; width: 200px; height: 180px; background: red; box-sizing: border-box; -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); -moz-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); } .poligon img { position: absolute; top: 2px; /* equal to border thickness */ left: 2px; /* equal to border thickness */ width: 196px; /* container height - (border thickness * 2) */ height: 176px; /* container height - (border thickness * 2) */ -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); -moz-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); }

Note that I did not calculate much here, but rather tried achieving a six-sided figure.

I will have the problem that my picture will have a transparent background, but I thought that I might produce a linear gradient to fill the background polygon. I will have to try that out first though ^^

I will not mark this as the final answer as my questions have not yet been answered truly. I still want to be able to create a hexagon as the one in the example I gave above where I would be able to adapt the heights and widths as well as the border thicknesses the way I want.

EDIT

As I did not find a better solution I have improved the one here and figured out the calculations needed:

HTML

<div class="poligon"> <div class="hex-background"> <img src="img.clipartfest/953d8641fe933437bbc41d48e6fc8492_yellow20stars20clipart-christmas-stars-clipart-without-background_532-506.png"> </div> </div>

CSS

.poligon { display: inline-block; position: relative; width: 120px; height: 103.92px; /* width * 0.866 */ background: red; box-sizing: border-box; -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); -moz-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); } .hex-background { position: absolute; background-color: white; top: 2px; /* equal to border thickness */ left: 2px; /* equal to border thickness */ width: 116px; /* container width - (border thickness * 2) */ height: 99.92px; /* container height - (border thickness * 2) */ -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); -moz-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); } .poligon img { position: absolute; width: 116px; /* container width - (border thickness * 2) */ height: 99.92px; /* container height - (border thickness * 2) */ -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); -moz-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%); }

The clip-path part is correct if you want a same-sided hexagon.

With the picture above you can see how I found those numbers ;) If you have further questions about this, don't hesitate to ask. I'll try to explain it the best I can.

更多推荐

html/css:如何创建六角形图像占位符

本文发布于:2023-11-30 02:44:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   html   css

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!