Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aligning a div to the right side of another div

I want to align a div to the right side of an other div using margin-left: auto; but for some reason this does nothing. If the div is not contained by another div, this approach works flawlessly.

This is an example of the problem:

<!DOCTYPE html>
<html>
<body>
    <style>
        #inner {
            margin-left: auto;
        }
    </style>

    <div id="outer">
        <div id="inner">
            Text
        </div>
    </div>
</body>
</html>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add a width to the inner div (#inner) and the margin will take up the remaining space.

#inner {
    margin-left: auto;
    width: 50%;
    border: 1px solid red;
}
  <div id="outer">
      <div id="inner">
          Text
      </div>
  </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading