浮动的子元素溢出父div

时间:2015-08-24 14:43:25

标签: html css css3

我的浮动子div不包含在父div中,我已经尝试了很多堆栈溢出的解决方案,但是没有一个有效。我使用100%页面大小的div,然后填充,两个包含两个浮动div。两个浮动div的高度设置为100%,但是,它们的高度不是父div的100%这就是为什么他们溢出来的原因。我不想使用隐藏溢出作为解决方案。



body,
html {
  height: 100%;
}
.all_convo_prev_container {
  padding: 0px;
  margin-right: 0px;
  width: 40%;
  height: 100%;
  float: left;
  border-left-style: solid;
  border-left-width: 2px;
  border-left-color: #E5E5E5;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #E5E5E5;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #E5E5E5;
  min-height: 100%;
}
.convo_prev_container {
  width: 100%;
  float: left;
  position: relative;
}
.convo_container {
  width: 59%;
  float: left;
  overflow: hidden;
  border-style: solid;
  border-width: 2px;
  border-color: #E5E5E5;
  height: 100%;
}
.square {
  float: left;
  position: relative;
  width: 28%;
  padding-bottom: 28%;
  /* = width for a 1:1 aspect ratio */
  margin: 2%;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  /* you change this to "contain" if you don't want the images to be cropped */
}
.img_1-1 {
  background-image: url(images/202482895414394109718201703998660.jpg);
}
.name {
  position: absolute;
  height: 50%;
  width: 100%;
  /* fill parent */
  z-index: -1;
  padding-left: 32%;
  overflow: hidden;
  padding-right: 2%;
}
.name P {
  margin: 0;
  position: absolute;
  /* 2 */
  top: 50%;
  /* 3 */
  transform: translate(0, -25%)
  /* 4 */
}
.last_message {
  position: absolute;
  height: 50%;
  width: 100%;
  bottom: 0;
  /* fill parent */
  z-index: -1;
  padding-left: 32%;
  margin-right: 2%;
}
.last_message P {
  margin: 0;
  position: absolute;
  /* 2 */
  top: 50%;
  /* 3 */
  transform: translate(0, -75%)
  /* 4 */
}
.wrapper {
  width: 100%;
  /* whatever width you want */
  display: inline-block;
  position: relative;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #E5E5E5;
}
.wrapper:after {
  padding-top: 28%;
  /* 16:9 ratio */
  display: block;
  content: '';
}

<div id="page-wrapper" style="height: 100%">

  <div style="max-width: 1000px; margin-left: auto; margin-       right: auto; padding-top: 3%; padding-bottom: 3%; min-height: 100%; max-height: 100%; position-absolute;">

    <div class="all_convo_prev_container">

      <div class="wrapper">
        <div class="square img_1-1"></div>
        <div class="name">
          <p><b>Test1</b>
          </p>
        </div>
        <div class="last_message">
          <p>Test2</p>
        </div>
      </div>

      <div class="wrapper">
        <div class="square img_1-1"></div>
        <div class="name">
          <p><b>Test3</b>
          </p>
        </div>
        <div class="last_message">
          <p>Test4</p>
        </div>
      </div>

    </div>
    <!-- all_convo_prev_container -->

    <div class="convo_container">
      <p>Test5</p>
    </div>
    <!-- convo_container -->

  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

绝对不是你想要的样子,但至少它会回答你的问题:

&#13;
&#13;
* {
  box-sizing: border-box;
}

body,
html {
  margin: 0;
  
  max-height: 100%;
}
.all_convo_prev_container {
  padding: 0px;
  margin-right: 0px;
  width: 40%;
  max-height: 100%;
  float: left;
  border-left-style: solid;
  border-left-width: 2px;
  border-left-color: #E5E5E5;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #E5E5E5;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #E5E5E5;
  min-height: 100%;
}
.convo_prev_container {
  max-width: 100%;
  float: left;
  position: relative;
}
.convo_container {
  width: 59%;
  float: left;
  overflow: hidden;
  border-style: solid;
  border-width: 2px;
  border-color: #E5E5E5;
  height: 100%;
}
.square {
  float: left;
  position: relative;
  width: 28%;
  padding-bottom: 28%;
  /* = width for a 1:1 aspect ratio */
  margin: 2%;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  /* you change this to "contain" if you don't want the images to be cropped */
}
.img_1-1 {
  background-image: url(images/202482895414394109718201703998660.jpg);
}
.name {
  position: absolute;
  height: 50%;
  width: 100%;
  /* fill parent */
  z-index: -1;
  padding-left: 32%;
  overflow: hidden;
  padding-right: 2%;
}
.name P {
  margin: 0;
  position: absolute;
  /* 2 */
  top: 50%;
  /* 3 */
  transform: translate(0, -25%)
  /* 4 */
}
.last_message {
  position: absolute;
  height: 50%;
  width: 100%;
  bottom: 0;
  /* fill parent */
  z-index: -1;
  padding-left: 32%;
  margin-right: 2%;
}
.last_message P {
  margin: 0;
  position: absolute;
  /* 2 */
  top: 50%;
  /* 3 */
  transform: translate(0, -75%)
  /* 4 */
}
.wrapper {
  width: 100%;
  /* whatever width you want */
  display: inline-block;
  position: relative;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #E5E5E5;
}
.wrapper:after {
  padding-top: 28%;
  /* 16:9 ratio */
  display: block;
  content: '';
}
&#13;
<div id="page-wrapper" style="height: 100%">

  <div style="max-width: 1000px; margin-left: auto; margin-       right: auto; padding-top: 3%; padding-bottom: 3%; min-height: 100%; max-height: 100%; position-absolute;">

    <div class="all_convo_prev_container">

      <div class="wrapper">
        <div class="square img_1-1"></div>
        <div class="name">
          <p><b>Test1</b>
          </p>
        </div>
        <div class="last_message">
          <p>Test2</p>
        </div>
      </div>

      <div class="wrapper">
        <div class="square img_1-1"></div>
        <div class="name">
          <p><b>Test3</b>
          </p>
        </div>
        <div class="last_message">
          <p>Test4</p>
        </div>
      </div>

    </div>
    <!-- all_convo_prev_container -->

    <div class="convo_container">
      <p>Test5</p>
    </div>
    <!-- convo_container -->

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

除非您的大小调整是边框,否则您的边距,填充和边框大小不会包含在您的高度/宽度计算中。他们是额外的!

更改为边框,以便在宽度/高度百分比内包含边框和填充。

还可以从body标签上的浏览器样式重置边距。

这是你要找的?

&#13;
&#13;
	* {
box-sizing: border-box;
}
body,
html {
height: 100%;
margin: 0px;
}
.all_convo_prev_container {
padding: 0px;
margin-right: 0px;
width: 40%;
height: 100%;
float: left;
border-left-style: solid;
border-left-width: 2px;
border-left-color: #E5E5E5;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #E5E5E5;
border-top-style: solid;
border-top-width: 2px;
border-top-color: #E5E5E5;
min-height: 100%;
}
.convo_prev_container {
width: 100%;
float: left;
position: relative;
}
.convo_container {
width: 59%;
float: left;
overflow: hidden;
border-style: solid;
border-width: 2px;
border-color: #E5E5E5;
height: 100%;
}
.square {
float: left;
position: relative;
width: 28%;
padding-bottom: 28%;
/* = width for a 1:1 aspect ratio */
margin: 2%;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
/* you change this to "contain" if you don't want the images to be cropped */
}
.img_1-1 {
background-image: url(images/202482895414394109718201703998660.jpg);
}
.name {
position: absolute;
height: 50%;
width: 100%;
/* fill parent */
z-index: -1;
padding-left: 32%;
overflow: hidden;
padding-right: 2%;
}
.name P {
margin: 0;
position: absolute;
/* 2 */
top: 50%;
/* 3 */
transform: translate(0, -25%)
/* 4 */
}
.last_message {
position: absolute;
height: 50%;
width: 100%;
bottom: 0;
/* fill parent */
z-index: -1;
padding-left: 32%;
margin-right: 2%;
}
.last_message P {
margin: 0;
position: absolute;
/* 2 */
top: 50%;
/* 3 */
transform: translate(0, -75%)
/* 4 */
}
.wrapper {
width: 100%;
/* whatever width you want */
display: inline-block;
position: relative;
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #E5E5E5;
}
.wrapper:after {
padding-top: 28%;
/* 16:9 ratio */
display: block;
content: '';
}
&#13;
<div id="page-wrapper" style="height: 100%">

<div style="max-width: 1000px; margin: 0px auto; padding-top: 3%; padding-bottom: 3%; height: 100%; ">

<div class="all_convo_prev_container">

  <div class="wrapper">
    <div class="square img_1-1"></div>
    <div class="name">
      <p><b>Test1</b>
      </p>
    </div>
    <div class="last_message">
      <p>Test2</p>
    </div>
  </div>

  <div class="wrapper">
    <div class="square img_1-1"></div>
    <div class="name">
      <p><b>Test3</b>
      </p>
    </div>
    <div class="last_message">
      <p>Test4</p>
    </div>
  </div>

</div>
<!-- all_convo_prev_container -->

<div class="convo_container">
  <p>Test5</p>
</div>
<!-- convo_container -->

</div>
</div>
&#13;
&#13;
&#13;