0

I have a parent div that's 960px wide and I have 5 divs inside that which I want to fill the width of the parent equally.

The reason I'm not just dividing 960 x 5 is because they will act as buttons and if I add or take one away I want them to adjust to the parent divs width.

I also want a 5px gap between each div so they're not touching.

This is my HTML set up at the moment:

<div class="parent">

   <a href="home.html"><div id="home">HOME</div></a>
   <a href="about.html"><div id="about">ABOUT</div></a>
   <a href="getoffer.html"><div id="getoffer">GET OFFER</div></a>
   <a href="testimonials.html"><div id="testimonials">TESTIMONIALS</div></a>
   <a href="contact.html"><div id="contact">CONTACT</div></a>

  </div>

Full JSFiddle

SaturnsEye
  • 6,297
  • 10
  • 46
  • 62

2 Answers2

1

if you want, you can use this JQuery code:

jsFiddle is here

var count = $(".parent a").length;
$(".parent div").width(function(){
    return ($(".parent").width()/count)-5;
}).css("margin-right","5px");

you can count children of parent element with count = $(".parent a").length;. resource

Community
  • 1
  • 1
Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
  • Doesn't work because if I add another div into the parent, it drops down underneath the others. I want all the divs to shorten to accommodate the new div – SaturnsEye Oct 21 '13 at 15:52
0

Try

width: 18%

You can adjust the width accordingly as you add or remove.

Lastly, in your CSS you have a lot of repetition which can be further cleaned up by using a class='button'.

http://jsfiddle.net/WeQwc/1/

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
  • this isn't making the divs fill out to the edge of the parent div. There is loads of gap after the far right div – SaturnsEye Oct 21 '13 at 15:36