Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text-align: right; not working for <label>

Simply enough I can't get text to align to right in a <label> element.

HTML

<div id="contact_form">  <label for="name" id="name_label">Name:</label>  </div> 

CSS

#contact_form label {   text-align: right; } 

My page: http://freshbeer.lv/development/en/contact.php

You can see labels for name, phone, email etc... are aligned to the left, but I need them to be aligned to the right, so could anyone please suggest something?

like image 604
Ilja Avatar asked Feb 01 '12 18:02

Ilja


People also ask

How do I right align text in a label?

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.

Why text-Align Center doesn't work?

Short answer: your text isn't centered because the elements are floated, and floated elements "shrink" to the content, even if it's a block level element.


1 Answers

Label is an inline element - so, unless a width is defined, its width is exact the same which the letters span. Your div element is a block element so its width is by default 100%.

You will have to place the text-align: right; on the div element in your case, or applying display: block; to your label

Another option is to set a width for each label and then use text-align. The display: block method will not be necessary using this.

like image 190
janhartmann Avatar answered Oct 08 '22 05:10

janhartmann