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">topdiv>
    <div class="banner">bannerdiv>
    <div class="main">
        <div class="left">leftdiv>
        <div class="right">rightdiv>
    div>
    <div class="footer">footerdiv>    
body>
html>

CSS实现几种常见布局_第1张图片

  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>1div>
        <div>2div>
        <div>3div>
    section>
body>    
html>

CSS实现几种常见布局_第2张图片

  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">topdiv>
    <div class="banner">bannerdiv>
    <div class="main">maindiv>
    <div class="footer">footerdiv>
body>    
html>

CSS实现几种常见布局_第3张图片

  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">topdiv>
    <div class="banner">bannerdiv>
    <div class="main">
        <ul>
            <li>1li>
            <li>2li>
            <li>3li>
            <li>4li>
        ul>
    div>
    <div class="footer">footerdiv>    
body>    
html>

CSS实现几种常见布局_第4张图片

你可能感兴趣的:(CSS,css,css3,html)