|
|||||||
| CSS Discussion and technical support relating to Cascading Style Sheets. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi all...
I need some help, if possible, and this seems like the right forum to post in. I'd like to have a graphic and text (in a table) center vertically and horizontally in the browser window. With HTML, I'd normally use the width and height attributes (set at 100%) in a master <TABLE> to do this but XHTML no longer recognizes the height attrib. Any ideas on how to use CSS to now do this? Thanks, - Michael H. |
|
#2
|
|||
|
|||
|
No <table> needed...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" /> <title>Hor & Vert Center</title> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <style title="Default" media="screen" type="text/css"> #center {width:200px; height:100px; position:absolute; top:50%; left:50%; margin:-50px auto auto -100px; border:1px solid black; text-align:center;} </style> </head> <body> <div id="center"> Your centered box. </div> </body> </html>
__________________
// Stefan Huszics Last edited by Stefan; 11-28-2002 at 03:18 PM. |
|
#3
|
|||
|
|||
|
Hi Stefan...
Thanks for your help. It wasn't exactly the solution I was looking for but did get me on the right track. Here's what I have so far. I've removed some irrelevant code for this post. <style type="text/css"> body { margin:0; padding:0; color: #33cccc; } .hold { position: absolute; height: 100%; width: 100%; margin: 0px; padding: 0px; } </style> </head> <body> <div class="hold" align="center"><img src="brand_cov01.gif" alt="Green Cover" name="brandcov" id="brandcov" width="147" height="313" border="0" align="middle"></div> </body> </html> Everything almost works as it's supposed to. The image I have in the div tag is centered horizontally but not vertically. Rather, it's aligned to the top center of the browser window. My overall goal was to have it in the center of a browser window no matter how wide or narrow, etc. Ideas on how to have it vertically in the middle? Thanks again, - Michael |
|
#4
|
|||
|
|||
|
Quote:
__________________
// Stefan Huszics |
|
#5
|
|||
|
|||
|
Alright... I don't understand XHTML all too clearly yet so bear with me. Your code does work correctly when I copy/paste, however, when I change out what's inside the DIV tag, such as with a graphic, what, if anything, do I have to adjust elsewhere. I say this because, for instance, I put a 349x313 graphic in the DIV and it was pushed off the right side of the browser a bit.
Thanks, - Michael |
|
#6
|
|||
|
|||
|
Hmm... never mind. Got the solution in the #center style tag. Thanks, though.
- Michael |
|
#7
|
|||
|
|||
|
Quote:
Just for referance here is a how it would look in "normal" HTML 4.01 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> <style title="Default" media="screen" type="text/css"> #center {width:200px; height:100px; position:absolute; top:50%; left:50%; margin:-50px auto auto -100px; border:1px solid black; text-align:center;} </style> </head> <body> <div id="center"> Your centered box. </div> </body> </html> Quote:
If you only work with inline content, like plain text and images you could also use the shorter CSS (since text-align:center; will center it horizontally anyway) #center {width:100%; height:313px; position:absolute; top:50%; left:0; margin:-157px auto auto; border:1px solid black; text-align:center;} (also adapted it to the image height, in this case 313px)
__________________
// Stefan Huszics Last edited by Stefan; 11-29-2002 at 03:40 AM. |
|
#8
|
|||
|
|||
|
To acheive what I anderstand you want to do, you should combine absolute and relative positioning like :
<div align="center"><div id="layer33" style="position: relative; left: 0px; width: 826px; height: 136px;"> <div id="Layer39" style="position: absolute; left: 0px; width: 276px; background: #993399; height: 136px;"> ...</div></div> HTH
__________________
{ Rodéric Allier } |
|
#9
|
|||
|
|||
|
Quote:
To me it doesn't even seem to be close to what the original posted asked for. Are you perhaps forgetting to include the CSS attached with the ID attributes?
__________________
// Stefan Huszics |
|
#10
|
|||
|
|||
|
Quote:
__________________
{ Rodéric Allier } |
|
#11
|
|||
|
|||
|
Quote:
With other content above it, first relative positioning will not make your content vertically centerd on screen. I also see other pontential problems with your code example eg width: 826px. That will give a horizontal scrollbar at all resolutions <1024px
__________________
// Stefan Huszics |
|
#12
|
|||
|
|||
|
The first example of Stefan works... untill you shrink the browser window beyond the size of the DIV size.
The DIV will be kept centered causing the TOP of the DIV to move upward. I'd like to know how I can keep the DIV from moving beyond the top boundry of the browserwindow. |
|
#13
|
|||
|
|||
|
me too!
|
|
#14
|
|||
|
|||
|
Horizontally and Vertically Centered No Scrollbars
I recently wrote an article on this - CSS Vertically and Horizontally Center - does that solve your problem?
|
|
#15
|
|||
|
|||
|
Quote:
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|