目次
行内元素:
(1)親要素にtext-align:centerを設定します
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
height: 100px;
background-color: deepskyblue;
text-align: center;
}
</style>
</head>
<body>
<div class="box1">行内元素水平居中</div>
</body>
</html>
ブロックレベルの要素:
(1)マージンの設定:0自動;(固定幅が必要)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: deepskyblue;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">设置 margin:0 auto;(需有固定宽度)</div>
</body>
</html>
(2)display:inline-blockを使用してインライン要素に変換し、親要素にtext-align:centerを設定します。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
height: 300px;
background-color: deepskyblue;
text-align: center;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
display: inline-block;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
用 display:inline-block 转成行内元素,在父元素设置 text-align:center
</div>
</div>
</body>
</html>
(3)絶対測位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: deepskyblue;
position: absolute;
left: 50%;
transform: translateX(-50%); /* 移动元素本身50% */
}
</style>
</head>
<body>
<div class="box1">绝对定位+translateX</div>
</body>
</html>
(3)フレックスレイアウトで実現
方法1:表示:flex;正当化-コンテンツ:中央
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 500px;
height: 200px;
background-color: deepskyblue;
display: flex;
justify-content: center
}
</style>
</head>
<body>
<div class="box1">内容水平居中</div>
</body>
</html>
方法2:親要素の表示:flex; flex-direction:column;子要素:align-self:center;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 800px;
height: 300px;
background-color: deepskyblue;
display: flex;
flex-direction: column;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
align-self: center;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">块元素水平居中</div>
</div>
</body>
</html>
方法3:親要素の表示:flex;子要素:margin:0 auto;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 800px;
height: 300px;
background-color: deepskyblue;
display: flex;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">块元素水平居中</div>
</div>
</body>
</html>