使子元素伸展父元素宽度的三分之一(在旋转木马中)

时间:2018-05-28 08:49:28

标签: html css twitter-bootstrap css3 flexbox

我正在研究一种水平对齐物品的(种类)旋转木马。

每个子元素(其中将有大约十几个)应该拉伸父元素宽度的三分之一。任何时候都应该看到三个项目。

我使用过Bootstrap 4和一些自定义CSS。不需要JavaScript,轮播依赖于手动滚动来显示其项目。

如您所见,为列表项设置width: 33.333%不适用于非常小或非常大的分辨率。我还希望这些项目能够以超小分辨率拉伸全宽

什么是可行的,特定于弹性盒的替代方案?

html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}

.page-wrapper {
  min-height: 100%;
}

.hero {
  height: 100vh;
  background: #212121;
  justify-content: center;
  align-items: center;
}

#carousel {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow-x: scroll;
  justify-content: left;
  align-items: center;
}

#carousel li {
  padding: 0 0 70px 0;
  width: 33.333%;
}

#carousel li a {
  text-decoration: none;
  color: #fff;
}

#carousel li img {
  display: block;
  width: 100%;
}

#carousel li .caption {
  padding-top: 20px;
}

#carousel li h2 {
  font-size: 20px;
  line-height: 1;
  margin: 0;
  padding: 0;
}

#carousel li p {
  font-size: 10px;
  padding: 0;
  margin: 5px 0 0 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="page-wrapper">
  <div class="hero d-flex">
    <ul id="carousel" class="d-flex">
      <li class="col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/people" alt="">
          <div class="caption">
            <h2>Lorem ipsum dolor</h2>
            <p>A true story</p>
          </div>
        </a>
      </li>
      <li class="col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/nature" alt="">
          <div class="caption">
            <h2>Lorem</h2>
            <p>Lorem ipsum dolor sit amet, consectetur</p>
          </div>
        </a>
      </li>
      <li class="col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/arch" alt="">
          <div class="caption">
            <h2>My neighborhood</h2>
            <p>Lorm ipsum dolor</p>
          </div>
        </a>
      </li>
      <li class="col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/animals" alt="">
          <div class="caption">
            <h2>Into the wild</h2>
            <p>Lorem ipsum dolor sit amet, consectetur</p>
          </div>
        </a>
      </li>
    </ul>
  </div>
</div>

3 个答案:

答案 0 :(得分:1)

添加col-4类而不是col-md-4。对于屏幕的所有分辨率,这将使div的宽度保持在33.3%

html, body {
  padding: 0;
  margin: 0;
  height: 100%;
}
.page-wrapper {
  min-height: 100%;
}
.hero {
  height: 100vh;
  background: #212121;
  justify-content: center;
  align-items: center;
}
#carousel {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow-x: scroll;
  justify-content: left;
  align-items: center;
}
#carousel li {
  padding: 0 0 70px 0;
}
#carousel li a {
  text-decoration: none;
  color: #fff;
}
#carousel li img {
  display: block;
  width: 100%;
}
#carousel li .caption {
  padding-top: 20px;
}
#carousel li h2 {
  font-size: 20px;
  line-height: 1;
  margin: 0;
  padding: 0;
}
#carousel li p {
  font-size: 10px;
  padding: 0;
  margin: 5px 0 0 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-wrapper">
  <div class="hero d-flex">
    <ul id="carousel" class="d-flex">
      <li class="col-4">
        <a href="#"><img src="https://placeimg.com/300/200/people" alt="">
        <div class="caption">
          <h2>Lorem ipsum dolor</h2>
          <p>A true story</p>
        </div>
        </a>
      </li>
      <li class="col-4">
        <a href="#"><img src="https://placeimg.com/300/200/nature" alt="">
        <div class="caption">
          <h2>Lorem</h2>
          <p>Lorem ipsum dolor sit amet, consectetur</p>
        </div>
        </a>
      </li>
      <li class="col-4">
        <a href="#"><img src="https://placeimg.com/300/200/arch" alt="">
        <div class="caption">
          <h2>My neighborhood</h2>
          <p>Lorm ipsum dolor</p>
        </div>
        </a>
      </li>
      <li class="col-4">
        <a href="#"><img src="https://placeimg.com/300/200/animals" alt="">
        <div class="caption">
          <h2>Into the wild</h2>
          <p>Lorem ipsum dolor sit amet, consectetur</p>
        </div>
        </a>
      </li>
    </ul>
  </div>
</div>

评论中的问题

编辑(未在工作答案中更新)

尝试使用此HTML标记,它应该按照您的预期工作。

<div class="hero container-fluid">
    <ul id="carousel" class="row">
      <li class="col-12 col-sm-6 col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/people" alt="">
        <div class="caption">
          <h2>Lorem ipsum dolor</h2>
          <p>A true story</p>
        </div>
        </a>
      </li>
      <li class="col-12 col-sm-6 col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/nature" alt="">
        <div class="caption">
          <h2>Lorem</h2>
          <p>Lorem ipsum dolor sit amet, consectetur</p>
        </div>
        </a>
      </li>
      <li class="col-12 col-sm-6 col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/arch" alt="">
        <div class="caption">
          <h2>My neighborhood</h2>
          <p>Lorm ipsum dolor</p>
        </div>
        </a>
      </li>
      <li class="col-12 col-sm-6 col-md-4">
        <a href="#"><img src="https://placeimg.com/300/200/animals" alt="">
        <div class="caption">
          <h2>Into the wild</h2>
          <p>Lorem ipsum dolor sit amet, consectetur</p>
        </div>
        </a>
      </li>
    </ul>
  </div>

答案 1 :(得分:1)

弹性容器的初始设置为flex-shrink: 1。这意味着允许弹性项目缩小,因此它们不会溢出容器。这就是您在布局中看到的内容。

您需要停用flex-shrink

而不是:

#carousel li {
    padding: 0 0 70px 0;
    width: 33.333%;
}

试试这个:

#carousel li {
    padding: 0 0 70px 0;
    width: 33.333%;
    flex-shrink: 0;
}

或者,甚至更好(为了提高效率),试试这个:

#carousel li {
    padding: 0 0 70px 0;
    flex: 0 0 33.333%; /* flex-grow, flex-shrink, flex-basis */
}

答案 2 :(得分:0)

我已经能够获得完全所需的效果,感谢社区中的2位成员:

html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}
.page-wrapper {
  min-height: 100%;
}
.hero {
  height: 100vh;
  background: #212121;
  justify-content: center;
  align-items: center;
}
#carousel {
  display: flex;
  list-style-type: none;
  overflow-x: scroll;
  margin: 0;
  padding: 0;
  justify-content: left;
  align-items: center;
}
#carousel li {
  padding: 0 0 70px 0;
  flex-shrink: 0;
}
#carousel a {
  text-decoration: none;
  color: #fff;
}
#carousel img {
  display: block;
  width: 100%;
}
#carousel .caption {
  padding: 20px 20px 0 20px;
}
#carousel h2 {
  font-size: 20px;
  line-height: 1;
  margin: 0;
  padding: 0;
}
#carousel p {
  font-size: 10px;
  padding: 0;
  margin: 5px 0 0 0;
}
<link href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-wrapper">
  <div class="hero d-flex">
      <ul id="carousel">
        <li class="col-xs-12 col-sm-6 col-md-4">
          <a href="#">
        <img src="https://placeimg.com/600/400/people" alt="">
        <div class="caption">
          <h2>Lorem</h2>
          <p>A true story</p>
        </div>
        </a>
        </li>
        <li class="col-xs-12 col-sm-6 col-md-4">
          <a href="#">
        <img src="https://placeimg.com/600/400/nature" alt="">
        <div class="caption">
          <h2>Lorem ipsum</h2>
          <p>Lorem ipsum. Dolor sit amet.</p>
        </div>
        </a>
        </li>
        <li class="col-xs-12 col-sm-6 col-md-4">
          <a href="#">
        <img src="https://placeimg.com/600/400/arch" alt="">
        <div class="caption">
          <h2>Lorem</h2>
          <p>Lorem ipsum. Dolor sit amet</p>
        </div>
        </a>
        </li>
        <li class="col-xs-12 col-sm-6 col-md-4">
          <a href="#">
        <img src="https://placeimg.com/600/400/animals" alt="">
        <div class="caption">
          <h2>Into the wild</h2>
          <p>Lorem ipsum dolor sit amet, consectetur</p>
        </div>
        </a>
        </li>
      </ul>
  </div>
</div>