CSS table padding - css

I'm learning CSS and HTML.
In my code I have:
<style>
table, td, th
{
padding: 5px;
}
</style>
This rule works on all tables on the page.
Now I want to make a table without padding:
Here is the source:
<table>
<tr>
<td>Login</td>
<td><input type="text" name="login" class="input"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="input"></tr>
</tr>
</table>
How to do this?

If you want to add specific styles to this table that override the default styles you've defined, then you'll need some way to reference it in CSS.
Typically, you would give it a class or an ID -- eg <table class='myspecialtable'>....</table>
Then you can have a stylesheet which overrides your default 5px styles, just for this table.
.myspecialtable, .myspecialtable td, .myspecialtable th {
padding: 0px;
}
If you can't add an ID or class to this table, then you could add it on a parent element, and the effect would be the same (as long as that parent doesn't contain any other tables, of course). In this case, your CSS would look something like this:
.myspecialtablecontainer table, .myspecialtablecontainer td, .myspecialtablecontainer th {
padding: 0px;
}

You should change your CSS to define a style instead.
.padded { ... }
Then you can set the class to that style for any tables you want to use that style.
<table class="padded">
</table>
When you set a style, as you have done, for all elements of a particular type, then the only way to remove them is to set the style to something else, or not include a reference to that CSS file from the page that you don't want to use them.

One way would be to give your table a class like so:
<table class="nopadding">
[... table rows and columns...]
</table>
And then put this in your css:
.nopadding, .nopadding td, .nopadding th
{
padding: 0;
}
Which says "any element with the class should have a padding of 0". The .nopadding th and .nopadding td has to be there and is a way of saying "all th and td who is inside an element of class nopadding shouldn't have any padding either", since you previously told all th and td to have a padding of 5px.

I remeber when I first started learning HTML.
What you're after is an id or a class attribute. You'd have two tables like so:
<table class="table1">
<tr>
<td>Login</td>
<td><input type="text" name="login" class="input"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="input"></tr>
</tr>
</table>
<table class="table2">
<tr>
<td>Login</td>
<td><input type="text" name="login" class="input"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="input"></tr>
</tr>
</table>
To make is so that table1 had padding, but table2 didn't, you would use the appropriate CSS rules to identify and style the tables:
.table1, .table1 td, .table1 th
{
padding: 5px;
}
.table2, .table2 td, .table2 th
{
padding: 0px;
}
There's many ways I could have done this with CSS. For example, You could also use ids in this case, but it's easier to use classes as an id can only be used once per document.
A slightly better approach in this case would be to take advantage of cascading rules. I could have kept your original CSS and just added the second set of rules:
.table, .table td, .table th
{
padding: 5px;
}
.table2, .table2 td, .table2 th
{
padding: 0px;
}
In this case, only tables with the class table2 would have the 0px padding - all other tables would have 5px padding.
It would be a good idea to read the W3CSchools introduction to CSS - http://www.w3schools.com/css/css_intro.asp. This will introduce you to the basics and get you on your way.

Simple use
<style>
table, td, th
{
padding: 0px;
}
</style>
Also try border-collapse: collapse;.

<table cellpadding='0'> If this don't work, you can create a css class that removes the padding and use that in your table: <table class='no_padding'>

Related

Get label under a row use the full width of the table

I am using jqxValidator to validate a group of radio buttons. When the red message (which is a label) appears under the row, it inherits the width of the first column, breaking the design:
I tried many things, finally changing the display of the label fixes the table layout, but the message get splitted in 2 lines.
Is there a way to extend the width of the label to use the whole table width?
Example: https://codepen.io/lhernand/pen/OEZbXb
HTML:
<table style="border:0; border-collapse:collapse; " width="100%">
<tbody>
<tr class="rowRequired jqx-validator-error-element" id="optionsID0EYE">
<td style="border:0; width:50%; "><input id="ID0EYE" name="Patient-MainGP" value="true" type="radio"><span> Yes</span></td>
<td style="border:0; width:50%; "><input style="margin-left:20px; " id="ID0EYE" name="Patient-MainGP" value="false" type="radio"><span> No</span></td>
</tr><label class="jqx-validator-error-label" style="position: relative; left: 0px; width: 304.688px; top: 2px;">Please choose one option!</label>
</tbody>
</table>
Note: it should work in IE11
Working with this kind of fixed structure requires some mayor hacking,
since jquery validator inserts a label after a td on the table, the element behave like a td with no match on the same row.
Using the chrome dev tools option to copy the selector on the inspector, I can get the most specific selector posible for the html structure you have, so aplying a 200% size to this element, tricks the table to not missbehave
#tablePatientDetailsContainer > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(2) > table > tbody > label{
width: 200% !important;
box-sizing: border-box;
}
Tested on Chrome, FF and Safari
My Working Code
https://codepen.io/Teobis/full/vrjZVR/
Hope this helps
You placed the id optionsID0EYE in the wrong place. jqxValidator will create a sibling for the element who has this id. You need to do something like so:
<tbody>
<tr class="rowRequired">
<div id="optionsID0EYE" style="display: flex; flex-wrap: wrap">
<div style="width: 50%">
<input id="ID0EYE" name="Patient-MainGP" value="true" type="radio"><span> Yes</span>
</div>
<div style="width: 50%">
<input style="margin-left:20px; " id="ID0EYE" name="Patient-MainGP" value="false" type="radio"><span> No</span>
</div>
</div>
</tr>
</tbody>
I moved the id optionsID0EYE to a new div I made.
Working example: https://codepen.io/anon/pen/YvLZeB?editors=1000

How to customize table size in github pages and sass template

I have a github pages website that is not rendering its tables at the desired size.
For example in this post a table is rendered normally as:
<table>
<thead>
<tr>
<th>Team</th>
<th>Natural Alignment</th>
<th>Natural Misalignment</th>
</tr>
</thead>
<tbody>
<tr>
<td>Development</td>
<td>Faster Delivery of features</td>
<td>Have to be engaged in operations, more “work” to do</td>
</tr>
<tr>
<td>Operations</td>
<td>Less fires, more consistency</td>
<td>Have to learn a new skillset and be a beginner</td>
</tr>
<tr>
<td>Security</td>
<td>More consistency, compliance</td>
<td>Automation can cause unknown vulnerabilities</td>
</tr>
<tr>
<td>Business</td>
<td>Faster ROI for development, lower cost for operations, and a scale model that works</td>
<td>Takes ongoing investment in culture and tools</td>
</tr>
</tbody>
</table>
But it shows with a very small font. My sass is:
/**
* Tables
*
*/
table, th, td {
border: 1px solid black
}
table {
width: 100%
}
th, td {
height: 50px;
font-size: 14px
}
tr:hover {
background-color: #f5f5f5
}
Here is the github repository with the jekyll site on it. Any idea how I can control the table size?
if you remove .table from your main css and replace it with table
table is intrinsic to html so to reference a body tag it's just body and not .body which is a class edit line 966
table {background-color: transparent;font-size: 3em;}

Skip first-child selector's style for specific element

Lets say we have generic css class for table to use on all places in our application. Lets consider also this style has first-child selector.
tr td:first-child { ... }
I would like to use this class for an specific table but skip all first-child styles.
Since this is generic style class for tables i cant remove from it and also dont want to handle this with inline styling.
Thanks
You could be more specific for that particular table, assuming it has a class or an id you can use:
tr td:first-child {
color: red;
}
.test tr td:first-child {
color: green;
}
<table>
<tr>
<td>First td</td>
<td>Second td</td>
</tr>
</table>
<table class="test">
<tr>
<td>First td</td>
<td>Second td</td>
</tr>
</table>
Excuse the limited markup, but you get the point. Because the .test tr td:first-child {} class is more specific than the generic style, it overrides it, but only for the table with the class test.
More info on specifity: https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/

CSS performance- descendant selector vs. class selector

I appending table dynamically using javascript in html with say 50000 cells.
<table id="dataTable">
<tbody>
<tr>
<td>data1</td>
<td>data2</td>
.....
<td>data1</td>
</tr>
..........
<tr>
<td>data1</td>
<td>data2</td>
.....
<td>data1</td>
</tr>
</tbody>
</table>
I'm styling the td with descendant selector as,
#dataTable td{
text-align:right;
border:1px solid #adadad;
padding-right:10px;
}
Another option is to give class to each td using class selector.
<table id="dataTable">
<tbody>
<tr>
<td class="format">data1</td>
<td class="format">data2</td>
.....
<td class="format">data1</td>
</tr>
..........
<tr>
<td class="format">data1</td>
<td class="format">data2</td>
.....
<td class="format">data1</td>
</tr>
</tbody>
</table>
Here we have used format class for styling.
.format{
text-align:right;
border:1px solid #adadad;
padding-right:10px;
}
I'm facing performance issue while rendering the table in browser. Is this because I've used DESCENDANT SELECTOR INSTEAD OF CLASS SELECTOR.
Or Browser is not able handle large data.
About the table performance
Tables can be slow to render mostly due to the dynamic column sizes that need to be calculated and set on every change.
You can solve this by specifying a fixed size for each column, like so:
#dataTable td {
width: 100px; /* Set sizes appropriately */
}
This should make your table more performant
About CSS performance
CSS selects by the last token first, so for example, to execute the following selector:
#dataTable td
CSS will first select ALL td elements and then check if each of them is a descendant of #dataTable. Technically, specifying a class for each cell is faster.
However, this is probably not significant enough to pay for by complicating your overall design.
I recommend reading Efficiently Rendering CSS by CSS-Tricks to get a better idea about CSS and performance.

Arranging elements within generated table

The selectOneRadio element in JSF is translated to a table, where the radio button and its label are put within the same <td> in a table.
<!-- JSF Element -->
<h:selectOneRadio id="types" label="Type"
value="#{bean.selectedType}"
layout="pageDirection">
<f:selectItems value="#{bean.types}"/>
</h:selectOneRadio>
<!-- Generated HTML -->
<table id="j_id_i:types">
<tbody>
<tr>
<td>
<input id="j_id_i:types:0" type="radio" value="VALUE1"
name="j_id_i:types"/>
<label for="j_id_i:types:0"> Value #1</label>
</td>
</tr>
<tr>...</tr>
...
</tbody>
</table>
Before I was using Bootstrap, the elements within the <td> would appear side by side, but now look under each other.
The processed CSS for the element is the following, as given by Firebug.
table {
border-collapse: collapse;
border-spacing: 0;
}
body {
color: #333333;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
line-height: 20px;
}
html {
font-size: 100%;
}
I have no clue what may be producing such behaviour. It's not a concern of width, as this is the single element within the <div>, and without bootstrap it is rendering side by side.
That's because the <label> has due to the Bootstrap CSS become a HTML block element which starts naturally at a new line.
You need to make it a HTML inline element again. So, you need to override the Bootstrap CSS accordingly. Perhaps you want to apply this for labels in table cells only. E.g.
td label {
display: inline;
}

Resources