freeCodeCamp “使用 CSS 和 HTML 创建更复杂的形状”练习-----创建“爱心”❤️形状

在本挑战中将用纯 CSS 创建一个心形。

首先需要了解伪元素 ::before 和 ::after。伪元素可以在所选元素之前或之后添加一些内容。

在下面的代码中,::before 伪元素用来给 class 为 heart 的元素添加一个正方形:

.heart::before {
  content: "";  /*必须有的*/
  background-color: yellow;
  border-radius: 25%;
  position: absolute;
  height: 50px;
  width: 70px;
  top: -50px;
  left: 5px;
}

::before 和 ::after 必须配合 content 来使用。这个属性通常用来给元素添加内容诸如图片或者文字。

尽管有时 ::before 和 ::after 是用来实现形状而非文字,但 content 属性仍然是必需的,此时它的值可以是空字符串

在上面的例子里,class 为 heart 元素的 ::before 伪类添加了一个黄色的长方形,长方形的高和宽分别为 50px 和 70px。 这个矩形有圆角,因为它的 border-radius 为 25%,它的位置是绝对位置,位于离元素左边和顶部分别是 5px50px 的位置。


把屏幕里的元素变成心形。

在 heart::after 选择器里,把 background-color 改成 pink,把 border-radius 改成 50%。

接下来,用类选择器选取 class 为 heart(只是 heart)的元素,为它添加 transform 属性。 使用 rotate() 函数并设置角度为 -45 度。

最后,在 heart::before 选择器里面,设置 content 属性值为空字符串。

<style>
  .heart {
    position: absolute;
    margin: auto;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: pink;
    height: 50px;
    width: 50px;
    transform:rotate(-45deg) ;
  }
  .heart::after {
    background-color: pink;
    content: "";
    border-radius: 50%;
    position: absolute;
    width: 50px;
    height: 50px;
    top: 0px;
    left: 25px;
  }
  .heart::before {
    content: "";
    background-color: pink;
    border-radius: 50%;
    position: absolute;
    width: 50px;
    height: 50px;
    top: -25px;
    left: 0px;
  }
</style>
<div class="heart"></div>

可可爱爱的粉色小❤️  就出来啦 ~

练习链接:https://chinese.freecodecamp.org/learn/responsive-web-design/applied-visual-design/create-a-more-complex-shape-using-css-and-html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值