在下面的代码中,我该如何水平居中固定位置的div?我已经尝试了几件事,但没有一件能在中心获得正确的结果。将固定位置的div水平居中
我想从顶部100px的div,并水平居中。
<!DOCTYPE HTML>
<html>
<head>
<title> New Document </title>
<style type="text/css">
.myDiv {
position: fixed;
top:100px;
left:auto;
padding:10px;
background-color: blue;
color: white;
}
</style>
</head>
<body>
<div class="myDiv">Something went wrong. God save the Queen!</div>
</body>
</html>
你试过左:50%;或什么? –
我做过了,但它仍然不在中心位置(我现在正在使用Firefox)。 – jmenezes
您需要在其上放一个负边距,即div的一半宽度。然后它将被集中。 –