473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I stack divs vertically?

Motoma
3,237 Expert 2GB
Hello all!

My end goal is to create an HTML layout that will expand and contract with the window size, while maintaining a variable number of content columns.

In the example below, you will see that if you stretch the browser window until there is two columns, divs on the right stack flush with each other vertically; however, divs on the left also stack flush with the divs on the right, leading do some gaps in the layout. The layout is further exacerbated when the window is expanded to more columns (add more divs to see this happen).

If someone knows of a way that I can get all divs to cutely stack below each other, while maintaining the liquidity of this layout, and accomplishing this all with each container styled uniformly, I would greatly appreciate it.

Thanks in advance,
Motoma.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  5. <title>A quick example of my issue</title> 
  6.  
  7. <style type="text/css"> 
  8. body
  9. {
  10.     margin: 0em auto;
  11.     width: 85%;
  12.     background: #204364;
  13.     font-family: verdana;
  14. }
  15.  
  16. div
  17. {
  18.     margin: 0.3em;
  19.     padding: 0.3em;
  20.     float:left;
  21.     width: 25em;
  22.     border: 1px gray dashed;
  23.     background: #ffffff;
  24. </style> 
  25.  
  26. </head> 
  27.  
  28. <body> 
  29. <div>
  30. <p>This div has only one entry</p>
  31. </div> 
  32. <div>
  33. <p>This div has a couple items</p>
  34. <p>This div has a couple items</p>
  35. </div> 
  36. <div>
  37. <p>This div has a couple items</p>
  38. <p>This div has a couple items</p>
  39. </div> 
  40. <div>
  41. <p>This div has only one entry</p>
  42. </div> 
  43. <div>
  44. <p>This div has a lot to say!</p>
  45. <p>This div has a lot to say!</p>
  46. <p>This div has a lot to say!</p>
  47. </div> 
  48. <div>
  49. <p>This div has only one entry</p>
  50. </div>
  51. </body> 
  52. </html>
  53.  
Oct 2 '09 #1
6 19281
Motoma
3,237 Expert 2GB
Partial solutions also accepted!
Oct 2 '09 #2
drhowarddrfine
7,435 Expert 4TB
Yep. It does that.

As you probably know, all those elements are block level and browsers will try and keep everything stacked on top of each other. Floating one removes it from the normal flow but the space for the non-floated blocks is maintained which is what is causing the gaps.

There are a couple of elegant solutions that I've seen but I'd have to look them up. I tend to create documents and stuff them in a folder with hundreds of others so that may take a while.

The first uses javascript and I might be able to find the link to that fairly quick. The second I don't recall and might take longer to find.

In either case, let me know if the js solution is acceptable.
Oct 2 '09 #3
drhowarddrfine
7,435 Expert 4TB
Found the javascript solution.
Oct 2 '09 #4
Motoma
3,237 Expert 2GB
Are you suggesting that I should, on resize, rearrange each div into the appropriate number of colunms?

That's a terrible hack, and it introduces the problem of measuring the heights of each element to get evenly tall columns.
Oct 5 '09 #5
drhowarddrfine
7,435 Expert 4TB
Your original post sounds like that's exactly what you're trying to do so I guess I don't understand.
Oct 5 '09 #6
Motoma
3,237 Expert 2GB
Yeah, I was hoping for some sexy CSS that would do the work for me :)
Oct 6 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Finn Newick | last post by:
By defining a style as follows: @media aural, handheld {td.layout {display:block;}} I'm hoping to linearise layout tables when viewed by screenreaders and handheld devices (it is also be in...
12
by: Michael Ramey | last post by:
Hi, I've have two divs, that are laid out side-by-side. (I'm floating the first div to the left) I need to set up css on these two divs so that the second div is vertically aligned to the...
20
by: Tyler Carver | last post by:
I would like to do something that seems so basic with two div's but I don't think it can be done. Tables can do it in a snap. I was convinced that css was the way to go for layout but without...
3
by: Oddball | last post by:
I have a problem with the positioning of two divisions. They are containined within a content division in which all the page content happens (ie not the menus, etc.). The behavior I would like...
2
by: Guadala Harry | last post by:
Please follow my thinking here and tell me if I'm nuts or if this is a reasonable thing to do given the objective: I'd like to present 3 data values on an aspx page - each styled uniquely, and...
8
by: maroger | last post by:
I'm sure this is a common issue, but I haven't come across anything that works for me thusfar. I am trying to fix my header, which works fine in firefox but has extra space in IE. Here are...
1
by: Erind | last post by:
Okay, I tried the above divs but they didnt work. My columns and whatnot were being pushed around and settled on top of each other. then I started all over and ended up with a layout 30px bigger on...
0
by: stan | last post by:
I have internal links/jumps within divs. The divs have vertical scrolling. When a link is clicked, both the space within the div as well as the entire page vertically scroll. I would like to...
4
by: Adam | last post by:
I have four roughly square DIVs, all the same height, which I need to line up horizontally across the page. What would be the best way to do this? At the moment I have a very botched solution where...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.