CSS画爱心的过程详解

第一步:

先画一个正方形。如图:

120632_HazC_3529780.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!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>
<div class="heart-body">
<div class="heart-shape"></div>
</div>
</body>
</html>

第二步:

将利用伪元素before和 :after,在正方形的左边和上边各画一个正方形,然后再利用border-radius: 50%;属性,修饰下这两个正方形,然后就得到了两个圆,如图所示:

120737_Po4G_3529780.png

120748_Yj23_3529780.png

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

/**兼容苹果;谷歌,等一些浏览器认*/

-moz-border-radius: 50%;

/**兼容火狐浏览器*/

-o-border-radius: 50%;

/**兼容opera浏览器*/

border-radius: 50%;

background-color: #ffc0cb;

 }

第三步:

类名为:heart-shape的div 利用transform: rotate(45deg); 属性将他们旋转45度,如图所示:

120850_t7fU_3529780.png

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

}

把圆的背景色和正方形的背景色没给统一的颜色,是为了大家更好的看到明显的效果图,接下来将其背景色设置成统一的,最终的爱心就出来了,如图所示:

120915_UnHe_3529780.png

.heart-shape:before,.heart-shape:after {

position: absolute;

content: '';

width: 100px;

height: 100px;

-webkit-border-radius: 50%;

/**兼容苹果;谷歌,等一些浏览器认*/

-moz-border-radius: 50%;

/**兼容火狐浏览器*/

-o-border-radius: 50%;

/**兼容opera浏览器*/

border-radius: 50%;

background-color: #f70e0e;

}

完整代码

<!DOCTYPE html>

<html>

 

<head>

<meta charset="UTF-8">

<title>css画桃心</title>

</head>

<style>

.heart_body {

width: 200px;

margin: 100px auto;

position: relative;

}

 

.heart_shape {

position: relative;

width: 100px;

height: 100px;

background: #f70e0e;

}

 

.heart_shape:before,

.heart_shape:after {

position: absolute;

content: '';

width: 100px;

height: 100px;

background: #f70e0e;

border-radius: 50%;

}

 

.heart_shape:before {

left: -45px;

}

 

.heart_shape:after {

top: -45px;

}

</style>

 

<body>

<div class="heart_body">

<div class="heart_shape"></div>

</div>

</body>

 

</html>

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3529780/blog/968240

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值