Center td inside a fixed width div - html

I have a fixed width div, and i need to center the two td in the middle.
here's my fiddle: http://codepen.io/anon/pen/YyrOZm
html:
<div class="managed-form">
<table>
<tr>
<td>
<label title="Company">
Company
</label>
<input size="30" type="text">
</td>
<td>
<label title="Title">
Title
</label>
<input class="" name="Title" size="30" type="text" value="">
</td>
</tr>
<table>
</div>
CSS:
.managed-form{
background-color: #5B8F22;
width: 311px;
height: 369px;
}

Make you inputs width 100%. Like this:
input[type="text"]{
width:100%;
}
CodePen

I added border-box to all elements (which may not be something you want to apply universally on the whole page, it will affect padding spacing etc.)
But, it does seem to center the items better:
CodePen
CSS:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
.managed-form{background-color: #5B8F22;
width: 311px;
height: 369px;}
input[type="text"]{
width:100%;
}
Further Reading:
Box-Sizing

I would do it like this
HTML:
<div class="managed-form">
<table>
<tr>
<td>
<label for="company">
Company:<br>
<input type="text">
</label>
</td>
</tr>
<tr>
<td>
<label for="title">
Title:<br>
<input tyoe="text">
</td>
</tr>
</table>
CSS:
.managed-form
{
background-color:red;
width: 311px;
height: 369px;
}
table {
margin-left:auto;
margin-right:auto;
}

Related

How do I make a td element the same width as the input element within?

I have a table consisting of a lot of input elements inside td elements.
<table>
<tbody>
<td class="first">
<div>
<input type="text" />
</div>
</td>
</tbody>
</table>
The width of the input elements should be 20px. The width of the table is set to 100%. The input element in the first td should only have a min-width set so it will strech out (since the table has 100% width). I want the other inputs to have a fixed width (20px). When I do this the td elements has a much larger width than the input elements:
https://jsfiddle.net/cbzuwyrg/
Is it possible to make the td elements the same width as the input elements without setting the same width in css?
This fiddle shows what I want to achieve, but without having to set the width of the td to 20px:
https://jsfiddle.net/cbzuwyrg/1/
I guess this will work with you too .
Edit : i edited the code so all tds be the same size as the input .
table {
width:100%;
border-collapse: collapse;
}
td.first{
width:auto;
border: 1px solid #000;
}
input[type="text"] {
width: 100%;
box-sizing: border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing: border-box;
}
<table>
<tbody>
<td class="first">
<div>
<input type="text" size="2">
</div>
</td>
<td class="first">
<div>
<input type="text" size="7">
</div>
</td>
<td class="first">
<div>
<input type="text" size="">
</div>
</td>
<td class="first">
<div>
<input type="text" placeholder="last" size="" >
</div>
</td>
</tbody>
</table>
The code is html and CSS no JavaScript were used .
<table>
<tbody>
<td class="first">
<div>
<input type="text" />
</div>
</td>
<td>
<div style="width:8px">
<input type="text" />
</div>
</td>
<td>
<div style="width:8px">
<input type="text" />
</div>
</td>
<td>
<div style="width:8px">
<input type="text" />
</div>
</td>
</tbody>
</table>
Add the width size to your div's
I think that the scss you are writing can be more useful if you nest it properly, this works fine just adding a calc and nesting the scss code. I hope it can helps.
In this case the td's with 20px of width use a class that stablish that width, and the inputs have a calc because when you use 100% width they use the whole space until next input.
Sass:
table {
width:100%;
border-collapse: collapse;
td {
border: 1px solid #000;
//display: inline-block;
width: 100%;
div {
input {
width: calc(100% - 4px) ;
&.td20 {
width: 20px;
}
}
}
}
}
HTML:
<table>
<tbody>
<td class="first">
<div>
<input type="text" />
</div>
</td>
<td>
<div>
<input type="text" class="td20"/>
</div>
</td>
<td>
<div>
<input type="text" class="td20" />
</div>
</td>
<td>
<div>
<input type="text" class="td20" />
</div>
</td>
</tbody>
</table>
Here you can see it working: https://jsfiddle.net/javierdp/adtj2t3o/8/

Table Layout to CSS Layout how to work around colspan?

If I create a layout with tables like the old-school way:
input {
width: 100%;
padding: 5px;
}
table {
border-collapse: collapse;
padding: 0;
margin: 0;
width: 50%;
}
td {
padding: 5px;
}
<table>
<tr>
<td colspan="3"><label>Label 1:</label></td>
</tr>
<tr>
<td colspan="3"><input type="text" /></td>
</tr>
<tr>
<td><label>Label 2:</label></td>
<td> </td><td>
<label>Label 3:</label></td>
</tr>
<tr>
<td><input type="text" /></td>
<td> </td>
<td><input type="text" /></td>
</tr>
<tr>
<td colspan="3"><label>Label 4:</label></td>
</tr>
<tr>
<td colspan="3"><input type="text" /></td>
</tr>
<tr>
<td colspan="3"><label>Label 5:</label></td>
</tr>
<tr>
<td colspan="3"><input type="text" /></td>
</tr>
</table>
https://jsfiddle.net/njb69anL/
I get a layout where it is like a grid on the screen. It resizes to the width of the browser easily. Everything is spaced and placed correctly.
However, my html markup is full of table tags. If I wanted to rid myself of the table I'd start by making the markup semantic:
<div id="grp">
<label>Label 1:</label><input type="text" />
<label>Label 2:</label><input type="text" />
<label>Label 3:</label><input type="text" />
<label>Label 4:</label><input type="text" />
<label>Label 5:</label><input type="text" />
</div>
But is it even possible to achieve the same table-like layout with this little markup? It seems display: table cannot do colspan, and the trickiest part is having a row with two label,input pairs on the same row. Is it possible to achieve this without adding a whole bunch of wrapper divs and thus making the original markup messy (non-semantic) anyways?
* {
box-sizing: border-box;
}
.labels-wrapper {
display: flex;
flex-wrap: wrap;
width: 40%;
}
label {
flex-basis: 100%;
padding: 10px;
}
label:nth-child(2),
label:nth-child(3) {
flex-basis: 50%;
}
input {
margin-top: 5px;
display: block;
width: 100%;
}
<div class="labels-wrapper">
<label>Label 1:
<input type="text" />
</label>
<label>Label 2:
<input type="text" />
</label>
<label>Label 3:
<input type="text" />
</label>
<label>Label 4:
<input type="text" />
</label>
<label>Label 5:
<input type="text" />
</label>
</div>
Adding flexbox example for an alternative. Please use the "full page" button to really get a feel for how it looks.
The only way to get it looking right using the html above is with position:absolute which to me feels like a hack. I have wrapped label 2 and label 3 in a div so they may be grouped together.
The below html / css gets very close to the fiddle:
#grp {
width: 50%;
white-space:nowrap;
}
#grp label,
#grp input {
width:100%;
display:block;
clear:left;
}
#grp input {
margin-bottom:1em;
}
#grp div {
float:left;
width:49%;
}
#grp div + div {
margin-left:2%;
}
<div id="grp">
<label>Label 1:</label><input type="text" />
<div><label>Label 2:</label><input type="text" /></div>
<div><label>Label 3:</label><input type="text" /></div>
<label>Label 4:</label><input type="text" />
<label>Label 5:</label><input type="text" />
</div>
This is definitely possible. I have created a fiddle for you. It's about the same amount of content, but it is definitely more easy to manipulate than a table.
I just use:
<wrapper>
<row>
<label></label>
<input>
</row>
</wrapper>
https://jsfiddle.net/Kiaaanabal/qv89yb56/
You would use css float or inline-block and width percentage to specify the amount of space each row element consumes.
EDITED CSS Version
Example: https://jsfiddle.net/njb69anL/3/
In order for this example to work, the label and input positions would need to be next to each other for them to appear on the same row. The end result is your markup looks like the display. Using nth-child allows you to specify which element in the list to apply your rule to.
HTML
<div id="grp">
<label>Label 1:</label>
<input type="text" />
<label>Label 2:</label><label>Label 3:</label>
<input type="text" /><input type="text" />
<label>Label 4:</label>
<input type="text" />
<label>Label 5:</label>
<input type="text" />
</div>
CSS
*{
box-sizing: border-box;
}
#grp {
width: 50%;
}
input,
label {
display: block;
float: left;
width: 100%;
padding: 5px;
}
label:nth-child(3),
label:nth-child(4){
width: 50%;
}
input:nth-child(5) {
width: 49%;
}
input:nth-child(6) {
width: 49%;
margin-left: 2%;
}
For example: https://jsfiddle.net/njb69anL/2/
updated with a responsive layout
HTML
<form>
<div class="full-row">
<label>
<span>Label 1:</span>
<input type="text" />
</label>
</div>
<div class="half-row">
<label>
<span>Label 2:</span>
<input type="text" />
</label>
</div>
<div class="half-row">
<label>
<span>Label 3:</span>
<input type="text" />
</label>
</div>
<div class="full-row">
<label>
<span>Label 4:</span>
<input type="text" />
</label>
</div>
</form>
CSS
* {
box-sizing: border-box;
}
form input{
width: 100%;
padding: 5px;
}
form {
width: 80%;
overflow: hidden;
}
form > div {
float: left;
padding: 5px;
}
.full-row {
width: 100%;
}
.half-row {
width: 50%;
}
label > span{
display: block;
}

Vertical center an absolutely positioned input field inside its surrounding element

I simply want to vertically center an input field inside its surrounding DIV, but at the same time all input fields on the page should be horizontally aligned with each other (should have the same left value, if you will).
Here's an example page of the problem:
.wrapper {
border: 1px solid #000;
border-radius: 10px;
margin: 10px;
padding: 10px;
line-height: 40px;
}
.inputClass {
position: absolute;
left: 25%;
/* top: 50%; transform: translate(0, -50%); */
/* not working as I need */
vertical-align: middle;
}
<div class="wrapper">
<div>
<div>
Some text
<input type="text" class="inputClass" />
</div>
<div>
Some more text
<input type="text" class="inputClass" />
</div>
</div>
</div>
Desired result (blue rim by Firebug):
You can group inputs+labels in fieldsets and then use the labels for moving the inputs towards the center,
try this:
.wrapper {
border: 1px solid #000;
border-radius: 10px;
margin: 10px;
padding: 10px;
line-height: 40px;
}
fieldset {
border: 0;
/*resets UA*/
}
/*can use label to move inputs right*/
label {
width:40%;
display:inline-block;
padding-right :15px;
text-align:right
}
<div class="wrapper">
<div>
<fieldset>
<label for='one'>Some text</label>
<input id='one' type="text" class="inputClass" />
</fieldset>
<fieldset>
<label for='two'>Some more text</label>
<input id='two' type="text" class="inputClass" />
</fieldset>
</div>
</div>
Put the text in a label (this is good practice anyway) and use inline block to position the elements beside each other. You can then give the label a width as required:
.wrapper {
border: 1px solid #000;
border-radius: 10px;
margin: 10px;
padding: 10px;
line-height: 40px;
}
.wrapper label {
display:inline-block;
width:25%;
vertical-align:middle;
}
.wrapper input {
display:inline-block;
vertical-align:middle;
}
<div class="wrapper">
<div>
<div>
<label for="field1">Some text</label>
<input id="field1" type="text" class="inputClass" />
</div>
<div>
<label for="field2">Some more text</label>
<input id="field2" type="text" class="inputClass" />
</div>
</div>
</div>
An excellent way to do this (in my opinion) would be using the table tag in HTML to organise the input fields and the labels. I would then use the "valign" property and set it to "TOP" e.g.
<table>
<tr>
<td valign="TOP">
Some text
</td>
<td valign="TOP">
<input type="text" class="inputClass" />
</td>
</tr>
<tr>
<td valign="TOP">
Some more text
</td>
<td valign="TOP">
<input type="text" class="inputClass" />
</td>
</tr>
</table>
The valign="TOP" assigned to the TD will make it be at the top of the cell in the table. This will ensure that they will be vertically and horizontally aligned with each other.
Another answer without using the table tag:
<style>
li {
width: 120px;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div>
<ul class="list-inline">
<li>Some text</li>
<li>
<input type="text">
</li>
</ul>
<ul class="list-inline">
<li>Some more text</li>
<li>
<input type="text">
</li>
</ul>
</div>
This would be used using BootStrap in order to list it in line.
A possible solution would be to put all the form labels in a div with class "col-left" & the input fields in a div with class "col-right". Both these are vertically middle aligned (vertical-align: middle) & the left column holding the labels have an equal width setting.
Please find below the complete solution:
.wrapper {
border: 1px solid #000;
border-radius: 10px;
margin: 10px;
padding: 10px;
line-height: 40px;
}
.col-left, .col-right {
vertical-align: middle;
display:inline-block;
}
.col-left {
width: 20%;
}
<div class="wrapper">
<div>
<div class="col-left">Some text</div>
<div class="col-right">
<input type="text" class="inputClass" />
</div>
</div>
<div>
<div class="col-left">Some more text</div>
<div class="col-right">
<input type="text" class="inputClass" />
</div>
</div>
</div>

How to have two input fields next to each other in a table cell, each being 50% wide?

I have this table:
<table>
<tr>
<td>
<input type="text" class="half"/>
<input type="text" class="half" />
</td>
<td>
<input type="text"/>
</td>
<td>
<input type="text"/>
</td>
</tr>
</table>
CSS:
table {
width: 100%;
border-spacing: 0;
}
.half {
float: left;
width: 50%;
}
How can I have two input fields next to each other each filling up 50% of the table cell's (natural / normal) width?
Right now, this doesn't work. The table cells containing the input fields of class half are far too wide and I can't see why this happens.
You need two things: DEMO
avoid the white-space in between inline-block element (you may then drop the float property).
include border size into width.
First, the easiest way is to remove white-space from html code and write it so :
<input ... /><input ... />
Second, is to switch to another box-model so :
box-sizing:border-box;
Add vendor-prefix whenever needed .
How about applying .half to the <td> parent?
<td class="half">
<input type="text" />
<input type="text" />
</td>
How about :
<table border="2px">
<tr>
<td>
<input type="text" class= "half"/>
<input type="text" class= "half"/>
</td>
<td>
<input type="text"/>
</td>
<td>
<input type="text"/>
</td>
</tr>
</table>
.css
table {
width: 100%;
border-spacing: 0;
}
td{
padding-left: 5px;
}
.half {
float: left;
width: 49%;
margin: 2px;
}
A quick way to resolve this, wrap then inputs in a span tag. Set the span to inline-block 50%, and the inputs to 100%. Using a box model maintains the inputs in the td
http://jsfiddle.net/HgxFf/
<table>
<tr>
<td>
<span><input type="text" class="half"/></span>
<span><input type="text" class="half" /></span>
</td>
<td>
<input type="text"/><input type="text"/>
</td>
<td>
<span><input type="text"/></span>
</td>
</tr>
</table>
CSS
table {
width: 100%;
border-spacing: 0;
}
td{border:1px solid #000000;font-size:0px; }
span{
display:inline-block;
width:50%;
padding:0px
}
.half {
width: 100%;
}
input[type="text"]{
box-sizing:border-box;
}
}

Centering a form within a div

I have a simple contact form within a div but I can't get it to center within the div. I have tried margins, padding, text-align:center but none of them work.
html: http://www.joekellywebdesign.com/churchsample/contact.html
<div id="contactbottomright">
<h2>Contact Form</h2>
<form action="feedback.php" method="post">
<table border="0" cellpadding="8" cellspacing="8">
<tr>
<td>
<label for="tswname">Name</label>:</td>
<td>
<input type="text" name="fullname" id="tswname" size="25" />
</td>
</tr>
<tr>
<td>
<label for="tswemail">Email address</label>:</td>
<td>
<input type="text" id="tswemail" name="email" size="25" />
</td>
</tr>
<tr>
<td colspan="2">
<label for="tswcomments">Comments</label>
<br />
<textarea rows="15" cols="35" name="comments" id="tswcomments"></textarea>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Submit" />
<br />
</td>
</tr>
</table>
</form>
</div>
css: http://www.joekellywebdesign.com/churchsample/css/styles.css
/* rules for contact page */
#contactbottomleft {z-index:26; width:450px;height: 700px; background-color:#92B681; margin: 30px 0 0 0; float:left;position: relative;}
#contactbottomright { z-index:27; width:450px; height: 700px; margin: 30px 0 0 0px; background-color:#428C3E; float:right;position: relative;}
form {
display: inline-block;
text-align: center;
}
/* end rules for contact page */
You could set a width to the form and then add margin: 0 auto; to the CSS. For example:
HTML:
<div>
<form></form>
</div>
CSS:
form {
margin: 0 auto;
width: 300px;
}
NOTE: For this to work, you must remove display:inline-block from form.
JS Fiddle Example (using your HTML/CSS).
You must set a Width of Ancestor element and then add margin:0 auto to its child.
See demo