当前位置:首页 > 技术 >

常见的css布局方式(css布局技巧大全)

来源:原点资讯(www.yd166.com)时间:2023-02-09 15:32:52作者:YD166手机阅读>>

常见的css布局方式,css布局技巧大全(1)

1.单列布局1.1 水平居中父元素 text-align:center;子元素:inline-block;
  • 优点:兼容性好;
  • 不足:需要同时设置子元素和父元素

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

.parent{ width: 500px; height: 200px; background: red; text-align: center; } .child{ display: inline-block; width: 300px; height: 100px; background: blue; }子元素 margin:0 auto;

  • 优点:兼容性好
  • 缺点:需要指定宽度

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

.parent{width: 500px; height: 400px; background: red; } .child{margin: 0 auto; width: 300px; height: 100px ;background: blue; }父元素:relative;子元素:absolute;left:50%;margin-left:-宽度的一半

  • 优点:兼容性好
  • 缺点:需要知道元素的宽度

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

.parent { position: relative; top: 0; left: 0; width: 500px; height: 400px; background: red; } .child { position: absolute; top: 0; left: 50%; margin-left: -150px; width: 300px; height: 100px; background: blue; }父元素:relative;子元素:absolute;left:50%;transform:translate(-50%,0);

  • 优点:不需要知道子元素的宽度
  • 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)

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

.parent { position: relative; top: 0; left: 0; width: 500px; height: 400px; background: red; } .child { position: absolute; top: 0; left: 50%; transform: translate(-50%, 0); width: 300px; height: 100px; background: blue; }弹性盒子:父元素:display:flex;justify-content:center;

  • 优点:简单
  • 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)

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

.parent { display: flex; justify-content: center; width: 500px; height: 400px; background: red; } .child { width: 300px; height: 100px; background: blue; }1.2 垂直居中vertical-align:center;

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

.parent { width: 500px; height: 400px; background: red; display: table-cell; vertical-align: middle; } .child { width: 300px; height: 100px; background: blue; }line-height

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

.parent { width: 500px; height: 400px; background: red; line-height: 400px; } .child { width: 300px; height: 100px; background: blue; display: inline-block; vertical-align: middle; }父元素:position:relative;子元素:positon:absolute;top:50%;transform:translate(0,-50%);

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

.parent { position: relative; top: 0; left: 0; width: 500px; height: 400px; background: red; } .child { position: absolute; top: 50%; left: 0; transform: translate(0, -50%); width: 300px; height: 100px; background: blue; }父元素:position:relative;子元素:positon:absolute;top:50%;margin-top:-子元素高度的一半;

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

.parent { position: relative; top: 0; left: 0; width: 500px; height: 400px; background: red; } .child { position: absolute; top: 50%; left: 0; margin-top: -50px; width: 300px; height: 100px; background: blue; }父元素:display:flex;align-items:center;

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

.parent { width: 500px; height: 400px; background: red; display: flex; align-items: center; } .child { width: 300px; height: 100px; background: blue; }1.3 水平垂直居中父元素:display:table-cell;vertical-align:middle;text-align:center;子元素;display:inline-block;

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

.parent { width: 500px; height: 400px; background: red; display: table-cell; vertical-align: middle; text-align: center; } .child { width: 300px; height: 100px; background: blue; display: inline-block; }父元素:position:relative;子元素:position:absolute?50%;left:50%;margin-left:宽度的一半;margin-top:高度的一半;或者 transform:translate(-50%,-50%);

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

.parent { width: 500px; height: 400px; background: red; position: relative; left: 0; right: 0; } .child { width: 300px; height: 100px; background: blue; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }父元素{display:flex;justify-content:center;align-items:center;}

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

.parent { width: 500px; height: 400px; background: red; display: flex; justify-content: center; align-items: center; } .child { width: 300px; height: 100px; background: blue; }2.多列布局2.1 左侧定宽,右侧自适应

left{float:left;width:100px;} .right{margin-left:100px;}

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

* { margin: 0; padding: 0; } .left { height: 400px; width: 300px; background: red; float: left; } .right { height: 400px; margin-left: 300px; background: blue; }


<div class="parent"> <div class="left"> left </div> <div class="right-fix"> <div class="right"> right </div> </div> </div>

* { margin: 0; padding: 0; } .left { width: 300px; height: 400px; float: left; background: red; } .right-fix { width: 100%; margin-left: -300px; float: right; } .right { margin-left: 300px; height: 400px; background: blue; }

.left{width:宽度值;float:left;} .right{overflow:hidden;}

<divclass="parent"> <divclass="left"> left </div><divclass="right"> right </div> </div>

/*设置overflow:hidden;创建BFC。根据BFC特性,BFC不会与float box 重叠。*/ * { margin: 0; padding: 0; } .left { width: 300px; height: 400px; float: left; background: red; } .right { height: 400px; background: blue; overflow: hidden; }

table 实现

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

* { margin: 0; padding: 0; } .parent { display: table; table-layout: fixed; width: 100%; } .left { width: 300px; height: 400px; background: red; display: table-cell; } .right { height: 400px; background: blue; display: table-cell; }

flex 实现

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

* { margin: 0; padding: 0; } .parent { display: flex; width: 100%; } .left { width: 300px; height: 400px; background: red; } .right { height: 400px; background: blue; flex: 1; }2.2 右侧定宽左侧自适应

float margin 实现

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

* { margin: 0; padding: 0; } .left { width: 100%; height: 400px; background: red; float: left; margin-right: -300px; } .right { height: 400px; background: blue; width: 300px; float: right; }

table 实现

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

* { margin: 0; padding: 0; } .parent { width: 100%; display: table; table-layout: fixed; } .left { width: 100%; height: 400px; background: red; display: table-cell; } .right { height: 400px; background: blue; width: 300px; display: table-cell; }

flex 实现

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

* { margin: 0; padding: 0; } .parent { width: 100%; display: flex; } .left { flex: 1; background: red; display: table-cell; } .right { height: 400px; background: blue; width: 300px; }2.3 左边两列定宽,右侧自适应

float margin 实现

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

* { margin: 0; padding: 0; } .parent { width: 100%; } .left, .center { background: red; float: left; width: 300px; height: 400px; } .center { background: yellow; } .right { height: 400px; background: blue; margin-left: 600px; }

float overflow 实现

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

* { margin: 0; padding: 0; } .parent { width: 100%; } .left, .center { background: red; float: left; width: 300px; height: 400px; } .center { background: yellow; } .right { height: 400px; background: blue; overflow: hidden; }

table 实现

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

*{

margin: 0;

padding: 0;

}

.parent{

width: 100%;

display: table;

table-layout: fixed;

}

.left,

.center{

background: red;

display: table-cell;

width: 300px;

height: 400px;

}

.center{

background: yellow;

}.right{

height: 400px;

background: blue;

display: table-cell;

}

想要更多关于前端培训学习资料,可以关注“广州蓝景”微信公众号,进行详细的了解。

栏目热文

css和html网页布局(网页框架与布局css)

css和html网页布局(网页框架与布局css)

三星 Galaxy Fold 和 Surface Duo 以及华为mate X等系列折叠屏手机问世至今已有三年多的时间。...

2023-02-09 15:37:35查看全文 >>

css有哪些页面布局(三种css布局)

css有哪些页面布局(三种css布局)

最近忙里偷闲,给自己加油充电的时候,发现自己脑海中布局这块非常的凌乱混杂,于是花了一些时间将一些常用的布局及其实现方法整...

2023-02-09 15:21:23查看全文 >>

常用的css布局方式(css常见的布局方式)

常用的css布局方式(css常见的布局方式)

浮动布局浮动布局示例相关CSS属性float - left:元素向左浮动 - right:元素向右浮动 - none:默...

2023-02-09 15:19:46查看全文 >>

css简单网页布局(css网页布局排版实例)

css简单网页布局(css网页布局排版实例)

简介布局在我们前端日常开发来说是非常重要的,一个好的布局能简化代码的同时还能提高网页的性能。常见的布局方法有浮动(flo...

2023-02-09 15:38:21查看全文 >>

css首页布局实例(css布局技巧实例)

css首页布局实例(css布局技巧实例)

前几天我在面试前端开发同学的时候,有问到关于margin基础布局相关内容的过程中,发现很多同学基本解释不清楚,今天刚好有...

2023-02-09 15:13:27查看全文 >>

海绵宝宝大结局正片(海绵宝宝全集大结局普通话)

海绵宝宝大结局正片(海绵宝宝全集大结局普通话)

大家看《海绵宝宝》这部动画片的时候是不是都很奇怪,其中的剧情好像一直发展着,并没有要结局的意思。所以有的网友就着急了,真...

2023-02-09 15:51:08查看全文 >>

海绵宝宝大结局电影中文版(海绵宝宝电影中文版在线观看)

海绵宝宝大结局电影中文版(海绵宝宝电影中文版在线观看)

{"rich_content":{"text":"海绵宝宝:海绵宝宝大结局?被海怪吃掉,所有人都被吃了!","spans...

2023-02-09 15:12:12查看全文 >>

海绵宝宝最好的结局(海绵宝宝会有大结局吗)

海绵宝宝最好的结局(海绵宝宝会有大结局吗)

《狂飙》终于“高开疯走”完美落幕,39集的剧情真是集集高能。结局也不出所料,所有反派集体落网,虽然这个结局也挺圆满的,但...

2023-02-09 15:39:21查看全文 >>

海绵宝宝大结局全集中文(海绵宝宝第一季大结局免费)

海绵宝宝大结局全集中文(海绵宝宝第一季大结局免费)

逐渐失去发言权“准备好了吗孩子们?”“是的船长!”“Ohhhhhhhhhhhh”很多人都知道这段让人“DNA动了”的开场...

2023-02-09 15:55:23查看全文 >>

海绵宝宝中文版大结局(海绵宝宝正版大结局)

海绵宝宝中文版大结局(海绵宝宝正版大结局)

这部剧由任嘉伦和李沁领衔主演的年代玄幻剧,从一开始的时候就没有多少悬念,然而却在最后一集,狠狠地来了一个开放式大结局,让...

2023-02-09 15:53:08查看全文 >>

文档排行