将容器放在全宽容器内?

时间:2017-02-09 17:39:24

标签: html css flexbox

我试图将相对于100%宽度的容器的最大宽度为1100像素的容器居中,但是我无法做到这一点。

<div class="container-full">
    <div class="container">
        <nav>
            <h1>Name</h1>
            <ul>
                 <li>Home</li>
                 <li>About</li>
                 <li>Contact</li>
            </ul>
        </nav>
    </div>
</div>

CSS:

.container-full {
    width:100%;
    background-color:blue;
}

.container {
    width:1100px;
    background-color:white;
}

nav {
    display:flex;
    justify-content:space-between;
    align-items:center;
}

ul {
    list-style-type:none;
    padding:0;
    display:flex;
}

我认为它与导航器是灵活盒有关,但我不确定如何解决这个问题。

4 个答案:

答案 0 :(得分:1)

水平居中定义宽度的最简单方法是使用 margin: auto;

margin-left: auto; margin-right: auto;
.container-full {
  width: 100%;
  background-color: blue;
}

.container {
  width: 1100px;
  background-color: white;
  margin: auto;
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

答案 1 :(得分:0)

为容器添加边距:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="row " style=" background-color:#F2F2F2;">
  <div class="col-xs-12">
    <div class="container ">
      <div class="row  footer-border " style="margin-top: 6em; border-right: 2px solid #ccc;">
        <div class="col-md-4 col-xs-12 ">
          <button class="btn btn-success button-def form-text btn-font" id="button" type="submit" name="go" style="margin-top: 1.2em; height: 50px; ">Get started with MMN free</button>

        </div>
        <div class="col-md-8 col-xs-12  ">
          <p class="slider-heading " style="margin-top: 1em;">Discover what technology can do for your NGO.</p>
        </div>
      </div>
      <div class="row" style="margin-top: 5em; ">
        <div class="col-xs-12 footer1">
          <p>ManageMyNGO</p>
        </div>
      </div>
      <div class="row">
        <div class="col-xs-12 footer2">
          <p>a ColoredCow product</p>
        </div>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

解决方案是将margin: 0 auto;添加到您的.container

请参阅工作小提琴here

enter image description here

.container-full {
    width:100%;
    background: blue;
}

.container {
    width:1100px;
    background: red;
    margin: 0 auto;
}

nav {
    display:flex;
    justify-content:space-between;
    align-items:center;
}

ul {
    list-style-type:none;
    padding:0;
    display:flex;
}
<div class="container-full">
    <div class="container">
        <nav>
            <h1>Name</h1>
            <ul>
                 <li>Home</li>
                 <li>About</li>
                 <li>Contact</li>
            </ul>
        </nav>
    </div>
</div>

答案 3 :(得分:0)

首先,我不确定你想做什么。如果您尝试将容器的内容相对于容器已满,那么下面的内容可能会有效。

如果你想集中任何东西,那么你可能想要使用以下mixin。

exaple codepen链接:http://codepen.io/hellouniverse/pen/PWyVbd

@mixin  align-items () {
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-flex-align: center;
  -webkit-align-items: center;
  -moz-align-items: center;
  -ms-align-items: center;
  align-items: center;
}

@mixin flexbox() {
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;
}

@mixin justify-content ($val) {
  -webkit-justify-content: $val;
  -moz-justify-content: $val;
  -ms-justify-content: $val;
  justify-content: $val;
}


%aligner,
.aligner {
  @include align-items();
  @include flexbox();
  @include justify-content(center);

  &-item {
    max-width: 50%;
    &--top {
      -webkit-align-content: flex-start;
      -moz-align-content: flex-start;
      -ms-align-content: flex-start;
      -ms-flex-line-pack: start;
      align-content: flex-start;
    }

    &--bottom {
      -webkit-align-content: flex-end;
      -moz-align-content: flex-end;
      -ms-align-content: flex-end;
      -ms-flex-line-pack: end;
      align-content: flex-end;
    }
  }
}

在添加上述scss mixin后,您的解决方案将会变为

.container-full {
  width: 100%;
  background-color: blue;
}

.container {
  width: 1100px;
  background-color: white;
}

<div class="container-full aligner">
    <div class="container aligner">
        <nav>
            <h1>Name</h1>
            <ul>
                 <li>Home</li>
                 <li>About</li>
                 <li>Contact</li>
            </ul>
        </nav>
    </div>
</div>