前端自学 - CSS 布局总结

一、左右两列布局

(一)两侧宽度固定布局

该种情况很简单,获取到宽度,更据需求,分别赋予两列固定的宽度即可;

<!--
 * @Date: 2021-08-23 14:11:52
 * @LastEditTime: 2021-10-04 17:10:05
-->
<!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;
        }

        .clear::before,
        .clear::after {
            content: '';
            display: table;
        }

        .clear::after {
            clear: both;
        }

        .box1 {
            width: 1002px;
            margin: 0 auto;
        }

        .box1_left,
        .box1_right {
            float: left;
            height: 100px;
        }

        .box1_left {
            width: 300px;
            background-color: #333;
        }

        .box1_right {
            width: 702px;
            background-color: #666;
        }
        
    </style>
</head>

<body>
    <!-- 左右宽度固定 -->
    <div class="box1 clear">
        <div class="box1_left"></div>
        <div class="box1_right"></div>
    </div>
</body>

</html>

(二)左列宽度固定,右列宽度不固定

该种情况

  • 将下方的盒子进行浮动左浮动;
  • 并给予左外边距,值为左边盒子的宽度;
  • 因为 margin 外边距填充是透明的,通过该种方法,可以将当前盒子向右移动,并露出下方盒子中的内容
<!--
 * @Date: 2021-08-23 14:11:52
 * @LastEditTime: 2021-10-04 17:10:05
-->
<!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;
        }

        .clear::before,
        .clear::after {
            content: '';
            display: table;
        }

        .clear::after {
            clear: both;
        }

        .box2 {
            width: 80%;
            margin: 20px auto 0;
        }

        .box2_left,
        .box2_right {
            height: 100px;
        }

        .box2_left {
            background-color: orangered;
            float: left;
            width: 300px;
        }

        .box2_right {
            background-color: yellow;
            margin-left: 310px;
        }

    </style>
</head>

<body>
    <!-- 左列宽度固定,右列宽度不固定 -->
    <div class="box2 clear">
        <div class="box2_left"></div>
        <div class="box2_right"></div>
    </div>
</body>

</html>

(三)左列宽度不固定,右列宽度固定

  • 左侧宽度不固定的盒子,给予一个父级宽度也设置为 100%的盒子;
  • 通过已知数据,将左侧宽度固定的盒子的右边,通过外边距填充的方式,留出空白;
  • 将两个盒子都做左浮动;
     
    在这里插入图片描述
     

此时大概效果如上

  • 当我们赋予蓝色盒子一个自身宽度的负值 左外边距时,下方的盒子就会跑到上方去,效果如下:
     
    在这里插入图片描述
     

这种情况个人理解如下:

  1. 因为盒子的总宽度 == content + padding * 2 + border * 2 + margin * 2
  2. 当我们的 盒子总宽度 为 “ 0 ”时,下方的盒子就会因为浮动原因,跑到上面去。
  3. 所以赋予盒子一个 左边的 外边距负值,使盒子在理论上成为宽度为 “ 0 ”。
<!--
 * @Date: 2021-08-23 14:11:52
 * @LastEditTime: 2021-10-04 17:10:05
-->
<!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;
        }

        .clear::before,
        .clear::after {
            content: '';
            display: table;
        }

        .clear::after {
            clear: both;
        }

        .box3 {
            width: 80%;
            margin: 20px auto 0;
        }

        .box3_left {
            float: left;
            width: 100%;
        }

        .box3_right {
            float: left;
            height: 100px;
            width: 300px;
            background-color: rgb(32, 235, 250);
            margin-left: -300px;
        }

        .box3_inner {
            height: 100px;
            background-color: #333;
            margin-right: 310px;
        }

    </style>
</head>

<body>
    <!-- 左列宽度不固定,右列宽度固定 -->
    <div class="box3 clear">
        <div class="box3_left">
            <div class="box3_inner"></div>
        </div>
        <div class="box3_right"></div>
    </div>
</body>

</html>

二、左中右三列布局(淘宝双飞翼 & 圣杯布局)

在这里插入图片描述
 
实现上图效果,就是采用上方的左侧不固定,右侧固定的布局方式解决,是一样的思想,利用盒子总宽度为 “ 0 ” 进行解决
 
当我们大概布好局之后效果如下:
 
在这里插入图片描述
 
此时我们最左侧的红方块想要移动到上方,就需要 左侧外边框进行负值赋予,当赋予到 -100px 时效果如下:
 
在这里插入图片描述
 

  1. 因为最左侧的方块上去了,所以后面的方块就占用了他的位置;
  2. 此时想让最左侧的方块到最左边,就要继续进行赋予负值,赋予一个多少值,并不知道;
  3. 大概需要赋予一个绿色盒子的总长度的值,一个盒子的总长度,我们习惯使用 100% 进行表示,所以,我们抛弃 px 这个单位, 使用 % 百分比;
  4. 赋予最左侧盒子一个 -100% 就可以将他的位置移动到最左侧;
  5. 最后将右侧的盒子,赋予一个自身宽度的值,就可以实现效果了;
<!--
 * @Date: 2021-08-23 14:11:52
 * @LastEditTime: 2021-10-04 20:43:37
-->
<!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;
        }

        .clear::before,
        .clear::after {
            content: '';
            display: table;
        }

        .clear::after {
            clear: both;
        }

        .box4 {
            width: 80%;
            margin: 20px auto 0;
        }

        .centent,
        .box4_left,
        .box4_right {
            float: left;
        }
        
        .box4_left,
        .box4_right {
            height: 100px;
            width: 100px;
            background-color: orangered;
        }
        
       
        .centent {
            width: 100%;
            
        }
        .box4_left {
            margin-left: -100%;
        }
        .box4_right {
            margin-left: -100px;
        }
        
        .box4_inner {
            background-color: palegreen;
            height: 100px;
            margin: 0 110px;
        }
    </style>
</head>

<body>
    <!-- 左列宽度固定,右列宽度固定,中间宽度不固定 -->
    <div class="box4 clear">
        <div class="centent">
            <div class="box4_inner"></div>
        </div>
        <div class="box4_left"></div>
        <div class="box4_right"></div>
    </div>


</body>

</html>

相关文章


在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只 小网虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值