html - 将带有背景的 div 居中,在顶部栏内,宽度为 100%

标签 html css

如何使宽度为 100% 的 Logo 居中放置在顶部栏中?我觉得自己像个彻头彻尾的白痴哈哈。 margin: 0px auto 不工作..

HTML

<div id="topbar-fullwidth">
        <div class="tb menu" id="unfoldMenu"></div>
        <div class="tb logo"></div>
</div>

CSS:

#topbar-fullwidth {
    width: 100%;
    height: 42px;
    background: #000;
    border-bottom: 2px solid #000;
    position: relative;
    overflow: hidden;
}

.tb.menu {
    width: 44px;
    height: 44px;
    float: left;
    background: url('../images/icons/unfold_menu@2x.png') no-repeat;
    background-size: 22px;
    background-position: 10px 13px;
}

.tb.logo {
    width: 140px;
    height: 44px;
    float: left;
    margin: 0px auto;
    display: block;
    background: url('../images/logo@2x.png') no-repeat;
    background-size: 125px;
    background-position: 10px;  
}

谢谢哈哈。

最佳答案

您不能在 float 元素上使用 margin:0 auto;。因此,您需要删除 Logo 上的 float:left;

FIDDLE

关于html - 将带有背景的 div 居中,在顶部栏内,宽度为 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106837/

相关文章:

HTML和CSS如何在将背景图像添加到该部分后隐藏文本

javascript - `DOCTYPE` 是否会影响在 JavaScript 中使用 `postMessage` 等 HTML5 功能?

html - 如何使用css在右下div添加图像

html - 如何修复 safari 和 chrome 上的谷歌字体像素化问题

html - float 和自动高度

html - 内容重叠

html - <p> 当文本超出宽度时,换行继续

java - Vaadin 网格表 : how to draw border for certain columns?

css - Safari 7.1 打破轮廓颜色和溢出?

javascript - 滚动数据时如何保持标题固定