css基础笔记十五, css布局样式大全.详细篇 包含浮动,定位,弹性盒子,栅格,网格等布局方式

目录

基础布局

浮动布局

定位布局

弹性盒子布局

栅格布局

grid网格布局

grid介绍

grid布局的属性

容器和项目属性

grid布局的案例

总结


基础布局

基础的按照html的规则,一次摆放html div的元素 然后css设置尺寸和美化样式

代码示例:

<!DOCTYPE html>
<!--
常用属性
width 宽度
height 高度
backround-color 背景颜色
float   浮动
-->
<html lang="en">
<!--上中下布局-->
<head>
    <meta charset="UTF-8">
    <title>布局网页常用结构</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .header{
      width: 100%;
      height:100px;
      background-color: #fcc;
    }
    .nav{
      width: 100%;
      height:500px;
      background-color: yellow;
    }
    .footer{
      width: 100%;
      height:200px;
      background-color: aqua;
    }
  </style>
</head>
<body>
<div class="header">页眉</div>
<div class="nav">导航</div>
<div class="footer">底部内容</div>

</body>
</html>

效果

浮动布局

使用浮动,改变html元素原本的位置,然后调整样式 展示不同的布局方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上中下左右结构布局</title>
  <style>
/*<!-- 清除浏览器默认边距   -->*/
    .root{
      margin: 0;
      padding: 0;
    }
    /*页眉和导航共同设置*/
    .header,.nav,.a123{
      width: 100%;
      height: 100px;
    }
    /*如果要显示页脚,要把中间部分也设置一下下,不然页脚不出来了*/
    .a1{
      width: 100%;
      height: 500px;
    }
    .left,.center,.right{
      width:33.3% ;
      height: 100%;
    }

  </style>
</head>
<body>
<div class="root">
  <div class="header" style="background-color: red">页眉</div>
  <div class="nav" style="background-color: #ffcccc">导航</div>
  <div class="a1">
    <div class="left" style="float: left;background-color: sandybrown;">左边文章</div>
    <div class="center" style="float:left;background-color: aquamarine">中间文章</div>
    <div class="right" style="float: left; background-color: deeppink">右边文章</div>
  </div>
  <div class="a123" style=" background-color: #006699;">页脚</div>
</div>
</body>
</html>

定位布局

通过不同的定位方式, 更改div容器的位置 实现布局

  1. 顶部和底部容器:使用position: fixed;固定在视口的顶部和底部。
  2. 容器(.container):使用position: relative;作为其他绝对定位元素的参照。
  3. 左侧、中间和右侧内容:使用position: absolute;相对于.container进行定位。通过leftrightwidth属性控制它们的位置和大小。
  4. 滚动:中间内容区(.main-content)设置overflow-y: auto;以允许垂直滚动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>布局示例</title>
  <style>
    body, html {
    height: 100%;
    margin: 0;
    padding: 0;
    font-family: Arial, sans-serif;
}

.header, .footer {
    position: fixed;
    width: 100%;
    background-color: #333;
    color: white;
    text-align: center;
    padding: 10px 0;
    z-index: 1000;
}

.header {
    top: 0;
}

.footer {
    bottom: 0;
}

.container {
    position: relative;
    height: calc(100% - 60px); /* 减去头部和底部的高度 */
    padding: 20px 0; /* 为内容添加一些内边距 */
    box-sizing: border-box;
}

.sidebar, .main-content, .aside {
    position: absolute;
    height: 100%;
}

.sidebar {
    left: 0;
    width: 20%;
    background-color: #f4f4f4;
    padding: 20px;
    box-sizing: border-box;
}

.main-content {
    left: 20%; /* 左侧栏的宽度 */
    width: 60%; /* 剩余空间的大部分给中间内容 */
    padding: 20px;
    box-sizing: border-box;
    overflow-y: auto; /* 如果内容太多,可以滚动 */
}

.aside {
    right: 0;
    width: 20%;
    background-color: #f4f4f4;
    padding: 20px;
    box-sizing: border-box;
}

/* 确保滚动时,中间内容区可以滚动 */
.main-content, .container {
    overflow: hidden;
}

/* 其他样式可以根据需要添加 */

  </style>


</head>
<body>
    <div class="header">顶部导航</div>
    <div class="container">
        <div class="sidebar">左侧内容</div>
        <div class="main-content"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值