網站首頁 編程語言 正文
如何讓一個有寬高的盒子垂直居中
1.利用盒子寬高和margin
<html>
<head>
<style type="text/css">
*{
padding: 0;
margin: 0;
list-style: none;
}
.fu{
width: 750px;
height: 600px;
border: 1px solid gray;
background-color: antiquewhite;
}
.son{
width: 300px;
height: 200px;
border: 1px solid red;
margin-left: 225px;
margin-top: 200px;
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="fu">
<div class="son"></div>
</div>
</body>
</html>
該方法是需要精確的父元素寬高的情況下
界面如下圖一:
2.利用盒子寬高和margin
<html>
<head>
<style type="text/css">
*{
padding: 0;
margin: 0;
list-style: none;
}
.fu{
width: 750px;
height: 600px;
border: 1px solid gray;
background-color: antiquewhite;
}
.son{
width: 300px;
height: 200px;
border: 1px solid red;
margin: 200px auto;
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="fu">
<div class="son"></div>
</div>
</body>
</html>
這種方法相比于第一種是只對上邊距有明確的控制,而對左右用auto,與第一種幾乎相同。
3.絕對定位+translate
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
*{
padding: 0;
margin: 0;
list-style: none;
}
.fu{
width: 750px;
height: 600px;
border: 1px solid gray;
background-color: antiquewhite;
position: relative;
}
.son{
width: 300px;
height: 200px;
border: 1px solid red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="fu">
<div class="son"></div>
</div>
</body>
</html>
結果同圖一
4.flex布局
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
*{
padding: 0;
margin: 0;
list-style: none;
}
.fu{
width: 750px;
height: 600px;
border: 1px solid gray;
background-color: antiquewhite;
display: flex;
justify-content: center;
align-items: center;
}
.son{
width: 300px;
height: 200px;
border: 1px solid red;
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="fu">
<div class="son"></div>
</div>
</body>
</html>
結果同圖一
這幾總方法都可以用,但是有優點也有缺陷,本人一般使用方法2 3多一些。
原文鏈接:https://blog.csdn.net/bjt_yy/article/details/118892887
相關推薦
- 2022-06-06 Element UI詳解el-scrollbar 滾動條組件 —— 監聽滾動條的滾動,跟隨頁面一起滾
- 2022-10-20 詳解Go?語言如何通過測試保證質量_Golang
- 2022-10-31 Python+Selenium實現網站滑塊拖動操作_python
- 2022-09-19 Nginx配置Tcp負載均衡的方法_nginx
- 2023-10-15 歸并排序三種常見寫法
- 2022-06-25 詳解如何基于Pyecharts繪制常見的直角坐標系圖表_python
- 2022-01-17 element日期時間選擇器提交時間格式不準確
- 2021-11-08 C++繼承模式詳解_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同步修改后的遠程分支