防止文本溢出DIV之外

时间:2018-01-18 18:48:55

标签: html css

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

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

<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>

1 个答案:

答案 0 :(得分:3)

如果您想要相同的高度,请使用 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