html - 防止文本溢出到 DIV 之外

标签 html css

我有 2 个并排的 div。在每个 div 中是另一个 div。然后我将我的文本放在两个内部 div 中。框和文本在宽屏桌面 View 中看起来很棒,但是一旦达到 1024 及以下,文本就会开始超出 div。

我希望 div 根据需要展开和折叠以包含文本并在边框和文本之间留出适当的间距。我不想使用 overflow hidden ,因为我需要显示所有文本。

<style>
.hometext {
    box-sizing: border-box;
}

.hometext {
    margin: 0;
}

/* Create two equal columns that floats next to each other */
.hometext {
    float: left;
    width: 50%;
    padding: 10px;
    height: 300px; /* Should be removed. Only for demonstration */
}

.hometextinner {
	border:1px solid #999;
	padding-top:10px;
	padding-left:20px;
	padding-right:20px;
	height:100%;
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}

/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media (max-width: 600px) {
    .hometext {
        width: 100%;
    }
	.hometextinner {
	border:1px solid #999;
	padding-top:10px;
	padding-left:20px;
	padding-right:20px;
	height:100%;
}
}
</style>
<body>
<div class="row">
  <div class="hometext">
    <div class="hometextinner"><h2>Column 1</h2>
    <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p></div>
  </div>
  <div class="hometext">
    <div class="hometextinner"><h2>Column 2</h2>
    <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p></div>
  </div>
</div>
</body>

最佳答案

如果您想要等高,请使用flexbox

例子

.row {
  display: flex;
}

.row>.hometext {
  flex: 1;
  padding: 20px;
  border: 1px solid;
}

@media screen and (max-width: 600px) {
  .row {
    flex-direction: column;
  }
}
<body>
  <div class="row">
    <div class="hometext">
      <div class="hometextinner">
        <h2>Column 1</h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
          up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
          Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
          a line in section 1.10.32.</p>
      </div>
    </div>
    <div class="hometext">
      <div class="hometextinner">
        <h2>Column 2</h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
          up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
          Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
          a line in section 1.10.32.</p>
      </div>
    </div>
  </div>
</body>

提示

一个优点是您不必处理floatclear

关于html - 防止文本溢出到 DIV 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48328331/

相关文章:

html - 看起来很流畅的文字

html - 试图在类别下的页面上显示标题

html - 这个溢出 'trick'是怎么实现的呢?

javascript - 如何创建过滤器搜索以从表中的两列中搜索数据?

html - 溢出文本左对齐

css - 类的第一个元素的CSS选择器

javascript - 在滚动 div 颜色变化

php - PHP数据表的 overflow hidden

javascript - 使用 css 或 jquery 将一张图片淡入另一张图片

html - 如果标题是html,网站如何在每个页面上保留标题?