Forum Moderators: open

Message Too Old, No Replies

Centering in Divs?

Need to center elements within a div

         

RossWal

5:56 pm on May 24, 2002 (gmt 0)

10+ Year Member



Hi All,
I just discovered this great forum and have already picked up a number of useful tips in reading the posts. I'm new to using CSS for layout, and could use some help. I want to center all the elements within a div. The only way I've found is to add the <center> tag in the html. Is there a 'better' way to accomplish this within CSS?

Also, I had a div without a width specified (it expands to fill the space between two fixed position divs) and noticed that in one case its content text extended beyond the width of the div. Other text wrapped fine. I fixed that problem with a width:100%. Unfortunately that nullified my margin-top:60px. This is in IE6. Anyone know what to make of it?

Thanks very much,
Ross

Nick_W

6:00 pm on May 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to center all the elements within a div. The only way I've found is to add the <center> tag in the html. Is there a 'better' way to accomplish this within CSS?

#your_div {
text-align: center;
}

or if it an image or element with a fixed width try:

#your_div {
margin-left: auto;
margin-right: auto;
}

Nick

RossWal

9:29 pm on May 24, 2002 (gmt 0)

10+ Year Member



Thanks Nick! Setting Marins to auto didn't do it. But, suprisingly, text-align:center worked on select boxes, subordinate divs, images, and........ Text!

Still curious about 'bug' where width:nn% negated margin-top: nnpx. But I worked around that with a padding-top:nnpx

Thanks again!
Ross