網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
如何讓一個(gè)有寬高的盒子垂直居中
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>
這種方法相比于第一種是只對(duì)上邊距有明確的控制,而對(duì)左右用auto,與第一種幾乎相同。
3.絕對(duì)定位+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>
結(jié)果同圖一
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>
結(jié)果同圖一
這幾總方法都可以用,但是有優(yōu)點(diǎn)也有缺陷,本人一般使用方法2 3多一些。
原文鏈接:https://blog.csdn.net/bjt_yy/article/details/118892887
相關(guān)推薦
- 2022-12-29 一文帶你了解Go語(yǔ)言中方法的調(diào)用_Golang
- 2022-09-26 快速搭建 ElasticSearch學(xué)習(xí)環(huán)境
- 2022-07-07 Pytorch卷積神經(jīng)網(wǎng)絡(luò)遷移學(xué)習(xí)的目標(biāo)及好處_python
- 2022-01-11 slice、substring、substr比較
- 2022-12-09 C++印刷模板使用方法詳解_C 語(yǔ)言
- 2023-03-03 Fragment通過FragmentManager實(shí)現(xiàn)通信功能詳細(xì)講解_Android
- 2022-12-24 darknet框架中YOLOv3對(duì)數(shù)據(jù)集進(jìn)行訓(xùn)練和預(yù)測(cè)詳解_python
- 2022-11-10 C++實(shí)現(xiàn)拷貝構(gòu)造函數(shù)的方法詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支