How about placing one element above another? What sets apart using the margin property versus the position property for achieving this effect?

As I pondered the concept of stacking context, a question arose in my mind. Through my readings, I learned that by not applying any CSS properties that create a stacking context (such as position), it is possible to stack elements on top of each other using negative margins. For instance, you can have three divs where the third one sits atop the second and first, while the second one sits on top of the first. So far, so good.

My query is this: if we maintain the hierarchy of the three divs without altering it (with the third on top of the second and first, and the second on top of the first) using either negative margins or a CSS property like position, what differences might emerge? Could this impact responsiveness or any other aspect?

I've included some code for reference below so you can better grasp the scenario. Feel free to toggle between the different CSS code blocks within the comments line. The outcome remains the same!

.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: 1px solid black;
width: 400px;
height: 400px;
}

.container div {
border: 1px solid black;
width: 50px;
height: 50px;
}


/*Effect with POSITION*/
.container :nth-child(3) {
background-color: palegoldenrod;
position: relative;
bottom: 10px;
left: 10px;
}

/*Effect with negative MARGIN*/
/*.container :nth-child(3) {
background-color: palegoldenrod;
margin-top: -10px;
margin-left: 20px;
}*/
<div class='container'>

<div></div>
<div></div>
<div></div>

</div>

Thank you!

Answer №1

When you remove the "negative margin" rule from container2 and the "position relative" rule from container1, the difference becomes apparent.

In container1: using position relative pushes the third box downward.
In container2: applying negative margin only moves the box while the third box remains in place.

<div class="mother">
  <div class='container'>

    <div></div>
    <div><span>hover on me</span></div>
    <div></div>

  </div>

  <div class='container2'>

    <div></div>
    <div><span>hover on me</span></div>
    <div></div>

  </div>

</div>
<style>
  .mother {
    display: flex;
    flex-direction: row;
    width: 500px;
  }
  
  .container {
    background-color: crimson;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border: 1px solid black;
    width: 250px;
    height: 250px;
  }
  
  .container2 {
    background-color: crimson;
    margin-left: 5px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border: 1px solid black;
    width: 250px;
    height: 250px;
  }
  
  .container div {
    font-family: helvetica;
    display: flex;
    text-align: center;
    align-items: center;
    color: gold;
    border: 1px solid white;
    width: 60px;
    height: 60px;
  }
  
  .container2 div {
    font-family: helvetica;
    display: flex;
    text-align: center;
    align-items: center;
    color: gold;
    border: 1px solid white;
    width: 60px;
    height: 60px;
  }
  /*Effect with POSITION*/
  
  .container :nth-child(2):hover {
    border: 2px solid lightseagreen;
    background-color: gold;
    position: relative;
    bottom: 10px;
    left: 10px;
    cursor: pointer;
    color: gold;
  }
  /*Effect with negative MARGIN
.container :nth-child(1) {
background-color: gold;
margin-top: -10px;
margin-left: 20px;
}*/
  /*Effect with POSITION
.container2 :nth-child(3) {
background-color: gold;
position: relative;
bottom: 10px;
left: 10px;
}*/
  /*Effect with negative MARGIN*/
  
  .container2 :nth-child(2):hover {
    border: 2px solid lightseagreen;
    background-color: gold;
    margin-top: -10px;
    margin-left: 10px;
    cursor: pointer;
  }
</style>

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

Collision of CSS styles with a different CSS stylesheet causing conflicts

Currently, I am in the process of developing a website and have encountered a specific issue. Within my page, there are various divs and tables present. I have meticulously crafted classes and styles to customize the appearance of these elements to align ...

Applying CSS styling to PHP documents

I am a beginner, so please go easy on me. I am looking to style variables like $product_name, $author, and $details. I was thinking of styling the echos, but in this scenario, the only echo is: <?php // Establish connection with the MySQL database ...

Increase the size of a div to equal the combined width of two divs located directly below it

I need help aligning 3 divs so that the top div stretches to match the width of the bottom 2 divs combined. Here is an image showing the expected div positioning. I attempted to use display: table-row for the divs. <div id="main_div" style="display: ta ...

Arranging two <ul> elements within an angular template

I need assistance with aligning two ul blocks. Currently, they are displaying next to each other, but their alignment is starting from the bottom instead of the top: <div class="ingredients-container"> <div style="display: inline-block; width: ...

Is there a way to stack multiple Divs on top of each other using Float instead of relying on absolute positioning?

I've decided to revamp my approach and transition from using absolute positions to utilizing floats for positioning elements the way I desire. Now the challenge lies in figuring out how to float multiple divs on top of each other, allowing users to s ...

Trouble with Click event not working in Electron and mouse cursor not changing when hovering over an HTML button?

I'm in the midst of a project that involves using the electron framework. Within my project, I have an HTML file, a CSS file, and a Javascript file. The issue at hand is that when I hover over a button, the expected hand pointer icon fails to appear d ...

Altering the color of a div based on a specified value condition

I am in need of assistance with a div structure similar to this: <div id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable ></div> <div id="position" tabindex="1" class="filled-text" placeholder="Input ...

Using varied colors to style list items

Is there a way to apply three different colors to my list items? What is the best method for achieving this? This is what I have in my code: li { width: 33.333%; float: left; padding: 15px; list-style: none; } .light { background-color: #0 ...

Arranging multiple Label and Input fields in HTML/CSS: How to create a clean and

I'm struggling with HTML and CSS, trying to figure out how to align multiple elements on a page. I've managed to line up all the rows on the page, but for some reason, the labels are appearing above the input fields when I want them to appear be ...

Using inline CSS to apply conditional styles to a component in React is a great way to customize the

I'm currently working on customizing the styles of some buttons depending on their 'active' status and whether the user is hovering over them. So far, it's partially working but I'm encountering behavior that I can't fully com ...

The appearance of HTML varies depending on the type of mobile device being used

My email template is having display variations on different mobile devices, with the textbox sometimes centered instead of left aligned as intended. Any suggestions on what might be causing this issue and how to resolve it? Desired Display on All Devices ...

Dynamic divs to occupy the rest of the available vertical area

I have been experimenting with a new website layout and created a form setup. However, I am facing an issue with the fluidity of the layout. While the items are floating into position, there is a problem where if the item on the right is 400px bigger than ...

Issue with bootstrap accordion not collapsing other open tabs automatically

I'm struggling to incorporate a Bootstrap accordion collapse feature into my project, specifically with the auto-collapsing functionality. In the scenario where there are 3 divs labeled A, B, and C, if A is open and I click on B, A should automaticall ...

The SVG image does not display in browsers other than Internet Explorer (IE)

I am encountering an issue with adding a menu toggle image in SVG format to my HTML. Unfortunately, the image is not displaying as expected. Here are screenshots for comparison between Explorer and Chrome: https://i.stack.imgur.com/74sqh.png https://i. ...

Addressing a HTML/CSS navigation bar formatting glitch

I have been attempting to replicate the design from , but with the menu positioned at the top of the screen. Despite reviewing tutorials on w3schools and other sources, I am still unable to understand why it is not aligning correctly at the top. Below is t ...

Tips for creating a mobile-friendly Bootstrap carousel that is both scalable and zoomable while maintaining the default slide functionality

I have a website using Bootstrap 4.x carousel. The layout is user-scalable with the following viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2, shrink-to-fit=no">. Currently, users can pinch to z ...

Adjust the width of a div based on its height dimension

I have a div called #slideshow that contains images with a 2:1 aspect ratio. To set the height of the image using jQuery, I use the following function: Keep in mind that the Slideshow Div is always 100% wide in relation to the browser window. If the use ...

Creating a dynamic feature in React where multiple icons change color individually when hovered over, all implemented

I'm looking to customize the icons in my footer by changing their colors when users hover over them. I have already created a CSS class with the necessary hover effects, but now I want to pass a parameter in my JSX file that specifies which color shou ...

How to emphasize a clicked hyperlink in AngularJS

Here's the HTML code I've been working on: <div id="Navigation" class="md-dialog-full"> <div class="products-options"> <a ng-click='addType("Physical")' href="javascript:void(0);"> < ...

Background color vanishes (occasionally) on the contact section of my website when viewed in Chrome

My contact page has an issue that only occurs in Chrome, where the background disappears most of the time. This problem is not present in Firefox, Edge, or Safari on my TA's MacBook, but interestingly, hovering over a box resolves the issue on her dev ...