将两个div中的内容垂直对齐,彼此相邻

时间:2015-11-13 11:13:09

标签: html css

我有两个div彼此相邻 左侧div包含徽标,右侧div包含一行链接(菜单)

我想将右侧div中的菜单垂直对齐到左侧div中的徽标 这是代码:

<div id="header">
    <div class="logo">
        image.jpg
    </div>
    <div class="navigation">
    link 1  | link 2 | link 3
    </div>
</div>

css:

#header{
padding: 3% 0 2% 0;
width: 100%;
display:table;
}

.logo{
display:inline-block;
max-width:19%;  
text-align:left;
}

.navigation{
display:inline-block;
text-align:right;
width:81%;
}

4 个答案:

答案 0 :(得分:1)

希望这是你想要的:

&#13;
&#13;
#header {
  font-family: sans-serif;
  padding: 3% 0 2% 0;
  width: 100%;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
      -ms-flex-align: center;
          align-items: center;
}

.logo {
  display: inline-block;
  max-width: 19%;
  text-align: left;
}

.navigation {
  display: inline-block;
  text-align: right;
  width: 81%;
}
&#13;
<div id="header">
    <div class="logo">
        image.jpg
    </div>
    <div class="navigation">
    link 1  | link 2 | link 3
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

Flexbox应该做到这一点。请参阅此fiddle

.header {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.logo {
    flex: 0 0 20%;
}

.navigation {
    flex: 0 0 80%;
}

答案 2 :(得分:0)

用表格单元替换内联块。它会起作用。

答案 3 :(得分:0)

作为CSS flexbox解决方案的替代方案......

https://jsfiddle.net/bLmcpc15/

<强> HTML:

<div id="header">
    <div class="logo">
        <img id="logoimg" src="http://design.ubuntu.com/wp-content/uploads/ubuntu-logo32.png">
    </div>
    <div id="navigation">
    link 1  | link 2 | link 3
    </div>
</div>

<强> JS:

$(document).ready(function(){
    var imgHeight = $("#logoimg").height();
    var navHeight = $("#navigation").height();
    var topMargin = (imgHeight-navHeight)/2;
    $("#navigation").css({"margin-top":topMargin});
});