CSS - 段落扩展了DIV的宽度

时间:2016-03-19 18:57:44

标签: html css mobile responsive-design paragraph

目前我在div中有一个段落。在移动模式(768px)下查看时,div放置了一段长文本作为段落。当我用短文本替换长文本时,例如'Box One',它不会拉伸div,因为我没有超过div宽度。在移动模式下查看时,如何在段落达到div宽度时将段落分成新行?我附上了我的jsfiddle作为例子。

https://jsfiddle.net/ModestGrey/az4ca9j8/

HTML

<div class="boxes">
<div class="one box"><p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p></div>
  <div class="two box"><p>Box Two</p></div>
  <div class="three box"><p>Box Three</p></div>
  <div class="four box"><p>Box Four</p></div>
</div>

CSS

.boxes {
  width: 50%;
  margin: 0 auto;
}

.box {
  display: inline-block;
  width: 24%;
  height: 15em;
  color: #fff;
}

.one {
  background: #333;
}

.two {
  background: #fff;
  color: #333;
}

.three {
  background: #aaa;
}

.four {
  background: #dc002e;
}

@media only screen and (max-width:48em) {
  .boxes {
    display: table;
    width: 100%;
  }
  .box {
    display: block;
    width: 100%;
  }
  .three {
    display: table-caption;
  }
  .four {
    display: table-caption;
  }
  .one {
    display: table-caption;
  }
  .two {
    display: table-caption;
  }
}

3 个答案:

答案 0 :(得分:0)

您可以在@media中使用word-wrap来强行破解长词:

 word-wrap: break-word;

答案 1 :(得分:0)

使用css word-wrap: break-word;允许长字能够断开并换行到下一行。

答案 2 :(得分:0)

你走了:

.box { display: block; float: left; }
.one.box p { overflow: hidden; word-wrap: break-word; }

https://jsfiddle.net/60Lmtdtd/