CSS Div 不居中

标签 css margin

我的意图是将标题居中 <div> ,但我做不到。

header {
   height: 54px;
   margin:0px auto;
   width: 1150px;
   background: #13171B;
   position: fixed;
   left: 0;
   top: 0;
   z-index: 990;
   margin-bottom:10px;
}

请帮帮我。

最佳答案

margin: 0 auto 只能在 staticrelative 位置居中元素。由于您的 div 处于 fixed 位置,您可以尝试使用

header {
   position: fixed;
   height: 54px;
   width: 1150px;
   ...
   left: 50%;
   margin-left: -575px; /* 1150px/2 */ 
   top: 0;
}

如果您事先不知道宽度,更好的解决方案涉及css3 转换,例如

header {
   position: fixed;
   height: 54px;
   ...
   left: 50%;
   transform: translateX(-50%);
   top: 0;
}

关于CSS Div 不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154748/

相关文章:

javascript - Jquery 动画整个 div 内容

javascript - 拖放图像上传不适用于某些浏览器

html - 使用 z-index 在 Div 之上的 Div

html - Div 不以某些浏览器为中心

ios - UIViewController 嵌套 View 到屏幕边缘

html - <div> 内的 <p> 和 <img> 的格式问题

css - 元素在给定动画属性时失去过渡

CSS:div Angular 上的点

javascript - 使用按钮/jquery 更改表的 css 样式

html - 在 Wordpress 中通过 CSS 将 Logo 居中