CSS:任何使标题水平居中的方法,标题有;位置:固定“;?

CSS:任何使标题水平居中的方法,标题有;位置:固定“;?,css,header,center,fixed,Css,Header,Center,Fixed,我用两个div容器创建了一个头,一个#头容器和一个#头: #header_container { background:#eeeeee; border:0px hidden; height:100px; position:fixed; width:1000px; top:0; margin: auto; display: block; } #header { line-height:60px; margin:0 auto; display:block; width:940

我用两个div容器创建了一个头,一个
#头容器
和一个
#头

#header_container { 
background:#eeeeee; 
border:0px hidden; 
height:100px; 
position:fixed; 
width:1000px; 
top:0; 
margin: auto; 
display: block;
}
#header { 
line-height:60px; 
margin:0 auto; 
display:block; 
width:940px; 
text-align:center;
}
当然,我无法同时拥有“固定”和“中心”,因此如何在保持“固定”属性的同时使标题居中

谢谢

如果将“左:50%;左边距:-500px;”添加到#标题容器中,它将居中。别忘了把页边空白放在页边空白后面:auto

因此,您的代码将是:

#header_container{
background: #eeeeee;
border: 0px hidden; 
height: 100px;
position: fixed;
width: 1000px;
top:0;
left: 50%;
margin-left: -500px;
display:block;
}

#header{
line-height: 60px;
margin: 0 auto;
display: block;
width: 940px;
text-align:center;
}
这是一把小提琴:

只用

#header {    margin: 0 30px;}

这应该可以使一个具有固定位置的div水平居中,其宽度不是100%:

position:fixed;
background-color:grey;  /* optional */
height:100px;           /* optional but useful for most of you, choose value you want*/
width:1280px;           /* optional but useful for most of you, choose value you want*/
top:0px;                /* optional but useful for most of you, choose value you want*/
margin-left:auto;
margin-right:auto;
left:0;
right:0;

更多信息请点击此处:

这可能会有所帮助。您只需要在代码中添加左边距标记



这是一次性解决这个问题的100%方法

给你:

首先,在“html”

然后在标题中,添加类“容器流体”

然后在“样式”部分中,键入:

header {
  position:fixed;
  top:0;
  left:0;
  right:0;
}
//boom!!! problem solved.

没有
position:center
,允许的值是:static | relative | absolute | fixed | inherit请更正您的问题。当我将“fixed”替换为“center”时,标题居中,但实际上这只是因为我去掉了“fixed”,谢谢您指出这一点!使用
position:center
时,浏览器忽略了该属性,并使用了默认的
position:static
,并且由于
margin:auto
的原因,
div
相对于根元素居中。Yep。对于CSS和HTML来说,这是一件棘手的事情,任何编程,真的-可能最好一次只更改一件事。你的意思是要将header\u容器居中吗?还是中锋?谢谢你,我想这样就行了。。。实际上,我在理解这一切的过程中遇到了一些问题,我从这个网站的另一篇文章中得到了CSS。。。。你会把东西放在哪里?例如:?或者在本节中不应使用DIV。HTML文件的这一部分用于告诉浏览器在哪里可以找到脚本、样式表以及诸如标题和元标记之类的内容。我建议使用一个单独的css文件,你可以将它包含在你的头部部分。在里面你可以添加这个部分。明白了,这可能是问题所在,它的格式有点滑稽,就像它与身体分开一样(显然,因为它…有一个愚蠢的时刻)。但没有做到这一点,不幸的是,标题太偏右了。但我认为你的想法是对的,所以如果我继续调整,我会做对的。我们解决了“固定”的问题,同时使其始终居中,谢谢!在我的屏幕上,容器和标题都居中。你能分享你现在的CSS和HTML吗?这听起来像是一个特定于引导CSS的答案,但我在问题中没有使用引导。。。当然,2013年的任何CSS内容都应该被忽略:P从那时起,一切都发生了巨大的变化。。。
header {
  position:fixed;
  top:0;
  left:0;
  right:0;
}
//boom!!! problem solved.