CSS传统布局的入门总结

双飞之图文混排

代码已经放置在gitee仓库:git clone https://gitee.com/TD1900s-88-keys/front-end-learning.git

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css"></style>
</head>
<body>
 <section>
   <article>
           <img src="./img/美国队长.png" alt="drumsticl_1">
           <h1>美国队长</h1>
           <p>穷爸爸:“你要好好学习,以后就能找一份好工作养活自己。”驱动学习的力量是恐惧。</p>
   </article>
   <article>
           <img src="./img/钢铁侠.png" alt="drumsticl_2">
           <h1>钢铁侠</h1>
           <p>富爸爸:“你要好好学习,以后就能开自己的公司,创造很多就业机会给别人。驱动力量是爱,好好学就会帮到更多的人。</p>
   </article>
 </section>
</body>
</html>

初始效果:

在这里插入图片描述

给上面代码添加css属性:

        /*初始化外边距、内边距*/
        * {
            margin: 0;
            padding: 0;
        }

        img {
            max-width: 100px;
        }

        /*添加背景色,用box-shadow属性来做出一种边框的感觉*/
        section {
            width: 980px;
            background: #f5f5f5;
            box-shadow: 0 0 1px rgba(0, 0, 0, .4) inset;
            /* overflow: hidden;
            在父元素里设置这个属性同样可以触发BFC达到清除盒子塌陷的效果 */
        }

        /*通过浮动来达到两列布局的效果*/
        article {
            width: 50%;
            float: left;
        }

        /*通过添加伪类来清除浮动和外部盒子塌陷的效果*/
        section::after {
            content: "";
            display: table;
            clear: both;
        }

第一阶段效果为:
在这里插入图片描述
接下来升华一下代码

        /*初始化外边距、内边距*/
        * {
            margin: 0;
            padding: 0;
        }

        h1 {
            font-size: 33px;
            margin-bottom: 13px;
            font-family: "NSimSun";
        }

        p {
            font-size: 22px;
            color: #F08;
            font-family: "KaiTi"

        }

        h1,
        p {
            width: 66%;
            margin-left: 33%;
        }

        img {
            position: absolute;
            top: 0;
            left: 80px;
            max-width: 60px;
        }

        /*添加背景色,用box-shadow属性来做出一种边框的感觉*/
        section {
            width: 980px;
            background: #f5f5f5;
            box-shadow: 0 0 1px rgba(0, 0, 0, .4) inset;
            /* overflow: hidden;
            在父元素里设置这个属性同样可以触发BFC达到清除盒子塌陷的效果 */

            box-sizing: border-box;
            padding: 33px;
        }

        /*通过浮动来达到两列布局的效果*/
        article {
            width: 49%;
            float: left;
            position: relative;
        }

        /*通过添加伪类来清除浮动和外部盒子塌陷的效果*/
        section::after {
            content: "";
            display: table;
            clear: both;
        }

        article:first-child {
            margin-right: 2%;
        }

最终效果为:
在这里插入图片描述

格子布局

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        img {
            width: 250px;
        }
    </style>
</head>

<body>
    <section>
        <article>
            <h1>壁纸一</h1>
            <p>招财进宝,日入斗金</p>
            <img src="./images/wallhaven-39pdov_1920x1080.png" alt="#">
        </article>
        <article>
            <h1>壁纸二</h1>
            <p>敛福生财,兴隆大业</p>
            <img src="./images/wallhaven-yj163l_3840x2160.png" alt="#">
        </article>
        <article>
            <h1>壁纸三</h1>
            <p>心想事成,吉祥如意</p>
            <img src="./images/wallhaven-9m95rd_3840x2160.png" alt="#">
        </article>
        <article>
            <h1>壁纸四</h1>
            <p>日转千阶,源源不断</p>
            <img src="./images/wallhaven-6om2qq_3840x2160.png" alt="#">
        </article>
    </section>
</body>

</html>

初始效果为
在这里插入图片描述

添加css代码

        section {
            width: 666px;
        }
        /*让图片两列排序*/
        article {
            box-sizing: border-box;
            width: 333px;
            height: 333px;
            padding: 20px;
            text-align: center;
            float: left;
        }

效果为:
在这里插入图片描述

增加边框线代码:

 /*添加边框线*/
        article {
            border-bottom: 1px solid rgba(0, 0, 0, .3);
            border-left: 1px solid rgba(0, 0, 0, .3);
        }

        article:nth-child(even) {
            border-right: 1px solid rgba(0, 0, 0, .3);

        }

        article:nth-child(1) {
            border-top: 1px solid rgba(0, 0, 0, .3);
        }

        article:nth-child(2) {
            border-top: 1px solid rgba(0, 0, 0, .3);
        }

效果为:
在这里插入图片描述

进阶版:
先删除掉第三个壁纸。
在这里插入图片描述

添加代码:

        /*补齐边框*/
        article:nth-child(3) {
            width: 100%;
            border-right: 1px solid rgba(0, 0, 0, .3);
        }

效果为:

在这里插入图片描述

跨行结束,接下来就是跨列:
修改代码:

        img {
            width: 250px;
        }

        section {
            width: 666px;
        }

        article {
            box-sizing: border-box;
            width: 333px;
            height: 333px;
            padding: 20px;
            text-align: center;
            float: left;
            border-bottom: 1px solid rgba(0, 0, 0, .3);
            border-left: 1px solid rgba(0, 0, 0, .3);
            border-top: 1px solid rgba(0, 0, 0, .3);
        }

        /*设置第二个盒子的高度*/
        article:nth-child(2) {
            height: 666px;
            padding-top: 200px;
            border-right: 1px solid rgba(0,0,0,.3);
        }

效果为:

在这里插入图片描述

由于第二个格子高度太高,直接将section撑大。第三个盒子就被推向远方。

        /*设置第三个盒子,让盒子往上面移动*/
        article:nth-child(3) {
            margin-top: -333px;
        }

最终效果为:
在这里插入图片描述

多列布局(多项等高布局)

通过我人为的设置下面图片相同的宽度,不同的高度

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>等高多列布局</title>
    <style type="text/css">
        img{
            width: 300px;
        }
        /*设置不同高度的图片*/
        div:nth-child(1) img
        {
            height: 200px;
        }
        div:nth-child(2) img
        {
            height: 300px;
        }
        div:nth-child(3) img
        {
            height: 100px;
        }
    </style>
</head>
</head>

<body>
    <div id="container">
        <div class="sanchaji">
            <img src="./images/wallhaven-m9d7v8.jpg">
            <h1>HTML</h1>
            <p>主流网页的文件后缀名</p>
        </div>
        <div class="sanchaji">
            <img src="./images/wallhaven-3kmpxv_3840x2160.png">
            <h1>CSS</h1>
            <p>基于CSS的常用文件后缀</p>
        </div>
        <div class="sanchaji">
            <img src="./images/wallhaven-eyj8dk_3840x2160.png">
            <h1>JavaScript</h1>
            <p>JavaScript,当下前端最火的语言</p>
        </div>
    </div>
</body>

</html>

效果为
在这里插入图片描述

        /*多列布局*/
        .sanchaji {
            width: 33.33%;
            float: left;
            text-align: center;
            box-sizing: border-box;
            padding: 22px;
            color: #FFF;
            background: #99BBFF;
        }

        .sanchaji img {
            width: 33%;
            margin-top: 33px;
        }

        /*设置不同的色彩*/
        .sanchaji:nth-child(1) {
            background: lightblue;
        }

        .sanchaji:nth-child(3) {
            background: lightskyblue;
        }

效果为:
在这里插入图片描述
三列布局中每一列文字或者图片等原因内容长度都不同,导致布局后的高度也不同。
因为不能提前预知这个不确定性的文本高度,所以会带来很多的麻烦。
解决方法:如果预期是300px的列高度,先设置一个超级大的padding-bottom:540px,然后通过负外边距减去多余的240px。然后通过overflow:hidden才会裁剪掉多余的像素。

.sanchaji {
	padding-bottom: 540px;
	margin-bottom:-240px;
	}

效果为:
在这里插入图片描述
表面看上去好像没有改变,但当给外部盒子设置这个属性后:

 #container {
            overflow: hidden;
        }

效果为:
在这里插入图片描述

自适应两列布局

浮动法

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>两列自适应布局演示</title>
    <style type="text/css">
        aside {
            width: 20%;
            height: 500px;
            background-color: pink;
            float: left;
        }
    
        main {
            width: 80%;
            height: 500px;
            margin-left: 20%;
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <aside></aside>
    <main></main>
</body>
</html>

效果为:
在这里插入图片描述

postion定位法

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>postion两列布局演示</title>
    <style type="text/css">
        aside {
            width: 20%;
            height: 500px;
            background-color: pink;
            position: absolute;
            top: 0;
            left: 0;
        }

        main {
            width: 80%;
            height: 500px;
            margin-left: 20%;
            background-color: lightblue;
        }

        /* 新增外层 */
        .container {
            position: relative;
        }
    </style>
</head>

<body>
    <div class="container">
        <aside></aside>
        <main></main>
    </div>
</body>

</html>

在这里插入图片描述

三列布局

float实现三列布局

<!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>float实现双飞翼布局</title>
    <style>
        body {
            text-align: center;
            min-width: 500px;
        }

        header,
        footer {
            background-color: lightgray;
            height: 70px;
        }

        .left {
            width: 20%;
            height: 500px;
            float: left;
            background-color: pink;
        }

        .right {
            width: 20%;
            height: 500px;
            float: right;
            background-color: lightblue;
        }

        .center {
            height: 500px;
            margin: 0 20%;
            background-color: lightskyblue;
        }
    </style>
</head>

<body>
    <header>#header</header>
    <div class="container">
        <div class="left">#left</div>
        <div class="right">#right</div>
        <div class="center">#center</div>
        <!-- 有两个点要注意:
        *   中间主内容的 HTML 代码必须放在最后,否则右边栏会掉到下一行
        *   中间主内容不能去指定它的宽度,因为我们需要中间列的宽度自适应,
        应该让它默认 auto 就可以了 -->
    </div>
    <footer>#footer</footer>
</body>

</html>

效果为:
在这里插入图片描述

postion实现三列布局

<!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>postion实现双飞翼布局</title>
    <style>
        body {
            text-align: center;
            min-width: 500px;
        }

        header,
        footer {
            background-color: lightgray;
            height: 70px;
        }
        .container
        {
            position: relative;
        }
        .left{
            width: 20%;
            height: 500px;
            position: absolute;
            top: 0;
            left: 0;
            background-color: pink;
        }
        .right
        {
            width: 20%;
            height: 500px;
            position: absolute;
            top:0;
            right: 0;
            background-color: lightblue;
        }
        .center{
            height: 500px;
            margin:0 20%;
            background-color: lightskyblue;
        }
    </style>
</head>

<body>
    <header>#header</header>
    <div class="container">
        <div class="left">#left</div>
        <div class="center">#center</div>
        <div class="right">#right</div>
    </div>
    <footer>#footer</footer>
</body>

</html>

效果:
在这里插入图片描述

margin负值法

关于一份关于负外边距使用的权威指南原文:https://www.smashingmagazine.com/2009/07/the-definitive-guide-to-using-negative-margins/

双飞翼布局

布局逻辑:先中间,再两边。中间分成内外两层,外层嵌套的div宽度是以100%撑满整个body元素来显示的向左浮动。

<!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>双飞翼布局</title>
    <style>
        body {
            text-align: center;
            min-width: 500px;
        }

        header,
        footer {
            background-color: lightgray;
            height: 70px;
        }
        /*外层嵌套的div宽度是以100%撑满整个body元素来显示向左浮动的*/
        .container {
            width: 100%;
            float: left;
        }

        .center {
            height: 500px;
            margin: 0 200px;
            background-color: lightskyblue;
        }
        /*让左边的浮动起来,并且margin-left: -100%;即为上移一行*/
        .left {
            width: 200px;
            height: 500px;
            background-color: pink;
            float: left;
            margin-left: -100%;
        }
        /*让右边的浮动起来*/
        .right {
            width: 200px;
            height: 500px;
            background-color: lightblue;
            float: left;
            margin-left: -200px;
        }
        /*清除浮动*/
        footer{
            clear:both;
        }
    </style>
</head>

<body>
    <header></header>
    <main>
        <div class="container">
            <div class="center"></div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
    </main>
    <footer></footer>
</body>

</html>

在这里插入图片描述

圣杯布局

注释:更详细的请看,探讨圣杯布局的构成和实现原理的文章,原文:https://alistapart.com/article/holygrail/

<!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>圣杯布局</title>
    <style>
        body {
            text-align: center;
            min-width: 500px;
        }

        header,
        footer {
            background-color: lightgray;
            height: 70px;
        }

        main {
            padding: 0 200px;
        }

        .center {
            width: 100%;
            height: 500px;
            background-color: lightskyblue;
            float: left;
        }

        .left {
            width: 200px;
            height: 500px;
            background-color: pink;
            float: left;
            margin-left: -100%;
            position: relative;
            right: 200px;
        }

        .right {
            width: 200px;
            height: 500px;
            background-color: lightblue;
            float: left;
            margin-left: -200px;
            position: relative;
            left: 200px;
        }
        footer{
            clear:both;
        }
    </style>
</head>

<body>
    <header>#header</header>
    <main>
        <div class="center">#center</div>
        <div class="left">#left</div>
        <div class="right">#right</div>
    </main>
    <footer>#footer</footer>
</body>

</html>

效果为:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值