網站首頁 編程語言 正文
web前端實現水平垂直居中、position、relative、absolute、transform、translate、auto、display、flex、justify、align、center
作者:web半晨 更新時間: 2022-06-06 編程語言目錄
1、文本水平垂直居中
HTML
<div class="text_box">
<span>文本實現垂直水平居中</span>
</div>
CSS
.text_box {
width: 230px;
height: 60px;
/* 水平居中 */
text-align: center;
/* 垂直居中 */
line-height: 60px;
background-color: #ccc;
}
2、使用定位實現水平居中/寬高未知
2.1、transform
HTML
<div class="unknown_wh_box">
<div></div>
</div>
CSS
.unknown_wh_box {
position: relative;
}
.unknown_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
2.2、margin
HTML
<div class="unknown_wh_box">
<div></div>
</div>
CSS
.unknown_wh_box {
position: relative;
}
.unknown_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
position: absolute;
left: 0;
right: 0;
margin: auto;
}
3、使用定位實現水平垂直居中/寬高已知
3.1、transform
HTML
<div class="known_wh_box">
<div></div>
</div>
CSS
.known_wh_box {
width: 70%;
height: 300px;
position: relative;
background-color: #aaa;
}
.known_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
3.2、margin
HTML
<div class="known_wh_box">
<div></div>
</div>
CSS
.known_wh_box {
width: 70%;
height: 360px;
position: relative;
background-color: #aaa;
}
.known_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
4、彈性布局實現水平垂直居中
4.1、未知寬高
高度會自動被撐開,且緊貼子容器。
HTML
<div class="unknown_wh_box">
<div></div>
</div>
CSS
.unknown_wh_box {
display: flex;
justify-content: center;
align-items: center;
}
.unknown_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
}
4.2、已知寬高
HTML
<div class="known_wh_box">
<div></div>
</div>
CSS
.known_wh_box {
width: 70%;
height: 360px;
background-color: #aaa;
display: flex;
justify-content: center;
align-items: center;
}
.known_wh_box>div {
width: 230px;
height: 230px;
background-color: #ccc;
}
原文鏈接:https://blog.csdn.net/weixin_51157081/article/details/124844634
相關推薦
- 2022-04-22 阿里云ECS服務器Linux下載安裝JDK8
- 2022-09-03 Nginx代理Redis哨兵主從配置的實現_nginx
- 2022-10-23 C#中的var關鍵字用法介紹_C#教程
- 2022-08-15 springboot的熱部署配置
- 2023-02-15 python中的lambda函數用法指南_python
- 2022-07-23 C#中的隨機數函數Random()_C#教程
- 2022-11-06 詳解Python中的null是什么_python
- 2022-09-13 C++?static詳解,類中的static用法說明_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支