joeypowell
06-17-2010, 10:50 PM
Okay I am trying my best to let go of HTML tables and move to the wonderful world of using CSS for layout. I continue to find that, although it may be an archaic way of doing things, most of the time using tables for layout is the quickest and most accurate way for me to get the job done.
Please don't hate, because I want to become more knowledgeable at using CSS!
So in my latest project I have a problem. I have a navigation bar that stretches always accross my page. In that navigation bar I have two primary things: on the left I have a menu section, and on the right I have a login section.
In the past I would have easily implemented the layout with tables, using something like this...
<table cellspacing="0" cellpadding="0" style="width:100%;">
<tr style="background-color: black;">
<td align="left">[stuff for my menu section]</td>
<td align="right">[stuff for my login section]</td>
</tr>
</table>
The end result would be a solid black navigation bar that would contain the stuff for the menu section on the left and the stuff for the login section on the right. Each would sit inside and flush against its respective side of the containing solid black navigation bar. Also, this navigation bar would automatically adjust its height to contain both sections, no matter how tall. Pretty simple stuff, right?
So now I am trying my best to figure out how to do this with CSS. At this point I have only a very crude understanding of how CSS and divs work in layouts.
I started out by making a container div...
<div id="NavigationBar" style="background-color:black;">
</div>
...then I embedded a div for each section...
<div id="NavigationBar" style="background-color:black;">
<div id="MenuSection">
[stuff for menu section]
</div>
<div id="LoginSection">
[stuff for login section]
</div>
</div>
...When I ran the page I quickly noticed the output was quite different than what I expected. It showed my black navigation bar, but it stacked the login section along the left side and just under the menu section.
So after some reading I discovered I should be able to solve the problem by assigning 'float:left' to the menu section div and 'float:right' to the login section div. And so I did.
While this did cause each to appear in the correct location (menu section on the left, login section on the right), it also had an undesirable effect.
Each section, after attachment of the 'float' styles, was no longer contained in the parent div. As a result each no longer contained the background color of the parent.
And so now, while I have a partial solution, I am still left wondering what I should do to continue on and solve this issue.
What I really need is for each section to be *contained* within the parent div and also use its background color, while also aligning properly to the left or right sides of the parent div.
In other words, I want the black background color of the navigation bar to be shown behind my menu and login sections. And if my menu should render at 40 pixels high, 300 pixels high...whatever, the containing parent div should adjust to contain it, just like an HTML table would.
For me, setting 'height' values for the parent div (essentialy in the background) and then using 'float' styles is not a good solution. What if the menu is rendered using a different font and thus changes heights? And even more important, what if a user does not view the page in standard 96DPI mode?
In either case the rendered heights of the menu and login sections could change. If I were using tables this would not be a problem; the table would simply adjust to the needed height, no matter what.
How can I get the same effect when using CSS and divs?
Please don't hate, because I want to become more knowledgeable at using CSS!
So in my latest project I have a problem. I have a navigation bar that stretches always accross my page. In that navigation bar I have two primary things: on the left I have a menu section, and on the right I have a login section.
In the past I would have easily implemented the layout with tables, using something like this...
<table cellspacing="0" cellpadding="0" style="width:100%;">
<tr style="background-color: black;">
<td align="left">[stuff for my menu section]</td>
<td align="right">[stuff for my login section]</td>
</tr>
</table>
The end result would be a solid black navigation bar that would contain the stuff for the menu section on the left and the stuff for the login section on the right. Each would sit inside and flush against its respective side of the containing solid black navigation bar. Also, this navigation bar would automatically adjust its height to contain both sections, no matter how tall. Pretty simple stuff, right?
So now I am trying my best to figure out how to do this with CSS. At this point I have only a very crude understanding of how CSS and divs work in layouts.
I started out by making a container div...
<div id="NavigationBar" style="background-color:black;">
</div>
...then I embedded a div for each section...
<div id="NavigationBar" style="background-color:black;">
<div id="MenuSection">
[stuff for menu section]
</div>
<div id="LoginSection">
[stuff for login section]
</div>
</div>
...When I ran the page I quickly noticed the output was quite different than what I expected. It showed my black navigation bar, but it stacked the login section along the left side and just under the menu section.
So after some reading I discovered I should be able to solve the problem by assigning 'float:left' to the menu section div and 'float:right' to the login section div. And so I did.
While this did cause each to appear in the correct location (menu section on the left, login section on the right), it also had an undesirable effect.
Each section, after attachment of the 'float' styles, was no longer contained in the parent div. As a result each no longer contained the background color of the parent.
And so now, while I have a partial solution, I am still left wondering what I should do to continue on and solve this issue.
What I really need is for each section to be *contained* within the parent div and also use its background color, while also aligning properly to the left or right sides of the parent div.
In other words, I want the black background color of the navigation bar to be shown behind my menu and login sections. And if my menu should render at 40 pixels high, 300 pixels high...whatever, the containing parent div should adjust to contain it, just like an HTML table would.
For me, setting 'height' values for the parent div (essentialy in the background) and then using 'float' styles is not a good solution. What if the menu is rendered using a different font and thus changes heights? And even more important, what if a user does not view the page in standard 96DPI mode?
In either case the rendered heights of the menu and login sections could change. If I were using tables this would not be a problem; the table would simply adjust to the needed height, no matter what.
How can I get the same effect when using CSS and divs?