Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align label to centre and left positions in DIV

Tags:

html

css

I have a DIV like this:

    <div id="mydiv">
        <label id="orderName" style="align:center;">Order Name</label>
        <label id="orderNo" style="align:left;">Order No</label>  
    </div>

I am trying to:
- align OrderName horizontally and vertically center of the div.
- align OrderNo horizontally left of the div (and vertically centred).

How can i do that?

    ----------------------------------
   |                                 |
   | OrderNo     OrderName           |
   |                                 |
    ----------------------------------
like image 965
Jasper Avatar asked Mar 11 '16 11:03

Jasper


People also ask

How do I center a label in a div?

Other way using display:flex . Here i have added height and border to display vertical center. And give margin:0 auto; to center. Ketan> Although vertical alignment works now - But Order Name only starts from center position(50%) now, instead of being centered - horizontally.

How do I move a div to the center left?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you center a label vertically in a div?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .


1 Answers

Following css will give your expected result:

div {
    text-align: center;
}
#orderName {
    vertical-align: middle;
}
#orderNo {
    float: left;
}
 <div id="mydiv">
        <label id="orderName" >Order Name</label>
        <label id="orderNo">Order No</label>  
    </div>

Solution 2:

Other way using display:flex. Here i have added height and border to display vertical center.

And give margin:0 auto; to center.

div {
    align-items: center;
    display: flex;
    height: 40px;
    border: 1px solid;
}


#orderName {
    order: 2;
    margin:0 auto;
}
<div id="mydiv">
        <label id="orderName" >Order Name</label>
        <label id="orderNo">Order No</label>  
    </div>
like image 69
ketan Avatar answered Sep 17 '22 12:09

ketan