Aligning text in a table with CSS - html

I have a really simple table with two rows and two columns. I want the text in the first column to be right-aligned and the second column to be left-aligned. I'm sure this is really easy but I just can't figure it out.
Here's the HTML:
<table>
<tr><td>Previous:</td><td>Link 1</td></tr>
<tr><td>Next:</td><td>Link 2</td></tr>
</table>
How would I do this?

I would use classes in this instance. You could get fancy but this is the best way for supporting.
CSS
.alignright { text-align: right; }
.alignleft { text-align: left; }
HTML
<td class="alignright"> </td>
<td class="alignleft"> </td>
You could go further by adding padding, margins and further classes. Depending on your TABLE css you might need to add some padding so that the cells aren't all padding: 0 and not showing any alignment.

You can either use :first-child to target the cells in the first column:
td {
text align: left;
}
td:first-child {
text-align: right;
}
But :first-child: doesn’t work in IE 6, so you might want to add a class to the cells in the first column, and use that instead:
<table>
<tr><td class="first">Previous:</td><td>Link 1</td></tr>
<tr><td class="first">Next:</td><td>Link 2</td></tr>
</table>
td {
text align: left;
}
td.first {
text-align: right;
}
As written, this will apply to all <td> elements, so you might also want to add a class to your table and limit the styles to <td>s in that table:
<table class="simple">
<tr><td class="first">Previous:</td><td>Link 1</td></tr>
<tr><td class="first">Next:</td><td>Link 2</td></tr>
</table>
table.simple td {
text align: left;
}
table.simple td.first {
text-align: right;
}

As an alternative and given your scenario and if you are able to - why don't you replace the <td>'s in your second column with <th>'s and then the CSS will be really simple:
td { text-align:right; }
th { text-align:left; }

set different classes on each td element
<style>
td.raligned {text-align: right;}
td.leftaligned {text-align: left;}
</style>
<table>
<tr>
<td class="raligned">blah</td>
<td class="leftaligned">blah</td>
</tr>
</table>

It's easy. Create a class of CSS
<style>
.aleft {text-align: left;}
.aright {text-align: right;}
</style>
Now add the classes to your table, it's easy.
<table>
<tr><td class="aright">Previous:</td><td>Link 1</td></tr>
<tr><td class="aleft">Next:</td><td>Link 2</td></tr>
</table>

HTML
<table>
<tr>
<td class="right-align">Previous:</td>
<td class="left-align">Link 1</td>
</tr>
<tr>
<td class="right-align">Next:</td>
<td class="left-align">Link 2</td>
</tr>
</table>​
And your stylesheet..
td.right-align {
text-align: right;
}
td.left-align {
text-align: left;
}

Use
<col align=right>
inside the <table> element, to right-align the first column on IE, and
td:first-child { text-align: right; }
to do the same on more standards-conforming browsers.
Data cells (td elements) are left-aligned by default, so you need not do anything with the second column.

Personally I would recommend not using tables and use a CSS solution that being said here's a jsfiddle option. basically the same as other have said but with jsfiddle you can manipulate it and make changes so you can see them immediately.
http://jsfiddle.net/rhoenig/rnAVH/

Related

Spacing issue with image placed in table - HTML and CSS

I'm building a table for my website, and I'm trying to place a logo inside of a data cell. The issue is that whenever I add the picture, the margins go really weird and I can't figure out why spacing is added. I tried to remove the padding and margins on the image, and the cell itself, but nothing fixes it.
Before image:
After image:
HTML:
<table class="table">
<thead class="tablehead">
<tr>
<th>Language</th>
<th>Year Initiated</th>
<th>Projects</th>
</tr>
</thead>
<tbody class="tablebody">
<tr>
<td><img src = "images/Java_Logo.png" class="tableimage"></td>
<td>2015</td>
<td>ENTER LINK</td>
</tr>
<tr>
<td>C#</td>
<td>2016</td>
<td>ENTER LINK</td>
</tr>
<tr>
<td>Python</td>
<td>2018</td>
<td>ENTER LINK</td>
</tr>
<tr>
<td>HTML and CSS</td>
<td>2018</td>
<td>ENTER LINK</td>
</tr>
</tbody>
</table>
CSS:
.table{
margin: auto;
}
.tablehead{
font-family: permanent marker;
font-size: 24px;
}
.tablebody{
font-family: body;
font-size: 20px;
}
.tableimage{
width: 15%;
padding:0px;
margin: 0px;
}
th, td{
border-bottom: 1px rgb(146, 40, 40) solid;
padding: 10px;
margin: 0;
}
I've also already tried multiple different images, so this does not seem to be the issue. I'd like all three columns to take up 1/3 of the space each.
Another option is to use this, which changes the behavior of the table overall. (Set your preferred width, or nothing at all):
table {table-layout: fixed; width: 50%;}
You only need to specify a width for the table cells. Try adding this to your CSS:
th, td {
width: 33%;
}
The table must some other css affecting it. When I put your code into JSFiddle, it seems to work the way you want. See example: https://jsfiddle.net/ruben/xg2joc1y/5/
You could try adding some css to your image:
.table img {
display: inline;
}

CSS : When to use display flex and display inline-block?

Hi, I have a scenario in which i want to make the above view. I am using Bootstrap4 and I know I can achieve this by using either display:flex or display:inline-block. Now I really wanna know which to use when ? What's the best practice ?
Right now i am doing something like this.
.job-details-container {
display: flex;
}
.job-details-container .job-details-type {
width: 15%
}
<div class="job-details-container">
<div class="job-details-type">Id</div>
<div class="job-details-content">0234</div>
</div>
Well, this is essentially a table. So I suggest using HTML tables. The cells will stretch automatically just like with flex. Tables are fully supported back to IE 8.
.job-details {
border-collapse: collapse;
width: 80%;
margin: auto;
}
td, th {
border-bottom: 1px solid #dddddd;
text-align: left;
padding: 10px;
}
<table class="job-details">
<tr>
<td>Id</td>
<td>0234</td>
</tr>
<tr>
<td>Service Type</td>
<td>Move</td>
</tr>
<tr>
<td>Schedule</td>
<td>11:00 am, Jan 1, 2019</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
</table>
Change to flex if you want to create layouts and complex designs. For displaying simple text or maybe some images, tables are your friends.

html table with centered cells

Essentially what I want is a table, with one row, 5 cells... The first / left cell should be left justfied, the last / right cell should be right justified, and the middle 3 cells need to be centered with equal amounts of spacing between each cell. The table itself is "100% width, so that is where the spacing between cells would come from.
How would I write this (using html / css)? "table" tags or "divs" etc are both valid, I don't really mind which approach is taken as long as the end result looks correct.
Edit:
The problem is the spacing; the table itself isn't an issue, but simply setting the alignment on the cells will not work correctly; the free space between the cells is not 100% divided equally between the cells.
I also don't want to specify cell width as the content is dynamic and there is no way to know before hand how much width is needed.
HTML only version
<table>
<tr>
<td width="20%"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="right"></td>
</tr>
</table>
CSS Version
<table>
<tr>
<td style="width:20%;"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:right"></td>
</tr>
</table>
If you are using a table, assign unique ids to each cell and then use css to justify as required, e.g.
HTML:
<td id="firstcell">...</td>
<td id="secondcell">...</td>
<td id="thirdcell">...</td>
<td id="fourthcell">...</td>
<td id="fifthcell">...</td>
CSS:
table {table-layout:fixed;} /* ensure the widths are absolutely fixed at the specified width*/
td{ width: 20%;} /* allocate space evenly between all 5 cells */
td#firstcell {text-align:left;}
td#secondcell, td#thirdcell, td#fourthcell {text-align:center;}
td#fifthcell {text-align:right;}
td
{
text-align:center;
}
td:first-child
{
text-align:left;
}
td:last-child
{
text-align:right;
}
<style type="text/css">
.five_columns {
width: 100%;
}
.five_columns > div {
width: 20%;
float: left;
text-align: center;
overflow: auto;
}
.five_columns > div:first-child {
text-align: left;
}
.five_columns > div:last-child {
text-align: right;
}
</style>
<div class="five_columns">
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
overflow: auto is set because if you strictly want the width to be the same there really isn't much you can do (as far as I know) other than force scrollbars on anything that's too long.

Vertical Alignment of text in a table cell

Here's a portion of my table (it's a form):
Those are just two <td>'s in a <tr>. I'm trying to get Description up top, to the top of the table cell, rather than resting on the bottom.
How can I do that?
td.description {vertical-align: top;}
where description is the class name of the td with that text in it
td.description {
vertical-align: top;
}
<td class="description">Description</td>
OR inline (yuk!)
<td style="vertical-align: top;">Description</td>
Try
td.description {
line-height: 15px
}
<td class="description">Description</td>
Set the line-height value to the desired value.
If you are using Bootstrap, please add the following customised style setting for your table:
.table>tbody>tr>td,
.table>tbody>tr>th,
.table>tfoot>tr>td,
.table>tfoot>tr>th,
.table>thead>tr>td,
.table>thead>tr>th {
vertical-align: middle;
}
valign="top" should do the work.
<tr>
<td valign="top">Description</td>
</tr>
I had the same issue but solved it by using !important. I forgot about the inheritance in CSS. Just a tip to check first.
Just add vertical-align:top for first td alone needed not for all td.
tr>td:first-child {
vertical-align: top;
}
<tr>
<td>Description</td>
<td>more text</td>
</tr>
CSS {vertical-align: top;} or html Attribute {valign="top"}
.table td,
.table th {
border: 1px solid #161b21;
text-align: left;
padding: 8px;
width: 250px;
height: 100px;
/* style for table */
}
.table-body-text {
vertical-align: top;
}
<table class="table">
<tr>
<th valign="top">Title 1</th>
<th valign="top">Title 2</th>
</tr>
<tr>
<td class="table-body-text">text</td>
<td class="table-body-text">text</td>
</tr>
</table>
For table vertical-align we have 2 options.
is to use css {vertical-align: top;}
another way is to user attribute "valign" and the property should be "top" {valign="top"}

How do I get elements in a 'column' to right-align?

I have a simple two column table; I want a way to align the data in the first column to the right and to be able to style the two elements separately. Perhaps a table is not the best solution here, but I don't know what else to try. I tried with column groups, but it isn't working. Even when I try applying text-align: right to the 'label' element.
<table>
<colgroup>
<col class="label" />
<col class="price" />
</colgroup>
<tr>
<td><label>Subtotal:</label></td>
<td>$135.00</td>
</tr>
<tr>
<td><label>Taxes:</label></td>
<td>$11.23</td>
</tr>
</table>
Since you're probably talking about heading cells, I'd go for a different approach:
<style type="text/css">
table th { text-align: right; }
table td { text-align: left; }
</style>
<table>
<tr>
<th>Right aligned</th>
<td>Left aligned</td>
</tr>
</table>
Give id or class to your HTML tags. eg ..
Then use css to style them as you want.
tr#cell1{
text-align:right;
}
Use this for every row yoou want to align seperately
Label doesn't right align because it is an inline-element. If you give it display:block or display:inline-block it will fill the whole table cell and apply your right align:
label {
display: block;
text-align: right;
}
Try to give the table cell a class
<td class="sub">…</td>
and then style them with CSS:
table td {
// style for all except .sub
}
table td.sub {
text-align: right;
// and other styles that differ from rest
}
This should do!
<html>
<head>
<style>
.one { width:100px; border:1px solid red; }
.one label { display:block; width:100%; text-align:right; }
.two { width:150px; border:1px solid green; }
</style>
</head>
<body>
<table>
<tr>
<td class="one"><label>one</label></td>
<td class="two">two</td>
</tr>
</table>
</body>
</html>
If you don't want to turn the td element into th you can do this:
<style type="text/css">
table td:first-child { text-align: right; }
</style>
<table>
<tr>
<td>Right aligned</td>
<td>Left aligned</td>
</tr>
</table>
It works well with Firefox, Chrome and IE 8 (probably IE 7 too).