天天看點

css繪制基本圖形

說明

css可以繪制各種圖形,基本圖形包括三角形、圓形、梯形、橢圓形、矩形等,但還有一些複雜圖形是由這些基本圖形組合而成

基本形狀

三角形

css繪制基本圖形
.triangle {
    width: 0;
    height: 0;
    border: 50px solid blue;
    /* 通過改變邊框顔色,可以改變三角形的方向 */
    border-color: blue transparent transparent transparent;
}
           

梯形

css繪制基本圖形
.trapzoid {
    width: 40px;
    height: 100px;
    border: 50px solid blue;
    border-color: transparent transparent blue transparent;
}
           

圓形

css繪制基本圖形
.circle{
	width:100px;
	height:100px;
	border-radius:50%;
	background:blue;
}
           

橢圓

css繪制基本圖形
.ellipse {
    width: 200px;
    height: 100px;
    border-radius: 50%;
    background: blue;
}
           

半圓

css繪制基本圖形
.semicircle {
    width: 50px;
    height: 100px;
    /*  "/"前四個值表示圓角的水準半徑,後四個值表示圓角的垂直半徑*/
    border-radius: 200% 0 0 200% / 100% 0 0 100%;

    /* 效果和用%一樣 */
    /* border-radius: 50px 0 0 50px; */
    background: blue;
}
           

菱形

css繪制基本圖形
.rhombus {
    width: 200px;
    height: 200px;
    transform: rotateZ(45deg) skew(30deg, 30deg);
    background: blue;
}
           

組合形狀

心形

心形由兩個圓形和一個矩形組合而成

css繪制基本圖形
.heart {
    width: 100px;
    height: 100px;
    transform: rotateZ(45deg);
    background: red;
}

.heart::after,
.heart::before {
    content: "";
    width: 100%;
    height: 100%;
    border-radius: 50%;
    display: block;
    background: red;
    position: absolute;
    top: -50%;
    left: 0;
}

.heart::before {
    top: 0;
    left: -50%;
}

           
css繪制基本圖形

五邊形

由一個三角形和一個梯形組成

css繪制基本圖形
.pentagonal {
    width: 100px;
    position: relative;
    border-width: 105px 50px 0;
    border-style: solid;
    border-color: blue transparent;
}

.pentagonal:before {
    content: "";
    position: absolute;
    width: 0;
    height: 0;
    top: -185px;
    left: -50px;
    border-width: 0px 100px 80px;
    border-style: solid;
    border-color: transparent transparent blue;
}

           
css繪制基本圖形

六邊形

由兩個三角形和一個矩形組成

css繪制基本圖形
.hexagon {
    width: 200px;
    height: 100px;
    background-color: blue;
    position: relative;
}

.hexagon:before {
    content: "";
    position: absolute;
    top: -60px;
    left: 0;
    width: 0;
    height: 0;
    border-left: 100px solid transparent;
    border-right: 100px solid transparent;
    border-bottom: 60px solid blue;
}

.hexagon:after {
    content: "";
    left: 0;
    width: 0;
    height: 0;
    bottom: -60px;
    position: absolute;
    border-left: 100px solid transparent;
    border-right: 100px solid transparent;
    border-top: 60px solid blue;
}

           
css繪制基本圖形