2016-04-05 85 views
0

無法讓我的圖像居中,而與我的圖標保持在同一水平面上。如何將元素居中在div中,同時保持與另一個元素處於同一水平面上?

HTML

<div class="smalltop w3-top"> 
    <span class="w3-opennav w3-xlarge" onclick="w3_open()"><i id="menui" class="material-icons w3-xxlarge">menu</i></span> 
    <a href="#"><img id="smalllogo" src="img/navlogo-invert.jpg"></a> 
</div> 

CSS

#smalllogo { 
    max-height: 50px; 
    width: auto; 
    display: block; 
    margin: 0 auto; 
    border: 5px solid; 
    margin-top: 5px; 
} 

#menui { 
    left: 0; 
} 

.smalltop { 
    background-color: #FFF; 
    list-style-type: none; 
    margin: 0 auto; 
} 

https://jsfiddle.net/pzLbruhx/

回答

2

相對定位容器和絕對定位添加到圖標

#menui { 
    left: 0; 
    position:absolute; 
} 
.smalltop { 
    background-color: #FFF; 
    list-style-type: none; 
    margin: 0 auto; 
    position:relative; 
} 

jsFiddle example

0

只要給物業float:left#menui,並觀察結果

0

使用display:table\table-row\table-cellvertical-align上圖標

#smalllogo { 
    max-height: 50px; 
    width: auto; 
    display: table-cell; 
    margin: 0 auto; 
    border: 5px solid black; 
    margin-top: 5px; 
} 

.w3-opennav { 
    display: table-cell; 
    vertical-align: middle; 
} 

.smalltop { 
    background-color: #FFF; 
    list-style-type: none; 
    margin: 0 auto; 
    display: table-row; 
} 

Fiddle

相關問題