首页IT科技前端布局方法(前端常用布局方式大全——细致讲解)

前端布局方法(前端常用布局方式大全——细致讲解)

时间2025-06-20 14:53:47分类IT科技浏览4106
导读:盒模型 点击打开视频教程 标准盒模型、怪异盒模型(IE盒模型)...

盒模型

点击打开视频教程

标准盒模型              、怪异盒模型(IE盒模型)

什么是盒模型?

盒模型的作用:规定了网页元素如何显示以及元素间的相互关系 盒模型的概念:盒模型是css布局的基石              ,它规定了网页元素如何显示以及元素间的相互关系             。 css定义所有的元素都可以拥有像盒子一样的外形和平面空间                      。即都包含内容区                   、补白(填充)        、 边框           、边界(外边距)这就是盒模型      。

2                  、盒模型是怎样组成的?

盒模型的组成部分 content(内容区)+ padding(填充区)+ border(边框区)+ margin(外边界区) <template> <div id="app"> <div class="box"></div> </div> </template> <script> export default { name: App, data(){ return { } }, mounted(){ }, methods:{ } } </script> <style scoped> .box{ width: 100px; height: 100px; border: 4px solid red; padding: 5px; margin: 5px; } </style>

效果:

标准盒模型的组成:宽高(content)+ padding + border + margin

怪异盒模型的组成:width(content+border+padding)+ margin

可以用css属性更改是标准盒模型还是怪异盒模型

css属性:box-sizing: border-box(怪异盒模型)/content-box(标准盒模型)

浮动布局

float 属性定义元素往哪个方向浮动         。 float元素脱离了文档流                   ,但是不脱离文本流

浮动布局主要是利用float属性来实现        ,可以给元素设置float属性让元素脱离文档流           ,然后设置left和right来边改元素在文档上的展示位置以此形成我们想要的布局方式                  ,下面用浮动布局完成一个左右宽度固定中间自适应的三栏布局                      。

float实现三栏布局

<template> <div id="app"> <div class="content"> <div class="left"></div> <div class="right"></div> <div class="middle"></div> </div> </div> </template> <script> export default { name: App, data(){ return { } }, mounted(){ }, methods:{ } } </script> <style scoped> .content{ width:100%; height:200px; } .left { width: 200px; height: 100px; float: left; background-color: red; } .right { width: 200px; height: 100px; float: right; background-color: yellow; } .middle { width:400px; height: 100px; margin-left:100px; background-color: blue; } </style>

效果:

注意:使用float浮动布局middle中间的元素在html中要放在最后           ,否则黄色区域会换行        ,因为div是块级元素使用margin后右边区域也是属于它的         。

注意:如果给非float元素加上宽度                   ,一定要记得给此元素加上margin-left/right属性              , 否侧非float元素会被float元素覆盖住一部份      。

定位布局

定位布局是利用position来实现    ,可以使用absolute绝对定位移动元素的位置                    ,使用绝对定位也会使元素脱离文档流                 ,下面使用绝对定位实现一个三栏布局                      。

绝对定位实现三栏布局:

<template> <div id="app"> <!-- 定位布局 --> <div class="content"> <div class="positionLeft"></div> <div class="positionMiddle"></div> <div class="positionRight"></div> </div> </div> </template> <script> export default { name: App, data(){ return { } }, mounted(){ }, methods:{ } } </script> <style scoped> /* 定位布局 */ .positionLeft { position: absolute; width: 200px; left: 0; top: 0; background-color: red; } .positionMiddle { background-color: blue; margin: 0 200px; } .positionRight { position: absolute; width: 200px; right: 0; top: 0; background-color: yellow; } </style>

效果:

表格布局

表格是很严格的一行就是一行,一列就是一列                 ,并且他们的位置不会发生变化                    ,所以我们可以利用表格布局来实现我们想要的布局    ,在以前还没有出现这些浮动           、定位属性的时候表格用的是最多的布局方式             。通过给父元素设置display: table;              ,给子元素设置display: table-cell;即可实现三栏布局                   ,使用表格布局还是比较方便的        ,几乎不需要写什么样式就可以实现   。

表格布局实现三栏布局:

<template> <div id="app"> <!-- 表格布局 --> <div class="parent"> <div class="tableLeft"></div> <div class="tableMiddle"></div> <div class="tableRight"></div> </div> </div> </template> <script> export default { name: App, data(){ return { } }, mounted(){ }, methods:{ } } </script> <style scoped> /* 表格布局 */ .parent { display: table; width: 100%; } .parent > div { display: table-cell; } .tableLeft { width: 300px; background-color: red; } .tableRight { width: 300px; background-color: yellow; } .tableMiddle { background-color: blue; } </style>

效果:

flex布局

flex布局也叫弹性布局           ,是利用css3新出的属性display: flex实现的                  , 这种布局方式很方便           ,也不会对文档的结构改变        ,是当下最热门的一种布局方式                   , 建议每个前端开发者都要掌握                     。

flex布局实现三栏布局:

<template> <div id="app"> <!-- flex布局 --> <div class="flexParent"> <div class="flexLeft"></div> <div class="flexMiddle"></div> <div class="flexRight"></div> </div> </div> </template> <script> export default { name: App, data(){ return { } }, mounted(){ }, methods:{ } } </script> <style scoped> /* flex布局 */ .flexParent{ display: flex; } .flexLeft { width: 300px; background-color: red; } .flexRight { width: 300px; background-color: yellow; } .flexMiddle { flex: 1; background-color: blue; } </style>

效果:

grid 布局(栅格布局)

百分比布局

响应式布局

等等....

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
箭头函数的this指向能改变吗(setTimeout中的this指向问题和箭头函数结合的区别)