Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left-aligning Tables with CSS

Tags:

html

css

I would like to left-align a table with CSS, in a way similar to align=left in standard HTML, but I understand that this is bad form. Is there any way to do this? If not, is there a way to format a left-aligned list of links that is next to content without using tables?

like image 238
Tom Johnson Avatar asked Jul 16 '09 20:07

Tom Johnson


3 Answers

For simplicity, take the stuff out of the style tags and use CSS classes instead:

<ul style="text-align: left; float: left;">
    <li><a href="#">Your link here!</a></li>
</ul>

Simple. With the list displayed, you'll need to give the <ul>'s containing element the style of overflow: auto; to remove the float for the next element that appears below it.

Following on from jeroen said, yes, a table should be left aligned by default unless you've set dir="rtl" somewhere in your DOM. And in that case, unless you're being creative or you're writing Hebrew, there's no reason for it to be there ;)

like image 196
Will Morgan Avatar answered Sep 23 '22 06:09

Will Morgan


Answering "how do I align the text inside the table?", not about divs or how to float them.

Here is how to make a table appear on the left of the page without using the deprecated align attribute:

<table style="margin-right:auto;margin-left:0px">

Be sure to specify a width less than the table's container yet large enough for your needs.

like image 44
Surfer Avatar answered Sep 23 '22 06:09

Surfer


You can use an unordered list for the links, put it in a div and float the div left. With a proper left margin for the contents, the list of links will float nicely next to the contents.

By the way, a table is normally aligned left to begin with, but if you want to position it left of you contents, you can also float the table left.

like image 36
jeroen Avatar answered Sep 22 '22 06:09

jeroen