Using the position property of relative on a DIV element causes a gap to appear between the DIV and the next

Two div elements are placed consecutively. When I move the first div using position: relative and top: -60px, it creates a gap between them.

Take a look at this example here: https://codepen.io/dusannis/pen/oNgBpoK

If you notice, there is a noticeable gap between the red and yellow divs. Is there a CSS property that can be added to the parent div to eliminate this gap or achieve a similar effect?

Here is the HTML structure:

<body>
  <div class="container">
    <div class="div-1">
      <p>something here</p>
    </div>

    <div class="div-2"></div>
  </div>
</body>

And here is the corresponding CSS:

body {
  background: blue;
  padding: 60px;
}
.div-1 {
  padding: 60px;
  position: relative;
  top: -50px;
  background: red;
}
.div-2 {
  height: 50px;
  background: yellow;
}

Answer №1

Utilize negative margin instead of using relative positioning.

body {
  background: blue;
  padding: 60px
}

.div-1 {
  padding: 60px;
  /* position: relative; --> not necessary */
  margin-top: -50px;
  /* modify this */
  background: red;
}

.div-2 {
  height: 50px;
  background: yellow;
}
<div class="container">
  <div class="div-1">
    <p>something here</p>
  </div>

  <div class="div-2"></div>
</div>

Check out the Codepen Demo showcasing the effects of different methods for "moving" elements:

"Relative Position vs Margin vs Transform".

Answer №2

If you want to align the second div, try giving it the same top/position values as the first div:

.box-1 {
  padding: 30px;
  position: relative;
  top: -30px;
  background: green;
}

.box-2 {
  position: relative;
  top: -30px;
  height: 40px;
  background: orange;
}

Another option is to add an inner div and apply padding there, then remove the padding from the parent and body (or adjust as needed):

<body>
  <div class="wrapper">
    <div class="box-1">
      <div class="inner-box-1">
         insert content here
       </div>
    </div>

    <div class="box-2"></div>
  </div>
</body>

body {
  background: lightgray;
  padding: 15px;
}
.box-1 {
  position: relative;
  background: green;
}
.inner-box-1 {
  padding: 30px;
  background: green;
}
.box-2 {
  height: 40px;
  background: orange;
}

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

Modifying text size using JavaScript string manipulation

I'm currently experimenting with a countdown script, but I'm struggling to change the size of the numbers displayed. Can anyone help me find where I can adjust the font and font size in this script? var eventdate = new Date("February 20, 2014 11 ...

Arrange two divs on either side of the center div when on a smaller screen

Looking for a straightforward approach to achieve this. Imagine it as follows: [A][__B_][A] to [A][A] [__B_] I hope that explanation is clear, but I'm happy to provide more details if necessary. Thank you in advance! ...

Ways to activate side-to-side scrolling?

Here on this specific page, I am looking to showcase the SPECIAL and RECOMMENDED sections together in a single line with horizontal scrolling. This is my current CSS: .box-product { width: 100%; overflow: auto; } I have attempted implementing it like th ...

Tips for adjusting the width of a table

add your image description hereImagine a scenario where there is a table featuring two columns. <table> <tr><td>Email</td> <td></td></tr> <tr><td>Full name</td> <td></td></tr> ...

I am attempting to dynamically align the images of two articles. The exact heights of the containers are not specified. Can anyone provide guidance on how to achieve this?

My code snippets: <article class="featured"> <img src="http://www.placehold.it/300x100/ff0000"> <p> <!--Text--> </p> </article> <article class="sub-featured"> <img src="http://www.placeh ...

The error message InvalidCharacterError is displayed when the attempt to create a new element using the 'createElement' method on the 'Document' object fails. This is due to the tag name provided ('/static/media/tab1.fab25bc3.png') not being a valid name

Hey everyone! I'm new to using React and I decided to try cloning Netflix by following a tutorial on YouTube. However, I've encountered an issue with rendering an image in a functional component. The error message I'm receiving is as follow ...

CSS fixed positioning involves positioning an element relative to the

My website includes a div positioned below the header that contains quick links. I have implemented the solution from http://davidwalsh.name/persistent-header-opacity to ensure this div remains on-screen at all times. However, I would like it to appear a ...

"Interactive bootstrap tabs nested within a dropdown menu that automatically close upon being clicked

Hey, I've got a dropdown with tabs inside. When I click on certain tabs within it, they close but the content inside the tabs that I click on changes, so it's functioning properly. However, the issue is that it closes when I want it to stay open ...

Ways to enhance widget titles with borders on Blogger

As a Blogger user, I am looking for guidance on how to add a border around the title of each widget/gadget. When I place widgets such as About Me, Follow Us, etc., in my sidebar, I want them to have borders like in this image. While I know how to customize ...

Expanding the <li> elements to create a wide horizontal navigation menu that fits within a 960 grid layout with 12 columns

I'm having trouble making the individual "li" elements inside the "nav" element stretch to fill a 600px space defined by the class "grid_8" in the HTML. I've tried setting width:100%; for the nav, li { width: 100%;} and various other solutions wi ...

Guide on developing a JavaScript script for implementing across numerous Bootstrap modals within a single webpage

I have been working on setting up a page with 14 different modals. Initially, all the modals were opening normally, but I recently discovered a way to make them draggable when opened. After some trial and error, I managed to implement this feature successf ...

Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS. Grid: Here's an example o ...

Half-extended Bootstrap 4 container reaching the edge of the page

I'm looking to create a layout with a .container that consists of two distinct halves. The left half should function as a standard .col-6, while the right half should extend all the way to the edge of the screen, similar to a .col-6 within a .contain ...

Styling child elements in Angular using css from its parent element

I have a question: Regarding the structure below <component-parent> <first-child> <second-child> <third-child></third-child> </second-child> </first-child> </component-parent> Now I want t ...

Tips for customizing the appearance of a specific cell in the first column of a Material UI table

Currently, I am modifying the material ui table found in this codesandbox link. My goal is to adjust the style of the first column's cell in the table when it is selected to match the image provided below. It seems like I may be making a mistake some ...

Exploring the use of color in text embellishments

Is it possible to change the color of the underline on text when hovering, while keeping it different from the text color? I have successfully achieved this in Firefox using the property "-moz-text-decoration-color". However, this does not work in other m ...

Highlight the parent item when the child item is being hovered over

I am working on a menu that has 2 submenus. I want to use jQuery to highlight the item when it is hovered over. However, I am struggling with how to also highlight the parent item when the cursor is on the child item. The class I am using for highlighting ...

Using the <blink> tag on Internet Explorer

Unfortunately, Internet Explorer does not support the <blink> tag or the text-decoration:blink; style in CSS. Are there any alternative methods for creating blinking text in IE? ...

Implement a CSS class specifically for products with low stock within the single product page on WooCommerce

I need some assistance with adding a class to 'Low stock' in the code snippet below, as I am not very familiar with php. My goal is to style the Low Stock text in WooCommerce to appear orange using CSS. function change_stock_text( $availability ...

Different ways to adjust the appearance of table borders in HTML

I find myself at a standstill, hence why I am reaching out with this question. This Table presents itself with various border sizes and colors, along with similar complexities in the background of its cells. Could anyone provide me with guidance on how t ...