SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Three Divs within a Div

    Hi, I'm new to this site, and after much research found this to be the most active one!

    So here it goes, if anyone could help me I'd be really greatful, I am new to web design so if this seems very simple I apologise.

    I am trying to construct a layout which contains three divs in another div, but I am having problems, this is my html code:
    Code:
    <div id="wrapper">
    	<div id="header"> LOGO WILL GO HERE </div>
        
    <div id="nav"> Navigation will go here </div>
    
      <div id="widgetcontainer">
    	<div id="widget1"></div>
    		<div id="widget2"></div>	
    		<div id="widget3"></div>
    			</div>  <!-- #widgetcontainter-->
                            </div>  <!--#wrapper-->
    And this is my CSS:

    Code:
    #widget1
    {
    float:left;
    width:30%;
    height: 200px;
    background-color:white;
    }
    
    #widget2
    {
    float:right;
    width:30%;
    height: 200px;
    background-color:green;
    }
    
    #widget3
    {
    margin: 0 auto;
    width:30%;
    height: 200px;
    background-color:blue;
    }
    I am finding that they are all evenly spaced within the div, but they do not contain themselves within the #widgetcontainer.

    Thanks in advance
    Last edited by Paul O'B; Oct 27, 2013 at 07:59. Reason: code tags added

  2. #2
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    40,281
    Mentioned
    179 Post(s)
    Tagged
    6 Thread(s)
    Hi, Welcome to Sitepoint

    Floats are removed from the flow so your widgetcontainer will remain at zero height if it contains only floats. You need to contain your floats and the simplest method assuming you don't need visible overflow is to use overflow:hidden.

    Code:
    #widgetcontainer{
    overflow:hidden;
    }
    This creates a new block formatting context which ensures that the parent encompasses its children (even floated children).

    If you need visible overflow then look at the clearfix method of containing floats.

  3. #3
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response Paul O'B.

    I have input this into my code but I still have the same results, the div widget2 is still overflowing?

    Thanks in advance

  4. #4
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Don't worry about this, I have solved it myself.

    Thanks for the initial help though

  5. #5
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    40,281
    Mentioned
    179 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ontargett View Post
    Don't worry about this, I have solved it myself.

    Thanks for the initial help though
    Glad you solved it

    When you solve a problem yourself it's a good idea to mention your solution here as it may help others in a similar situation - unless it was just a typo of course.

  6. #6
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep, no problem.

    I found that if I set the following code for the margins of each widget:

    #widget1
    {
    float:left;
    width: 31.5%;
    height: 200px;
    background-color:white;
    margin-left: 15px;
    }

    #widget2
    {
    float:right;
    width:31.5%;
    height: 200px;
    background-color:green;
    margin-right: 15px;
    }

    #widget3
    {
    margin: 0 auto;

    width:31.5%;
    height: 200px;
    background-color:blue;
    }

    So by having margin attributes for the left and right widget, then setting the central widget to have left and right auto margins it spaced them evenly and didn't need to use the overflow: hidden; for #widgetcontainer.

    Hope this helps someone else

  7. #7
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    40,281
    Mentioned
    179 Post(s)
    Tagged
    6 Thread(s)
    Hi,

    Thanks for that

    You have to be careful here because #wrapper only takes note of the non-floated #widget3. Should the floats either side be taller than the wrapper will not grow with the floats because the floats are not contained.

    This may not be an issue if those boxes are fixed height images or similar and therefore won't grow. However, if those boxes hold text then a user resizing the text in the browser would break that structure.

    A couple of other pointers (that may not be relevant but worth mentioning) are that you have three elements of 31.5% and then you have margins right and left of 15px. How wide does that make the whole structure?

    31.5% + 31.5% + 31.5% + 15px + 15px =??

    The above has to always add up to less than 100% or something will overlap. This won't be true because at some stage the total will be greater than 100% and elements will overlap. You could have used percentage margins instead or perhaps have used some padding on a parent wrapper instead to create the side gutters. Of course I don't know your set up so this could be wide of the mark.

  8. #8
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much for the advice, I am new to all of this so the help is much appreciated.

    To be honest this was just a trial layout, and I had changed the percentages when I was trying to fit them all in- in future I will use either percentages or pixels explicitly to avoid any problems.

    I understand where you're coming from making the divs grow, as in the future I will want them to grow, as this was just a mock up layout as I said. How would I do this?

    Thanks in advance and for the speedy replies thus far

  9. #9
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    40,281
    Mentioned
    179 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ontargett View Post

    I understand where you're coming from making the divs grow, as in the future I will want them to grow, as this was just a mock up layout as I said. How would I do this?
    Hi,

    To allow the divs to grow you would just need to remove the height from them. If you want an initial height then use min-height instead. You will then of course need to contain the floats as mentioned a couple of times which can be done with overflow:hidden on the wrapper or by using the clearfix method that I mentioned.

    Before you get too far with your design you should think about the content that you want to display. If you have a lot of images to display then percentage columns may not be the best approach as columns may get too small too quickly and you may need perhaps a fixed column and a fluid one. It does all depend on the content so before you go to far down the design route you need yo shove some content in there to play with and to see what happens. Then you can really start to see how things can evolve and what needs to be done


    If you are expecting the three columns to grow in unison (equal column colours) then that won't happen unless you instead use the display:table-cell properties (ie8+) instead of floating.

  10. #10
    SitePoint Zealot ontargett's Avatar
    Join Date
    Oct 2013
    Location
    England, UK
    Posts
    155
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for the advice, to be honest this was just to try and get use to doing 3 divs as I'd not done, bit of an experiment but will bear this advice in mind for when I do get round to using it in a website

    Thanks


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •