为什么这篇文章将div推到一个新的行?

时间:2017-08-02 13:54:10

标签: jquery html css twitter-bootstrap

我有3个并排的div,但当我在div中放入太多文本时,它会将其推到新行上。



.consoleRed {
  background: #d83435;
  border-radius: 5px;
  border: 1px #000 solid;
  color: #fff;
  margin: 5px;
}

.consoleIcon {
  float: left;
}

.consoleDesc {
  float: left;
  vertical-align: top;
}

.consoleDesc h3 {
  margin: 1px;
  padding: 1px;
  font-size: 14px;
}

<div class="container">
  <h2>Select a Console</h2>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">Little Text is fine</div>
    <div class="clearfix"></div>
  </div>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">When i add too much text it ends up pushing the whole div onto the new line</div>
    <div class="clearfix"></div>
  </div>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">Not sure why</div>
    <div class="clearfix"></div>
  </div>
</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/ud7xbnd7/3/

3 个答案:

答案 0 :(得分:1)

您可以使用 flexbox 。这非常适合处理CSS中的并排处理。

您可以尝试以下代码:

.replace(..., '')
.consoleRed {
  background: #d83435;
  border-radius: 5px;
  border: 1px #000 solid;
  color: #fff;
  margin: 5px;
  display : flex;
}

.consoleDesc {
  float: left;
  vertical-align: top;
}

.consoleDesc h3 {
  margin: 1px;
  padding: 1px;
  font-size: 14px;
}

答案 1 :(得分:0)

Added the row class and aligned text using CSS:    
<div class="container">
      <h2>Select a Console</h2>
      <div class="row">
        <div class="col-lg-4 consoleRed">
          <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
          <div class="consoleDesc">Little Text is fine</div>
          <div class="clearfix"></div>
        </div>
        <div class="col-lg-4 consoleRed">
          <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
          <div class="consoleDesc">When i add too much text it ends up pushing the whole div onto the new line</div>
          <div class="clearfix"></div>
        </div>
        <div class="col-lg-4 consoleRed">
          <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
          <div class="consoleDesc">Not sure whyffsdfsdfsdfsdfsdfsdfsdfdsfdsfdsfsdfsdfsdfsdfsdfdsfdsfsdfdsfsdfdsfsdfdfdsfdsfdf</div>
          <div class="clearfix"></div>
        </div>
      </div>
    </div>
    <style>
    .consoleRed {
      background: #d83435;
      border-radius: 5px;
      border: 1px #000 solid;
      color: #fff;
      margin: 5px;
    }

    .consoleIcon {
      float: left;
    }
    .consoleDesc h3{
      margin: 1px;
      padding: 1px;
      font-size: 14px;
      text-align:justify;
    }
    </style>

答案 2 :(得分:0)

只需删除float:left

即可
.consoleDesc {
  float: left;<-------------Remove this
  //more code...
}

&#13;
&#13;
.consoleRed {
  background: #d83435;
  border-radius: 5px;
  border: 1px #000 solid;
  color: #FFF;
  margin: 5px;
}

.consoleIcon {
  float: left;
}

.consoleDesc {
  vertical-align: top;
}

.consoleDesc h3 {
  margin: 1px;
  padding: 1px;
  font-size: 14px;
}
&#13;
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
  <h2>Select a Console</h2>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">Little Text is fine</div>
    <div class="clearfix"></div>
  </div>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">When i add too much text it ends up pushing the whole div onto the new line</div>
    <div class="clearfix"></div>
  </div>
  <div class="col-lg-4 consoleRed">
    <div class="consoleIcon"><img width="64" height="64" src="images/3ds.png"></div>
    <div class="consoleDesc">Not sure why</div>
    <div class="clearfix"></div>
  </div>
</div>
&#13;
&#13;
&#13;