Align multiple labels to left of form input - html

I'm trying to stack two <span> tags in a form input label. At the moment, they're sitting next to each other, but I'm not sure how to move span.label-sm under the regular span. I've already set it to display: block;.
Desired outcome: http://i.imgur.com/CkOKeWl.jpg.
HTML:
<div id="appraisals-form" class="contact-form">
<form role="form" method="post" action="contact-form.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="email"><span>Phone</span><input type="tel" class="input-field" name="phone" required data-errormessage-value-missing="Please enter your phone number." /></label>
<label for="name"><span>Type of Artwork</span><span class="label-sm">(i.e. sculpture, painting...)</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your item's type of artwork." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<div class="centred-button"><input type="submit" value="" class="submit-button" /></div>
</form>
</div>
CSS:
.contact-form {
margin: 0 auto;
position: relative;
top: 50%;
transform: translateY(-50%);
max-width: 600px;
font-family: 'LinotypeUniversW01-Thin_723604', Arial, sans-serif;
font-size: 20px;
}
.contact-form label {
display: block;
margin: 0px 0px 15px 0px;
text-transform: uppercase; /* New */
}
.contact-form label > span {
padding-top: 8px;
}
.contact-form label > span, #recaptcha::before {
width: 100px;
text-align: right;
float: left;
padding-right: 20px;
content: "";
}
.contact-form input, .contact-form textarea {
margin-bottom: 15px;
padding: 10px;
border: none;
}
.contact-form input.input-field {
width: 70%;
height: 20px;
font-size: 18px;
}
.contact-form .textarea-field {
height: 250px;
margin-bottom: 11px;
}
.contact-form .textarea-field, .g-recaptcha {
width: 70%;
font-size: 18px;
display: inline-block;
}
.g-recaptcha {
height: 78px !important;
}
#recaptcha {
display: block;
margin: 0px 0px 24px 0px;
}
textarea {
resize: none;
}
textarea:focus, input:focus {
outline: 0;
}
input.submit-button {
background-image: url("../img/submit-button.jpg");
width: 225px;
height: 60px;
border: none;
}
.appraisals .section-title {
width: 100%;
}
#appraisals-form {
width: 100%;
max-width: 700px;
top: auto;
transform: none;
}
#appraisals-form label > span {
width: 150px;
font-size: 16px
}
#appraisals-form span.label-sm {
display: block;
font-size: 14px;
text-transform: none;
}
#appraisals-form input, #appraisals-form textarea {
background-color: #d7d7d7;
}
#appraisals-form textarea {
height: 100px;
}
#appraisals-form .centred-button {
text-align: center;
}
Fiddle: http://jsfiddle.net/tcap2Lcp/.
Any help would really appreciated!

As an alternative to my first answer, which uses CSS positioning properties and width adjustments to align the labels on the left of the input, this solution keeps things very simple with only HTML.
The idea is to display the small labels as placeholder text.
This element gets deleted:
<span class="label-sm">(i.e. sculpture, painting...)</span>
But the text goes into a placeholder:
<label for="name">
<span>Type of Artwork</span>
<input type="text" class="input-field" name="name" required
data-errormessage-value-missing="Please enter your item's type of artwork."
placeholder="(i.e. sculpture, painting...)" >
</label>
And that's it!
Clean, simple, reusable code (as you requested). Easy to maintain. User-friendly. And no changes to the CSS...
unless you decide you want to style the placeholder text.

You can accomplish this task with CSS positioning properties.
HTML
<!-- ADJUSTED BLOCK START -->
<label for="name" style="position: relative; width: 100%;">
<span style="width: 24%; padding: 0; position: relative; left: -2%;">
Type of Artwork</span>
<span style="width: 24%; padding: 0; position: absolute; left: -2%; bottom: 30%;"
class="label-sm">(i.e. sculpture, painting...)</span>
<input style="width: 70%" type="text" name="name"
required data-errormessage-value-missing="Please enter your item's type of artwork.">
</label>
<!-- ADJUSTED BLOCK END-->
DEMO
I used inline styles for brevity and clarity. Hope this helps.

This will nest the two <span> tags without changing the appearnce:
<span>Type of Artwork</span><span class="label-sm">(i.e. sculpture, painting...)</span></span>
I'm unsure what you are trying to achieve though, so not sure if your CSS needs a small change, possibly:
.label-sm {
display: block;
font-size: 14px;
text-transform: none;
}

If i get you right this should work for you - just add float: none; to #appraisals-form span.label-sm.
Here is the whole css:
.contact-form {
margin: 0 auto;
position: relative;
top: 50%;
transform: translateY(-50%);
max-width: 600px;
font-family: 'LinotypeUniversW01-Thin_723604', Arial, sans-serif;
font-size: 20px;
}
.contact-form label {
display: block;
margin: 0px 0px 15px 0px;
text-transform: uppercase; /* New */
}
.contact-form label > span {
padding-top: 8px;
}
.contact-form label > span, #recaptcha::before {
width: 100px;
text-align: right;
float: left;
padding-right: 20px;
content: "";
}
.contact-form input, .contact-form textarea {
margin-bottom: 15px;
padding: 10px;
border: none;
}
.contact-form input.input-field {
width: 70%;
height: 20px;
font-size: 18px;
}
.contact-form .textarea-field {
height: 250px;
margin-bottom: 11px;
}
.contact-form .textarea-field, .g-recaptcha {
width: 70%;
font-size: 18px;
display: inline-block;
}
.g-recaptcha {
height: 78px !important;
}
#recaptcha {
display: block;
margin: 0px 0px 24px 0px;
}
textarea {
resize: none;
}
textarea:focus, input:focus {
outline: 0;
}
input.submit-button {
background-image: url("../img/submit-button.jpg");
width: 225px;
height: 60px;
border: none;
}
.appraisals .section-title {
width: 100%;
}
#appraisals-form {
width: 100%;
max-width: 700px;
top: auto;
transform: none;
}
#appraisals-form label > span {
width: 150px;
font-size: 16px
}
#appraisals-form span.label-sm {
display: block;
font-size: 14px;
text-transform: none;
float: none;
}
#appraisals-form input, #appraisals-form textarea {
background-color: #d7d7d7;
}
#appraisals-form textarea {
height: 100px;
}
#appraisals-form .centred-button {
text-align: center;
}
hope it helps :)
Further explanation
The problem was caused by float: left; at .contact-form label > span which floatet your span in general.

The easyest way is to add this class to CSS :
label {float: left; width: 10em; margin-right: 1em; text-align: right;}

Related

How do i place my label over my text box input?

I have the below code to make a login form but i cant get the checkbox label to be like always against the edge of the text area. I always sits to the right of the text area. I cant get it to be dependant on the div it is in. On inspection it sits outside the div.
Different things i have tried have included giving the label a left value but this messes it up when the screen size changes.
I want something like this
Here is a jsfiddle if this is easier
function showHidePassword() {
var x = document.getElementById("pass");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
body {
background-color: #ffffff;
}
* {
box-sizing: border-box;
}
input[type=text],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
input[type=password],
select,
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
label {
padding: 12px 12px 12px 0;
display: inline-block;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: right;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container1 {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
margin: 15% 30%;
}
.signup {
border-radius: 25px;
background-color: #f2f2f2;
padding: 40px;
position: center;
opacity: 0.96;
}
.container1 .new-body {
background: #f2f2f2;
}
.signup .new-body {
background: #f2f2f2;
}
.signup .row {
padding-top: 5px;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 65%;
margin-top: 6px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
#media screen and (max-width: 600px) {
.col-25,
.col-75,
input[type=submit] {
width: 100%;
margin-top: 0;
}
.col-70,
input[type=submit] {
width: 95%;
margin-top: 0;
}
}
.passw {
cursor: pointer;
width: 30px;
height: 20px;
}
.col-75 label {
padding-top: 16px;
position: absolute;
z-index: 100;
}
<form>
<div class="row">
<div class="col-25">
<label for="pass">Password</label>
</div>
<div class="col-75">
<input type="password" id="pass" name="password" minlength="5" pattern="[A-Za-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*" placeholder="Password" title="A valid password is a set of 5 characters, each consisting of an
upper or lower-case letter, or a digit. The password must begin with a letter and contain at least one digit" autocomplete="current-password" required>
<label for="passShowIcon" id="showHide"><input name="passShowIcon" type="checkbox" class="passw" onclick="showHidePassword();">
<span class=" "></span></label>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
If you wanted to make sure the checkbox appears inside the text input. You could wrap both input fields with a relative class, and then apply absolute positioning to the checkbox.
Like so:
https://jsfiddle.net/x0o46g7a/2/
.wrapper {
position: relative;
}
.text {
width: 100%;
height: 100%;
}
.checkbox {
position: absolute;
top: -8px;
right: -8px;
}
Something to note:
I would recommend adding some padding-right to your text input, to make sure it's text does not overlap/underlap the absolute positioned checkbox.
Based on your code, add the following rules in your css.
float: right to .col-75 instead of float left
right: 0 to .col-75 label
those will ensure that checkbox will remain inside the input field.

css for 2 inc/dec buttons by an input that reposition properly when resizing screen

I want to display an input box in html and 2 small buttons to the right, one on top of each other. Something like this
https://jsfiddle.net/danaya/yw94f0Lt/3/
The html code is simple
<div class="list">
<div class="name">product</div>
<div class="input">
<input type="text" size="3" value="1">
<div class="inc">+</div>
<div class="dec">-</div>
</div>
<div class="price">2.99</div>
</div>
And this is the css
.list {
display: flex;
}
.name,
.input,
.price {
padding: 10px 10px;
border: 1px solid red;
}
.name {
width: 50%;
}
.input,
.price {
text-align: right;
width: 25%;
}
.input {
position: relative;
top: -5px;
}
input {
line-height: 25px;
}
.inc,
.dec {
display: block;
position: absolute;
width: 13px;
height: 13px;
background-color: #999;
text-align: center;
color: #fff;
line-height: 12px;
}
.inc {
top: 11px;
left: 40px;
}
.dec {
top: 25px;
left: 40px;
}
As it is right now, when I resize the window, the div.input is resized and so the buttons, being related to the input, lose their position by the input element.
I need to keep the flex display in the .list element, but other than that I can change anything. I also need the buttons to not increase the width of the div.input element, that's why I'm using the position:relative.
Any ideas?
Thanks
Would this work for you?
.list {
display: flex;
align-items: center;
}
.name,
.price {
padding: 0 10px;
}
.input, input {
box-sizing: border-box; /*to set the equal height to bot .input and input*/
height: 31px; /*13 + 13 (buttons) + 5 (paddings for buttons) = 31px */
}
.input {
position: relative;
/*width: 25%; Use this if you want to increate the width of your div.input*/
}
input {
padding-right: 15px; /* 15px set so that input characters do not go underneath the + and - buttons */
width: 100%; /* set 100% so that input will take 100% width of its parent size */
}
.inc,
.dec {
box-sizing: border-box;
display: block;
position: absolute;
width: 13px;
height: 13px;
background-color: #999;
text-align: center;
color: #fff;
line-height: 12px;
}
.inc {
top: 2px;
right: 2px;
}
.dec {
bottom: 2px;
right: 2px;
}
<div class="list">
<div class="name">product</div>
<div class="input">
<input type="text" size="3" value="1">
<span class="inc">+</span>
<span class="dec">-</span>
</div>
<div class="price">2.99</div>
</div>

Positioning textarea label above (with or without table)

I'm trying to position my two labels ("your message:" and "your email:") to the top-left of each of the textarea's, with "your message:" on the top textarea. Although even through this method: How will I align the label of a text area to top? I can't get it to work. Is the table required for it to work correctly? I've tried other methods to get it to function, such as grouping the form within a -p- tag but that also didn't work.
HTML
<div id="ContactUsForm">
<p>Contact Us</p>
<form>
<table>
<td>
<label for="message">your message:</label>
<textarea id="message" class="contact" name="message"</textarea>
<label for="email">your email:</label>
<textarea id="email" class="contact" name="email"></textarea>
</td>
</table>
</form>
</div>
CSS
#ContactUsForm {
position: absolute;
width: 1400px;
height: 400px;
top: 227px;
left: 42px;
border: 2px solid #E64A19;
}
#ContactUsForm p {
text-decoration: underline;
font-weight: bold;
position: absolute;
top: -15px;
left: 50px;
font-size: 30px;
}
.contact {
position: absolute;
background: white;
border: 2px solid #E64A19;
}
#message {
width: 500px;
height: 150px;
top: 100px;
left: 30px;
resize: none;
position: absolute;
}
#email {
width: 500px;
height: 70px;
position: absolute;
resize: none;
top: 300px;
left: 30px;
}
label {
vertical-align: top;
}
Get rid of position: absolute, I don't see a point of using it inside table in this case.
<div id="ContactUsForm">
<p>Contact Us</p>
<form>
<table>
<tr>
<td>
<label for="message">your message:</label>
<textarea id="message" class="contact" name="message"></textarea>
<label for="email">your email:</label>
<textarea id="email" class="contact" name="email"></textarea>
</td>
</tr>
</table>
</form>
</div>
#ContactUsForm {
width: 1400px;
height: 400px;
border: 2px solid #E64A19;
}
#ContactUsForm p {
text-decoration: underline;
font-weight: bold;
font-size: 30px;
}
.contact {
background: white;
border: 2px solid #E64A19;
}
#message {
width: 500px;
height: 150px;
top: 100px;
left: 30px;
resize: none;
display: block;
}
#email {
width: 500px;
height: 70px;
display: block;
resize: none;
top: 300px;
left: 30px;
}
label {
vertical-align: top;
}
Here's JSFiddle: https://jsfiddle.net/pa4r62eu/
You don't need to use a table for that.
You can achieve what you want with just two changes:
Remove all the position: absolute; of your code. In this way, each element will be placed above the next one (and you can remove all your left and top rules).
Add a display: block; rule to your labels. It will make your labels occupy all the width of its parent, and the textareas will be placed under them.
Looking at the snippet, you can see how much cleaner your code will be:
#ContactUsForm {
width: 1400px;
height: 400px;
border: 2px solid #E64A19;
padding: 10px;
}
#ContactUsForm p {
text-decoration: underline;
font-weight: bold;
font-size: 30px;
}
.contact {
background: white;
border: 2px solid #E64A19;
}
#message {
width: 500px;
height: 150px;
resize: none;
}
#email {
width: 500px;
height: 70px;
resize: none;
}
label {
display: block;
}
<div id="ContactUsForm">
<p>Contact Us</p>
<form>
<label for="message">your message:</label>
<textarea id="message" class="contact" name="message"></textarea>
<label for="email">your email:</label>
<textarea id="email" class="contact" name="email"></textarea>
</form>
</div>
Hope it helps!

Positioning reCAPTCHA widget with CSS

I'm trying to align the reCAPTCHA widget with my input fields, but styling .g-recaptcha doesn't seem to achieve much. Does anyone know of any selectors I can use?
Form HTML:
<div class="contact-form">
<form role="form" method="post" action="contact-form.php">
<label for="name"><span>Name</span><input type="text" class="input-field" name="name" required data-errormessage-value-missing="Please enter your name." /></label>
<label for="email"><span>Email</span><input type="email" class="input-field" name="email" required data-errormessage-value-missing="Please enter your email address." /></label>
<label for="message"><span>Message</span><textarea name="message" class="textarea-field" required data-errormessage-value-missing="Please enter your message."></textarea></label>
<span> </span>
<div id="recaptcha">
<div class="g-recaptcha" data-sitekey="6LcBawsTAAAAAKBPfGs1jApXNRLvR2MIPng0Fxol" style="margin: 0;"></div>
</div>
<label><span> </span><input type="submit" value="" class="submit-button" /></label>
</form>
</div>
CSS:
.contact-form {
margin: 0 auto;
position: relative;
top: 50%;
transform: translateY(-50%);
max-width: 600px;
font-family: 'LinotypeUniversW01-Thin_723604', Arial, sans-serif;
font-size: 20px;
}
.contact-form label {
display: block;
margin: 0px 0px 15px 0px;
}
.contact-form label > span {
width: 100px;
text-align: right;
float: left;
padding-top: 8px;
padding-right: 20px;
}
.contact-form input, .contact-form textarea {
margin-bottom: 15px;
padding: 10px;
border: none;
}
.contact-form input.input-field {
width: 70%;
height: 20px;
font-size: 18px;
}
.contact-form .textarea-field {
width: 70%;
height: 250px;
font-size: 18px;
}
textarea {
resize: none;
}
textarea:focus, input:focus {
outline: 0;
}
#recaptcha {
width: 304px;
margin: 0;
}
.g-recaptcha {
text-align: left;
}
input.submit-button {
background-image: url("../img/submit-button.jpg");
width: 225px;
height: 60px;
border: none;
}
Any help would be really appreciated!
Just modify your existing CSS with this:
.contact-form label, #recaptcha {
display: block;
margin: 0px 0px 15px 0px;
}
.contact-form label > span, #recaptcha::before {
width: 100px;
text-align: right;
float: left;
padding-top: 8px;
padding-right: 20px;
content:""; //Make sure you add this as well
}
.contact-form .textarea-field, .g-recaptcha {
width: 70%;
height: 250px;
font-size: 18px;
display: inline-block;
}
Pseudo elements are used here so that you don't have to change your markup. :before works as a span inside the label thus providing uniformity in your html structure and hence solving the alignment issue
To align reCAPTCHA with rest of the other form-fields you have to change your DOM structure something like this:
HTML:
<label for="captcha">
<span> </span>
<div id="recaptcha">
...
then also change/write the CSS:
#recaptcha {
margin: 0px;
width: 304px;
position: relative;
float: left;
}
#recaptcha iframe {
position: absolute;
left: 0px;
top: 0px;
}
This will do the trick for you.

The content of a HTML date input field is shifting to the left when I input data in it

I don't know how to fix this problem. In my HTML form I have 2 ´input[type="date"]` fields like this:
<div class="search-wrapper">
<div class="advanced-search">
<form>
<input type="text" placeholder="Dateiname">
<input type="text" placeholder="Benutzer">
<input type="date" placeholder="Erstelldatum">
<input type="date" placeholder="Änderungsdatum">
<textarea placeholder="Erste Zeile"></textarea>
<select>
<option disabled selected>Kategorie</option>
<option>Rechnungen</option>
<option>Notizen</option>
</select>
<select>
<option disabled selected>Projekt</option>
</select>
<img id="advSearchSubmit" src="icons/search.png" />
</form>
</div>
</div>
And the corresponding SCSS:
.search-wrapper {
position: fixed;
.advanced-search {
display: none;
float: right;
width: 600px;
padding: 40px 10px 10px 10px;
background-color: $accent-color;
color: $font-color-secondary;
overflow: hidden;
img {
vertical-align: top;
width: 25px;
height: 25px;
margin-top: 10px;
cursor: pointer;
}
input[type="text"], input[type="date"], select, textarea {
vertical-align: top;
width: 110px;
height: 25px;
margin: 10px;
padding: 2px;
background: $data-background-color;
color: #555;
border: 1px solid $background-color;
outline-color: $main-color;
font-size: 100%;
}
textarea {
height: 2em;
width: 41.2em;
}
}
}
As soon as I enter a date in the input field it gets shifted to the left and is not fully visible. Here is what it looks like when I don't have anything entered in the input field and when there is something entered:
Date Input Screenshot
How do I fix this behaviour?
I managed to fix it myself. I just had to increase the width of the input[type="date"] field to at least 120px
.search-wrapper and .advanced-search missing closing braces
remove display none property if you are using it without js
.search-wrapper {
position: fixed;
}
.advanced-search {
float: right;
width: 600px;
padding: 40px 10px 10px 10px;
background-color: $accent-color;
color: $font-color-secondary;
overflow: hidden;
}
img {
vertical-align: top;
width: 25px;
height: 25px;
margin-top: 10px;
cursor: pointer;
}
input[type="text"], input[type="date"], select, textarea {
vertical-align: top;
width: 110px;
height: 25px;
margin: 10px;
padding:0px 4px;
box-sizing: border-box;
padding: 2px;
background: $data-background-color;
color: #555;
border: 1px solid $background-color;
outline-color: $main-color;
font-size: 100%;
}
input[type="date"]{
width:140px; }
textarea {
height: 2em;
width: 41.2em;
}