css浮动的一些小练习

1.小米模块练习

需求:
使用浮动,完成设计图中的布局效果
效果图如下:
在这里插入图片描述

我写的,
和老师不一样的是我添加了蓝色的左边距,他添加的右边距。
添加左边距的话,八个方块的情况就都是一样的,就不需要添加伪类元素来清楚4、8的右边距了

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 消除自带的边距 */
        * {
            margin: 0;
            padding: 0;
        }

        .one {
            /* 版心居中 */
            margin: 0 auto;
            width: 1226px;
            height: 614px;
            /* background-color: orange; */
        }

        .two {
            /* 浮动 */
            float: left;
            width: 234px;
            height: 614px;
            background-color: #800080;
        }

        li {
            /* 浮动:右 */
            float: right;
            width: 234px;
            height: 300px;
            background-color: #87ceeb;
            margin-left: 14px;
            margin-bottom: 14px;
        }

        /* 消除 li 自带的小圆点 */
        ul {
            list-style: none;
        }
    </style>
</head>

<body>
    <div class="one">
        <div class="two"></div>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>

</html>

老师给的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 0 auto;

            width: 1226px;
            height: 614px;
            /* background-color: pink; */
        }

        .left {
            float: left;

            width: 234px;
            height: 614px;
            background-color: #800080;
        }

        .right {
            float: right;

            width: 978px;
            height: 614px;
            /* background-color: green; */
        }

        ul {

            /* 去掉列表的符号 */
            list-style: none;
        }

        .right li {
            float: left;

            margin-right: 14px;
            margin-bottom: 14px;

            width: 234px;
            height: 300px;
            background-color: #87ceeb;
        }

        /* 如果父级的宽度不够, 子级会自动换行 */
        /* 第四个li和第八个li右侧间距清除 */
        .right li:nth-child(4n) {
            margin-right: 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left"></div>
        <div class="right">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
</body>
</html>

2.网页导航
效果图如下:
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 顺序:浮动 盒子模型 文字样式 */

        * {
            margin: 0;
            padding: 0;
        }

        div {
            /* 居中 */
            margin: 0 auto;
            width: 640px;
            height: 50px;
            background-color: #ffc0cb;
        }

        li {

            float: left;
            width: 80px;
            height: 50px;

        }

        a {
            display: inline-block;
            color: #fff;
            text-decoration: none;
            /* 设置行高 */
            line-height: 50px;
        }

        /* 悬停效果 */
        a:hover {
            background-color: green;
        }
    </style>
</head>

<body>
    <div class="one">
        <ul>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
            <li><a href="#">新闻</a></li>
        </ul>
    </div>
</body>

</html>
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值