CSS 如何将标题图像居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20149206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:07:02  来源:igfitidea点击:

How to center a header image

cssimagecenter

提问by user3022396

I have a problem. The answers to other people questions didn't solve my problem.

我有个问题。其他人问题的答案并没有解决我的问题。

It's about image in my header, here's the code:

这是关于我的标题中的图像,这是代码:

HTML

HTML

<div id="site">

    <div id="header">
        <img class="center" src="http://i.imgur.com/jfDhpP5.png"/>    
    </div>

    <div id="mainNav">
        <li><a href="#">one</a></li>
        <li><a href="#">two</a></li>
        <li><a href="#">three</a></li>
        <li><a href="#">four</a></li>
    </div>

</div>

CSS

CSS

#container{
    width: 1000px;
    margin: 1em auto;
}

#header{
    display: block;
    width: 1000px;
    margin: 0 auto;
}

body{    
    background-color: grey;
    color: black;
    font-family: sans-serif;
}

img.center{
    text-align:center;
    margin: 0 auto;
    padding:0px;
}

I also try this:

我也试试这个:

div#pictures {
  text-align: center;
}

also not working... checked in firefox and IE, the newest versions.... o.O

也无法正常工作...检查了 Firefox 和 IE,最新版本.... oO

Please help!

请帮忙!

回答by SW4

CSS for header

CSS 为 header

width:100%;
text-align:center;

CSS for your img

CSS 为您 img

display:inline-block;
margin: 0 auto;

See example here

请参阅此处的示例

That said, merely having text-align:center;on your headershould be find, as in here

也就是说,只要有text-align:center;你就header应该找到,就像在这里