CSS实现几种常见布局

CSS实现几种常见布局

  1. 两列左窄右宽型布局
<!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>
    	* {
            margin: 0;
            padding: 0;
        }
        
        .top,
        .banner,
        .main,
        .footer {
            width: 960px;
            margin: 0 auto;
            text-align: center;
            border: 1px dashed #ccc;
            background-color: #eee;
            margin-bottom: 8px;
        }
        
        .top {
            height: 80px;
        }
        
        .banner {
            height: 150px;
        }
        
        .main {
            height: 500px;
        }
        
        .left {
            width: 360px;
            height: 500px;
            background-color: pink;
            float: left;
        }
        
        .right {
            width: 592px;
            height: 500px;
            background-color: purple;
            float: right;
        }
        
        .footer {
            height: 120px;
        }
    </style>
</head>   
<body>
	<div class="top">top</div>
    <div class="banner">banner</div>
    <div class="main">
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <div class="footer">footer</div>    
</body>
</html>

在这里插入图片描述

  1. 伸缩三等分布局
<!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>
        section {
            width: 80%;
            height: 200px;
            border: 1px solid pink;
            margin: 100px auto;
            /* 父盒子添加flex */
            display: flex;
            min-width: 500px;
            /* 垂直排列 */
            flex-direction: column;
        }
        
        section div {
            height: 100%
        }
        
        section div:nth-child(1) {
             /* 子盒子添加份数 */
            flex: 1;
            background-color: pink;
        }
        
        section div:nth-child(2) {
            flex: 1;
            background-color: purple;
        }
        
        section div:nth-child(3) {
            flex: 1;
           	background-color: blue;
        }
    </style>
</head>
<body>
    <section>
    	<div>1</div>
        <div>2</div>
        <div>3</div>
    </section>
</body>    
</html>

在这里插入图片描述

  1. 一列固定宽度且居中
<!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>
    	/* 清除内外边距 */
        * {
            margin: 0; 
            padding: 0;
        }
		
        .top,
        .banner,
        .main,
        .footer {
            width: 960px;
            /* 盒子居中对齐 */
            margin: 0 auto;
            text-align: center;
            margin-bottom: 10px;
            border: 1px dashed #ccc;
        }
        
        .top {
            height: 80px;
            background-color: pink;
        }
        
        .banner {
            height: 120px;
            background-color: purple;
        }
        
        .main {
            height: 500px;
            background-color: hotpink;
        }
        
        .footer {
            height: 150px;
            background-color: skyblue;
        }                
    </style>
</head>
<body>
    <div class="top">top</div>
    <div class="banner">banner</div>
    <div class="main">main</div>
    <div class="footer">footer</div>
</body>    
</html>

在这里插入图片描述

  1. 通栏平均分布型
<!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>
    	* {
            margin: 0;
            padding: 0;
        }
        
        ul {
            list-style: none;
        }
        
        .top {
            height: 60px;
            background-color: #000;
        }
        
        .banner {
            width: 960px;
            height: 400px;
            background-color: skyblue;
            margin: 20px auto;
            border-radius: 15px;
        }
        
        .main {
            width: 960px;
            height: 200px;
            margin: 0 auto;
        }
        
        .main ul li {
            width: 240px;
            height: 200px;
            background-color: pink;
            /* 让多个块级元素一行显示 */
            float: left;
        }
        
        /* 偶数换色 */
        .main ul li:nth-child(even) {
            background-color: pink;
        }
        
        .footer {
            height: 100px;
            background-color: #000;
            margin-top: 20px;
        }
    </style>
</head>
<body>
	<div class="top">top</div>
    <div class="banner">banner</div>
    <div class="main">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
    <div class="footer">footer</div>    
</body>    
</html>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

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

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

打赏作者

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

抵扣说明:

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

余额充值