javascript - 当导航栏中还有另一个元素时,如何使导航栏中的 Logo 居中?

标签 javascript jquery html css navigation

在我的导航栏中有一个应该居中的 Logo ,以及一个应该向左浮动的菜单按钮图像(3 行)。我已经让菜单按钮向左浮动,但似乎无法让 Logo 位于导航栏的正中心,它总是有点偏右。

我试过将它们放在 div 中并将 div 宽度设置为 50%,然后将图像向左浮动以使其居中,但没有成功。

HTML:

<div id="resp-navbar">
    <div id="resp-nav-contents">
        <img id="exp-menu-img" src="3lines.png">
        <img id="resp-logo" src="MSLOGO.jpg">
    </div>
</div>

CSS:

#resp-navbar{
    height: 15%;
    text-align: center;
    width: 100%;
    display: inline-block;
    position: fixed;
    z-index:51;
    background-color: white;
    border-bottom: solid;
    border-width: 1px;
    border-color: #afafaf;

}   

#resp-nav-contents{
    min-width: 300px;
}

#exp-menu-img{
    height: 30%;
    position: absolute;
    left: 2%;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.4;
    cursor: pointer;
}

#resp-logo{
    height: 50%;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: inline;
 }

最佳答案

添加:左:0;右:0; margin: auto;#resp-logo

#resp-navbar{
    height: 15%;
    text-align: center;
    width: 100%;
    display: inline-block;
    position: fixed;
    z-index:51;
    background-color: white;
    border-bottom: solid;
    border-width: 1px;
    border-color: #afafaf;

}   

#resp-nav-contents{
    min-width: 300px;
}

#exp-menu-img{
    height: 30%;
    position: absolute;
    left: 2%;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.4;
    cursor: pointer;
}

#resp-logo {
    height: 50%;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: inline;
    left: 0;
    right: 0;
    margin: auto;
}
<div id="resp-navbar">
    <div id="resp-nav-contents">
        <img id="exp-menu-img" src="https://placehold.it/20x20">
        <img id="resp-logo" src="https://placehold.it/500x100">
    </div>
</div>

关于javascript - 当导航栏中还有另一个元素时,如何使导航栏中的 Logo 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344668/

相关文章:

javascript - 在 ajax 成功响应上调用 data-id

javascript - 使用 $.ajax() 问题检查文件是否存在

javascript - 使用 jquery.html() 函数将文本分配给 div 时如何在 div 中显示多行文本

html - jquery mobile data-filter css样式--如何?

c# - 正则表达式从网页中提取 Favicon url

javascript - Bootstrap-UI - 如何在不操作 URL 的情况下将 TemplateUrl 用于选项卡 View ?

javascript - 将响应选项设置为 true 时,Chartjs 不会渲染图表

php - 单击提交表单的复选框

javascript - 在 div 已经附加到一个 div 之后,将 div 附加到另一个 div

javascript - 如何取消绑定(bind)并重新绑定(bind) on() 单击动画?