Home > other >  How to align two containers
How to align two containers

Time:03-04

How to align two containers to the right edge of the upper container, but at the same time, it is necessary that they are centered. Don't know maybe I can use the calc function somehow?

.container1 {
  padding: 1rem;
  max-width: 800px;
  min-height: 400px;
  margin: 3rem auto;
  border: 5px solid orange;
}

.container2 {
  padding: 1rem;
  max-width: 900px;
  min-height: 400px;
  margin: 3rem auto;
  border: 5px solid orange;
}
<div ></div>
<div ></div>

I want like that: IMAGE

CodePudding user response:

Nest your code in a parent container and use flex and define a width on that parent. Then you can remove all margins you had set on containers 1 & 2 and use margin: auto; on container2, this will horizontally align this container. Finally, just add align-items: flex-end; on the new parent you created to align the first flex-item at the end.

.container1 {
  padding: 1rem;
  max-width: 800px;
  min-height: 400px;
  border: 5px solid orange;
}

.container2 {
  padding: 1rem;
  max-width: 900px;
  min-height: 400px;
  border: 5px solid orange;
  margin: auto;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-end;
  gap: 1em;
  width: fit-content;
  margin: auto;
}
<div >
  <div >Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by
    English versions from the 1914 translation by H. Rackham.</div>
  <div >Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by
    English versions from the 1914 translation by H. Rackham.</div>
</div>

CodePudding user response:

I think that flex is a great option. Here is another option just using an outer container that has margin: auto; to center the content.

.container1 {
    padding: 1rem;
    max-width: 800px;
    min-height: 400px;
    margin: 3rem 0 3rem auto;
    border: 5px solid orange;
}

.container2 {
    padding: 1rem;
    max-width: 900px;
    min-height: 400px;
    margin: 3rem 0 3rem auto;
    border: 5px solid orange;
}

.new-container {
    margin: auto;
    max-width: 900px;
}
<div >
  <div ></div>
  <div ></div>
</div>

  • Related