Skip to content

Instantly share code, notes, and snippets.

@levito
Created February 18, 2013 00:22
Show Gist options
  • Save levito/4974350 to your computer and use it in GitHub Desktop.
Save levito/4974350 to your computer and use it in GitHub Desktop.
A CodePen by Veit Lehmann. Responsive form layout - Adjusts label position depending on screen width. If you switch layout with a container class, it has the advantage of same markup for both layout variants. This makes using helper snippets for templating very easy.
<div class="formrow">
<div class="formitem">
<label class="label" for="a">
Foo bar
</label>
<input type="text" id="a"/>
</div>
<div class="formitem">
<label class="label" for="b">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</label>
<textarea id="b" rows="8"></textarea>
</div>
</div>
<div class="formrow">
<div class="formitem">
<label class="label" for="c">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</label>
<input type="text" id="c"/>
</div>
<div class="formitem">
<label class="label" for="d">
Foo bar
</label>
<textarea id="d" rows="8"></textarea>
</div>
</div>
* {
box-sizing:border-box;
}
html {
font-family:sans-serif;
}
input,
textarea,
.label {
font-size:12px;
line-height:18px;
}
input,
textarea {
width:100%;
display:block;
}
.formrow {
font-size:0;
line-height:0;
}
.label {
display:inline-block;
}
.formitem {
display:inline-block;
width:50%;
padding-left:10px;
}
@media screen and (min-width:801px) {
input,
textarea {
display:inline-block;
vertical-align:top;
}
.formrow {
padding-left:200px;
}
.label {
width:210px;
margin-left:-210px;
vertical-align:top;
padding-right:10px;
}
.formitem ~ .formitem .label {
display:none;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment