【问题标题】:How to center an image within a div如何在 div 中使图像居中
【发布时间】:2012-05-06 18:27:29
【问题描述】:
【问题讨论】:
标签:
css
wordpress
html
header
center
【解决方案1】:
这应该有效:
#header {
width: 500px;
margin: 0 auto;
}
</style>
<div id="header">
<!-- div content here -->
</div>
【解决方案2】:
下一次,只发布您问题中的相关代码位。这使得人们更有可能会费心回答。
如果这是您所指的代码:
<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>
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;
}
那么你需要修改css如下:
1.从站点标题中删除float: left。没必要。
2. 请注意,您的padding-right of 40px 会导致您的width: 100% 出现问题
3. 将text-align: center; 添加到站点标题。
4. 将margin: 0 auto; 添加到您的站点标题a
下面的代码会做你想做的事:
#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;
}
【解决方案3】:
将此添加到#site-title
text-align: center;