473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to align a table to the middle of tha page

1 New Member
i know its a simple question, but how would i align a <table> tag to the center of the page, using css?

thanks
Oct 11 '06 #1
24 122221
le007
14 New Member
<table style="width: 80%; align: center;">
Oct 11 '06 #2
stnick
11 New Member
Or you could enclose the entire table in a <div align="center"> <table ...>

</table></div>

is also a viable solution, this may work better in various browsers as some may or may not recognize the style in the table tag.

~nicholas
Oct 13 '06 #3
mikek12004
200 New Member
and what about vertical alignment? (for any scrren resolution I want a table to be centered in the center of the page)
Mar 10 '09 #4
sarang07
1 New Member
Hi All,
Stnick, Le007 sorry to say but you both given wrong techniques.

Thatguy , it's very simplest way to center align a table is:

Expand|Select|Wrap|Line Numbers
  1. <table style="width:90%; margin:auto">
  2. ....
  3. </table>
or if you are using <div> then:
in CSS make a class:
for center align
Expand|Select|Wrap|Line Numbers
  1. .automargin
  2. { margin:auto;
  3. position:absolute;
  4.  }
and call it in div tag:
Expand|Select|Wrap|Line Numbers
  1. <div class="automargin"></div>
Thanks.
Mar 14 '09 #5
hsriat
1,654 Recognized Expert Top Contributor
CSS:
Expand|Select|Wrap|Line Numbers
  1. table#yourTable {
  2.     position: fixed;
  3.     left: 50%;
  4.     top: 50%;
  5.     margin: -(H/2)px -(W/2)px;
  6.     width: Wpx;
  7.     height: Hpx;
  8. }
Replace H and W as required.
And don't forget the Strict DTD for IE.
Mar 21 '09 #6
David Laakso
397 Recognized Expert Contributor
To center the table rail to rail put this in the head of the document:
Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2. table {border: 1px dashed fuchsia/*for testing only*/;overflow:hidden/* to open the table if it is necessary*/; width:whatever its width is in pixels, percent, or em; margin: 0 auto;}
  3. </style>
  4.  
Centering a table vertically, or anything else, assuming it has movable text within it and no height hard coded, is difficult but possible with CSS. We would need to see what you got.

OTOH, if the contents of the table is not tabular data, remember not to forget this is 2009 not 1998; and that you ought to use a tableless layout-- not a table...
Mar 21 '09 #7
Stomme poes
14 New Member
At least one right answer got in there : (
Mar 24 '09 #8
Frinavale
9,735 Recognized Expert Moderator Expert
Today I learned how to vertically center a button within a <div>.
I thought that I could apply this to help you solve your problem too but this is not the case.

I think the reason is because a table is treated as a block of data and by default has a display style set to "block"; whereas the button element is not a block of data and has a default display style set to "inline".

I created a simple table:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>   
  8.         <table id="someTable" class="myTableStyle" border="1px">
  9.             <tr>
  10.                 <td>colum1 row1</td>
  11.                 <td>column2 row1</td>
  12.            </tr>
  13.            <tr>
  14.                 <td>colum1 row2</td>
  15.                 <td>column2 row2</td>
  16.            </tr>
  17.         </table>
  18. </body>
I then started trying to align this in the center of the page using CSS alone.

Centering the table horizontally was pretty easy.

As I said before, tables are treated as blocks of data and so, by default, have a display style set to "block". In order to align a element with a display style set to "block", you need set it's width style. Once the width has been set you can use the margin style to horizontally align the element in the middle of it's parent element (in this case the parent element is the <body> element).

Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2.         .myTableStyle
  3.         {
  4.             width:200px;
  5.  
  6.             /*
  7.                 Now that the width has been set, we can use
  8.                 the margin style to center it horizontally.
  9.                 It will center horizontally by setting the
  10.                 margin-left and margin-right styles to 'auto'
  11.  
  12.                 The following style sets the margin-left,
  13.                 margin-right, margin-top, and margin-bottom to 'auto'.
  14.                 Alternatively you could do:
  15.                     margin:0px auto;
  16.                 This will set the margin-top and margin-bottom to 0px and
  17.                 the margin-left and margin-right to auto.
  18.  
  19.                 You can also specify each one:
  20.                     margin:5px auto 10px auto;
  21.                 This will set the margin-top to 5px, bottom to 10px,
  22.                 and left & right to auto.
  23.  
  24.                 Or you can simply do:
  25.                    margin-left:auto;
  26.                    margin-right:auto;
  27.             */
  28.  
  29.  
  30.             margin: auto;
  31.         }
  32.     </style>
  33.  
To align an "inline" element vertically in the center you could simply set it's position to "relative" and move it down from the top of it's parent element by 50%. However this will not work with a block element.

So why don't you just set the display style of the table element to "inline" you ask?

Because the table rows and data are blocks as well and the content will be somewhat messed up. If you set these to "inline" as well you will lose table layout of your table.

The only solution I've found is to set the position of the table to either absolute or fixed and move the table 50% from the top (or bottom) and 50% from the left(or right).

Expand|Select|Wrap|Line Numbers
  1.  
  2. <style type="text/css">
  3.         .myTableStyle
  4.         {
  5.            position:absolute;
  6.            top:50%;
  7.            left:50%; 
  8.  
  9.             /*Alternatively you could use: */
  10.            /*
  11.               position: fixed;
  12.                bottom: 50%;
  13.                right: 50%;
  14.            */
  15.  
  16.  
  17.         }
  18.     </style>
Setting the margins of the table once it has a position of absolute or fixed will have no effect on how the element centered...so it's not necessary to set them for this example.

There are 2 things to note about using this method to center your table:

The first thing is that the table will be moved to 50% from the left and 50% from the top. This means that the top left corner of the table will be moved to the center and the table will be drawn from there. So, the top of your table will be in the center of the window and the rest of the table will be below this point.

To get around this, you should set the height and width of the table to some percentage of the page and subtract half of that value from the top value and left value:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <style type="text/css">
  3.         .myTableStyle
  4.         { position:fixed;
  5.            height:50%;
  6.            width:50%;
  7.            top:25%;
  8.            left:25%;
  9.             /*Alternatively you could use: */
  10.            /*
  11.                bottom:25%;
  12.                right:25%;
  13.            */
  14.  }
  15.     </style>
Please be aware that setting the height to 50% when using a position:absolu te doesn't actually do anything to the table; however setting the height to 50% when the table has a fixed position will effect the table's height.

The second thing to note is that setting the table's position to absolute or fixed will draw the table in such a way that it is no longer positioned relative to the rest of the page's content. As such it will appear to "float on top of" any other content in the page.

-Frinny
Mar 24 '09 #9
drhowarddrfine
7,435 Recognized Expert Expert
@Frinavale
Well, no. I'm not sure inline is default but you can set it to either block or inline.

I didn't read this whole thread but here's a solution to centering a button inside a div. It's the same as in the other thread.
Expand|Select|Wrap|Line Numbers
  1. <style type='text/css'>
  2. div{height:400px;width:200px;outline:1px solid;}
  3. button{position:relative;top:50%}
  4. </style>
  5.  
  6. <div>
  7.         <button type="submit">1</button>
  8. </div>
  9.  
Mar 24 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2663
by: Ben | last post by:
The page below isn't picking up details or location. What have I missed? Thanks in advance, Ben ____________________________________________ <?PHP
3
9414
by: Joe Weinstein | last post by:
Hi all. I am debugging a JDBC application. I find that a certain query that has an order-by clause fails with a SQL1585N A system temporary table space with sufficient page size does not exist. I can understand if there is not enough room for the temp data for sorting, but why should the page *size* matter? thanks, Joe Weinstein at BEA (DB2 ignorant)
1
5577
by: RA | last post by:
Hi When I create a table and aligned it to the left, how can I have some padding added from the left so that the user will see the table border on the left? I don't want to use the vs style builder. Thanks
8
7445
by: ALI-R | last post by:
How do I allign all my content in aspx page in the middle of page like a lot of websites ? Dose it work with differnet screen resolutions? Thanks for your help.
2
3645
by: vito | last post by:
Hi I want to center in vertical position my links (in all resolutions) and I don't how to do it. My page www.exterior.pl/bumaga
2
11719
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
3
1581
by: ravib | last post by:
how to transfer one page to an other page through middle page in javascript
4
11020
by: Puppy Breath | last post by:
I can center a table in any browser using: <div style="text-align:center"> <table border="1" style="width: 50%; margin:0 auto"> <tr><td>etc</td></tr> </table> </div> But I can't figure out how to right-align the table. I don't want to use deprecated align= in the <table> tag.
2
1445
oranoos3000
by: oranoos3000 | last post by:
hi I have a login page in my site for login member I want to any user that arrival correctly directly go to page1.php I use header statement(header("Location: page1.php")) in middle page that befor I have Html tage and the other echo statement now header statement dont direct user to page1.php because before this statement this html tags and echo statement go to output what do I have to for direct user to this page ? thanks alot
2
2211
m6s
by: m6s | last post by:
Good morning. I have a major issue with iText. Major but most of all ridiculous. After the cration of a table like that tbl = new Table(7, dataTbl.Rows.Count); tbl.setBorderColor(System.Drawing.Color.LightGray); tbl.setDefaultCellBorderColor(System.Drawing.Color.LightGray); tbl.setAlignment(Element.__Value.ALIGN_CENTER); tbl.setPadding(2); tbl.setWidth(( w - 495 ));
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.