CSS六角形无法在Android上正确呈现

编程入门 行业动态 更新时间:2024-10-24 06:37:51
本文介绍了CSS六角形无法在Android上正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个纯CSS六边形,可以在所有浏览器上正常显示,但某些Android浏览器(例如:Galaxy Note)除外。由生成的内容创建的圆角三角形无法正确呈现。

我创建了一个小提琴来显示我的代码。

输出:

I have created a pure css hexagon which renders fine on all browsers except on some Android Browsers e.g.: Galaxy Note. The rounded triangles created by the generated content are not rendered correctly.

I have created a fiddle to show my code. jsfiddle/mistermelotte/r8X8c/

HTML

<span class="hexagon"></span>

CSS

.hexagon { position: relative; margin: 1em auto; width: 80px; height: 55px; border-radius: 5px; background: #a0d1e6; display: block; } .hexagon:before { position: absolute; top: -19px; left: 0; width: 0; height: 0; border-right: 40px solid transparent; border-bottom: 20px solid #a0d1e6; border-left: 40px solid transparent; border-radius: 5px; content:""; } .hexagon:after { position: absolute; bottom: -19px; left: 0; width: 0; height: 0; border-top: 20px solid #a0d1e6; border-right: 40px solid transparent; border-left: 40px solid transparent; border-radius: 5px; content:""; } .lt-ie9 .hexagon:before { top: -20px; } .lt-ie9 .hexagon:after { bottom: -20px; }

All help is appreciated.

解决方案

This answer contains two different solutions both tested to be working in my nexus 4 stock browser and chrome.

  • Triangles, losing the border radius
  • Rectangles rotated with border radius
Triangles, losing the border radius

Avoiding the :before and :after approach seems to fix the issue, I've tested the following code on my mobile devices (Android as asked) and they work perfectly, the problem in this particular case is the lack of rounded corners. This one is created by positioning the triangles on top and bottom of a rectangle giving the effect of an hexagone. Once again, here I cannot control the border radius property.

HTML:

<div id='hexagon'> <div id="top"></div> <div id="content"></div> <div id="bottom"></div> </div>

CSS:

#hexagon #content { width: 104px; height: 60px; background-color: #6C6; } #hexagon #top { width: 0; border-bottom: 30px solid #6C6; border-left: 52px solid transparent; border-right: 52px solid transparent; } #hexagon #bottom { width: 0; border-top: 30px solid #6C6; border-left: 52px solid transparent; border-right: 52px solid transparent; }

DEMO: Fiddle

OUTPUT:

Rectangles rotated with border radius

It can also be achieved by rotating rectangles 60 degrees, in this one I also avoid the use of the pseudo clases for :before and :after, this option does allow rounded corners so it may be more suitable for your particular question:

HTML

<div id='hexagon'> <div id="corner-1"></div> <div id="content"></div> <div id="corner-2"></div> </div>

CSS

#hexagon { width:100px;height:57px; background-color: #6C6; background-repeat: no-repeat; background-position: 50% 50%; background-size: auto 173px; position: relative; margin:40px 5px; text-align:center; zoom:1; } #hexagon #corner-1 { z-index:-1; transform:rotate(60deg); -ms-transform:rotate(60deg); -webkit-transform:rotate(60deg); } #hexagon #corner-2 { z-index:-1; transform:rotate(-60deg); -ms-transform:rotate(-60deg); -webkit-transform:rotate(-60deg); } #hexagon #corner-1, #hexagon #corner-2 { position: absolute; top:0; left:0; width:100%; height:100%; background: inherit; z-index:-2; overflow:hidden; backface-visibility: hidden; }

And finally, set the border radius (for this size, anything greater than 3-4px results in strange corners and would probably require some fiddling with the positions to correct.

#hexagon, #corner-1, #corner-2 { border-radius:3px; }

DEMO: Fiddle

OUTPUT:

更多推荐

CSS六角形无法在Android上正确呈现

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

发布评论

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

>www.elefans.com

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