Skip to content Skip to sidebar Skip to footer

Boxes In Boxes + Text, Alignment On The Page Html/css

I need to arrange my text in boxes, two boxes side by side, and each box has a header-type of element at the top-right corner. This is what I have come up so far: does what I want,

Solution 1:

I highly recommend you to use a css-grid or a flexbox for modern approach. In this case a css-grid is the better approach as it can be controlled in height and width simultaniosly.

For that I wrapped your 2 boxes in another div with the class: grid-wrapper. I changed that box to a grid system by using: display: grid;. To get 2 columns you use: grid-template-columns: repeat(2, 1fr);. If you want 3 columns, you change the 2 into a 3 or any number you like for more. To seperate the boxes, you can use: grid-gap.

One thing I want to make you aware of: You had an invalid HTML use by using the ID #header twice. An ID has to be unique. ALWAYS! As such, I changed it into a class.

body {
  margin: 0;
}

.grid-wrapper {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 10px;
  padding: 10px;
}

#first,
#second {
  background: LightBlue;
  border: 1px solid black;
  padding: 10px;
}

.headers {
  width: 40%;
  height: auto;
  margin: -8px -8px0.2em0.2em;
  background: Orange;
  float: right;
  border: 1px solid black;
  text-align: center;
}
<divclass="grid-wrapper"><divid="first"><divclass="headers"> First box </div>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
    content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
    versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </div><divid="second"><divclass="headers"> Second box</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.
  </div></div>

Solution 2:

My solution is using flex.

And this solution is responsive mobile devices, without the use of media queries. Run the snippet and resize the browser window. The blocks will adapt to the size of the window.

Also, never use the id attribute multiple times. id is a unique attribute. Use class for many elements.

And always wrap your content in the main parent element.

.main {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

p {
  width: 40px;
  height: 80px;
  background-color: Orange;
  font-size: large;
  float: right;
  border: 1px solid black;
}

.headers {
  background: Orange;
  float: right;
  border: 1px solid black;
  padding: 3px;
}

#first,
#second {
  background: LightBlue;
  border: 1px solid black;
  padding: 10px;
  flex: 300px;
}
<divclass="main"><divid="first"><divclass="headers"> First box </div>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
    content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions
    have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </div><divid="second"><divclass="headers"> Second box</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.
  </div></div>

Post a Comment for "Boxes In Boxes + Text, Alignment On The Page Html/css"