2016-11-27 155 views
0

我想在我的div中垂直居中内容。用CSS垂直居中一个元素

HTML:

<div id="header"> 
     <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="logo-img-div"><img id="logo-img" src="logo.png"></div> 
     <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3" id="logo-text">Text</div> 
    </div> 

CSS:

@media only screen and (min-width: 768px) { 
     .col-sm-1 {width: 8.33%; float: left;} 
     .col-sm-2 {width: 16.66%; float: left;} 
     .col-sm-3 {width: 25%; float: left;} 
     .col-sm-4 {width: 33.33%; float: left;} 
     .col-sm-5 {width: 41.66%; float: left;} 
     .col-sm-6 {width: 50%; float: left;} 
     .col-sm-7 {width: 58.33%; float: left;} 
     .col-sm-8 {width: 66.66%; float: left;} 
     .col-sm-9 {width: 75%; float: left;} 
     .col-sm-10 {width: 83.33%; float: left;} 
     .col-sm-11 {width: 91.66%; float: left;} 
     .col-sm-12 {width: 100%; float: left;} 


    } 

* { 
    color: #2c2c2c; 
    height: 100%; 
    width: 100%; 
    margin: 0 !important; 
    font-family: 'Raleway'; 
} 

#header { 
    height: 10%; 
    background-color: #2c2c2c; 
    color: #c4c3c3; 
    font-family: 'title'; 
} 

#logo-img { 
    height: 80%; 
    width: auto; 

} 

#logo-img-div { 

} 


#logo-text { 
    color: #c4c3c3; 

} 

我想中心徽标IMG-DIV和标志文字的内容,但保留那些两个div上的标题内容的左侧。我发现了很多类似的问题,但没有任何解决方案可行。

+0

[在一个div元素的垂直取向(的可能的复制http://stackoverflow.com/questions/79461/vertical-alignment-of-elements-in-a-div ) –

回答

0

尽量去除余量(*)的元素。由于边距是所有其他元素(包括身体本身)之间的距离,因此不允许您对齐它们。或者你可以有一个利润率仅为左,右:

* { 
    color: #2c2c2c; 
    height: 100%; 
    width: 100%; 
    margin-left: 0 !important; 
    margin-right: 0 !important; 
    font-family: 'Raleway'; 
} 

如果你试图用“头”的ID垂直居中的DIV试试这个:

#header{ 
    position: relative; 
    top: 40%; 
    height:10% 
    background-color: #2c2c2c; 
    color: #c4c3c3; 
    font-family: 'title'; 
} 

试图找到高度你的元素的%,所以你可以从50%的测量中减去它们。这使得你的元素将完美居中。 在这种情况下,由于高度设置为10%,50%会转换为10%。

+0

使标题顶部:50%,因为你把高度设置为10%。 –

+0

忽略那个评论我写错了 - 顶:40%是真正的 –

+0

谢谢!如果我把它放在logo-div-img(不在标题中),它就可以工作。但它不适用于logo-text div(无论是:logo-div-img还是标题)。有关于文本div的具体内容吗?像线条高度或什么的? – user3597446

0

使用flex来对齐子项目。哦,你可能不想在的一切上设置高度和宽度为100%。

@media only screen and (min-width: 768px) { 
 
    .col-sm-1 {width: 8.33%; float: left;} 
 
    .col-sm-2 {width: 16.66%; float: left;} 
 
    .col-sm-3 {width: 25%; float: left;} 
 
    .col-sm-4 {width: 33.33%; float: left;} 
 
    .col-sm-5 {width: 41.66%; float: left;} 
 
    .col-sm-6 {width: 50%; float: left;} 
 
    .col-sm-7 {width: 58.33%; float: left;} 
 
    .col-sm-8 {width: 66.66%; float: left;} 
 
    .col-sm-9 {width: 75%; float: left;} 
 
    .col-sm-10 {width: 83.33%; float: left;} 
 
    .col-sm-11 {width: 91.66%; float: left;} 
 
    .col-sm-12 {width: 100%; float: left;} 
 
} 
 

 
* { 
 
    color: #2c2c2c; 
 
    margin: 0; 
 
    font-family: 'Raleway'; 
 
} 
 

 
html, body { 
 
    height: 100%; 
 
} 
 

 
#header { 
 
    height: 10%; 
 
    background-color: #2c2c2c; 
 
    color: #c4c3c3; 
 
    font-family: 'title'; 
 
    display: flex; 
 
    align-items: center; 
 
} 
 

 
#logo-img { 
 
    height: 80%; 
 
    width: auto; 
 
} 
 

 
#logo-text { 
 
    color: white; 
 
}
<div id="header"> 
 
    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="logo-img-div"><img id="logo-img" src="logo.png"></div> 
 
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3" id="logo-text">Text</div> 
 
</div>