gpt4 book ai didi

html - 关于使 div 中心对齐的基本 css

转载 作者:行者123 更新时间:2023-11-28 11:21:17 24 4
gpt4 key购买 nike

我有一个关于使 div 中心对齐的非常快速的问题:

我有一个容器

#container {    
width: 100%;
margin: 0 auto;
max-width: 1300px !important;
background: #ccc;
}

还有一个 Header 里面

#header{
font-family: "Helvetica Neue";
font-size: 13px;
height: 125px;
width: 100%;
max-width: 1300px;
position: fixed;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
background: rgba(255, 255, 255, 0.9);
z-index: 1000;
}

和标题中的一个类

#header .inside{
max-width: 80em;
margin: 0 auto;
position: relative;
height: 90px;
display: block;
}

标题在响应模式下很好,但 inside 在其他分辨率下不会保持居中对齐。我应该向文件添加什么 CSS 以使其在所有分辨率下居中对齐?

谢谢

最佳答案

fixed 中居中元素与 margin:auto; 的位置不连贯,因为它不再处于流程中。

你可以使用一个固定的容器,全宽并在其内部流动,就像在 <body> 中一样使用带有 width 的容器和 margin:auto;

http://codepen.io/anon/pen/cvids

<div id="container">
<header id="header">
<div class="maxwidthmarginauto">
<div class="inside">
inside header
</div>
</div>
</header>
</div>
body {
margin:0;
}
#container {
width: 100%;
margin: 0 auto;
max-width: 1300px;
background: #ccc;
/* demo purpose */
min-height:1500px;
background:linear-gradient(to bottom, gray,lightgray);
}
header{
font-family: "Helvetica Neue";
font-size: 13px;
height: 125px;
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
div.maxwidthmarginauto {
max-width: 1300px; margin: 0 auto;
background: rgba(255, 255, 255, 0.9);

}
header .inside{
max-width: 80em;
margin: 0 auto;
position: relative;
height: 90px;
background:rgba(0,50,100,0.7);

}

/* see them all */
body * {box-shadow:inset 0 0 0 2px red;border-radius:15px;}
body *:before {
content:attr(class)' 'attr(id);
position:absolute;
background:yellow;
z-index:2;
margin:2.5em;
}

关于html - 关于使 div 中心对齐的基本 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com