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

163,539

Solution 1

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.

Solution 2

You can make a text align to the right inside of any element, including labels.

Html:

<label>Text</label>

Css:

label {display:block; width:x; height:y; text-align:right;}

This way, you give a width and height to your label and make any text inside of it align to the right.

Solution 3

As stated in other answers, label is an inline element. However, you can apply display: inline-block to the label and then center with text-align.

#name_label {
    display: inline-block;
    width: 90%;
    text-align: right;
}

Why display: inline-block and not display: inline? For the same reason that you can't align label, it's inline.

Why display: inline-block and not display: block? You could use display: block, but it will be on another line. display: inline-block combines the properties of inline and block. It's inline, but you can also give it a width, height, and align it.

Share:
163,539

Related videos on Youtube

Ilja
Author by

Ilja

That dev at the local ☕️ shop sipping on late.

Updated on April 12, 2020

Comments

  • Ilja
    Ilja about 4 years

    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?

    • defau1t
      defau1t over 12 years
      give display block property to label and it will work