CSS 水平,垂直,水平垂直居中方案——持续更新,前端开发面试2024

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

left: 50%;

transform: translate(-50%, 0);

}

.inline {

background: pink;

position: absolute;

width: 100px;

left: 50%;

transform: translate(-50%, 0);

}

在这里插入图片描述

绝对定位 left/right: 0


position: absolute;

width: 宽度;

left: 0;

right: 0;

margin: 0 auto;

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

left: 0;

right: 0;

margin: 0 auto;

}

.inline {

background: pink;

position: absolute;

width: 100px;

left: 0;

right: 0;

margin: 0 auto;

}

在这里插入图片描述

垂直居中

===============================================================

行内元素


父元素 height: 高度;

行内元素 line-height: 高度;

.parent {

background: #eee;

width: 300px;

height: 300px;

}

.inline {

background: pink;

line-height: 300px;

}

在这里插入图片描述

table


父元素 display: table-cell;

子元素 vertical-align: middle;

.parent {

background: #eee;

width: 300px;

height: 300px;

display: table-cell;

vertical-align: middle;

}

在这里插入图片描述

flex


display: flex;

align-items: center;

.parent {

background: #eee;

width: 300px;

height: 300px;

display: flex;

align-items: center;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

}

.inline {

background: pink;

}

在这里插入图片描述

绝对定位 定高


position: absolute;

top: 50%;

height: 高度;

margin-top: -0.5高度;

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

top: 50%;

margin-top: -50px;

}

.inline {

background: pink;

height: 100px;

position: absolute;

top: 50%;

margin-top: -50px;

}

在这里插入图片描述

绝对定位 不定高


position: absolute;

top: 50%;

transform: translate( 0, -50%);

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

top: 50%;

transform: translate( 0, -50%);

}

.inline {

background: pink;

height: 100px;

position: absolute;

top: 50%;

transform: translate( 0, -50%);

}

在这里插入图片描述

绝对定位 top/bottom: 0;


position: absolute;

height: 高度;

top: 0;

bottom: 0;

margin: auto 0;

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

top: 0;

bottom: 0;

margin: auto 0;

}

.inline {

background: pink;

position: absolute;

height: 100px;

top: 0;

bottom: 0;

margin: auto 0;

}

在这里插入图片描述

元素水平垂直居中

===================================================================

绝对定位 不定宽高


绝对定位 transform

position:absolute;

top:50%;

left:50%;

transform:translate(-50%,-50%)

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50% ,-50%);

}

在这里插入图片描述

绝对定位 定宽高


绝对定位 margin

position:absolute;

top:50%;

left:50%;

margin-top:-(height/2)px;

margin-left:-(width/2)px;

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

top: 50%;

left: 50%;

margin-top: -50px;

margin-left: -50px;

}

绝对定位 margin:auto

position:absolute/fixed;

top:0;left:0;right:0;bottom:0;

margin:atuo;

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

width: 100px;

height: 100px;

position: absolute;

top: 0;

bottom: 0;

left: 0;

right: 0;

margin: auto;

}

在这里插入图片描述

绝对定位 calc 水平、垂直居中也可以使用此方法

position:absolute;

top:calc(50% - (height/2)px);

left:calc(50% - (width/2)px);

.parent {

background: #eee;

width: 300px;

height: 300px;

position: relative;

}

.block {

background: skyblue;

position: absolute;

width: 100px;

height: 100px;

top:calc(50% - 50px);

left:calc(50% - 50px);

}

  • calc() 函数

calc() 函数用于动态计算长度值

需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);

任何长度值都可以使用calc()函数进行计算;

calc()函数支持"+", "-", "*", "/"运算;

calc()函数使用标准的数学运算优先级规则

flex


display: flex;

justify-content: center;

align-items: center;

.parent {

background: #eee;

width: 300px;

height: 300px;

display: flex;

justify-content: center;

align-items: center;

}

在这里插入图片描述

table


display: table-cell;

vertical-align: middle;

text-align: center;

如果是块级元素 需要 display: inline-block;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

React

  • 介绍一下react

  • React单项数据流

  • react生命周期函数和react组件的生命周期

  • react和Vue的原理,区别,亮点,作用

  • reactJs的组件交流

  • 有了解过react的虚拟DOM吗,虚拟DOM是怎么对比的呢

  • 项目里用到了react,为什么要选择react,react有哪些好处

  • 怎么获取真正的dom

  • 选择react的原因

  • react的生命周期函数

  • setState之后的流程

  • react高阶组件知道吗?

  • React的jsx,函数式编程

  • react的组件是通过什么去判断是否刷新的

  • 如何配置React-Router

  • 路由的动态加载模块

  • Redux中间件是什么东西,接受几个参数

  • redux请求中间件如何处理并发

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

ct生命周期函数和react组件的生命周期

  • react和Vue的原理,区别,亮点,作用

  • reactJs的组件交流

  • 有了解过react的虚拟DOM吗,虚拟DOM是怎么对比的呢

  • 项目里用到了react,为什么要选择react,react有哪些好处

  • 怎么获取真正的dom

  • 选择react的原因

  • react的生命周期函数

  • setState之后的流程

  • react高阶组件知道吗?

  • React的jsx,函数式编程

  • react的组件是通过什么去判断是否刷新的

  • 如何配置React-Router

  • 路由的动态加载模块

  • Redux中间件是什么东西,接受几个参数

  • redux请求中间件如何处理并发

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

  • 18
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值