[css] How do I right align div elements?

The body of my html document consists of 3 elements, a button, a form, and a canvas. I want the button and the form to be right aligned and the canvas to stay left aligned. The problem is when I try to align the first two elements, they no longer follow each other and instead are next to each other horizontally?, heres the code I have so far, I want the form to follow directly after the button on the right with no space in between.

_x000D_
_x000D_
#cTask {_x000D_
  background-color: lightgreen;_x000D_
}_x000D_
_x000D_
#button {_x000D_
  position: relative;_x000D_
  float: right;_x000D_
}_x000D_
_x000D_
#addEventForm {_x000D_
  position: relative;_x000D_
  float: right;_x000D_
  border: 2px solid #003B62;_x000D_
  font-family: verdana;_x000D_
  background-color: #B5CFE0;_x000D_
  padding-left: 10px;_x000D_
}
_x000D_
<!DOCTYPE html>_x000D_
<html lang="en">_x000D_
<head>_x000D_
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>_x000D_
  <script type="text/javascript" src="timeline.js"></script>_x000D_
  <link rel="stylesheet" href="master.css" type="text/css" media="screen" />_x000D_
</head>_x000D_
_x000D_
<body bgcolor="000" TEXT="FFFFFF">_x000D_
  <div id="button">_x000D_
    <button onclick="showForm()" type="button" id="cTask">_x000D_
        Create Task_x000D_
    </button>_x000D_
  </div>_x000D_
  <div id="addEventForm">_x000D_
    <form>_x000D_
      <p><label>Customer name: <input></label></p>_x000D_
      <p><label>Telephone: <input type=tel></label></p>_x000D_
      <p><label>E-mail address: <input type=email></label></p>_x000D_
    </form>_x000D_
  </div>_x000D_
  <div>_x000D_
    <canvas id="myBoard" width="1000" height="600">_x000D_
      <p>Your browser doesn't support canvas.</p>_x000D_
    </canvas>_x000D_
  </div>_x000D_
</body>_x000D_
</html>
_x000D_
_x000D_
_x000D_

This question is related to css html

The answer is


I know this is an old post but couldn't you just use <div id=xyz align="right"> for right.

You can just replace right with left, center and justify.

Worked on my site:)


If you are using bootstrap, then:

<div class="pull-right"></div>

Floats are okay, but problematic with IE 6 & 7.
I'd prefer using the following on the inner div:

margin-left: auto; 
margin-right: 0;

See the IE Double Margin Bug for clarification on why.


If you have multiple divs that you want aligned side by side at the right end of the parent div, set text-align: right; on the parent div.


You can simply use padding-left:60% (for ex) to align your content to right and simultaneously wrap the content in responsive container (I required navbar in my case) to ensure it works in all examples.


If you don't have to support IE9 and below you can use flexbox to solve this: codepen

There's also a few bugs with IE10 and 11 (flexbox support), but they are not present in this example

You can vertically align the <button> and the <form> by wrapping them in a container with flex-direction: column. The source order of the elements will be the order in which they're displayed from top to bottom so I reordered them.

You can then horizontally align the form & button container with the canvas by wrapping them in a container with flex-direction: row. Again the source order of the elements will be the order in which they're displayed from left to right so I reordered them.

Also, this would require that you remove all position and float style rules from the code linked in the question.

Here's a trimmed down version of the HTML in the codepen linked above.

<div id="mainContainer">
  <div>
    <canvas></canvas>
  </div>
  <div id="formContainer">
    <div id="addEventForm"> 
      <form></form>
    </div>
    <div id="button">
      <button></button>
    </div>
  </div>
</div>

And here is the relevant CSS

#mainContainer {
  display: flex;
  flex-direction: row;
} 

#formContainer {
  display: flex;
  flex-direction: column;
}

Old answers. An update: use flexbox, pretty much works in all browsers now.

_x000D_
_x000D_
<div style="display: flex; justify-content: flex-end">_x000D_
  <div>I'm on the right</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

And you can get even fancier, simply:

_x000D_
_x000D_
<div style="display: flex; justify-content: space-around">_x000D_
  <div>Left</div>_x000D_
  <div>Right</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

And fancier:

_x000D_
_x000D_
<div style="display: flex; justify-content: space-around">_x000D_
   <div>Left</div>_x000D_
   <div>Middle</div>_x000D_
  <div>Right</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Maybe just:

  margin: auto 0 auto auto;

You can use flexbox with flex-grow to push the last element to the right.

<div style="display: flex;">
  <div style="flex-grow: 1;">Left</div>
  <div>Right</div>
</div>

Simple answer is here:

<div style="text-align: right;">
    anything:
    <select id="locality-dropdown" name="locality" class="cls" style="width: 200px; height: 28px; overflow:auto;">
    </select>
</div>

Do you mean like this? http://jsfiddle.net/6PyrK/1

You can add the attributes of float:right and clear:both; to the form and button


Other answers for this question are not so good since float:right can go outside of a parent div (overflow: hidden for parent sometimes might help) and margin-left: auto, margin-right: 0 for me didn't work in complex nested divs (I didn't investigate why).

I've figured out that for certain elements text-align: right works, assuming this works when the element and parent are both inline or inline-block.

Note: the text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.

An example:

<div style="display: block; width: 80%; min-width: 400px; background-color: #caa;">
    <div style="display: block; width: 100%">
        I'm parent
    </div>
    <div style="display: inline-block; text-align: right; width: 100%">
        Caption for parent
    </div>
</div>

Here's a JS Fiddle.