CSS div Positioning made Easy

Hello, I am a beginner in HTML and CSS and I am facing an issue with the positioning of blocks on my webpage. Can anyone provide guidance? Here is what I am trying to achieve:

The first block should be aligned to the left, the second block to the right, and the third and fourth blocks should appear below the second one. Currently, they are all displaying in a line. How can I adjust the layout so that blocks 3 and 4 are positioned beneath the second block?

.body {
  width: auto;
}

.box {
  display: inline-block;
  vertical-align: bottom;
}

.box-1 {
  background-color: rebeccapurple;
  width: 300px;
  height: 300px;
  float: left;
}

.box-2 {
  background-color: brown;
  width: 400px;
  height: 100px;
  vertical-align: top;
  /* margin-left: 40px; */
}

.box-3 {
  background-color: chartreuse;
  width: 200px;
  height: 200px;
}

.box-4 {
  background-color: coral;
  width: 200px;
  height: 200px;
}
<div class="section">
  <div class=" box box-1"></div>
  <div class=" box box-2"></div>
  <div class=" box box-3"></div>
  <div class=" box box-4"></div>
</div>

https://i.sstatic.net/fldIn.png

https://i.sstatic.net/OizZK.png

Answer №1

To avoid using float to position these elements, consider utilizing the CSS Grid layout instead:

html, body {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.section {
  /* Implementing CSS Grid layout: */
  display: grid;
  /* Defining named areas of the grid with ASCII-art style layout: */
  grid-template-areas:
      "box1 box2 box2"
      "box1 box3 box4";
  /* Setting the width of the three columns: */
  grid-template-columns: 300px 200px 200px;
  /* Specifying the height of the two rows: */
  grid-template-rows: 100px 200px;
  /* Determining the gap between Grid items for proper spacing: */
  grid-gap: 1em;
}

.box-1 {
  background-color: rebeccapurple;
  /* Positioning the element in the named grid-area: */
  grid-area: box1;
}

.box-2 {
  background-color: brown;
  grid-area: box2;
}

.box-3 {
  background-color: chartreuse;
  grid-area: box3;
}

.box-4 {
  background-color: coral;
  grid-area: box4;
}
<div class="section">
  <div class="box box-1"></div>
  <div class="box box-2"></div>
  <div class="box box-3"></div>
  <div class="box box-4"></div>
</div>

Check out the JS Fiddle demo.

Learn more about:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

If a <section> element contains a <header>, must it also include a <footer>?

Here is the current state of my code: <section id="one"> <h2>Section Title</h2> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> <p>Lorem ipsum...</p> </section> <section id="two"&g ...

Angular's Child Component At Its Best

As I develop an application with a consistent design pattern for lists of elements, I find myself creating specific components for different object types. For instance, when dealing with objects of type A, I create AComponent which takes in input a, follow ...

Guide to creating a hierarchical navigation system with HTML

Is there a way to create a menu tree using just HTML and some scripting, without downloading additional software? I tried searching on Google but all the results require downloads. Can anyone provide guidance or help with this task? Thank you in advance. ...

One crucial factor to consider when dealing with dual screen setups is the

Imagine you have the following code snippet : <ul> <li>Menu1 <ul class="submenu"> <li class="firstmenu">submenu-1</li> <li>submenu-2</li> < ...

What is the reason behind the default font size being longer than 1em?

During my experimentation, I encountered an issue. I set the height of a div to 1em, expecting it to contain the entire text within. However, it did not. In my browser, 1em is equivalent to 16px. On not specifying the height of the div, it successfully co ...

How can I ensure that my content stays fixed at the bottom of a scrolling div in Material UI React?

I have developed a React JS component using Material UI that includes a chat input feature. However, I am facing an issue where the width of the chat input is always half the screen size on desktop or mobile devices. When I try to change the width to 100%, ...

Pedaling back and forth along a sequence

Is there a way to implement forward and backward buttons for a clickable list without using arrays, as the list will be expanding over time? I have already achieved changing color of the listed items to red, but need a solution to navigate through the list ...

Using the transform property causes mix-blend-mode to become ineffective

When I combine mix-blend-mode with the transform property, the mix-blend-mode functionality ceases to work. Is there a workaround for this issue? Although similar questions have been asked before like Does CSS mix-blend-mode work with transform?, they did ...

What is the best way to apply inline styling to a video/gif embed code that is outside of the document object model (DOM)?

My collection includes a variety of embed codes from different gif/video sites, such as: <iframe src="https://player.vimeo.com/video/122375452?title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowf ...

Struggling to figure out Visual Studio Code - Having trouble loading images and fonts into my CSS file

It seems like I'm missing something here (just so you know, I'm totally new at this). I attempted to use fonts that are loaded on my computer for my CSS stylesheet (font file in my VSC), but no luck. I also tried using images from my files as bac ...

Conceal element with PHP and CSS upon filling another field

Currently working on a Dealer locator customization in Wordpress, and I have a specific requirement to adjust certain fields. One of the tasks is to conditionally hide a field based on another field's input. The situation: If the user enters data i ...

What is causing my inline JSX styling to override my media query?

After exhausting all my options and searching high and low for a solution, I am completely stuck. Despite trying various methods like importing media queries from a separate file and using ancestors to target elements, I still cannot get this to work. The ...

Diverse Values within the Inner Border

Is it possible to create an inner border with unique values for each side? For instance: Top: 20px Right: 80px Bottom: 40px Left: 10px Can you provide an example of how this can be achieved? Thank you! :) ...

Circular arrow button encased by a trio of line breaks

I'm in the process of creating a multi step form, and I am looking to incorporate a unique round arrow button surrounded by three steps (representing the number of pages) with each step changing color once it has been completed. While horizontal step ...

What is the best way to find out the exact height of the viewport or window on mobile browsers?

When working on my computer or PC, adjusting the height of a div to match the window size is simple. I just use the following CSS code: div{ height: 100vh; } But when viewing the same page on mobile browsers, the div's height extends beyond the vie ...

Using React Native to create a concise text component that fits perfectly within a flexbox with a

Within a row, there are two views with flex: 1 containing text. <View style={{ flexDirection: "row", padding: 5 }}> <View style={{ flex: 1 }}> <Text>Just a reallyyyyyyyy longgggg text</Text> </View> ...

What is the best way to retrieve the value of a div using AutoIt?

For this particular scenario, my objective is to extract a specific value from the last div element in the following list: <div id="past"> <div class="ball ball-8" data-rollid="242539">11</div> <div class="ball ball-8" data-ro ...

Is there a rationale behind using intricate CSS classes such as page-left-column-container-box-element-header for technical purposes?

What is the reasoning behind using this specific CSS and HTML structure for styling web pages? <div class="page"> <div class="page-left-column"> <div class="page-left-column-container-box"> <div class="page-left-column-con ...

Cannot apply "!important" using Jquery to a dynamically sized tag in IE11

Here is a script I have: box.copyHeight = function() { var heights = [], i, hT; for (i = 0; i < arguments.length; i++) { if ($(arguments[i])) { $(arguments[i]).css('height', ''); ...

jQuery prioritizes enforcing style over using a CSS file

I am facing an issue with a nested div functioning similar to a drop-down menu. The problem arises when I click on the #two div as I want it to disappear. To handle this, I utilized jQuery and tried using both .hide() and .slideUp(). However, upon implem ...