1.块级盒子没有指定宽度
解决:设置width
2.浮动、绝对定位、固定定位的盒子失效
解决办法:①利用css3新增的transform translate属性
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
position: fixed;
top: 0;
left: 50%;
width: 100px;
height: 50px;
background-color: red;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
3.行内元素失效
解决方法:①设置display:block;②给定要居中的行内元素的宽度。
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
span {
display: block;
width: 100px;
margin: 0 auto;
background-color: red;
}
</style>
<body>
<span>1111</span>
</body>
</html>