How to remove extra white space in html table cells? - html

How do I remove the extra line space between blocks of text? (i.e. January 29th and 31st cells in image).
And is there a way to remove padding from td so words could fill the cell width a little more? (i.e. "Observational" in week 2 topic cell to bottom left of image).
Here is some of the code:
th {
width=16.66%;
height: 30;
text-align: center;
}
td {
text-align: center;
font-size: 65%;
padding: 0;
margin: 0;
}
table {
table-layout: fixed;
margin: 0;
padding: 0;
}
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>Week</th>
<th>Mon</th>
<th>Tues</th>
<th>Wed</th>
<th>Thurs</th>
<th>Fri</th>
</tr>
<tr>
<td style="line-height:1px;margin:0;"></td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">27</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">28</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">29</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">30</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">31</p>
</td>
</tr>
<tr>
<td><b>Week 2: Chapters 2-3</b><br>(Observational Studies)</td>
<td></td>
<td></td>
<td>
<p style="color:red;font-size:100%;">HW Assignment 1 Due</p><br>
<p style="color:blue;font-size:100%;">Chapter 2 Part 1 Prelecture Due</p>
</td>
<td></td>
<td>
<p style="color:red;font-size:100%;">HW Assignment 2 Due</p><br>
<p style="color:blue;font-size:100%;">Chapter 2 Part 2 Prelecture Due</p>
</td>
</tr>
</table>
</div>

You have a <br> between your 2 p tags on each of those that is adding extra space. Just remove that.
If you want even less space you could also lower/remove the margin on your p tags
p {
margin: 0;
}
Not all of your CSS seems to be in your question here so other styles might be interfering also, but you can likely change the margin of the p tag to whatever you'd like.

To remove border gap between blocks in table, you need to add border-collapse: collapse in your table style.
<table border-collapse="collapse" border="1">

You have a <br> between your 2 p tags, remove margin: 0 on your p tag.

to add from an earlier comment.
there's a few elements with default margins and padding that you can reset , you can reset these at once for all of them with : * {margin:0;padding:0;} if it bothers you to mind each element.
To set a box away from another , you can also reset margin , or padding only to the few that matters . p+p is an option to add a margin top only from the second p appearing so you can remove the <br> to be used inside a piece of text or in between inline boxes.
You can also increase line-height if that's what you want at every line of text , usually line-height:1.6em is an average value for reading confort you can use.
CSS requires : in between rules and values, not = (there was a typo for th width ).
example with line-height and the sibbling selector + to remove the br tags , also a display reset on your b tag.
* {
margin: 0;
padding: 0;
}
p+p {
margin-top: 1em;
}
b {
display: block;
margin-bottom: 1em;
}
th {
width: 16.66%;
height: 30px; /* value was missing */
text-align: center;
line-height: 1.6em;
}
td {
text-align: center;
font-size: 65%;
padding-top:1em;
}
table {
table-layout: fixed;
}
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>Week</th>
<th>Mon</th>
<th>Tues</th>
<th>Wed</th>
<th>Thurs</th>
<th>Fri</th>
</tr>
<tr>
<td style="line-height:1px;margin:0;"></td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">27</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">28</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">29</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">30</p>
</td>
<td>
<p style="font-size:95%;line-height:1px;margin:0;text-align:center;">31</p>
</td>
</tr>
<tr>
<td><b>Week 2: Chapters 2-3</b>(Observational Studies)</td>
<td></td>
<td></td>
<td>
<p style="color:red;font-size:100%;">HW Assignment 1 Due</p>
<p style="color:blue;font-size:100%;">Chapter 2 Part 1 Prelecture Due</p>
</td>
<td></td>
<td>
<p style="color:red;font-size:100%;">HW Assignment 2 Due</p>
<p style="color:blue;font-size:100%;">Chapter 2 Part 2 Prelecture Due</p>
</td>
</tr>
</table>
</div>
if you want some reading :
https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
The line-height CSS property sets the height of a line box. It's commonly used to set the distance between lines of text. On block-level elements, it specifies the minimum height of line boxes within the element. On non-replaced inline elements, it specifies the height that is used to calculate line box height.
https://developer.mozilla.org/en-US/docs/Web/CSS/display
The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex.
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator
The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.
also usefull to know : https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
I hope this helps you find more options you can have via CSS to set for your layouts.

Related

Adding a character on ANY position of a cell without changing the TD centering text already displayed

I have
<table>
<tr>
<td style='text-align:center'>
CENTERED
</td>
</tr>
</table>
I would like to ADD a single character either on the left or on the right of CENTERED, without affecting the x-position of the CENTERED string.
To be clear: NOT on the left of the cell, but on the left of the CENTERED string. And the CENTERED string can be anything, so I do not want to calculate something pixel-perfect and move it by some constant pixel value.
I tried fiddling with position:absolute/relative and padding-left/left but I couldn't find a working solution.
Ideally if it could be build with classes:
<table>
<tr>
<td style='text-align:center'>
<span class='centeredRegardless'>CENTERED</span>
<span class='onTheLeftWithoutAffectingThePreviousOne'>*</span>
</td>
</tr>
</table>
Then I will need those classes defined :)
Thank you
If you want some cells to have the extra character and some not you could put the extra character as content in a pseudo element positioned absolutely so it doesn't change the positioning of the actual text at all.
For a left character position is right: 100% and for a character on the right the positioning is left: 100%.
td {
position: relative;
}
td.extraChar::before {
content: '*';
position: absolute;
right: 100%;
}
<table>
<tr>
<td style='text-align:center' class="extraChar">
<span class='centeredRegardless'>CENTERED</span>
</td>
</tr>
<tr>
<td style='text-align:center'>
<span class='centeredRegardless'>CENTERED</span>
</td>
</tr>
</table>
Something like this? (remove and collapse the border when happy)
td { border: 1px solid black; min-width:20px; }
.left { text-align:left;}
.right { text-align:right;}
.center { text-align:center;}
<table>
<tr>
<td class="right">></td>
<td class="center">
CENTERED
</td>
<td class="left"><</td>
</tr>
</table>

table cell, wrapped text, fit width to content, remove extra right hand white space

I have a set width table container, it will contain 3 text elements separated by single characters (>).
These text elements may contain text that cannot be fit in the container on a single line along with the rest, and must be wrapped.
The issue is when the text wraps, the cell will contain extra white space on the right hand side that then forces the other elements to wrap, where normally, without the white space, the succeeding elements would each fit on a single line.
Here is the desired behavior:
Where the first text element cannot fit on a single line and must wrap.
But any of the other text elements may also not fit on a single line and must wrap, leaving no extra white space.
Using a basic table layout:
<table class="table">
<tbody>
<tr>
<td>Membership Clubs and Organizations</td>
<td>></td>
<td>Books Wholesaler</td>
<td>></td>
<td>Music Management</td>
</tr>
</tbody>
</table>
.table {
width:450px;
border:1px solid black;
}
Here there is extra whitespace, causing the succeeding elements to also wrap.
After a lot of research, the closest i have come is by setting width:0.1% for the text elements.
Unfortunately this results in the separating characters having their own extra white space, which i have not been able to remove, i have been unable to reduce their width to fit their contents.
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
.text {
width:0.1%;
}
.table {
width:450px;
border:1px solid black;
}
I settled on using tables because it got me closer to what i need, but i am open to use any format, the only requirement is that it be in pure css, and not use any javascript.
.text {
width:0.1%;
}
.table {
width:450px;
border:1px solid black;
}
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
The width of the table is forcing the white-space to be there, no matter what. table-cells have extra space, so the words wrap when necessary, or the cells have no extra space, so the words wrap on every word.
I think the only option for zero whitespace is
td { word-break: break-all; }
.table {
width:450px;
border:1px solid black;
}
td {
word-break: break-all;
}
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
just remove
.text {
width:0.1%;
}

Why style is applied twice?

I have class definition:
.small, td.small, td.small > nobr, td.small > a
{
font-size: 90%;
}
The purpose is to make text smaller. That should be applied to anything: text in anchor, text in cell, etc.
But in fact, style is applied TWICE if anchor is inside of the cell:
<table>
<tbody>
<tr>
<td class="small">
VERY small content
</td>
<td class="small">Smaller text - looks as required</td>
</tr>
</tbody>
</table>
Why? How to make sure that style is applied only once?
Thank you.
Just remove the last part of the style, td.small > a. Then it will get applied to everything inside the <td>. Note, I changed the size of the font to 60% just so that the size change is apparent.
.small, td.small, td.small > nobr
{
font-size: 60%;
}
<table>
<tbody>
<tr>
<td class="small">
Small content
</td>
<td class="small">Should be smaller as well</td>
</tr>
</tbody>
</table>

Div usage in tables in HTML/CSS?

I'm trying to write some HTML/CSS to display a certain row with some of the elements left-aligned and some of them in the center. This was my HTML code:
<tr class="mainInfo" id="header">
<td> Item </td>
<td> Color </td>
<td> Size </td>
<div class="mid">
<td> Subtotal </td>
<td> Tax </td>
<td> Total </td>
</div>
</tr>
And this is my CSS code:
.mid {
text-align: center;
}
.mainInfo {
font: bold 13px Tahoma;
}
#header {
background-color: #68891;
color: white;
}
But the last three elements are not moving to the center, and I really don't understand why not. I tried putting class="mid" in the <td> tags and that worked, but doesn't that defeat the purpose of DRY?
Fiddle Demo
You cannot put a div instead of td element.
You should validate your HTML code with w3 validator.
If you'll do so you'll see you get this error message:
document type does not allow element "DIV" here; missing one of "TH", "TD" start-tag
Maybe you can do it this way:
<table>
<tr class="mainInfo" id="header">
<td> Item </td>
<td> Color </td>
<td> Size </td>
<td class="center">Subtotal</td>
<td class="center">Tax</td>
<td class="center">Total</td>
</tr>
</table>
JSFiddle example
No, you should not put divs inside tr's or tables.
And you should not use tr's or td's without table-element.
<table>
<tr>
<td>hello world</td>
<!-- This is bare minimum to use tables properly -->
</tr>
</table>
You can insert whatever(not tr or td, but could start new table) you want inside TD-elements though.
It's possible to use other elements to replace these standard ones with css display-property set to table-row etc., but you should stick to conventional tags.
Use colspan/rowspan to span over multiple table columns or rows.
CSS classes are designed to be used as often you need/want to. Only IDs should appear once per page.
Of course you should always keep the DRY concept in mind but in your case it's totally fine. It wouldn't if you would set your .mid class to every <td> because in that case you could just set the properties directly to the <td> element.
middle is not a valid value for text-align, so I'm going to assume, in your CSS, that's meant to be vertical-align. If so, it's because vertical-align will only apply to table cells, not divs - that would explain why it is only being successfully applied to your tds.
Additionally, you shouldn't really put a div inside a table (and shouldn't put a td inside of that) but that's not related to your problem.
Assign one class for left alignment and other for center like so...
.left {
text-align:left;
}
.center {
text-align:center;
}
Asign class to TD elements
<tr class="mainInfo" id="header">
<td class='left'> Item </td>
<td class='center'> Color </td>
</tr>

How to force min-height on table

I have this code :
<table cellspacing="0" cellpadding="0" border="0" style="width:415px">
<tbody>
<tr valign="top">
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;">
This is my text that I need in 2 lines
</td>
</tr>
<tr valign="top">
<td style="font-size:12px;line-height:14px">
Second Line
</td>
</tr>
</tbody>
</table>
As you can see, the first tr/td should be height 60px (min-height:60px) but in fact it isn't.
For many reasons, I can't use height directly (this code is formatted trought back office system, in a newsletter).
So, how can I take the whole height on the td trought min-height?
Also, tried putting min-height:60px; on tr, but nothing change...
min-height doesn't work for table elements:
In CSS 2.1, the effect of 'min-width' and 'max-width' on tables, inline tables, table cells, table columns, and column groups is undefined.
I can only assume this applies to td and tr as well.
What should always work is wrapping the content in a div, and applying min-height to that, as shown in this JSFiddle:
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;">
<div style="min-height: 60px; background-color: green">
This is my text that I need in 2 lines
</div>
</td>
Edit: You say this doesn't work with Outlook.
Alternative idea: Place a 60 px tall image in the td, and make it float: left:
<td>
<img src="..." style="float: left">
</td>
Use <td height="60"> not CSS height or min-height
For HTML email set your table cell as <td height="60"> and it will treat that as the min-height. If your content is more than 60px, it will expand accordingly.
Put a DIV in the cell, style the DIV instead.
Min-height doesn't works on tables.
It is sometimes useful to constrain the height of elements to a certain range. Two properties offer this functionality: min-height & max-height
But these can't be used on non-replaced inline elements, table columns, and column groups.
You can't set min-height and min-width, but you can use some CSS3 for achievements this same effect.
.default-table table {
border-collapse: collapse;
}
.default-table table td {
padding: 0;
}
.default-table tr:before {
width: 0px;
content: '';
float: left;
overflow: hidden;
height: 28px;
font-size: 0;
}
.default-table {
overflow: hidden;
}
<div class="default-table">
<table>
<tbody>
<tr>
<td>Steve</td>
<td>Smith</td>
<td>stevesmith#gmail.com</td>
</tr>
<tr>
<td>Jone</td>
<td>Polanski</td>
<td>jonep#gmail.com</td>
</tr>
</tbody>
</table>
</div>
but if u having collapse or padding in td. You must give for .default-table table minus margin-left.
HTML :
<table></table>
CSS :
table{
height:0px; /*Set any facultative length value to Height (percentage value doesn't work)*/
min-height:100vh;
}
That's how I always resolve this problem ...
Add display block
<td style="font-family:Arial;min-height:60px;font-size:12px;line-height:14px;display:block;">
Here's a solution that works in Outlook (tested) and other e-mail clients:
<td style="mso-line-height-rule:exactly;line-height:300px;"> </td>
This is cleaner than using an image, which could negatively affect your spam score, and does the exact same thing.
If you have other content in the <td> that you don't want to have that line height, you can just wrap the non-breaking space in a <span> and set the line-height on that tag:
<td><span style="mso-line-height-rule:exactly;line-height:300px"> </span>**Other content without 300px line-height here**</td>
The reason height or min-height works on <div> tags and not <td> is because <td> are set to display:table-cell and do not respect height the same way that display:block (<div>) elements do.
I have resolved this issue by adding display:block; to its style as
<td style="display:block; min-height:200px;">
min-height does not work in td, Set height that will work like min-height and automatic increase height if needed. That is worked for me
Here is a solution that does not depend on the height in pixels. It works in all email clients:
<table cellspacing="0" cellpadding="0" border="0" style="width:415px">
<tbody>
<tr valign="top">
<td style="font-family:Arial;font-size:12px;line-height:14px;">
This is my text that I need in 2 lines
</td>
<td style="font-family:Arial;font-size:12px;line-height:14px;">
<br/><br/>
</td>
</tr>
<tr valign="top">
<td style="font-size:12px;line-height:14px">
Second Line
</td>
</tr>
</tbody>
The solution works by adding a zero-width column with two lines to the right of the first one. It uses the  character, which is a non-breaking zero-width space.
It may be reviving a 2012 post, for those who searched and found this post like me:
Note: Check these addresses for the email client support before using this method, at the time of writing this answer, the support was around 50% -ish.
E-mail client support range of :first-child
E-mail client support range of ::before
table tr:first-child td:before {
min-height: 100px;
display: block;
content: ""
}
<table border="1">
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
What I found !!!, In tables CSS td{height:60px;} works same as CSS td{height:60px;}