Table Layout to CSS Layout how to work around colspan? - html

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;
}

Related

How to align input element in HTML?

How can I do that?
My HTML code is
<p> <input type="text" name="Usuari" size="20" maxlength="60"/>
<p>CLAU:</label> <input type="text" name="Clau" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="ACCÉS" /></p>-->
and my CSS code is
input {
padding: 5px;
font-weight: bold;
font-size: 1em;
color: #008040;
background: #FFFFFF;
border:1px dotted #004080;
}
I would place "USUARI:" and "CLAU:" into div with class and give them fixed size.
<div>
<div>
<div class="hints">
USUARI:
</div>
</div><input type="text" name="Usuari" size="20" maxlength="60"/>
</div>
<div>
<div class="hints">CLAU:
</div>
<input type="text" name="Clau" size="20" maxlength="20" />
</div>
</div>
<input type="submit" name="submit" value="ACCÉS" />
<style>
input {
padding: 5px;
font-weight: bold;
font-size: 1em;
color: #008040;
background: #FFFFFF;
border:1px dotted #004080;
}
.hints
{
width: 80px;
float: left;
}
</style>
https://jsfiddle.net/gm5wtw1e/
There are several ways to do that.
You can either use flex or with just a plain table:
<table>
<tr>
<td>Name:</td>
<td><input name="name"></td>
</tr>
<tr>
<td>Sur Name:</td>
<td><input name="surname"></td>
</tr>
</table>
---
<table>
<tr>
<td style="text-align: right;">Name:</td>
<td><input name="name"></td>
</tr>
<tr>
<td>Sur Name:</td>
<td><input name="surname"></td>
</tr>
</table>
p{
display: table-cell;
text-align: right;
}
input {
display: table-cell;
}
div.row{
display:table-row;
}
<div class="row"><p> <input type="text" name="Usuari" size="20" maxlength="60"/></div>
<div class="row"><p>CLAU:</label> <input type="text" name="Clau" size="20" maxlength="20" /></p></div>
<p><input type="submit" name="submit" value="ACCÉS" /></p>
label
{
display: block;
}
label span
{
display: inline-block;
text-align: left;
width: 100px;
}
<label>
<span>USARI:</span>
<input type="text" />
</label>
<label>
<span>CLAU:</span>
<input type="text" />
</label>
<label>
<span></span>
<input type="password" />
</label>
there are few error in your HTML code, you close the tag label but you never open it.
You should also wrap your fields in tag and wrap your label in tag .
You don't need to open the tag all the time, you can open it and close it at the beginning and at the end of the form.
There is also a --> at the end of the code, I assume that's another mistake.
Here is my solution to your question:
HTML
<label>
<span>USARI:</span>
<input type="text" />
</label>
<label>
<span>CLAU:</span>
<input type="text" />
</label>
<label>
<span></span>
<input type="password" />
</label>
And here is my css code:
CSS
label
{
display: block;
}
label span
{
display: inline-block;
text-align: left;
width: 100px;
}
You can now play with the CSS code in order to find the design that you like the most.

Center td inside a fixed width div

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;
}

Horizontal checkbox layout with label underneath

I just started coding a small page for myself after not doing any web design for a couple of years. As I now learned, laying out the page with tables is not state-of-the-art anymore (not sure if it ever really was).
Now I am trying to layout my page with CSS but couldn't find anything on:
How to align 7 checkboxes horizontally and put the corresponding label centered below the checkboxes?
How to align 2 selects horizontally and put the corresponding label centered above the selects?
The initial pure table-code was the following:
.form fieldset {
display: table;
border: 1px solid #c6c7cc;
border-radius: 5px;
}
.form label {
display: table-cell;
text-align: right;
padding: 5px;
}
.form input,
.form select {
display: table-cell;
}
.form .cssRow {
display: table-row;
}
.form .submit {
display: table-cell;
caption-side: bottom;
display: table-caption;
text-align: center;
}
<table>
<tr>
<td align="right">Name</td>
<td align="left">
<input name="name" type="text">
</td>
</tr>
<tr>
<td align="right">Day(s) of week</td>
<td align="center">
<table>
<tr>
<td align="center">
<input type="checkbox" name="day[]" value="mo" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="tu" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="we" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="th" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="fr" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="sa" checked>
</td>
<td align="center">
<input type="checkbox" name="day[]" value="su" checked>
</td>
</tr>
<tr>
<td align="center">Mo</td>
<td align="center">Tu</td>
<td align="center">We</td>
<td align="center">Th</td>
<td align="center">Fr</td>
<td align="center">Sa</td>
<td align="center">Su</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="right">Validity</td>
<td align="center">
<table>
<tr>
<td>Valid from</td>
<td>Valid to</td>
</tr>
<tr>
<td>
<select>
<option>January</option>
<option>February</option>
</select>
</td>
<td>
<select>
<option>January</option>
<option>February</option>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="submit" value="Refresh">
</td>
</tr>
</table>
My CSS attempt so far looks like this:
<div class="form">
<fieldset>
<legend>Search</legend>
<div class="cssRow">
<label for="name">Name</label>
<input name="name" type="text" size="30" maxlength="30">
</div>
<div class="cssRow">
<label for="day[]">Day(s) of week</label>
<input name="day[]" type="text" value="ToDo" size="30" disabled>
</div>
<div class="cssRow">
<label>Validity</label>
<input type="text" value="ToDo" size="30" disabled>
</div>
<div class="submit">
<input type="submit" name="submit" value="Suchen">
</div>
</fieldset>
</div>
To illustrate my problem, I created the following JSFiddle: http://jsfiddle.net/c9a7ezyk/
Any suggestions are welcome, although I prefer a simple solution, as I am just (re)learning HTML and CSS.
I prefer a slightly different approach to the other answer, where the <input> element is nested inside of a <label>, this implicitly associates the label with the input to give all kinds of nice bonuses.
It also makes for a simpler to follow markup, with less nested containers.
Example
<label>
<input type="checkbox">
<span class="label">Sunday</span>
</label>
And then
label {
display: inline-block;
text-align: center;
}
span.label {
display: block;
}
Notice how clicking the labels check the associated checkbox properly. Selects behave exactly the same way. Because <input> and <select> are inlines by default, it means that they'll be affected by text-align: center.
Checkbox with label:
<div class="checkbox-label">
<label for="checkbox">Sunday</label>
<div class="checkbox-container">
<input name="checkbox" type="checkbox">
</div>
</div>
.checkbox-label {
display: inline-block
}
.checkbox-container {
text-align: center;
}
Notice that the checkbox is inline so you can put it in a container and use text-align: center
Also notice that I use display: inline-block on .checkbox-label so that they can be aligned horizontally (block elements, default for div, takes up a whole line and drops the following element beneath it)
I use the same principals for the selects
You can see the whole thing here:
http://codepen.io/Vall3y/pen/QwdWOe
Semantic Purity
I am a bit of an HTML purist, so here is an HTML form without any extra markup:
The legends are floated to the left and vertically centered using a line-height that matches the legends height
The inputs are wrapped in a label with display: inline-block which is given a width to force the text below / above the input
The fieldset:before properties allow us to vertically center the labels with vertical-align: middle
Full Example
The background colours are just to illustrate the layout.
* {
margin: 0;
padding: 0;
}
fieldset {
border: none;
height: 70px;
}
fieldset:before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
background: #F90;
width: 0;
}
legend {
height: 100%;
line-height: 70px;
width: 150px;
text-align: center;
background: #F90;
float: left;
}
input[type=checkbox] {
margin: 0 5px;
}
.days label {
background: #F90;
width: 30px;
display: inline-block;
vertical-align: middle;
text-align: center;
}
.validity label {
background: #F90;
width: 100px;
display: inline-block;
vertical-align: middle;
text-align: center;
}
<form>
<fieldset class="days">
<legend>Day(s) of Week</legend>
<label for="monday">
<input type="checkbox" id="monday" />Mo
</label>
<label for="tuesday">
<input type="checkbox" id="tuesday" />Tu
</label>
<label for="wednesday">
<input type="checkbox" id="wednesday" />We
</label>
<label for="thursday">
<input type="checkbox" id="thursday" />Th
</label>
<label for="friday">
<input type="checkbox" id="friday" />Fr
</label>
<label for="saturday">
<input type="checkbox" id="saturday" />Sa
</label>
<label for="sunday">
<input type="checkbox" id="sunday" />Su
</label>
</fieldset>
<fieldset class="validity">
<legend>Validity</legend>
<label for="from">Valid From
<select id="from">
<option>Option</option>
</select>
</label>
<label for="to">Valid to
<select id="to">
<option>Option</option>
</select>
</label>
</fieldset>
</form>
Demo here
Here is the code:
<html>
<body>
<div>Name <input type="text"></input></div><br>
<div>Day(s) of week</div>
<div style="margin-left: 120px;margin-top: -25px;">
<div><input type="checkbox" checked><br>Mo</input></div>
<div style="
width: 10px;
margin-left: 30px;
margin-top: -37px;">
<input type="checkbox" checked><br>Tu</input></div>
<div style="
width: 10px;
margin-left: 60px;
margin-top: -37px;">
<input type="checkbox" checked><br>We</input></div>
<div style="
width: 10px;
margin-left: 90px;
margin-top: -37px;">
<input type="checkbox" checked><br>Th</input></div>
<div style="
width: 10px;
margin-left: 120px;
margin-top: -37px;">
<input type="checkbox" checked><br>Fr</input></div>
<div style="
width: 10px;
margin-left: 150px;
margin-top: -37px;">
<input type="checkbox" checked><br>Sa</input></div>
<div style="
width: 10px;
margin-left: 180px;
margin-top: -37px;"><input type="checkbox" checked><br>Su</input>
</div>
</div>
<br>
<div>Validity
<select>
<option>January</option>
<option>February</option>
</select>
<select>
<option>January</option>
<option>February</option>
</select>
</div>
</body>
</html>

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;
}
}

align right side of button and input

I have a text input field and 2 buttons on the next line within a dialog box.
How may I line up the input field in the center of the dialog and align the buttons to the right hand edge of the input field?
I have managed to achieve the results I'm after with the following code, but it feels like there must be a better way as this aligns the text input to the right also:
<style>
.container
{
border: 1px solid #ccc;
text-align: right;
width: 100%;
padding-right: 20px;
}
</style>
<div class="container">
<p><input id="selContactLists" name="selContactLists" type="text" class="textbox" id="textbox" /></p>
<input name="" type="button" value="Button"/>
<input name="" type="button" value="Button"/>
CSS:
.container {
display: inline-block;
}
input.text {
width: 150px;
display: block;
}
.right {
float: right;
}
HTML:
<div class="container" >
<input type="text" class="text" />
<span class="right">
<input type="button" value="button" />
<input type="button" value="button" />
</span>
</div>
Working example: http://jsfiddle.net/GTTFY/
<style>
.container
{
margin-left:auto;
margin-right:auto;
width:150px;
}
</style>
<div class="container" >
<input name="" type="text" class="textbox" id="textbox" />
<input name="" type="button" style="float:right;" value="Button"/>
<input name="" type="button" style="float:right;" value="Button"/>
</div>
<style>
.test
{
margin-left:auto;
margin-right:auto;
}
.test td
{
float:right;
}
</style>
<table class="test">
<tr>
<td colspan=2><input name="" type="text" class="textbox" id="textbox" /></td>
</tr>
<tr>
<td><input name="" type="button" value="Button"/> </td>
<td><input name="" type="button" value="Button"/> </td>
</tr>
</table>