CSS如何?畫愛心的範例程式碼分享

來源:互聯網
上載者:User

今天小穎給大家分享一個用CSS畫的愛心,底下有代碼和製作過程,希望對大家有所協助。

第一步:


先畫一個正方形。



<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>css畫桃心</title>    <style media="screen">        .heart-body {            width: 500px;            margin: 100px auto;            position: relative;        }        .heart-shape {            position: relative;            width: 100px;            height: 100px;            background-color: #f70e0e;        }    </style></head><body>    <p class="heart-body">        <p class="heart-shape"></p>    </p></body></html>

第二步:

將利用虛擬元素before和 :after,在正方形的左邊和上邊各畫一個正方形,然後再利用border-radius: 50%;屬性,修飾下這兩個正方形,然後就得到了兩個圓,:




.heart-shape:before,        .heart-shape:after {            position: absolute;            content: '';            width: 100px;            height: 100px;            background-color: #ffc0cb;        }        .heart-shape:before {            left: -45px;        }        .heart-shape:after {            top: -45px;        }


利用border-radius: 50%; 屬性:

.heart-shape:before,        .heart-shape:after {            position: absolute;            content: '';            width: 100px;            height: 100px;            -webkit-border-radius: 50%;            /**相容蘋果;Google,等一些瀏覽器認*/            -moz-border-radius: 50%;            /**相容Firefox瀏覽器*/            -o-border-radius: 50%;            /**相容opera瀏覽器*/            border-radius: 50%;            background-color: #ffc0cb;        }

第三步:

類名為:heart-shape的p 利用transform: rotate(45deg); 屬性將他們旋轉45度,:



.heart-shape {            position: relative;            width: 100px;            height: 100px;            background-color: #f70e0e;            -webkit-transform: rotate(45deg);            /* Safari 和 Chrome */            -moz-transform: rotate(45deg);            /* Firefox */            -ms-transform: rotate(45deg);            /* IE 9 */            -o-transform: rotate(45deg);            /* Opera */            transform: rotate(45deg);        }

小穎把圓的背景色和正方形的背景色沒給統一的顏色,是為了大家更好的看到明顯的,接下來小穎將其背景色設定成統一的,最終的愛心就出來了,:



.heart-shape:before,        .heart-shape:after {            position: absolute;            content: '';            width: 100px;            height: 100px;            -webkit-border-radius: 50%;            /**相容蘋果;Google,等一些瀏覽器認*/            -moz-border-radius: 50%;            /**相容Firefox瀏覽器*/            -o-border-radius: 50%;            /**相容opera瀏覽器*/            border-radius: 50%;            background-color: #f70e0e;        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.