LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2024, 02:45 AM   #1
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,321
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
HTML element TABLE failing to center in DIV despite CSS settings


I am unable to place a table in the center of the page, on Firefox and Chromium. The other elements, such as P and IMG, center just fine. But the TABLE element stays stuck over on the left side of the page no matter what I have tried so far. Here is what I have at the moment.

Code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test</title>
  <style type="text/css" media="screen">
        DIV   { text-align: center; background-color: #edd; }
        IMG   { border: thin solid #080; }
        TABLE { border: thin solid #080;  }
  </style>
</head>
<body>
  <div>
    <p>A Test</p>
    <img src="/graphic/sample.png" width="1000"
    height="400" alt="sample graphic" />
    <br />
    <table>
      <caption>alignment test</caption>
      <thead>
        <tr> <th>&nbsp;</th> <th>One</th> <th>Two</th> </tr>
      </thead>
      <tbody>
        <tr> <th>1</th> <td>aaa</td> <td>bbb</td> </tr>
        <tr> <th>2</th> <td>ccc</td> <td>ddd</td> </tr>
      </tbody>
    </table>
  </div>
</body>
</html>
I would think that TABLE would behave like P and IMG within DIV in regards to its horizontal position. What have I missed?
 
Old 04-23-2024, 04:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,321

Original Poster
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
This has bugged me for a longer time but posting it here, even without an answer, has brought me to a work-around:

Code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test</title>
  <style type="text/css" media="screen">
        DIV   { text-align: center; background-color: #edd; }
        IMG   { border: thin solid #080; }
        TABLE { border: thin solid #080; 
                margin-left: auto; margin-right: auto; }
  </style>
</head>
<body>
  <div>
    <p>A Test</p>
    <img src="/graphic/sample.png" width="1000"
    height="400" alt="sample graphic" />
    <br />
    <table>
      <caption>alignment test</caption>
      <thead>
        <tr> <th>&nbsp;</th> <th>One</th> <th>Two</th> </tr>
      </thead>
      <tbody>
        <tr> <th>1</th> <td>aaa</td> <td>bbb</td> </tr>
        <tr> <th>2</th> <td>ccc</td> <td>ddd</td> </tr>
      </tbody>
    </table>
  </div>
</body>
</html>
The work-around seems to be to set margin-left: auto and margin-right: auto for some strange reason. I wonder why that is even necessary and how much of a bug it can be considered in both Firefox's and Chromium's rendering engines.
 
Old 04-23-2024, 06:58 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,725

Rep: Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919
It isn't a workaround, text align only applies to text not a table which is a block level element.
 
Old 04-23-2024, 07:33 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,610

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553

To have a table element obey its container's text-align, set it to "display:inline-table".

For a non-table element, use "display:inline-block" (assuming one needs to control width/etc), otherwise "display:inline" indicates a regular text-based/formatting element.

Otherwise, "margin:auto" is the easiest way to horizontally center non-inline elements, and is not a workaround but part of the CSS specification.
(For vertical margins, auto resolves to zero - use "margin:TOPBOTTOM auto" or "margin:TOP auto BOTTOM" if needed.)


Last edited by boughtonp; 04-23-2024 at 07:39 AM.
 
1 members found this post helpful.
  


Reply

Tags
css, xhtml



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[HTML|CSS] Move div with placeholder so that div in row above may overlap brianmcgee Programming 1 06-15-2011 01:08 PM
HTML / CSS: Overflow div in a table rubadub Programming 3 06-08-2010 07:04 PM
css div - fixed size + center placement Ephracis Programming 6 12-05-2008 05:54 PM
simplest way to center a <div> on the screen via css tanoatlq Programming 4 06-02-2008 02:50 AM
css/html wtf? extra space below div nkoplm Programming 1 06-27-2007 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration