Checkbox causes incorrect height on parent element - css

I am trying so solve a problem that causes me and my colleagues a splitting headache. Even though we've been provided a painkiller each we continue to be baffled by the way Google Chrome renders a checkbox in a container that has a font-size of 10pt.
The problem
Google Chrome seems to render some extra space around a checkbox element that I cannot seem to get rid of. The checkbox and its parent element must be 15 pixels tall. Setting the font-size of the parent element to 10pt causes that parent element to be 16 pixels in height.
An example
Take a look at the snippet below.
I have yet to find out (or understand) what causes the checkbox element to render as 15 pixels tall but effectively making its parent element 16 pixels tall.
td,
th
{
padding: 0;
font-size: 10pt;
}
input[type=checkbox]
{
height: 15px;
border: none;
padding: 0;
margin: 0;
}
<table width="100%" class="data">
<tr>
<th>Label 1</th>
<td>Value 1</td>
</tr>
<tr>
<th>Checkbox 2</th>
<td><input type="checkbox" disabled="disabled" /></td>
</tr>
<tr>
<th>Label 3</th>
<td><input type="checkbox" disabled="disabled" checked="checked" /></td>
</tr>
</table>
Exchanging the table for a div, a p or any other container also results in an incorrect height for that container.
Possible solution?
Setting the checkbox height to 14px makes the parent row 15 pixels tall. But then the checkbox itself does not meet the designer's requirements and thus this solution will not be accepted. Any workarounds?

input has default font property:
font: 13.3333px Arial
If you ensure that both the input and text in adjacent cells are the same font it should remove differences in line-height (I think).
Also, add display: block to input
td,
th {
padding: 0;
font-size: 10pt;
font-family: sans-serif;
}
input[type=checkbox] {
height: 15px;
border: none;
padding: 0;
margin: 0;
display: block;
}
<table width="100%" class="data">
<tr>
<th>Label 1</th>
<td>Value 1</td>
</tr>
<tr>
<th>Checkbox 2</th>
<td><input type="checkbox" disabled="disabled" /></td>
</tr>
<tr>
<th>Label 3</th>
<td><input type="checkbox" disabled="disabled" checked="checked" /></td>
</tr>
</table>

Another solution:
First, add display: block; to the input.
You can change the parent behaviour with the box-sizing property.
td {
box-sizing: content-box;
}
should fix your problem.
td,
th {
padding: 0;
font-size: 10pt;
}
tr {
box-sizing: content-box;
}
input[type=checkbox]{
display: block;
height: 15px;
border: none;
padding: 0;
margin: 0;
}
<table width="100%" class="data">
<tr>
<th>Label 1</th>
<td>Value 1</td>
</tr>
<tr>
<th>Checkbox 2</th>
<td><input type="checkbox" disabled="disabled" /></td>
</tr>
<tr>
<th>Label 3</th>
<td><input type="checkbox" disabled="disabled" checked="checked" /></td>
</tr>
</table>

I also had this problem in 2020, on Chrome and Firefox, and none of the solutions here worked. This is what worked for me
input[type=checkbox] {
height: 21px;
vertical-align: top;
position:relative;
top:-5px;
}
.checkboxcontainer{
max-height: 16px;
}
In my case we had a structure
<div>
<div data-x="lots of these" />
<div data-x="lots of these" />
<div data-x="lots of these" />
<div>
<div class="checkboxcontainer">
<input type="checkbox" ...>
And the outer div was being given a huge top margin. Deleting the checkbox in f12 confirms that the checkbox was the culprit, and the css provided seemed to work ok

Related

How to make the element next to the radio button list

Hi I have a asp radio button list. I need to put the drop down next to the radio button; all of them on the same row. Currently it is on the next row. Would someone help me how to fix it.
.post40
{
font-size: 11px;
text-align: right;
font-weight: bold;
padding: 2px 5px 2px 3px;
width: 40%;
}
<table id="rdList" class="rdList" border="0">
<tbody>
<tr>
<td class="post40">Attend:</td>
<td>
<input id="rdList" type="radio" name="rdList" value="1" checked="checked">
<label for="rdList_0">Yes</label>
</td>
<td>
<input id="rdList_1" type="radio" name="rdList" value="0">
<label for="rdList_1">No</label>
</td>
</tr>
</tbody>
</table>
<select name="dropNo" id="dropNo" style="position:relative; right:20px;"></select>
The <table> is a block element, so it's taking up the full width of the parent. You can set the <table> to be inline-block so it doesn't fill the width of the parent.
Once you do that, the position: relative logic of the <select> element doesn't line up. But replacing right: 20px with top: -10px seems to line it up:
.post40
{
font-size: 11px;
text-align: right;
font-weight: bold;
padding: 2px 5px 2px 3px;
width: 40%;
}
#rdList
{
display: inline-block;
}
#dropNo
{
position: relative;
top: -10px;
}
<table id="rdList" class="rdList" border="0">
<tbody>
<tr>
<td class="post40">Attend:</td>
<td>
<input id="rdList" type="radio" name="rdList" value="1" checked="checked">
<label for="rdList_0">Yes</label>
</td>
<td>
<input id="rdList_1" type="radio" name="rdList" value="0">
<label for="rdList_1">No</label>
</td>
</tr>
</tbody>
</table>
<select name="dropNo" id="dropNo"></select>
For reference, an indispensable tool for debugging your CSS is the browser's element inspector. At least in Chrome, hovering over any given inspected element highlights the styling "box" in the rendered page. And selecting the element shows you the specific styling rules applied to it, which ones over-rule which others, and how the overall styling is computed. This can help you find the actual size of your element and what styling rules are making that happen. You can also make tweaks to those styling rules directly in the inspector to test them out without modifying the underlying code.

safari browser showing table contents vertical but google chrome is showing horizontal . I want to safari to show table contents horizantal

Want to show table contents(text / hyperlink/ div contents) horizontal in safari browser but dont know how to fix it as in google chrome table contents are absolutely showing fine as horizontal
enter code here
<div class="itemFields">
<table>
<tbody><tr>
<td class="label" dir="ltr">Call Number</td>
<td><div>PS169.F7 H3 1961</div></td>
</tr><tr>
<td class="label" dir="ltr">Publisher</td>
<td><div><span dir="ltr">F. Ungar Pub. Co.</span></div></td>
</tr><tr>
<td class="label" dir="ltr">Year</td>
<td><div><span dir="ltr">1961</span></div></td>
</tr>
</tbody></table>
</div>
div.itemFields {
margin: 0;
margin-right: 15em;
}
.itemFields table tr td {
vertical-align: top;
}
.itemFields table tr td {
padding: 0;
}
.itemFields table tr td.label {
padding-right: 1em;
font-weight: bold;
white-space: nowrap;
}
.itemFields table tr td div {
display: table;
table-layout: fixed;
width: 100%;
border-collapse: collapse;
word-wrap: break-word;
}
.itemFields table tr td div a,
.itemFields table tr td div span {
display: inline;
}
this itemFields class content is showing contents of it vertical in safari browser
Try This Code ! You have to need reset code also better if you use bootstrap............. This code just simple HTML5 Table format & Style. You can change table width 960px & more..........
<div class="itemFields">
<table width="960px" align="center" border="1">
<tbody>
<tr>
<td class="label" dir="ltr">Call Number</td>
<td>
<div>PS169.F7 H3 1961</div>
</td>
</tr>
<tr>
<td class="label" dir="ltr">Publisher</td>
<td>
<div><span dir="ltr">F. Ungar Pub. Co.</span></div>
</td>
</tr>
<tr>
<td class="label" dir="ltr">Year</td>
<td>
<div><span dir="ltr">1961</span></div>
</td>
</tr>
</tbody>
</table>
</div>

How do I resize the td of a table styled with bootstrap?

I read http://getbootstrap.com/css/#tables which didn't mention resizing tds in any specific way, so I tried the following css:
.cbCell {
width: 25px;
}
.smallerCell {
width: 240px;
}
.textfield {
width: 230px;
}
with this html
<thead>
<tr>
<th class="cbCell"><input type='checkbox' class='form-control' id='selectall'></th>
<th class="text-center smallerCell">Employee Name</th>
<th class="text-center smallerCell">Mail</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td class="cbCell"><input type='checkbox' class='form-control case'></td>
<td class="smallerCell"><input type="text" name='name0' placeholder='Name' class="form-control textfield"/></td>
<td class="smallerCell"><input type="text" name='mail0' placeholder='Mail' class="form-control textfield"/></td>
</tr>
<tr id='addr1'>
</tr>
</tbody>
The textfields have been resized but the td has not, the 'smallerCell' style isn't applied
I tried Change column width bootstrap tables which didn't work for me. I used the grid system to float the establishment info to the right, I ultimately want the name and mail columns to fit the smaller textfield size, and for there to be a margin and vertical rule dividing the People and Establishment columns.
Change table width to auto. In boot strap table width is set to 100%
.table {
width: 100%;
margin-bottom: 20px;
}
so make it
.table {
width: auto;
}
in you css.

font-size changes td height if containing an input

having trouble with font-size ; demo: http://jsfiddle.net/9QMky/2/
body{
font-size: 20px;
}
input, div{
height: 10px;
width: 10px;
}
Reading this CSS, I hoped to see the very same dimensions for both input and div in this HTML:
<table>
<tr>
<td>
<input type="button"/>
</td>
</tr>
<tr>
<td>
<div></div>
</td>
</tr>
</table>
But they do not: the td containing the input is greater.
font-size seems to be a part of the problem, using property
display: flex;
on the td seems to work. Why?
Note: have played only on Chrome.

Cols, colgroups and css ":hover" pseudoclass

I'm trying to create a table to display an individual's BMI.
As a part of this, I'd like, on :hover, for the <tr> and <col> (or <colgroup>) to be highlighted also, in order for the intersection to be more apparent.
As the table will feature both metric and imperial measurements, the :hover doesn't have to stop at the cell (from any direction) and would, in fact, be a bonus if it extended from one axis to the other. I'm also using the XHTML 1.1 Strict doctype, if this makes a difference?
So... an example (the real table's... larger), but this should be representative:
<script>
tr:hover {background-color: #ffa; }
colgroup:hover,
col:hover {background-color: #ffa; }
</script>
...
<table>
<col class="weight"></col><colgroup span="3"><col class="bmi"></col></colgroup>
<tr>
<th></th>
<th>50kg</th>
<th>55kg</th>
<th>60kg</th>
</tr>
<tr>
<td>160cm</td>
<td>20</td>
<td>21</td>
<td>23</td>
</tr>
<tr>
<td>165cm</td>
<td>18</td>
<td>20</td>
<td>22</td>
</tr>
<tr>
<td>170cm</td>
<td>17</td>
<td>19</td>
<td>21</td>
</tr>
</table>
Am I asking the impossible, do I need to go JQuery-wards?
Here's a pure CSS method using no JavaScript.
I used ::before and ::after pseudo-elements to do the row and column highlighting. z-index keeps the highlighting below the <td>s in case you need to handle click events. position: absolute allows them to leave the confines of the <td>. overflow: hidden on the <table> hides the highlight overflow.
It wasn't necessary, but I also made it select just the row or column when you're in the headers. The .row and .col classes take care of this. If you wish the simplify, you can remove them.
This works in all modern browsers (and degrades gracefully on older browsers by doing nothing).
Demo: http://jsfiddle.net/ThinkingStiff/rUhCa/
Output:
CSS:
table {
border-spacing: 0;
border-collapse: collapse;
overflow: hidden;
z-index: 1;
}
td, th, .row, .col {
cursor: pointer;
padding: 10px;
position: relative;
}
td:hover::before,
.row:hover::before {
background-color: #ffa;
content: '\00a0';
height: 100%;
left: -5000px;
position: absolute;
top: 0;
width: 10000px;
z-index: -1;
}
td:hover::after,
.col:hover::after {
background-color: #ffa;
content: '\00a0';
height: 10000px;
left: 0;
position: absolute;
top: -5000px;
width: 100%;
z-index: -1;
}
HTML:
<table>
<tr>
<th></th>
<th class="col">50kg</th>
<th class="col">55kg</th>
<th class="col">60kg</th>
<th class="col">65kg</th>
<th class="col">70kg</th>
</tr>
<tr>
<th class="row">160cm</th>
<td>20</td><td>21</td><td>23</td><td>25</td><td>27</td>
</tr>
<tr>
<th class="row">165cm</th>
<td>18</td><td>20</td><td>22</td><td>24</td><td>26</td>
</tr>
<tr>
<th class="row">170cm</th>
<td>17</td><td>19</td><td>21</td><td>23</td><td>25</td>
</tr>
<tr>
<th class="row">175cm</th>
<td>16</td><td>18</td><td>20</td><td>22</td><td>24</td>
</tr>
</table>
There is a very decent jQuery plugin I've come across located here which does a very good job of this kind of thing with loads of examples. Preferentially I'd use that.
AFAIK CSS Hovers on TR's aren't supported in IE anyway, so at best the TR part of that will only work in Firefox.
Never even seen a :hover work on a col/colgroup so not sure if that's possible...
Think you might be stuck with a Javascript implementation.
Theres an example here that works (rows & cols) in Firefox
but again its broken in IE... cols don't work.
Live answer (https://jsfiddle.net/craig1123/d7105gLf/)
There are already CSS and JQuery answers; however, I have written a simple pure javascript answer.
I first find all the col and td tags, get the column index of each cell by doing element.cellIndex, and then add a CSS class with a background on mouseenter and removing it on mouseleave.
HTML
<table id='table'>
<col />
<col />
<col />
<col />
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Birthdate</th>
<th>Preferred Hat Style</th>
</tr>
</thead>
<tbody>
<tr>
<td>Abraham Lincoln</td>
<td>204</td>
<td>February 12</td>
<td>Stovepipe</td>
</tr>
<tr>
<td>Winston Churchill</td>
<td>139</td>
<td>November 30</td>
<td>Homburg</td>
</tr>
<tr>
<td>Rob Glazebrook</td>
<td>32</td>
<td>August 6</td>
<td>Flat Cap</td>
</tr>
</tbody>
</table>
CSS
body {
font: 16px/1.5 Helvetica, Arial, sans-serif;
}
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
}
table th {
text-align: left;
}
table tr, table col {
transition: all .3s;
}
table tbody tr:hover {
background-color: rgba(0, 140, 203, 0.2);
}
table col.hover {
background-color: rgba(0, 140, 203, 0.2);
}
tr, col {
transition: all .3s;
}
tbody tr:hover {
background-color: rgba(0,140,203,.2);
}
col.hover {
background-color: rgba(0,140,203,.2);
}
JS
const col = table.getElementsByTagName('col');
const td = document.getElementsByTagName('td');
const columnEnter = (i) => col[i].classList.add('hover');
const columnLeave = (i) => col[i].classList.remove('hover');
for (const cell of td) {
const index = cell.cellIndex;
cell.addEventListener('mouseenter', columnEnter.bind(this, index));
cell.addEventListener('mouseleave', columnLeave.bind(this, index));
}
Here is a fiddle
I came accross this neat way of doing it its from css-tricks.com I also prepared a fiddle whilst messing around with it nothing fancy but u can get the idea its with the same code provided by that css-trick page
//The Html
<table>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<colgroup></colgroup>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
//The Js
$(function(){
$("table").delegate('td','mouseover mouseleave', function(e) {
if (e.type == 'mouseover') {
$(this).parent().addClass("hover");
$("colgroup").eq($(this).index()).addClass("hover");
}
else {
$(this).parent().removeClass("hover");
$("colgroup").eq($(this).index()).removeClass("hover");
}
});
})
Check out the fiddle here

Resources