方法一text-align:针对于文字
<!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>
.box {
background-color: green;
height: 300px;
width: 600px;
text-align: center;
}
P {
color: red;
}
</style>
</head>
<body>
<div class="box">
<p>1111</p>
</div>
</body>
</html>
方法二单个块状元素解决方案margin左右居中
<!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>
.box {
background-color: green;
height:300px;
width: 600px;
text-align:center;
}
.box2{
background-color: red;
width: 20px;
height: 20px;
margin:0 auto;
}
</style>
</head>
<body>
<div class="box">
<div class="box2"></div>
</div>
</body>
</html>
方法三 多个块状元素 父元素设为text-align子元素设置为行内块元素:display:inline-block;
<!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>
.box {
background-color: green;
height: 300px;
width: 600px;
text-align: center;
}
.box2, .box3 {
background-color: red;
width: 20px;
height: 20px;
display: inline-block;
}
</style>
</head>
<body>
<div class="box">
<div class="box2"></div>
<div class="box3"></div>
</div>
</body>
</html>
方法四多个块状元素 使用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>
.box {
background-color: green;
height: 300px;
width: 600px;
display: flex;
justify-content: center;
}
.box2, .box3, .box4 {
background-color: red;
width: 20px;
height: 20px;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="box">
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</div>
</body>
</html>
方法五不确定宽度设置:使用relative进行定位
<!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>
.box {
float: left;
position: relative;
left: 50%;
background: #2ac7d2;
}
.box ul {
position: relative;
left: -50%;
background: #7c4a03;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li>111</li>
<li>111</li>
<li>111</li>
</ul>
</div>
</body>
</html>
https://github.com/7117/frontendCollection/tree/master/CSS
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。