Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have 4 divs inside a div that are all equal in size and are not joined together?

Tags:

html

css

Here is an example of what I'm describing:

Valid XHTML

like image 842
Mo. Avatar asked Dec 05 '25 07:12

Mo.


2 Answers

Simple way to achieve the thing you want : use of float:left do work for you

<div style="width:100%">
 <div style="width:100%">
   <div style="width:45%; float:left">  
      div1
   </div>
   <div style="width:45%; ">  
      div2
   </div>
 </div>
 <div style="width:100%">
   <div style="width:45%; float:left">  
      div3
   </div>
   <div style="width:45%; ">  
      div4
   </div>
 </div>      
</div>
like image 122
Pranay Rana Avatar answered Dec 07 '25 20:12

Pranay Rana


Use float and margin to solve your problem as rahul already suggested. To have 2 floats next to each other use width.

CSS:

.outer {
  border: 1px solid black;
  width: 100%;
  float: left;
}

.inner {
  margin: 10px;
  float:left;
  width:45%;
  border: 1px solid black;
}

HTML:

<div class="outer">
  <div class="inner">div</div>
  <div class="inner">div</div>
  <div class="inner">div</div>
  <div class="inner">div</div>
</div>

I used 45% for the width of the floats, to make sure that they fit. 50% is not working due to the margin. The 45% could be increased slight more I guess, but that depends on the margin of the inner divs.

like image 30
Veger Avatar answered Dec 07 '25 19:12

Veger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!