简体   繁体   English

如何在div中居中放置图像

[英]How to center an image within a div

I'm attempting to center the logo within my header. 我试图将徽标放在标题的中心。 I tried the display:block and margin:auto within my #site-title class, but it isn't working. 我在#site-title类中尝试了display:blockmargin:auto ,但是它不起作用。

The link to the code is: http://pastebin.com/iNvhTSBL And the CSS is: http://pastebin.com/f5mPTjuU 该代码的链接是: http : //pastebin.com/iNvhTSBL而CSS是: http : //pastebin.com/f5mPTjuU

I'm not sure what to do...maybe I'm not doing the CSS correctly? 我不确定该怎么做...也许我没有正确执行CSS?

Any help would be appreciated! 任何帮助,将不胜感激! The CSS was from a wordpress theme, just letting you all know. CSS来自wordpress主题,只是让大家知道。

Thank you! 谢谢!

Next time, post only the relevant code bits in your question. 下次,仅在您的问题中张贴相关的代码位。 That makes it MUCH more likely people will bother to answer. 这使得人们更愿意回答。

If this is the code you are referring to: 如果这是您指的代码:

<div id="header">
            <div id="site-title">
                    <a href="http://www.panduzee.com/wordpress"><img src="http://i.imgur.com/ysxPP.jpg"></a>
            </div>
            <!-- other bits stripped for brevity -->
            <div class="clear"></div>
    </div>

and the css is this: CSS是这样的:

#header {
    border-bottom: 1px solid #dedfe0; 
    padding-bottom: 50px;
}
#site-title { 
    float: left; 
    width: 100%; 
    padding-right: 40px; 
    overflow: hidden; 
    line-height: 27px; 
    font-size: 23px; 
    display:block; 
    margin: auto;
}  
#site-title a {
    color: #333; 
    font-weight: bold; 
    text-decoration: none; 
}

Then you need to revise the css as follows: 然后,您需要修改css,如下所示:
1. remove the float: left from the site-title. 1.删​​除网站标题float: leftfloat: left Unecessary. 没必要
2. Note that your padding-right of 40px is going to cause problems with your width: 100% 2.请注意,您padding-right of 40px会导致width: 100%问题width: 100%
3. Add text-align: center; 3.添加text-align: center; to the site-title. 网站标题。
4. Add margin: 0 auto; 4.增加margin: 0 auto; to your site-title a 给您的网站标题

The following code will do what you want: 以下代码将执行您想要的操作:

#header {
    border-bottom: 1px solid #dedfe0; 
    padding-bottom: 50px;
}
#site-title { 
    width: auto; 
    padding-right: 40px; 
    overflow: hidden; 
    line-height: 27px; 
    font-size: 23px;  
    text-align: center;
}  
#site-title a {
    color: #333; 
    font-weight: bold; 
    text-decoration: none; 
    margin: 0 auto;
}

将此添加到#site-title

text-align: center;

This should work: 这应该工作:

#header {
width: 500px;
margin: 0 auto;
}
</style>
<div id="header">
<!-- div content here -->
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM