用CSS画阴阳图

在这里插入图片描述

方法一:直接用CSS画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>阴阳</title>
    <style>
        .fr {
            float: right;
        }
        .clearfix:after {
            content: '';
            display: block;
            clear: both;
        }


        div[class~="yy"] {
            position: relative;
            width: 200px;
            height: 200px;
            background: linear-gradient(to bottom, #ffffff 0%,#ffffff 50%,#000000 50%,#000000 100%);
            border: 1px solid red;
            border-radius: 50%
        }
        div[class~="whi"] {
            position: absolute;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: #fff;
            top: 50px;
            right: 0;
        }
        div[class~="bla"] {
            position: absolute;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: #000;
            top: 50px;
            left: 0;
        }
        div[class="x1"] {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #000;
            position: absolute;
            top: 40px;
            left: 40px;
        }
        div[class="x2"] {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #fff;
            position: absolute;
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="yy">
        <div class="whi">
            <div class="x1"></div>
        </div>
        <div class="bla">
            <div class="x2"></div>
        </div>
    </div>
</body>
</html>
  • 关键:渐变做颜色(用border做也可以)
    定位找位置
  • 注意若父元素已经绝对定位了,子元素也可以用绝对定位来控制位置
    定位中的top,bottom,left,right的值是子元素相当于父元素上下左右空出来的距离

方法二:用伪类选择器画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>阴阳02</title>
</head>
<style>
    .clearfix::after {
        content: '';
        display: block;
        clear:both;
    }
    div[class="box"] {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid red;
        border-radius: 50%;
        background: linear-gradient(to bottom, #ffffff 0%,#ffffff 50%,#000000 50%,#000000 100%);
    }
    div[class="box"]:before {
        content: '';
        position: absolute;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #fff;
        border: 40px solid #000;
        top: 50px;
        left: 0px;
    }
    div[class="box"]:after {
        content: '';
        position: absolute;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #000;
        border: 40px solid #fff;
        top: 50px;
        right: 0px;
        box-sizing: border-box/content-box;
    }
</style>
<body>
    <div class="box"></div>
</body>
</html>
  • 关键:伪类选择器的使用,定位
  • 注意伪类选择器必须有content=’’;语句,否则没有任何效果
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值