網站首頁 編程語言 正文
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-07-21 數據庫表數據操作-新增、刪除、修改
- 2022-05-08 C++?vector的簡單實現_C 語言
- 2022-03-15 使用Sqlyog遠程連接數據庫報錯解決方案_數據庫其它
- 2022-08-04 Go?slice切片make生成append追加copy復制示例_Golang
- 2023-07-27 全屏情況下Fragment中的webview中的文本框被軟鍵盤擋住的問題
- 2023-01-01 Kotlin?ContentProvider使用方法詳解_Android
- 2022-08-15 前端el-select下拉數據太長顯示...鼠標浮上去顯示全部
- 2022-10-11 ArrayList源碼中的MAX_ARRAY_SIZE
- 最近更新
-
- 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同步修改后的遠程分支