如何阻止列表项突破新行

时间:2017-07-03 09:36:58

标签: html css html-lists

我希望列表项中的图像与浏览器窗口调整大小保持在同一行,而不是打破新行。这可能吗?我尝试使用nowrap,因为我看到其他人提到它但它对我不起作用。

*{
  padding: 0;
  margin: 0;
}

body{
  width: 100%;
  margin: 0;
  padding: 0;
}

.cont{
  position: relative;
  font-size: 0;/*removes white space*/
  margin: 60px auto;
  padding: 0;
}

.carousel{
  position: relative;
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  width: 100%;
  max-width: 2400px;
  height: 100%;
  max-height: 350px;
}

.carousel li{
  float: left;
}

.carousel li img{
  width: 100%;
  height: auto;
}

#next{
  position: absolute;
  top: 45%;
  right: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  font-size: 0;
  z-index: 1;
}

#prev{
  position: absolute;
  top: 45%;
  left: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  z-index: 1;
}

.img_cont{
  width: 100%;
  max-width: 600px;
  height: 100%;
  max-height: 300px;
  padding: 150px 0;
}

.active{
  width: 100%;
  max-width: 1200px;
  height: 100%;
  max-height: 600px;
  padding: 0;
}
<div class="cont">
  <div id="next">
  </div> 
  <div id="prev">
  </div> 
  <ul class="carousel">
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
    </li>
    <li class="img_cont active">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
    </li>
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
    </li>
  </ul>
</div>

3 个答案:

答案 0 :(得分:1)

制作liinline-block并将overflow-x: visible; white-space: nowrap;添加到.carousel,以便将所有li放在一行

* {
  padding: 0;
  margin: 0;
}

body {
  width: 100%;
  margin: 0;
  padding: 0;
}

.cont {
  position: relative;
  font-size: 0;
  /*removes white space*/
  margin: 60px auto;
  padding: 0;
}

.carousel {
  padding: 0;
  margin: 0;
  list-style-type: none;
  min-width: 100%;
  max-width: 2400px;
  height: 100%;
  max-height: 350px;
  overflow-x: visible;
  white-space: nowrap;
}

.carousel li {
  display: inline-block;
  padding: 0;
}

.carousel li img {
  width: 100%;
  height: auto;
}

#next {
  position: absolute;
  top: 45%;
  right: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  font-size: 0;
  z-index: 1;
}

#prev {
  position: absolute;
  top: 45%;
  left: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  z-index: 1;
}

.img_cont {
  width: 100%;
  max-width: 600px;
  height: 100%;
  max-height: 300px;
  padding: 150px 0;
}

.active {
  width: 100%;
  max-width: 1200px;
  height: 100%;
  max-height: 600px;
  padding: 0;
}
<div class="cont">
  <div id="next">
  </div>
  <div id="prev">
  </div>
  <ul class="carousel">
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
    </li>
    <li class="img_cont active">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
    </li>
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
    </li>
  </ul>
</div>

答案 1 :(得分:0)

尝试:

.carousel{
  display:flex;
}

答案 2 :(得分:0)

&#13;
&#13;
*{
  padding: 0;
  margin: 0;
}

body{
  width: 100%;
  margin: 0;
  padding: 0;
}

.cont{
  position: relative;
  font-size: 0;/*removes white space*/
  margin: 60px auto;
  padding: 0;
}

.carousel{
  position: relative;
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  width: 100%;
  max-width: 2400px;
  height: 100%;
  max-height: 350px;
}

.carousel li{
  float: left;
}

.carousel li img{
  width: 100%;
  height: auto;
}

#next{
  position: absolute;
  top: 45%;
  right: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  font-size: 0;
  z-index: 1;
}

#prev{
  position: absolute;
  top: 45%;
  left: 0;
  width: 40px;
  height: 40px;
  background-color: blue;
  z-index: 1;
}

.img_cont{
  width: 10%;
  max-width: 600px;
  height: 100%;
  max-height: 300px;
  padding: 150px 0;
}

.active{
  width: 80%;
  max-width: 1200px;
  height: 100%;
  max-height: 600px;
  padding: 0;
}
&#13;
<div class="cont">
  <div id="next">
  </div> 
  <div id="prev">
  </div> 
  <ul class="carousel">
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
    </li>
    <li class="img_cont active">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
    </li>
    <li class="img_cont">
      <img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

这是你想要的吗?