Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: align two element, to the left and right in the same line WHITHOUT floats

Tags:

I am trying to align two inline elements, one to the left and one to the right. I'd like to accomplish this without using floats.

In my case, I have a h1, with a span and a link. I'd like the span to align to the left and the link to align to the right.

http://jsfiddle.net/Lkfacta3/

h1>span {    text-align: left;  }  h1>a {    text-align: right;  }
 <h1><span>Align me left</span> <a href="">align me right</a></h1>

This is the desired result:

desired-result

like image 964
Omar Avatar asked Apr 11 '15 00:04

Omar


People also ask

How do you align right and left in the same line in CSS?

The style attribute can be used to align html text. Style attributes specify inline styling for an element. The HTML *p style attribute is used with the CSS property text-align for the center, left, and right alignments.

How do I align two elements on the same line in CSS?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.

How do you align two elements left and right?

You can use CSS display:table + display:table-cell . Or, do it with display:inline-block . Note, either way above will make the <a> clickable area larger, wrap it into another <span> if you need to correct that. The display: inline-block; solution worked for me.

How do I move a div to the right without floating?

If you need to align it to the right, then set margin-left: auto and margin-right: 0; . Basically, to align a block element to one side horizontally, set the margin of that side to 0 and the opposite side to auto.


1 Answers

You can use some flexbox magic:

h1 {    display: flex;    justify-content: space-between;  }
<h1>    <span>Align me left</span>    <a href="">align me right</a>  </h1>
like image 122
Oriol Avatar answered Oct 22 '22 02:10

Oriol