前端系统复习之页面布局篇(居中问题,三栏布局,圣杯双飞问题)

这里写图片描述 一.圣杯看本人的这篇文章
https://blog.csdn.net/xuehangongzi/article/details/80783430
二.居中问题
转载:【基础】这15种CSS居中的方式,你都用过哪几种?

简言
CSS居中是前端工程师经常要面对的问题,也是基本技能之一。今天有时间把CSS居中的方案汇编整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种。如有漏掉的,还会陆续的补充进来,算做是一个备忘录吧。

1、水平居中
1.1 内联元素水平居中
利用 text-align: center 可以实现在块级元素内部的内联元素水平居中。此方法对内联元素(inline), 内联块(inline-block), 内联表(inline-table), inline-flex元素水平居中都有效。

核心代码:
.center-text {
text-align: center;
}
1
2
3
1.2 块级元素水平居中
通过把固定宽度块级元素的margin-left和margin-right设成auto,就可以使块级元素水平居中。

核心代码:
.center-block {
margin: 0 auto;
}
1
2
3
1.3 多块级元素水平居中
1.3.1 利用inline-block
如果一行中有两个或两个以上的块级元素,通过设置块级元素的显示类型为inline-block和父容器的text-align属性从而使多块级元素水平居中。

核心代码:
.container {
text-align: center;
}
.inline-block {
display: inline-block;
}
1
2
3
4
5
6
1.3.2 利用display: flex
利用弹性布局(flex),实现水平居中,其中justify-content 用于设置弹性盒子元素在主轴(横轴)方向上的对齐方式,本例中设置子元素水平居中显示。

核心代码:
.flex-center {
display: flex;
justify-content: center;
}
1
2
3
4
2、垂直居中
2.1 单行内联(inline-)元素垂直居中
通过设置内联元素的高度(height)和行高(line-height)相等,从而使元素垂直居中。

核心代码:

v-box {

height: 120px;
line-height: 120px;

}
1
2
3
4
2.2 多行元素垂直居中
2.2.1 利用表布局(table)
利用表布局的vertical-align: middle可以实现子元素的垂直居中。

核心代码:
.center-table {
display: table;
}
.v-cell {
display: table-cell;
vertical-align: middle;
}
1
2
3
4
5
6
7
2.2.2 利用flex布局(flex)
利用flex布局实现垂直居中,其中flex-direction: column定义主轴方向为纵向。因为flex布局是CSS3中定义,在较老的浏览器存在兼容性问题。

核心代码:
.center-flex {
display: flex;
flex-direction: column;
justify-content: center;
}
1
2
3
4
5
2.2.3 利用“精灵元素”
利用“精灵元素”(ghost element)技术实现垂直居中,即在父容器内放一个100%高度的伪元素,让文本和伪元素垂直对齐,从而达到垂直居中的目的。

核心代码:
.ghost-center {
position: relative;
}
.ghost-center::before {
content: ” “;
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
width: 20rem;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2.3 块级元素垂直居中
2.3.1 固定高度的块级元素
我们知道居中元素的高度和宽度,垂直居中问题就很简单。通过绝对定位元素距离顶部50%,并设置margin-top向上偏移元素高度的一半,就可以实现垂直居中了。

核心代码:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px;
}
1
2
3
4
5
6
7
8
9
2.3.2 未知高度的块级元素
当垂直居中的元素的高度和宽度未知时,我们可以借助CSS3中的transform属性向Y轴反向偏移50%的方法实现垂直居中。但是部分浏览器存在兼容性的问题。

核心代码:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
1
2
3
4
5
6
7
8
3、水平垂直居中
3.1 固定宽高元素水平垂直居中
通过margin平移元素整体宽度的一半,使元素水平垂直居中。

核心代码:
.parent {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px;
}
1
2
3
4
5
6
7
8
9
10
11
12
3.2 未知宽高元素水平垂直居中
利用2D变换,在水平和垂直两个方向都向反向平移宽高的一半,从而使元素水平垂直居中。

核心代码:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
1
2
3
4
5
6
7
8
9
3.3 利用flex布局
利用flex布局,其中justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式;而align-items属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。

核心代码:
.parent {
display: flex;
justify-content: center;
align-items: center;
}
1
2
3
4
5
3.4 利用grid布局
利用grid实现水平垂直居中,兼容性较差,不推荐。

核心代码:
.parent {
height: 140px;
display: grid;
}
.child {
margin: auto;
}
1
2
3
4
5
6
7
3.5 屏幕上水平垂直居中
屏幕上水平垂直居中十分常用,常规的登录及注册页面都需要用到。要保证较好的兼容性,还需要用到表布局。

核心代码:
/方法一/
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}

.middle {
display: table-cell;
vertical-align: middle;
}

.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
}
/方法二/
.element{
width: 300px;
height: 300px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}

三、三栏布局

前端面试(前言)

面试基础

  • 页面布局

  • CSS盒模型:是CSS的基石。

  • DOM事件

  • HTTP协议

  • 面向对象

  • 原型链:能说出原型链的始末

面试进阶

  • 通信:普通的通信、跨域通信

  • 安全:CSRF、XSS。

  • 算法

回答问题时要注意的

(1)题干的要求真的是字面要求的这么简单吗?

(2)答案怎么写,技巧在哪里

(3)如果想证明我的实力,应该有几种答案?

本文来讲一下页面布局。

题目:页面布局

问题:假设高度默认100px ,请写出三栏布局,其中左栏、右栏各为300px,中间自适应。

分析:

初学者想到的答案有两种:

  • 方法1:浮动

  • 方法2:绝对定位。

但要求你能至少写出三四种方法,才算及格。剩下的方法如下:

  • 方法3:flexbox。移动开发里经常用到。

  • 方法4:表格布局 table。虽然已经淘汰了,但也应该了解。

  • 方法5:网格布局 grid。

下面分别讲解。

方法1 和方法2

方法1、浮动:

左侧设置左浮动,右侧设置右浮动即可,中间会自动地自适应。

方法2、绝对定位:

左侧设置为绝对定位, left:0px。右侧设置为绝对定位, right:0px。中间设置为绝对定位,left 和right 都为300px,即可。中间的宽度会自适应。

<
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我可以告诉您如何实现一个基础的前端三栏布局页面。首先,我们可以使用HTML和CSS来完成这个页面。 HTML部分: ``` <!DOCTYPE html> <html> <head> <title>三栏布局页面</title> <style type="text/css"> /* 设置外层容器的宽度和居中 */ .container { width: 960px; margin: 0 auto; } /* 设置三栏的样式 */ .left-column, .right-column, .content { float: left; height: 500px; position: relative; } /* 设置左侧栏的样式 */ .left-column { width: 200px; background-color: #EEE; } /* 设置右侧栏的样式 */ .right-column { width: 200px; background-color: #EEE; } /* 设置中间区域的样式 */ .content { width: 560px; background-color: #FFF; } /* 清除浮动 */ .clear { clear: both; } </style> </head> <body> <div class="container"> <div class="left-column"> <!-- 左侧栏内容 --> </div> <div class="content"> <!-- 中间内容 --> </div> <div class="right-column"> <!-- 右侧栏内容 --> </div> <div class="clear"></div> </div> </body> </html> ``` CSS部分: ``` /* 设置外层容器的宽度和居中 */ .container { width: 960px; margin: 0 auto; } /* 设置三栏的样式 */ .left-column, .right-column, .content { float: left; height: 500px; position: relative; } /* 设置左侧栏的样式 */ .left-column { width: 200px; background-color: #EEE; } /* 设置右侧栏的样式 */ .right-column { width: 200px; background-color: #EEE; } /* 设置中间区域的样式 */ .content { width: 560px; background-color: #FFF; } /* 清除浮动 */ .clear { clear: both; } ``` 以上就是一个基础的前端三栏布局页面的实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值