Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align Inline Element to the Right

Tags:

css

How to align two text elements, one to the left and the other to the right, also on the same line. I'm aware it can be done using floats but I would like a float less solution. I'm looking for a way to do it using display:inline.

HTML:

<div class="contailner">
    <div class="inlineLeft">Item 1</div>
    <div class="inlineRight">Item 2</div>
</div>

CSS:

.container {
    width: 600px;
    border: 1px solid blue;
}

.inlineLeft, .inlineRight {
    display: inline;
}

.inlineRight {
    ...align right...   
}
like image 942
Robert Peek Avatar asked Jul 21 '13 15:07

Robert Peek


People also ask

How do you align inline elements to the right?

Make sure you set the text-align property for the first list item to "left" (ul li:first-child), and set the text-align property for the other list items (ul li) to "right". That aligns them to the left and to the right but on two suppurate lines.

How do I move an inline-block to the right?

You can use Flexbox instead and set margin-left: auto on inner div to move it to right.

How do you align inline-block elements?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.


1 Answers

you could just use position:absolute on the inline elements and position:relative on the container. Then you can align the inline elements the way you want relative to the container. Something like this:

.container {
    position: relative;
    width: 600px;
    border: 1px solid blue;
}

.inlineLeft, .inlineRight {
    position: absolute;
    display: inline;
}

.inlineRight {
    right: 0;
}

DEMO

like image 193
Martin Turjak Avatar answered Sep 30 '22 03:09

Martin Turjak