html - float 图像 CSS HTML 后水平居中文本

标签 html css-float css

我无法将文本相对于我的网页宽度居中,紧挨着设置为 float 到页面左侧的图像。

这是 HTML 代码:

<div class="header_img">
    <a href="index.php" class="head"><img border="0" src="images/logo.png" alt="Home" width="200" height="35"></a>
</div>
<div class="header">
    <a href="index.php" class="body">HOME</a><a href="contact.php" class="body">CONTACT</a>
</div>

这是 CSS 代码:

.header {
    background:#000000;
    font-size: 150%;
    color: #666666;
    font-family: ProximaNovaLight, Proxima Nova Light;
    clear: both;
    text-align: center; 
    display:inline;
}

.header_img {
    float: left;
}

我目前看到图像如我所愿地向左浮动,但文本(在“header”类中)也向左浮动,紧挨着图像。 任何帮助将不胜感激!

最佳答案

将此添加到您的 CSS:

.header { width: 40%; margin: 0 auto; }

并删除:

clear:both
display:inline

会给你:

.header_img {
    float: left;
}
.header {
    background:#000000;
    font-size: 150%;
    color: #666666;
    font-family: ProximaNovaLight, Proxima Nova Light;
    text-align: center;
    margin: 0 auto;
    width: 40%;
}
<div class="header_img">
    <a href="index.php" class="head">
        <img border="0" src="images/logo.png" alt="Home" width="200" height="35"/>
    </a>
</div>
<div class="header">
    <a href="index.php" class="body">HOME</a><a href="contact.php" class="body">CONTACT</a>
</div>

jsFiddle

关于html - float 图像 CSS HTML 后水平居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26017807/

相关文章:

css - IE 无法识别 "float:left"

html - 简单的两列布局

css - float 列表问题 : Removing Indentation

javascript - 如何缩小 liquid 模板生成器生成的 javascript 文件

javascript - 删除基于子div的div类

javascript - 将按钮定位在背景图像之上

javascript - HTML5 Canvas globalCompositeOperation 更轻,同时在源码之上

javascript - 防止浏览器在加载文档时部分呈现页面

css -::在浏览器中选择背景颜色和颜色渲染

css - 转换 Firefox 中的错误?