当前位置: 首页 > news >正文

【(一)页面布局】

页面布局

  • 一、常见css布局单位
  • 二、两栏布局
    • 1.浮动
    • 2.float + BFC
    • 3.flex
    • 4.定位
    • 5.定位
  • 三、三栏布局
    • 1.绝对定位
    • 2.flex
    • 3.float
  • 四、水平垂直居中
    • 1.绝对定位+translate
    • 2.绝对定位+margin:auto
    • 3.绝对定位
    • 4.flex
  • 五、响应式设计

一、常见css布局单位

布局单位描述
px(像素)一个像素等于计算机屏幕上的一个点;固定像素
%(百分比)相对单位,基于父元素,实现响应式效果
em文本相对长度单位,默认行内文本16px
remcss3新增,相对于根元素的font-size倍数,实现简单的响应式布局
vw相对于视窗的宽度,视窗宽度是100vw
vh相对于视窗的高度,视窗高度是100vh

二、两栏布局

左边一栏宽度固定,右边一栏宽度自适应

    <div class="outer"><div class="left"></div><div class="right"></div></div>

1.浮动

在这里插入图片描述

左浮动,宽200px;
右宽auto,margin-left:200px
浮动会使左脱离文档流

//给父元素高度避免浮动带来的高度坍塌
.outer {height: 100px;width: 100%;}.left {float: left;width: 200px;height: 100px;background: tomato;
}.right {margin-left: 200px;width: auto;height: 100px;background: gold;
}

2.float + BFC

在这里插入图片描述

overflow:hidden创建bfc,自动避开浮动元素

.left{width: 100px;height: 200px;background: red;float: left;}.right{height: 300px;background: blue;overflow: hidden;//创建bfc,自动避开浮动元素}

3.flex

在这里插入图片描述

左边固定,右边flex:1

.outer {display: flex;height: 100px;
}
.left {width: 200px;background: tomato;
}
.right {flex: 1;background: gold;
}

4.定位

在这里插入图片描述

父亲相对定位,左边绝对定位
绝对定位会使左边脱离文档流

.outer {position: relative;height: 100px;width: 100%;
}.left {position: absolute;width: 200px;height: 100px;/*background: tomato;*/
}.right {margin-left: 200px;background: gold;height: 100px;;
}

5.定位

在这里插入图片描述

父元素相对定位;右边绝对定位
使用绝对定位时,浏览器会自动计算宽高:
/* 浏览器自动计算:height = 父容器高度 - top - bottom /
/
浏览器自动计算:width = 父容器宽度 - left - right */

.outer {position: relative;height: 100px;
}.left {width: 200px;height: 200px;background: tomato;}.right {position: absolute;top: 0;right: 0;bottom: 0;left: 200px;//如果不给left,无法自动计算宽,right就成了一条线
//因为auto 宽度 + 无内容 = 宽度为 0,background: gold;
}

三、三栏布局

左右固定,中间自适应

    <div class="outer"><div class="left"></div><div class="center"></div><div class="right"></div></div>

1.绝对定位

左右绝对定位,中间给margin

.outer {position: relative;height: 100px;
}.left {position: absolute;width: 100px;height: 100px;background: tomato;
}.right {position: absolute;top: 0;right: 0;width: 200px;height: 100px;background: gold;
}.center {margin-left: 100px;margin-right: 200px;height: 100px;background: lightgreen;
}

2.flex

左右固定,中间自适应

.outer {display: flex;height: 100px;
}.left {width: 100px;background: tomato;
}.right {width: 100px;background: gold;
}.center {flex: 1;background: lightgreen;
}

3.float

左右固定并浮动,中间margin,且中间必须放到最后

    <div class="outer"><div class="left"></div><div class="right"></div><div class="center"></div>
//center放最后
//浮动元素会按照HTML顺序依次排列</div>
.outer {height: 100px;
}.left {float: left;width: 100px;height: 100px;background: tomato;
}.right {float: right;width: 200px;height: 100px;background: gold;
}.center {height: 100px;margin-left: 100px;margin-right: 200px;background: lightgreen;
}

四、水平垂直居中

    <div class="parent"><div class="child"></div></div>

在这里插入图片描述

1.绝对定位+translate

父元素宽高和相对定位
子元素相对定位;top:50%和left:50%;translate

.parent {position: relative;height: 400px;width: 100%;background-color: #f0f0f0;}.child {position: absolute;//left,top将线定位到中心left: 50%;top: 50%;//translate来调整元素的中心点到页面的中心transform: translate(-50%, -50%);width: 200px;height: 100px;background-color: tomato;
}

2.绝对定位+margin:auto

适用于盒子有宽高

.parent {position: relative;height: 400px;width: 100%;background-color: #f0f0f0;}.child {position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;width: 200px;height: 100px;background-color: tomato;
}

3.绝对定位

top: 50%; left: 50%;定位到中间,再通过margin负值

.parent {position: relative;height: 400px;width: 100%;background-color: #f0f0f0;}.child {position: absolute;top: 50%;left: 50%;margin-top: -50px;/* 自身 height 的一半 */margin-left: -50px;/* 自身 width 的一半 */width: 200px;height: 100px;background-color: tomato;
}

4.flex

.parent {display: flex;justify-content: center;align-items: center;height: 400px;width: 100%;background-color: #f0f0f0;
}.child {width: 200px;height: 100px;background-color: tomato;
}

五、响应式设计

  • 响应式网站设计是一个网站能够兼容多个终端,而不是为每一个终端做一个特定的版本
  • 基本原理是通过媒体查询(@media)查询检测不同的设备屏幕尺寸做处理。
  • 关于兼容: 页面头部必须有mate声明的viewport。
<meta name="’viewport’" content="”width=device-width," initial-scale="1." maximum-scale="1,user-scalable=no”"/>
http://www.dtcms.com/a/329506.html

相关文章:

  • 海康视觉平台VM创建项目
  • JAVA实战小项目——输入验证码
  • rtmp 推流
  • 浅层神经网络
  • Dimensional Analysis量纲分析入门
  • 猫粮哪个牌子质量好性价比高?2025适合幼猫的猫粮推荐
  • LangGraph 指南篇-基础控制
  • GaussDB 动态内存过高处理办法
  • 从表单校验到API网关:全链路输入安全防护指南
  • SeaTunnel MCP Server 入选《中国信通院开源商业产品及企业典型案例集(2025)》
  • 开源日志log4cplus—如何将 string类型转为tstring类型,又如何将char*类型转换为tstring类型?
  • 机器学习入门:核心概念详解与Python示例代码
  • 飞算JavaAI的“盾牌”计划:手撕Spring Security + JWT认证链
  • 【debian系统】cuda13和cudnn9.12详细安装步骤
  • 常用机器学习公开数据集大全
  • Spring、Spring MVC、Spring Boot与Spring Cloud的扩展点全面梳理
  • model层实现:
  • 设计模式笔记_行为型_策略模式
  • 【前端Vue】使用ElementUI实现表单中可选择可编辑的下拉框
  • 用 Qt C++ 从零打通“前端界面 → 后端接口”的数据交互
  • 为什么 sim(3) 中的尺度 s 与旋转 R 相乘,而不是平移 t?
  • Go语言实战案例:使用Gin处理路由参数和查询参数
  • 商品分类拖拽排序设计
  • Vue 3 快速入门 第七章
  • 第三天-经典CAN2.0 DBC快速切换为CANFD DBC
  • day39_2025-08-13
  • 手动编译 JSONCPP 静态库​(CMake)
  • aliases 的意义和作用?
  • Mac M1探索AnythingLLM+SearXNG
  • nginx配置代理服务器