【问题标题】:CSS - Paragraph is stretching the width of the DIVCSS - 段落正在拉伸 DIV 的宽度
【发布时间】:2016-03-20 06:18:04
【问题描述】:

目前我在 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;
  }
}

【问题讨论】:

    标签: html css mobile responsive-design paragraph


    【解决方案1】:

    给你:

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

    https://jsfiddle.net/60Lmtdtd/

    【讨论】:

    • 太棒了!非常感谢您的帮助!
    【解决方案2】:

    您可以在您的@media 中使用word-wrap 来强制中断长词:

     word-wrap: break-word;
    

    【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 添加了word-wrap: break-word它什么都不做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多