当前位置:首页 > 技术 >

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

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

实现水平垂直同时居中我们可以使用绝对定位table布局flex布局grid布局来实现。

首先我们创建一个需要居中的盒子。

<div class="box"></div> 纯绝对定位

.box { position: absolute; width: 200px; height: 100px; background: red; top: 0; left: 0; right: 0; bottom: 0; margin: auto; }

点击查看代码运行实例

绝对定位加负外边距

这种方式需要知道居中元素的具体宽高,不然负的margin没法设置。

.box { position: absolute; width: 200px; height: 100px; background: red; left: 50%; top: 50%; margin-left: -100px; margin-top: -50px; }

点击查看代码运行实例

绝对定位加平移

这种平移的方式就不需要考虑居中盒子的具体宽高了。

.box { position: absolute; width: 200px; height: 100px; background: red; left: 50%; top: 50%; transform: translate(-50%, -50%); }

点击查看代码运行实例

使用flex实现

html,body { height: 100%; } body { background: gray; display: flex; align-items: center; justify-content: center; } .box { width: 200px; height: 100px; background: red; }

点击查看代码运行实例

使用grid实现

html,body { height: 100%; } body { background: gray; display: grid; /* align-content: center; justify-content: center; */ /* align-content和justify-content的简写 */ place-content: center; } .box { width: 200px; height: 100px; background: red; }

点击查看代码运行实例

使用table加外边距实现

使用table布局需要注意如下

  1. display: tablepadding会失效
  2. display: table-rowmargin、padding同时失效
  3. display: table-cellmargin会失效

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

.box { background: red; height: 300px; width: 600px; display: table-cell; vertical-align: middle; } .child { width: 200px; height: 200px; background: lightgreen; margin: 0 auto; }

点击查看代码运行实例

单栏布局

单栏布局我们常用在网页框架上,一般我们把网页分为 headercontentfooter三部分。

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

在不同的项目我们可能对这三部分的样式需求有所差别,比如需要顶部固定、需要底部固定等等。

顶底部都不固定

比如想实现如下效果,footer在内容不足的时候吸附在窗口底部,当内容多的时候又可以被抵到窗口下面。

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

使用padding加负margin实现

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> </div> <div class="footer">footer</div>

html, body { height: 100%; margin: 0; } .wrap { min-height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box; } .header { height: 50px; background: lightblue; } .content { background: lightpink; /* 这里的高度只是为了模拟内容多少 */ height: 100px; /* height: 1000px; */ } .footer { height: 50px; background: lightgreen; margin-top: -50px; }

点击查看代码运行实例

使用flex实现

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div> </div>

html, body { height: 100%; margin: 0; } .wrap { display: flex; flex-direction: column; min-height: 100%; } .header { height: 50px; background: lightblue; } .content { background: lightpink; /* 这里的高度只是为了模拟内容多少 */ height: 100px; /* height: 1000px; */ flex-grow: 1; } .footer { height: 50px; background: lightgreen; }

点击查看代码运行实例

顶部固定使用padding加负margin加fixed实现顶部固定布局

<div class="header">header</div> <div class="wrap"> <div class="content">content</div> </div> <div class="footer">footer</div>

html, body { height: 100%; margin: 0; } .header { height: 50px; background: lightblue; position: fixed; width: 100%; } .wrap { min-height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box; } .content { margin-top: 50px; background: lightpink; /* 这里的高度只是为了模拟内容多少 */ height: 100px; /* height: 1000px; */ } .footer { height: 50px; background: lightgreen; margin-top: -50px; }

点击查看代码运行实例

使用flex加fixed定位实现

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div> </div>

html, body { height: 100%; margin: 0; } .wrap { display: flex; min-height: 100%; flex-direction:column; } .header { height: 50px; background: lightblue; position: fixed; width: 100%; } .content { background: lightpink; /* 这里的高度只是为了模拟内容多少 */ /* height: 100px; */ height: 1000px; margin-top: 50px; flex-grow: 1; } .footer { height: 50px; background: lightgreen; }

点击查看代码运行实例

底部固定使用padding加负margin实现底部固定布局

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> </div> <div class="footer">footer</div>

html, body { height: 100%; margin: 0; } .wrap { height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box; } .header { height: 50px; background: lightblue; } .content { background: lightpink; height: 100px; height: 1000px; } .footer { height: 50px; background: lightgreen; margin-top: -50px; }

点击查看代码运行实例

使用flex加fixed定位实现

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div> </div>

html, body { height: 100%; margin: 0; } .wrap { display: flex; min-height: 100%; flex-direction:column; } .header { height: 50px; background: lightblue; } .content { background: lightpink; /* 这里的高度只是为了模拟内容多少 */ /* height: 100px; */ height: 1000px; flex-grow: 1; margin-bottom: 50px; } .footer { height: 50px; background: lightgreen; position: fixed; width: 100%; bottom: 0; }

点击查看代码运行实例

顶底部都固定使用fixed实现顶底部固定布局

<div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div> 复制代码

html, body { height: 100%; margin: 0; } .header { height: 50px; background: lightblue; position: fixed; width: 100%; } .content { background: lightpink; padding-top: 50px; padding-bottom: 50px; /* height: 100px; */ height: 1000px; } .footer { height: 50px; background: lightgreen; position: fixed; bottom: 0; width: 100%; }

点击查看代码运行实例

使用flex加fixed定位实现

<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div> </div>

html, body { height: 100%; margin: 0; } .wrap { display: flex; min-height: 100%; flex-direction:column; } .header { height: 50px; background: lightblue; position: fixed; width: 100%; } .content { background: lightpink; /* 这里的高度只是为了模拟内容多少 */ /* height: 100px; */ height: 1000px; flex-grow: 1; margin-bottom: 50px; margin-top: 50px; } .footer { height: 50px; background: lightgreen; position: fixed; width: 100%; bottom: 0; }

点击查看代码运行实例

两栏布局

两栏布局就是一边固定,另外一边自适应,效果如下

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

实现两栏布局的方法也有很多,笔者接下来介绍用的比较多的几种方式。

左 float,然后右 margin-left(右边自适应)

<div class="aside"></div> <div class="main"></div>

div { height: 500px; } .aside { width: 300px; float: left; background: yellow; } .main { background: aqua; margin-left: 300px; }

点击查看代码运行实例

右 float,然后右 margin-right(左边自适应)

<div class="aside"></div> <div class="main"></div>

div { height: 500px; } .aside { width: 300px; float: right; background: yellow; } .main { background: aqua; margin-right: 300px; }

点击查看代码运行实例

absolute定位加margin-left(右边自适应)

<div class="wrap"> <div class="aside"></div> <div class="main"></div> </div>

div { height: 500px; } .wrap { position: relative; } .aside { width: 300px; background: yellow; position: absolute; } .main { background: aqua; margin-left: 300px; }

点击查看代码运行实例

absolute定位加margin-right(左边自适应)

<div class="wrap"> <div class="aside"></div> <div class="main"></div> </div>

div { height: 500px; } .wrap { position: relative; } .aside { width: 300px; background: yellow; position: absolute; right: 0; } .main { background: aqua; margin-right: 300px; }

点击查看代码运行实例

使用flex实现

<div class="wrap"> <div class="aside"></div> <div class="main"></div> </div>

div { height: 500px; } .wrap { display: flex; } .aside { flex: 0 0 300px; background: yellow; } .main { background: aqua; flex: 1 1; }

点击查看代码运行实例

使用grid实现

<div class="wrap"> <div class="aside"></div> <div class="main"></div> </div>

height: 500px; } .wrap { display: grid; grid-template-columns: 300px auto; } .aside { background: yellow; } .main { background: aqua; }

点击查看代码运行实例

三栏布局

三栏布局就是两边固定,中间自适应布局,效果如下

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

栏目热文

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

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

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

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

css布局设计方案(css布局100种方法)

css布局设计方案(css布局100种方法)

我们在网页制作中,会有许多的术语例如:CSS、HTML、DHTML、XHTML等等。下面开始使用Div CSS进行网页布...

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

使用css样式布局网页外观(网页设计css布局方式)

使用css样式布局网页外观(网页设计css布局方式)

作者有话要说:此文是作者自己的学习总结,供大家参考,不足之处还请见谅和指正~在学习完了基本的HTML CSS标签之后,就...

2023-02-09 15:22:05查看全文 >>

div css布局技巧

div css布局技巧

我们在网页制作中,会有许多的术语例如:CSS、HTML、DHTML、XHTML等等。下面开始使用Div CSS进行网页布...

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

css grid布局详细图解(图解css grid 布局)

css grid布局详细图解(图解css grid 布局)

分享兴趣,传播快乐,增长见闻,留下美好!亲爱的您,这里是LearningYard新学苑。今天小编为大家带来华硕前端1...

2023-02-09 15:18:07查看全文 >>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1.单列布局1.1 水平居中父元素 text-align:center;子元素:inline-block;优点:兼容性好...

2023-02-09 15:32:52查看全文 >>

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

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

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

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

文档排行