container div not applyin background-color to the two floating divs inside it

限于喜欢 提交于 2020-01-16 05:26:12

问题


I'm trying to apply background-color to a div that has two floating divs inside it, but it is not applying it. both divs inside the container are cleared and displaying beside each other like i want tehm to be, but the overall background-color is not applying

jsfiddle here

 <div class="contactformcontainer">
    <div class="maincontactform">
      <h4>SEND US A MAIL</h4>
      <form>
        <input type="text" placeholder="What is your name ?">
        <input type="text" placeholder="Email">
        <textarea type="text" placeholder="What is your message to us ?"></textarea> 
        <input type="submit" value="submit">
      </form>
    </div>
    <div class="maincontactdetails">
      <h4>Email : </h4><p>office@blah.org</p>
      <h4>Tel : </h4><p>(434)-5564-63443534</p>
      <h4>Address : </h4><p>blah blah blah.</p>
    </div>
    </div>

CSS

 .contactformcontainer{
width:100%;
background-color: green;
}

.maincontactform{
width: 47%;
padding: 24px;
float:left;
background-color: blue;
clear:both;
  }

 .maincontactdetails{
width: 40%;
padding: 24px;
background-color: red;
float:right;
}

回答1:


The good old clearfix "hack" is needed here. To force the container to self-clear its children give the div with a class of contactformcontainer another class of clearfix and add these styles

.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}

.clearfix {
display: inline-block;
}

html .clearfix { height: 1%; }
.clearfix { display: block; }

Or just float the outer container to the left , your choice :)




回答2:


float: left; .contactformcontainer :)

.contactformcontainer{
  float: left;
  width:100%;
  background-color: green;
}



回答3:


Adding position: absolute; might solve your problem.

See this fiddle.



来源:https://stackoverflow.com/questions/18048102/container-div-not-applyin-background-color-to-the-two-floating-divs-inside-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!