CSS 繪製各類形狀

說明

使用 CSS 能夠繪製出許多形狀,好比三角形、梯形、圓形、橢圓,等 並不僅是能夠繪製矩形。下面來看看怎麼實現這些形狀的吧。
爲了容易理解,文章分爲基本形狀 和 組合形狀來講,基本形狀是比較容易實現的,而利用這些基本形狀進行組合,就能夠實現稍微複雜點的組合形狀了。css

基本形狀

三角形

.triangle {
    width: 0;
    height: 0;
    border: 50px solid blue;
    /* 經過改變邊框顏色,能夠改變三角形的方向 */
    border-color: blue transparent transparent transparent;
}

在這裏插入圖片描述

查看示例html

梯形

.trapzoid {
    width: 40px;
    height: 100px;
    border: 50px solid blue;
    border-color: transparent transparent blue transparent;
}

在這裏插入圖片描述

查看示例動畫

圓形

.circle{
    width:100px;
    height:100px;
    border-radius:50%;
    background:blue;
}

在這裏插入圖片描述

查看示例spa

球體

.sphere {
    height: 200px;
    width: 200px;
    border-radius: 50%;
    background: radial-gradient(circle at 70px 70px, #5cabff, #000);
}

在這裏插入圖片描述

查看示例3d

橢圓

.ellipse {
    width: 200px;
    height: 100px;
    border-radius: 50%;
    background: blue;
}

在這裏插入圖片描述

查看示例code

半圓

.semicircle {
    width: 50px;
    height: 100px;
    /*  "/"前四個值表示圓角的水平半徑,後四個值表示圓角的垂直半徑*/
    border-radius: 200% 0 0 200% / 100% 0 0 100%;

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

在這裏插入圖片描述

查看示例orm

菱形

.rhombus {
    width: 200px;
    height: 200px;
    transform: rotateZ(45deg) skew(30deg, 30deg);
    background: blue;
}

在這裏插入圖片描述

查看示例htm

組合形狀

心形

心形是由兩個圓形和一個矩形進行組合獲得的。blog

在這裏插入圖片描述

.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%;
}

在這裏插入圖片描述

查看示例圖片

扇形

扇形是由一個圓形和一個矩形進行組合獲得的,用矩形遮住圓形的一部分就造成了扇形。

在這裏插入圖片描述

.sector {
    width: 142px;
    height: 142px;
    background: #fff;
    border-radius: 50%;
    background-image: linear-gradient(to right, transparent 50%, #655 0);
}

.sector::before {
    content: '';
    display: block;
    margin-left: 50%;
    height: 100%;
    width: 100%;
    background-color: inherit;
    transform-origin: left;
    /*調整角度,改變扇形大小*/
    transform: rotate(230deg);
}

在這裏插入圖片描述

查看示例

五邊形

五邊形是由一個三角形和一個梯形進行組合獲得的。

在這裏插入圖片描述

.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;
}

在這裏插入圖片描述

查看示例

六邊形

六邊形是由兩個三角形和一個矩形進行組合獲得的。

在這裏插入圖片描述

.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;
}

在這裏插入圖片描述

查看示例

長方體

長方體是由六個矩形進行組合獲得的。

在這裏插入圖片描述

.cuboid {
    width: 200px;
    height: 200px;
    transform-style: preserve-3d;
    transform: rotateX(-30deg) rotateY(-80deg);
}

.cuboid div {
    position: absolute;
    width: 200px;
    height: 200px;
    opacity: 0.8;
    transition: .4s;
}

.cuboid .front {
    transform: rotateY(0deg) translateZ(100px);
    background: #a3daff;
}

.cuboid .back {
    transform: translateZ(-100px) rotateY(180deg);
    background: #a3daff;
}

.cuboid .left {
    transform: rotateY(-90deg) translateZ(100px);
    background: #1ec0ff;
}

.cuboid .right {
    transform: rotateY(90deg) translateZ(100px);
    background: #1ec0ff;
}

.cuboid .top {
    transform: rotateX(90deg) translateZ(100px);
    background: #0080ff;
}

.cuboid .bottom {
    transform: rotateX(-90deg) translateZ(100px);
    background: #0080ff;
}
<div class="cuboid">
    <!--前面 -->
    <div class="front"></div>
    <!--後面 -->
    <div class="back"></div>
    <!--左面 -->
    <div class="left"></div>
    <!--右面 -->
    <div class="right"></div>
    <!--上面 -->
    <div class="top"></div>
    <!--下面 -->
    <div class="bottom"></div>
</div>

在這裏插入圖片描述

查看示例

圓柱體

圓柱體是由一個橢圓和一個圓角矩形進行組合獲得的。

在這裏插入圖片描述

.cylinder {
    position: relative;
    transform: rotateX(70deg);
}

.ellipse {
    width: 100px;
    height: 100px; 
    background: deepskyblue;
    border-radius: 50px;
}

.rectangle {
    width: 100px;
    height: 400px;
    position: absolute;
    opacity: 0.6;
    background: deepskyblue;
    top: 0;
    left: 0; 
    border-radius: 50px;
    z-index: -1;
}
<div class="cylinder">
    <div class="ellipse"></div>
    <div class="rectangle"></div>
</div>

在這裏插入圖片描述

查看示例

若是使用了漸變色,看上去會更像一些。

background-image: linear-gradient(to right, rgba(255, 255, 255, 0.2) 0, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0.2) 100%);

在這裏插入圖片描述

查看示例

棱錐

棱錐是由四個三角形和一個矩形進行組合獲得的。

在這裏插入圖片描述

.pyramid {
    width: 200px;
    height: 200px;
    transform-style: preserve-3d;
    transform: rotateX(-30deg) rotateY(-80deg);
} 
.pyramid div {
    position: absolute;
    top: -100px;
    width: 0px;
    height: 0px;
    border: 100px solid transparent;
    border-bottom-width: 200px;
    opacity: 0.8;
}

.pyramid .front {
    transform: translateZ(100px) rotateX(30deg);
    border-bottom-color: #a3daff;
    transform-origin: 0 100%;
}

.pyramid .back {
    transform: translateZ(-100px) rotateX(-30deg);
    border-bottom-color: #1ec0ff;
    transform-origin: 0 100%;
}

.pyramid .left {
    transform: translateX(-100px) rotateZ(30deg) rotateY(90deg);
    border-bottom-color: #0080ff;
    transform-origin: 50% 100%;
}

.pyramid .right {
    transform: translateX(100px) rotateZ(-30deg) rotateY(90deg);
    border-bottom-color: #03a6ff;
    transform-origin: 50% 100%;
}

.pyramid .bottom {
    transform: translateX(-100px) rotateZ(90deg) rotateY(90deg);
    background: cyan;
    width: 200px;
    height: 200px;
    border: 0;
    top: 0;
    transform-origin: 50% 100%;
}
<div class="pyramid">
    <!--前面 -->
    <div class="front"></div>
    <!--後面 -->
    <div class="back"></div>
    <!--左面 -->
    <div class="left"></div>
    <!--右面 -->
    <div class="right"></div>

    <!--下面 -->
    <div class="bottom"></div>
</div>

在這裏插入圖片描述

查看示例

總結

文中實現的各類形狀,也許你以爲實現的很複雜,其實你也可使用 clip-path 這一個屬性,繪製各類形狀。
CSS 能繪製的東西,不單單隻有這些,還有不少不少,文中都沒有說出來,而即使是文中已經實現的形狀也不僅有一種實現方式,有興趣的小夥伴能夠繼續去探索。

最後

這裏有一個使用各類形狀進行組合,造成魔法陣的例子。

在這裏插入圖片描述

咱們還能夠給魔法陣中的形狀增長動畫,使魔法陣看上去更有趣。

在這裏插入圖片描述

查看示例

相關文章
相關標籤/搜索