HTML/CSS: Placing DIVs Next to Each Other

  • Context: HTML/CSS 
  • Thread starter Thread starter davee123
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the challenge of placing multiple DIV elements next to each other in a web layout, particularly in the context of creating a tabbed navigation interface. Participants explore various CSS properties and techniques to achieve this layout, while also debating the appropriateness of using DIVs versus TABLEs for layout purposes.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses frustration with using DIVs for layout, noting that they can only get DIVs to float to the left or right, and struggles to align multiple DIVs in a row.
  • Another participant suggests using a style attribute or a class attribute defined in a stylesheet to control the layout.
  • A proposed solution includes setting a container's width and using float properties for individual boxes, with specific CSS code provided to illustrate this approach.
  • Some participants argue that DIVs are not the best choice for layout, suggesting that list elements (LI) are more commonly used for menus, while others defend the use of DIVs in modern web design.
  • There is a disagreement about the continued validity of using TABLEs for layout, with some asserting that DIVs are now the standard for web layouts, while others maintain that TABLEs are still acceptable.

Areas of Agreement / Disagreement

Participants express differing views on the use of DIVs versus TABLEs for layout, with no consensus reached on the best approach. The discussion remains unresolved regarding the optimal method for placing DIVs next to each other.

Contextual Notes

Some participants mention the importance of container width and margin settings, but there are no definitive conclusions about the best practices for using DIVs in layout design.

davee123
Messages
671
Reaction score
4
So, I always used to use TABLE tags. And nowadays, everyone says "don't use TABLEs-- use DIVs instead!" But, for the love of all that is sacred, I can't figure out how to place DIVs next to each other.

I can figure out how to make a DIV float to the extreme left or extreme right, but what if I don't want to do that? I just want (for example) 4 divs in a row in the middle of whatever container I put them in.

Effectively, I'm making "tabbed" navigation. I'm hoping to make each "tab" in the navigation be a separate DIV tag, and have them all line up. And of course in the CSS, I'll probably have different named-styles, like "selected", "unselected", "highlighted", so that they'll appear different when the mouse is over them or when they're currently on the desired page.

Anyway, the problem I have is that each time I try to put 2 DIVs next to each other, the 2nd DIV always goes below the first one! Is there some hidden "display" setting I should set it to? Is there another high-level CSS property that's defaulted to do this?

I found one other example that does what I want, but they use LI (list element) tags rather than DIVs. Was that because DIVs don't do what's required? Or did this person similarly just throw in the towel?

DaveE
 
Technology news on Phys.org
use a style attribute or (more elegantly) you could use a class attribute with the class defined on your style sheet
 
something like this should work. remember you control the floats by appropriately setting the container "width". If you want them to float less extreme then set the container to a lower width. alternatively you can change the box left and right margins.HTML
Code:
<div id="container">
<div id="box1"> Box1 </div>
<div id="box2"> Box2 </div>
<div id="box3"> Box3 </div>
</div>

CSS
Code:
#container {

width:100%;
height: 300px;
margin:3px auto;
padding:3px;
text-align:center;

}

#box1 {

float:left;
margin:5px;
padding:5px;
width:30%;

}

#box2 {

float:left;
margin:5px;
padding:5px;
width:30%;

}

#box3 {

float:left;
margin:5px;
padding:5px;
width:30%;
clear:right;

}
 
Last edited:
after rereading your question I realize that you seem to be asking for a horizontal menu. Most people use list for menus these days. Div isn't the best tag for defining layouts. I'm not sure who you mean by everyone. Tables are still an acceptable way to layout your page. I use div to define different parts of my document not so much for layout.
 
{~} said:
I'm not sure who you mean by everyone. Tables are still an acceptable way to layout your page. I use div to define different parts of my document not so much for layout.

Gotta disagree. View any major website (yes PF is behind) and the entire layout is now Div. Tables are for displaying data.
 
Greg you probably know better than me what's a good site to learn from

w3 schools leaves something to be desired I think
 
Huh! I guess ... that did the trick! Now I'll just have to see the differences between that and what I've been trying already!

DaveE
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K