垂直以CSS为中心

  • 本文关键字:为中心 CSS 垂直 html css
  • 更新时间 :
  • 英文 :


我试图在我的Div中垂直中心内容。

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和徽标文本的内容,但将这两个divs放在标头内容的左侧。我发现了许多类似的问题,但是解决方案都没有。

您将属性应用于所有选择器。http://www.w3schools.com/cssref/css_selectors.asp由于您的边距为0,因此您无法对齐您的DIV元素。

通常,您可以使用左右左右,移动许多PX或EM的...

尝试删除(*)元素的边距。这不允许您对齐它们,因为边距是包括身体本身在内的所有其他元素之间的距离。或者,您只能为左右右图:

* {
  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%。

使用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>

最新更新