Ask Your Question
1

What is the arrangement of three divs with "inline-block" property where one is positioned higher than the other two?

asked 2021-11-01 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-10-20 20:00:00 +0000

lalupa gravatar image

There are different ways to achieve this arrangement, but one possible solution is:

HTML code:

<div class="wrapper">
  <div class="top"></div>
  <div class="bottom"></div>
  <div class="bottom"></div>
</div>

CSS code:

.wrapper {
  text-align: center; /* centers the inline-blocks horizontally */
}
.top {
  display: inline-block;
  width: 100px;
  height: 100px;
  background-color: red;
  margin-bottom: 10px; /* creates space below this div */
  vertical-align: top; /* aligns this div to the top of the line */
}
.bottom {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-color: blue;
  margin-left: 5px; /* creates space between the bottom divs */
  vertical-align: bottom; /* aligns these divs to the bottom of the line */
}

This code creates a wrapper div that centers the inner divs horizontally using text-align: center. It also sets the vertical-align property to top for the top div and bottom for the bottom divs, so that they align to the respective edges of the inline line. Additionally, it adds some margin between the top and bottom divs using margin-bottom and margin-left, and sets the width, height, and background color for each div.

Here's a visual representation of the layout:

|---------|   |---------|   |---------|
|   RED   |   |  BLUE   |   |  BLUE   |
| 100x100 |   | 50x50   |   | 50x50   |
| (top)   |   | (bottom)|   | (bottom)|
|         |   | (1)     |   | (2)     |
|---------|   |---------|   |---------|

In this example, the top div is positioned higher than the other two because it has a margin-bottom of 10px that creates space below it, and it is also aligned to the top of the inline line using vertical-align: top. The bottom divs are aligned to the bottom of the inline line using vertical-align: bottom, so they appear below the top div.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-01 11:00:00 +0000

Seen: 12 times

Last updated: Oct 20 '22