溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

用css畫正六邊形的方法

發(fā)布時間:2020-09-14 14:56:11 來源:億速云 閱讀:698 作者:小新 欄目:web開發(fā)

小編給大家分享一下用css畫正六邊形的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

在之前要先了解一下正六邊形內(nèi)角和邊的關(guān)系,正六邊形的每個內(nèi)角是60deg,如圖(√3其實是根號3):

用css畫正六邊形的方法

方法一:原理把正六邊形分成三部分,左中右分別是:before部分,div部分,after部分,如圖:

用css畫正六邊形的方法

before三角形部分是div的before偽元素,after三角形部分是div的after偽元素。

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>用css畫正六邊形</title>
		<style type="text/css">
			.div {
                position: relative;
                width: 50px;
                height: 86.6px;
                margin: 50px auto;
                background-color: red;
            }
            .div:before {
                content: '';
                display: block;
                position: absolute;
                width: 0;
                height: 0;
                right:50px;
                border-width: 43.3px 25px;
                border-style: solid;
                border-color: transparent red transparent transparent;
            }
            .div:after {
                content: '';
                display: block;
                position: absolute;
                width: 0;
                height: 0;
                left:50px;
                border-width: 43.3px 25px;
                border-style: solid;
                border-color: transparent transparent transparent red;
                top:0;
            }
		</style>
	</head>

	<body>
		<div class='div'></div>
	</body>

</html>

效果圖:

用css畫正六邊形的方法

注意div及偽元素的寬高需要根據(jù)上面的公式計算。

方法二:也是把正六邊形分成三個寬高相同的div,然后使用定位以及css3 transform:rotate分別向左右旋轉(zhuǎn)60deg形成正六邊形,如圖:

用css畫正六邊形的方法

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>用css畫正六邊形</title>
		<style type="text/css">
			.one {
                width: 50px;
                height: 86.6px;
                margin: 0 auto;
                border-top: 1px solid red;
                border-bottom: 1px solid red;
            }
            .two {
                position: absolute;
                width: 50px;
                height: 86.6px;
                left: 25px;
                top: 0;
                transform: translate(-50%,-50%);
                transform: rotate(60deg);
                border-top: 1px solid red;
                border-bottom: 1px solid red;
            }
            .three {
                position: absolute;
                width: 50px;
                height: 86.6px;
                left: 25px;
                top: 0;
                transform: translate(-50%,-50%);
                transform: rotate(300deg);
                border-top: 1px solid red;
                border-bottom: 1px solid red;
            }
		</style>
	</head>

	<body>
		<div style='position:relative;width:100px;margin:0 auto;'>
		    <div class='one'></div>
		    <div class='two'></div>
		    <div class='three'></div>
		</div>
	</body>

</html>

效果圖:

用css畫正六邊形的方法

看完了這篇文章,相信你對用css畫正六邊形的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI