Align text in a table header - html

It seems align is not working for the th element. Here is my HTML:
<div style="width: 100%; height: 175px; overflow: auto;">
<table class="grid" id="table">
<thead>
<tr>
<th class="not_mapped_style" style="display: none;" align="center">id</th>
<th class="not_mapped_style" align="center">DisplayName</th>
<th align="center">PrimaryEmail</th>
<th align="center">Age</th>
<th align="center">Phone</th>
</tr>
</thead>
<caption>Contacts</caption>
<tbody>
<tr>
<td style="display: none;" property_value="0" property_name="id" align="center">0</td>
<td property_value="rpcuser" property_name="DisplayName" align="center">rpcuser</td>
<td property_value="admin#example.com" property_name="PrimaryEmail" align="center">admin#example.com</td>
<td property_value="69" property_name="Age" align="center">69</td>
<td property_value="+722616807" property_name="Hand_Phone" align="center">+18007</td>
</tr>
</tbody>
</table>
</div>
Text aligning is working just fine for the td elements but fails for the th. Why?

Try:
text-align: center;
You may be familiar with the HTML align attribute (which has been discontinued as of HTML 5). The align attribute could be used with tags such as
<table>, <td>, and <img>
to specify the alignment of these elements. This attribute allowed you to align elements horizontally. HTML also has/had a valign attribute for aligning elements vertically. This has also been discontinued from HTML5.
These attributes were discontinued in favor of using CSS to set the alignment of HTML elements.
There isn't actually a CSS align or CSS valign property. Instead, CSS has the text-align which applies to inline content of block-level elements, and vertical-align property which applies to inline level and table cells.

Try to use text-align in style attribute to align center.
<th class="not_mapped_style" style="text-align:center">DisplayName</th>

Try using style for th
th {text-align:center}

If you want to center the th of all tables:
table th{ text-align: center; }
If you only want to center the th of a table with a determined id:
table#tableId th{ text-align: center; }

In HTML5, the easiest, and fastest, way to center your <th>THcontent</th> is to add a colspan like this:
<th colspan="3">Thcontent</th>
This will work if your table is three columns. So if you have a four-column table, add a colspan of 4, etc.
You can manage the location furthermore in the CSS file while you have put your colspan in HTML like I said.
th {
text-align: center; /* Or right or left */
}

HTML:
<tr>
<th>Language</th>
<th>Skill Level</th>
<th> </th>
</tr>
CSS:
tr, th {
padding: 10px;
text-align: center;
}

For inline css (which which you really shouldn't do), you should do it like this:
<th style="text-align: center;">I'm a heading!</th>
The answers provided by others above tell you how to do it in a css file, which is a better practice. Using inline styling can make things harder to read and harder to expand. Not to mention, harder to debug. (gotta maintain separation of concerns).

Your code works, but it uses deprecated methods to do so. You should use the CSS text-align property to do this rather than the align property. Even so, it must be your browser or something else affecting it. Try this demo in Chrome (I had to disable normalize.css to get it to render).
Source: http://jsfiddle.net/EDSWL/
Demo: http://jsfiddle.net/EDSWL/show

For me none of the above worked. I think it is because I have two levels of header and a fixed width on level 1. So I couldn't align the text inside the corresponding columns on level 2.
+---------------------------+
| lvl 1 |
+---------------------------+
| lvl 2 col a | lvl 2 col b |
+---------------------------+
I had to use the combination of width:auto and text:align-center :
<th style="width:auto;text-align:center">lvl 2 col a</th>
<th style="width:auto;text-align:center">lvl 2 col b</th>

I had to put my th text inside a div with display: flex in order for the text to be middle aligned.
<th style="width: 15%;">
<div style="width: 100%; display: flex; flex-direction: row; align-items: center; justify-content: center">
Blah blah blah
</div>
</th>

Related

table elements acts different with align="center" vs css text-align: center;

I've got table elements within a table. The parent table has a width of 100% and the child table has a width of 300px. I want the child to be centered, so I tried with css to set it with text-align: center;. (https://jsfiddle.net/wrzo7LLb/1/)
<table class="body">
<tr>
<td class="align center"> <!-- CSS text-align: center; -->
<table class="wrapper">
<tr>
<td>
some text
</td>
</tr>
</table>
</td>
</tr>
</table>
But that doesn't work. And then I tried it with align="center" and that did work. (https://jsfiddle.net/wrzo7LLb/)
<table class="body">
<tr>
<td align="center"> <!-- align="center" -->
<table class="wrapper">
<tr>
<td>
some text
</td>
</tr>
</table>
</td>
</tr>
</table>
Could someone explain to me why align="center" works, but text-align: center; doesn't?
I know I can set margin: 0 auto;, but that doesn't explain why align="center" works and the other doesn't.
Semantically (and technically) speaking, text-align should only be used to align inline level elements, of which a table is not.
The align property on a table doesn't refer to text but to
align
This enumerated attribute indicates how the table must be aligned inside the containing document.
As per the table docs above, align has been deprecated, and it is suggested that you do indeed use margin:0 auto; to "center" a table element
Usage Note
Do not use this attribute, as it has been deprecated. The <table> element should be styled using CSS. Set margin-left and margin-right to auto or margin to 0 auto to achieve an effect that is similar to the align attribute.
text-align:center only works for inline elements and obviously table is a table element.
set and try again
table table {
display:inline;
}

Force table column widths to always be fixed regardless of contents

I have an html table with table-layout: fixed and a td with a set width. The column still expands to hold the contents of text that doesn't contain a space. Is there a way to fix this other than wrapping the contents of each td in a div?
Example: http://jsfiddle.net/6p9K3/29/
<table>
<tbody>
<tr>
<td style="width: 50px;">Test</td>
<td>Testing 1123455</td>
</tr><tr>
<td style="width: 50px;">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>B</td>
</tr>
</tbody>
</table>
table
{
table-layout: fixed;
}
td
{
border: 1px solid green;
overflow: hidden;
}
In the example, you can see that the column with AAAAAAAAAAAA... expands despite being explicitly set to 50px wide.
Specify the width of the table:
table
{
table-layout: fixed;
width: 100px;
}
See jsFiddle
Try looking into the following CSS:
word-wrap:break-word;
Web browsers should not break-up "words" by default so what you are experiencing is normal behaviour of a browser. However you can override this with the word-wrap CSS directive.
You would need to set a width on the overall table then a width on the columns. "width:100%;" should also be OK depending on your requirements.
Using word-wrap may not be what you want however it is useful for showing all of the data without deforming the layout.
Make the table rock solid BEFORE the css. Figure your width of the table, then use a 'controlling' row whereby each td has an explicit width, all of which add up to the width in the table tag.
Having to do hundreds html emails to work everywhere, using the correct HTML first, then styling w/css will work around many issues in all IE's, webkit's and mozillas.
so:
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="50"></td>
<td width="100"></td>
<td width="150"></td>
</tr>
<tr>
<td>your stuff</td>
<td>your stuff</td>
<td>your stuff</td>
</tr>
</table>
Will keep a table at 300px wide. Watch images that are larger than the width by extremes
You can add a div to the td, then style that. It should work as you expected.
<td><div>AAAAAAAAAAAAAAAAAAA</div></td>
Then the css.
td div { width: 50px; overflow: hidden; }
You can also use percentages, and/or specify in the column headers:
<table width="300">
<tr>
<th width="20%">Column 1</th>
<th width="20%">Column 2</th>
<th width="20%">Column 3</th>
<th width="20%">Column 4</th>
<th width="20%">Column 5</th>
</tr>
<tr>
<!--- row data -->
</tr>
</table>
The bonus with percentages is lower code maintenance: you can change your table width without having to re-specify the column widths.
Caveat: It is my understanding that table width specified in pixels isn't supported in HTML 5; you need to use CSS instead.
You can also work with "overflow: hidden" or "overflow-x: hidden" (for just the width). This requires a defined width (and/or height?) and maybe a "display: block" as well.
"Overflow:Hidden" hides the whole content, which does not fit into the defined box.
Example:
http://jsfiddle.net/NAJvp/
HTML:
<table border="1">
<tr>
<td><div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div></td>
<td>bbb</td>
<td>cccc</td>
</tr>
</table>
CSS:
td div { width: 100px; overflow-y: hidden; }
EDIT: Shame on me, I've seen, you already use "overflow". I guess it doesn't work, because you don't set "display: block" to your element ...
I would try setting it to:
max-width: 50px;
This works for me
td::after {
content: '';
display: block;
width: 30px;
}

html table border issue

I have a table inside some other HTML elements and it renders strange - the border space appears even if I set it in css as collapsed.
The table looks like this:
and the html is this:
<table cellspacing="0" border="1" class="scrollable-headers-only" title="" cellpading="0">
<thead title="">
<tr title="">
<th class="check" title=""></th>
<th class="type" title=""></th>
<th class="name" title="">Name</th>
<th class="address center-column" title="">Address</th>
<th class="domain" title="">Domain</th>
<th class="status" title="">Status</th>
</tr>
</thead>
</table>
and the css, as it appears in FireBug is this:
for the main table:
for each th:
I added border only too see the s[aces between cells. My issue is to eliminate that space. I added cellpading and cellspacing = 0 to the table, but doesn't make difference. I added css to eliminate that space, without any change in rendering.
This table is used as header for another table, and I need to align it's columns to the other one - as you can see from image, there are small lines under the table. I need to align this table with the second one, for this reason is important to get 100% precision.
What's wrong?
Thank you.
I would start with removing display: inline-block; for th and table elements. Firefox treats those as inline elements and inserts extra space. Another way to eliminate it is to set font-size: 0; for table and in example font-size: 12px; for th element.

Rule Selection in CSS

I have an HTML page in which I want my content to be centered but, within a specific table on that page, I need to have many of the cells be left-aligned and many to be right-aligned and one cell to be center-aligned. Here's a snippet of HTML & CSS that should give you an idea of what I'm trying to do:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
.contentWrapper {
width: 1000px;
border: 1px solid black;
margin: auto;
}
.centerAligned {
text-align: center;
}
.myTable td {
width: 200px;
text-align: left;
}
.myTable td.label {
text-align: right;
}
</style>
</head>
<body>
<div class="contentWrapper centerAligned">
<p>A label for this table...</p>
<table class="myTable" border="1" align="center">
<tr>
<td class="label">Label 1 (Right Aligned)</td>
<td>Value 1 (Left Aligned)</td>
<td class="label">Label 2 (Right Aligned)</td>
<td>Value 2 (Left Aligned)</td>
</tr>
<tr>
<td class="label">Label 3 (Right Aligned)</td>
<td>Value 3 (Left Aligned)</td>
<td class="label">Label 4 (Right Aligned)</td>
<td>Value 4 (Left Aligned)</td>
</tr>
<tr>
<td colspan="4" class="centerAligned">
<input type="button" value="Push Me!">
</td>
</tr>
</table>
<p>Some more content...</p>
</div>
</body>
</html>
I didn't really want to put a class into every single td just to handle how they should be aligned, so I opted to set a default alignment for ".myTable td" of left. This allowed me to leave all the "value" cells to be without a class, but I still need to define one for my "label" cells to get a right alignment for those.
When it comes to the button at the bottom, which I would like to be center aligned, I want to be able to use the class "centerAligned". Unfortunately, using it here doesn't do anything as the ".myTable td" class is considered "more precise" and that cell is given a left alignment instead of a centered one.
I'm using "centerAligned" in other places, so I don't want to simply do away with that class, nor do I want to change the name to something else. I can do this:
.centerAligned, .myTable td.centerAligned {
text-align: center;
}
That seems to work, but this whole thing seems kinda smelly to me. Is there a better way to handle styling these table cells to get the effect that I want without having to define a specific class for every single td?
Thanks!
Use col
Have a look here:
http://www.w3.org/TR/html401/struct/tables.html#h-11.2.4
Or here for XHTML
http://www.w3.org/TR/2004/WD-xhtml2-20040722/mod-tables.html#sec_26.2.
Why don't you just use a 'th' tag for your labels and put a css on that? That way you wont need to put a label class on all of the td 'labels'. So
.myTable th {
width: 200px;
text-align: right;
}
as mentioned in another answer this should be able to be done with colgroup and col but it can't and the colspan is the reason why, how would a 'colspanned' row know which col to take it's alignment from?
I would perhaps feel like suggesting :nth-child but I think it might suffer the same issue when a colspan was met, and you wouldn't get IE support so here's a fiddle which needs no classes and still uses specificity to get the desired result
working example based on opening code with a colspan - JSFIDDLE
You can use jQuery
$('#myTable tr td:eq(0)').css('text-align', 'left');
$('#myTable tr td:eq(1)').css('text-align', 'center');
$('#myTable tr td:eq(2)').css('text-align', 'right');
eq is zero based. So all first cells will be left aligned, all second cells will be center aligned and so on. Adjust to your needs.
You didn't quite describe which position the cells are that need to be right aligned vs. left aligned. But as Bazz suggested you can use col to set styles for all s in that col or maybe you can do the same with a style if your right-aligned cells are in the same row.
If you're able to use col, all you need is:
<table>
<col>
<col class="label">
<col>
<tr>...
Then
.label { text-align: right }
There's nothing wrong with the way you're doing it though...

Table width occupies 100% of page, and I need table on the same row with other elements

I can't get over what appears to be a simple CSS formatting problem in Chrome: I want to put a table towards the right, and a label and some buttons toward the left, inside the same paragraph. This works easy enough in other browsers (Firefox, IE7 & 8), but in Chrome the table stretches over the entire page, under the label.
HTML:
<div class="formrow">
<label> </label>
<div style="display: inline; width: 208px; ">
<table id="tbl_Index" class="grid" style="display: inline; width: 208px; table-layout: fixed;">
<thead>
<tr class="">
<th style="width: 50px;"></th>
<th style="width: 150px;" class=""></th>
</tr>
</thead>
<tbody>
<tr id="1" class="">
<td style="width: 50px;"></td>
<td style="width: 150px;"></td>
</tr>
<!-- etc... -->
</tbody>
</table>
</div>
</div>
Thing is, I've tried just about any trick I found to get the table limited at 200px:
I tried assigning table-layout: fixed, which, in conjunction with width, should have limited my table to 208px;
I tried wrapping the table in a div and setting the div inline, to limit the table this way
I tried styling the grid with display: inline; and then setting max-width: 208px; (you might know that max-width only applies to inline or block, and table is neither of those.)
What's really annoying for me is that if I go in the console and try to get width for the table, it does tell me 208; but when I look in the compiled properties for the table, it displays 0px;
Add to the table's style: float:right; and you want to use inline-block and not inline this should do the trick
You can try to use this css above:
div.formrow { clear:left; }
div.formrow label { float:left; }
div.formrow tabel { width:208px; border-collapse:collapse; display:inline-block; }