網站首頁 編程語言 正文
方法一:兒子絕對定位或相對定位,這樣兒子脫離標準流,父親有固定寬高,用兒子的margin去擠父親
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
position: absolute;
margin: 200px 150px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
注意:如果兒子沒有絕對定位或相對定位,且父親沒有border,那么兒子的margin實際上踹的是“流”,踹的是這“行”。如果用margin-top,父親整體也掉下來了。如下:
方法二、如果給父親加一個border,就可以父親,兒子都不定位,用兒子的margin去擠父親,實現兒子居中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
border: 1px solid white;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
margin: 200px 150px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
方法三、子絕父相(兒子絕對定位,父親相對定位),定位到大盒子的寬高一半的位置,再上下各margin負的自己寬高的一半
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
position: relative;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
方法四、子絕父相,定位到大盒子的寬高一半的位置,再用transform: translate向左向上移動自己寬高的一半
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
position: relative;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
position: absolute;
top: 50%;
left: 50%;
/*用transform向左(上)平移它自己寬度(高度)的50%,也就達到居中效果了*/
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
方法五:絕對布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
position: relative;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
方法六:使用table-cell
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
display: inline-block;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
display: table-cell;和?vertical-align: middle;兩句實現垂直居中
text-align: center;和display: inline-block;兩句實現水平居中
方法七、用margin: 0 auto;代替text-align: center;和display: inline-block;這兩句
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
display: table-cell;
vertical-align: middle;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
?
方法八:利用行高和文本居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
text-align: center;
line-height: 600px;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
display: inline-block;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
方法九:使用彈性布局flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.father{
width: 600px;
height: 600px;
background-color: orangered;
display: flex;
/*水平居中*/
justify-content: center;
/*垂直居中*/
align-items: center;
}
.son{
width: 300px;
height: 200px;
background-color: lawngreen;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
上面幾種方法結果都是如下所示:
補充知識點:
絕對定位:
一個絕對定位的元素,如果父輩元素中出現了也定位了的元素,那么將以父輩這個元素,為參考點。
工程上,“子絕父相”有意義,父親沒有脫標,兒子脫標在父親的范圍里面移動。
絕對定位之后,所有標準流的規則,都不適用了。所以margin:0 auto;失效。
display:inline和display:inline-block,會使margin:0 auto;失效。
固定寬度的盒子才能使用margin:0 auto;居中
原文鏈接:https://blog.csdn.net/sleepwalker_1992/article/details/80656170
相關推薦
- 2022-10-12 python繪制發散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖_python
- 2022-03-24 C語言指針的圖文詳解_C 語言
- 2022-11-07 react-native?實現漸變色背景過程_React
- 2023-02-15 PyQt5頁面跳轉問題及解決方式_python
- 2022-08-13 微信公眾號--根據用戶的opneId發送模版消息
- 2022-09-30 Docker容器搭建本地私有倉庫詳情_docker
- 2023-05-21 使用react完成點擊返回頂部操作_React
- 2022-04-04 git: master (pre-receive hook declined)
- 最近更新
-
- 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同步修改后的遠程分支