HTML/CSS: Placing DIVs Next to Each Other

  • Thread starter davee123
  • Start date
In summary, the conversation discusses the use of TABLE tags versus DIV tags for layout, specifically for creating a "tabbed" navigation. The problem of placing multiple DIVs next to each other is mentioned, and a solution using floats and margins is provided. The use of list elements for menus is also mentioned as an alternative to DIVs. The conversation ends with a suggestion to learn more about CSS and DIVs from reputable sources, such as w3 schools.
  • #1
davee123
672
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
 
Computer science news on Phys.org
  • #2
use a style attribute or (more elegantly) you could use a class attribute with the class defined on your style sheet
 
  • #3
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:
  • #4
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.
 
  • #5
{~} 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.
 
  • #6
Greg you probably know better than me what's a good site to learn from

w3 schools leaves something to be desired I think
 
  • #7
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
 

1. How can I place multiple DIVs next to each other using HTML/CSS?

To place multiple DIVs next to each other, you can use the CSS property "display: inline-block;" on the DIVs you want to be next to each other. This will make them behave like inline elements and sit next to each other horizontally.

2. Can I use float to place DIVs next to each other?

Yes, you can also use the CSS property "float: left;" on the DIVs to place them next to each other. However, this method may require additional CSS to ensure the DIVs are lined up correctly and may cause layout issues on smaller screens.

3. How do I ensure equal spacing between the DIVs when placing them next to each other?

You can use the CSS property "margin" to add equal spacing between the DIVs. For example, you can use "margin: 10px;" to add 10 pixels of space on all sides of each DIV.

4. Is there a limit to the number of DIVs I can place next to each other?

No, there is no specific limit to the number of DIVs that can be placed next to each other. However, too many DIVs may cause layout issues and make the page difficult to read on smaller screens.

5. Can I use HTML tables to place DIVs next to each other?

While it is possible to use HTML tables to place DIVs next to each other, it is not recommended. Using tables for layout purposes goes against the intended use of tables and can cause accessibility issues. It is best to use CSS to achieve the desired layout.

Similar threads

  • Computing and Technology
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
19
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
9
Views
534
  • STEM Academic Advising
Replies
2
Views
622
Replies
9
Views
1K
  • Computing and Technology
Replies
6
Views
2K
Back
Top