CSS Shrink label & td cell to accommodate text - html

I have a checkbox list that I need to make horizontal. The checkboxes and labels are all appearing on the same row but the spacing between them is very far apart, is there a way to shrink each cell to be only as large as the checkbox and label inside of it? JSFiddle here: http://jsfiddle.net/yrcZ2/
<table id="form_3D7" class="scfCheckBoxList">
<tbody>
<tr>
<td>
<input id="form_3D7" type="checkbox" value="One" name="form_3D7">
<label for="form_3D7">One</label>
</td>
<td>
<input id="form_3D8" type="checkbox" value="Two" name="form_3D8">
<label for="form_387">Two</label>
</td>
<td>
<input id="form_3D9" type="checkbox" value="Three" name="form_3D9">
<label for="form_3D9">Three</label>
</td>
</tr>
</tbody>
</table>
CSS:
.scfCheckBoxList {
margin: 0;
width: 100%;
}
tbody {
display: table-row-group;
vertical-align: middle;
}
tr {
display: table-row;
vertical-align: inherit;
}
.scfCheckBoxList td {
vertical-align: top;
}
.scfCheckBoxList input {
float: left;
width: 30px;
}
.scfCheckBoxList label {
clear: none;
font-weight: normal;
padding: 0;
width: 68%;
}
.scfForm input {
box-sizing: border-box;
}
I've inherited this code and trying to shrink the table cells to only be as large as the text inside of them. Any help greatly appreciated.

Related

make width of <td> equal to its text contents

I have below snippet where td takes up space unnecessarily. For e.g. if the text contents inside td take 20 pixels width, td should also be 20 pixels wide. Can you help me fix it?
tr {
width: 100%;
}
.td2{
width:auto;
max-width: 180px;
word-wrap: break-word;
background-color:red;
padding-left: 0;
padding-right: 0;
}
span {
background-color: #7e7;
}
<h3 style="width:600px;">The image in below strucure should be displayed immediately after text ends
but the td is eating up space unnecessarily
</h3>
<table>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span>Excessive exhaust materialssss</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span>Excessive</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
</table>
By default, table cells will expand in order to fit all their content and the width of the cell will depend on the widest cell in that column.
So trying to shrink-wrap a table-cell doesn't really make much sense.
If you do actually want a table-like layout, but you want a particular column to take up as little space as possible - you could set the cell width: 1px; - that will make the column as wide as the longest word in the cells of that column.
table {
table-layout: fixed;
}
tr {
width: 100%;
}
.td2{
width:auto;
max-width: 180px;
word-wrap: break-word;
background-color:red;
padding-left: 0;
padding-right: 0;
width: 1px; /* <--- */
}
span {
background-color: #7e7;
}
<h3 style="width:600px;">The image in below strucure should be displayed immediately after text ends
but the td is eating up space unnecessarily
</h3>
<table>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span>Excessive exhaust materialssss</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span>Excessive</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
</table>
Please replace this code with your old code.
I hop here you find your solution.
tr {
width: 100%;
display:table-row;
}
tr td
{
display:inline-block;
}
.td2{
width:auto;
word-wrap: break-word;
background-color:red;
padding-left: 0;
padding-right: 0;
max-width: 180px;
}
.text{
max-width: 180px;
font-size: 14px;
}
span {
background-color: #7e7;
}
<h3 style="width:600px;">The image in below strucure should be displayed immediately after text ends
but the td is eating up space unnecessarily
</h3>
<table>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span class="text">Excessive exhaust materialssss</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
<tr>
<td class="td1"><input type="checkbox" name="checkbox" value="value"></td>
<td class="td2"><span>Excessive</span></td>
<td class="td3"><img src="https://www.w3schools.com/html/smiley.gif"></td>
</tr>
</table>
try adding the following css code
tr {
width: 100%;
display:table-row;
}
tr td
{
display:inline-block;
}
here is a link for reference
hope this helps..

CSS style checkbox inside td

i would like to set the checkbox inside a table to middle / center
the dirty way i am using is
<td>
<input type="checkbox" style="text-align:center;" ng-model="x.dedbuffer">
</td>
However, i am looking a way that all the checbox inside the table will be centered. So i am come out with a css
.table td input[type="checkbox"] {
text-align: center;
}
but the css is not working as expected. how to style it in css?
Set it on the input's parent, in this case the td.
Updated showing how-to when targeting only specific cells
table td {
text-align: center;
}
table {
width: 300px;
}
table td {
border: 1px solid;
}
table td.centered {
text-align: center;
}
<table>
<tr>
<td class="centered">
<input type="checkbox" style="text-align:center;" ng-model="x.dedbuffer">
</td>
<td>
Not centered
</td>
</tr>
</table>
If you have more content in same cell, add a wrapper
table {
width: 300px;
}
table td {
border: 1px solid;
text-align: center;
}
table td div {
text-align: left;
}
<table>
<tr>
<td>
<input type="checkbox" style="text-align:center;" ng-model="x.dedbuffer">
<div>
Not centered
</div>
</td>
</tr>
</table>
try this
td input[type="checkbox"] {
width:20px;
height:20px;
display:block;
argin:0px auto;
}
function alerrt() {
alert("I am centered! checkbox");
}
table {
width: 300px;
}
table td {
min-width: 150px;
border: 1px solid;
text-align: center;
padding:5px;
}
table td lable{
margin-right:10px;
}
<table>
<tr>
<td class="centered">
<lable> please check </lable> <input type="checkbox" onclick="alerrt()" style="text-align:center;" ng-model="x.dedbuffer">
</td>
</tr>
</table>

Vertical-align not working on table cell containing button

The following code is self-explanatory but does not produce the desired effect:
.t5{margin: auto; width: 50%; font-size: 35px;}
.t5 td{border-style: solid; border-width: 1px; text-align: center; height:100px;}
.t5 td.r1{vertical-align: top;};
.t5 td.r2{vertical-align: middle;}
.t5 td.r3{vertical-align: bottom;}
<table class="t5">
<tr>
<td class="r1"><INPUT type = "button" value = " Does not align at top"></td>
<td class="r2"><INPUT type = "button" value = " Does not align in middle"></td>
<td class="r3">Large Text</td>
</tr>
</table>
I have tried using line-height to rectify the problem, but cannot get it to resolve the issue completely.
If font-size: 35px; is removed, vertical-align works properly.
The problem is that, when you increase font-size, browsers will also increase line-height's initial value:
normal: Tells user agents to set the used value to a "reasonable" value based on the font of the element. The value has the same meaning
as . We recommend a used value for 'normal' between 1.0
to 1.2.
That means that, with font-size: 35px, a line-height: normal will probably be between 35px and 42px.
Therefore, the line box which contains the button will be, at least, as tall as that value:
'line-height' specifies the minimal height of line boxes within the
element.
However, you button is not so tall. Therefore, the button is vertically aligned inside the line box according to button's vertical-align:
[vertical-align] affects the vertical positioning inside a line box of
the boxes generated by an inline-level element.
To fix that, you can
Apply the same vertical-align as the table cell to the button.
You can do this easily with
.t5 input[type=button] {
vertical-align: inherit;
}
.t5 {
font-size: 35px;
}
.t5 td {
border: 1px solid;
text-align: center;
height: 100px;
}
.t5 td.r1 {
vertical-align: top;
}
.t5 td.r2 {
vertical-align: middle;
}
.t5 td.r3 {
vertical-align: bottom;
}
.t5 input[type=button] {
vertical-align: inherit;
}
<table class="t5">
<tr>
<td class="r1">
<input type="button" value="Does not align at top" />
</td>
<td class="r2">
<input type="button" value="Does not align in middle" />
</td>
<td class="r3">Large Text</td>
</tr>
</table>
Reduce the line-height, so that th height of the line box will be as tall as the button.
.r1, .r2 {
line-height: 0;
}
.t5 {
font-size: 35px;
}
.t5 td {
border: 1px solid;
text-align: center;
height: 100px;
}
.t5 td.r1 {
vertical-align: top;
}
.t5 td.r2 {
vertical-align: middle;
}
.t5 td.r3 {
vertical-align: bottom;
}
.r1, .r2 {
line-height: 0;
}
<table class="t5">
<tr>
<td class="r1">
<input type="button" value="Does not align at top" />
</td>
<td class="r2">
<input type="button" value="Does not align in middle" />
</td>
<td class="r3">Large Text</td>
</tr>
</table>
Just set the line-height of your first 2 tds to 0 by adding td.r1, td.r2{line-height: 0;} to your CSS and it will work.
.t5{margin: auto; width: 50%; font-size: 35px;}
.t5 td{border-style: solid; border-width: 1px; text-align: center; height:100px;}
.t5 td.r1{vertical-align: top;}
.t5 td.r2{vertical-align: middle;}
.t5 td.r3{vertical-align: bottom;}
td.r1, td.r2{line-height: 0;}
<table class="t5">
<tr>
<td class="r1" valign="top"><INPUT type = "button" value = " Does not align at top"></td>
<td class="r2" valign="middle"><INPUT type = "button" value = " Does not align in middle"></td>
<td class="r3">Large Text</td>
</tr>
</table>

IE11 input width changes when clicked

So this only happens in IE11 and I cannot figure out what is going on. When I click the input element (basically the focus event) the width of the element changes. I cannot figure out the CSS to fix this. Anyone have any ideas?
http://jsfiddle.net/JmRby/2/
TABLE
<table class="fields" id="EntityDetails_Fields" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="Form">Name:</td>
<td>
<input name="Name" id="Name" type="text" value="Memorial Park Group" />
</td>
</tr>
<tr>
<td class="Form">NPI:</td>
<td>
<input name="NPI" class="TextBox" id="NPI" type="text" value="" />
</td>
</tr>
<tr>
<td class="Form">Tax ID:</td>
<td>
<input name="TaxIDNumber" id="TaxIDNumber" type="text" value="21-21212121" />
</td>
</tr>
<tr>
<td class="Form">Medicare:</td>
<td>
<input name="MedicareNumber" id="MedicareNumber" type="text" />
</td>
</tr>
<tr>
<td class="Form">Medicaid:</td>
<td>
<input name="MedicaidNumber" id="MedicaidNumber" type="text" value="1234567" />
</td>
</tr>
<tr>
<td class="Form">In Use:</td>
<td><span id="InUse"><input name="InUse$ctl00" id="InUse_ctl00" type="checkbox" checked="checked" /></span>
</td>
</tr>
</tbody>
CSS
table.fields {
width: 98%;
margin: 4px 1%;
}
table.fields .Form, table.fields .fl {
padding: 2px;
}
table.fields > tbody > tr > td {
padding: 2px 1%;
vertical-align: middle;
}
.Form, .form, .fl {
white-space: nowrap;
width: 10%;
vertical-align: middle;
}
table.fields > tbody > tr > td {
padding: 2px 1%;
vertical-align: middle;
}
table.fields input[type='text'], table.fields input[type='password'], table.fields select, .inpt {
width: 98%;
border: solid 1px #666;
color: #000;
box-sizing: border-box;
}
Yeah mate, here you go: http://jsfiddle.net/3TwKF/
input::-ms-clear {
display:none;
}
This is the culprit (ms-clear)'s documentation.
input::-ms-clear
{
display:none;
}
Changing the text in text box changes the width in IE 11 for following link too. Above solution works for clicking the text. Here is a jsfiddle:
http://jsfiddle.net/3TwKF/20/
Please let me know for any solution

input field width is not what I set it to be, misaligned. Additional border in input

http://jsfiddle.net/HnnHf/1/
Trying to understand what I do wrong. Plain table, I want input boxes to fill cells evenly. On first row you see 2 inputs and second row has one input spanned across cells.
Their right sides don't match. Why? When I run inspector it shows additional pixels?
Part of my HTML:
<div style="width: 1000px; margin-left: auto; margin-right: auto; padding-left: 20px; padding-top: 10px;">
<table>
<tr>
<td style="width: 80px;"><label>From </label></td>
<td style="width: 120px;">
<input type="text" class="fill-space" />
</td>
<td style="width: 80px;"><label>To </label></td>
<td style="width: 120px;">
<input type="text" class="fill-space" />
</td>
<td style="width: 80px;"><label>Sort by </label></td>
<td></td>
</tr>
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td></td>
<td colspan="3">
<input type="text" class="search" />
</td>
<td></td>
<td>
Refresh button
</td>
</tr>
</table>
</div>
Style:
td label {
width: 100%;
color: #F1F1F1;
text-align: right;
vertical-align: central;
}
input.fill-space {
width: 100%;
}
input.search {
width: 100%;
background-image: url("/images/Search.png");
background-position: right center;
background-repeat: no-repeat;
}
</style>
My live site misalignment:
Also, why do I get this another border inside input if I set background?
Working fiddle: http://jsfiddle.net/ghUEw/
Default padding and margins for table elements differ in different browsers.
So you'd better use a CSS reset on table elements.
table * {
margin: 0;
padding: 0;
}
Then, comes the border-collapse property. It determines whether the table borders are collapsed into a single border or rendered individually, let's say for neighboring table cells. You need to set it as following to make them collapsed since you have different number of cells per table row.
table {
border-collapse: collapse;
}
Then, you need to set the borders of the inputs in your table if you want them look the same.
table input {
border: 1px solid #aaa;
}
If you don't want any borders to appear, replace it with border: none;
Then, in your CSS, for the labels to appear the way you want, you can apply float:right; (also corrected vertical-align: middle;)
td label {
width: 100%;
color: #F1F1F1;
text-align: right;
vertical-align: middle;
float:right;
}