Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Form with Label above Inputs

Tags:

html

css

I would like to produce the following form style:

 Name                    Email [.................]     [.................]  Subject [.................]  Message [.........................................] [.........................................] [.........................................] [.........................................] 

The HTML code I have is:

<form name="message" method="post">     <section>     <label for="name">Name</label>     <input id="name" type="text" value="" name="name">     <label for="email">Email</label>     <input id="email" type="text" value="" name="email">     </section>     <section>     <label for="subject">Subject</label>     <input id="subject" type="text" value="" name="subject">     <label for="message">Message</label>     <input id="message" type="text" value="" name="message">     </section> </form> 

At the moment it is producing:

 Name    [...................] Email   [...................] Subject [...................] Message [.........................................] [.........................................] [.........................................] [.........................................] 

What would be the best way to do this? I keep getting in a muddle my floats!

like image 845
xylar Avatar asked May 18 '11 14:05

xylar


People also ask

How do you style inputs and labels?

There are two ways to pair a label and an input. One is by wrapping the input in a label (implicit), and the other is by adding a for attribute to the label and an id to the input (explicit). Think of an implicit label as hugging an input, and an explicit label as standing next to an input and holding its hand.

Does label go before or after input?

Note that the label is positioned after input elements of type="checkbox" and type="radio" . Note 1: Elements that use explicitly associated labels are: input type="text"

How do I put input and label side by side?

We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.


2 Answers

I'd make both the input and label elements display: block , and then split the name label & input, and the email label & input into div's and float them next to each other.

input, label {      display:block;  }
<form name="message" method="post">      <section>      <div style="float:left;margin-right:20px;">      <label for="name">Name</label>      <input id="name" type="text" value="" name="name">    </div>      <div style="float:left;">      <label for="email">Email</label>      <input id="email" type="text" value="" name="email">    </div>      <br style="clear:both;" />        </section>        <section>        <label for="subject">Subject</label>      <input id="subject" type="text" value="" name="subject">      <label for="message">Message</label>      <input id="message" type="text" value="" name="message">        </section>  </form>
like image 167
fin1te Avatar answered Sep 20 '22 06:09

fin1te


Probably a bit late but this worked for me. i simply used column flex-direction on the label and input elements HTML

<form id="survey-form">     <label for="name">Name</label>     <input type="text" id="name">          <label>Email</label>     <input type="email" id="email">   </form>  CSS  label,input{   display:flex;   flex-direction:column; } 
like image 29
Mardiya Avatar answered Sep 20 '22 06:09

Mardiya