不需要任何圖片,只要善用 CSS3 的偽元素與 transform 旋轉屬性,就能畫出一顆紅通通的愛心。請務必使用支援 CSS3 的瀏覽器,例如 Chrome、IE9 以上或 Firefox。
做法是透過:before 與:after 兩個偽元素各畫一個上方圓潤的方塊,再分別向左右傾斜 45 度拼接成愛心輪廓。完整原始碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3畫愛心</title>
<style>
.heart_keleyi_com{width:160px;height:200px;position:relative}
.heart_keleyi_com:before{content:" ";border: 0 solid transparent; -moz-border-radius:100px;-webkit-border-radius:100px;border-radius:100px 100px 0 0;width:80px;
height:120px;background:#ff0000;-moz-transform: rotate(-45deg);-ms-transform: rotate(-45deg);-o-transform: rotate(-45deg);-webkit-transform: rotate(-45deg);
position:absolute;left:20px;}
.heart_keleyi_com:after{content:" ";border: 0 solid transparent; -moz-border-radius:100px;-webkit-border-radius:100px;border-radius:100px 100px 0 0;width:80px;
height:120px;background:#ff0000;-moz-transform: rotate(45deg);-ms-transform: rotate(45deg);-o-transform: rotate(45deg);-webkit-transform: rotate(45deg);
position:absolute;left:48px;top:0px;}</style>
</head>
<body>
<div class="heart_keleyi_com"></div>
</body>
</html>