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

Issues with the FlexBox styling of Bootstrap 4 Modals in Internet Explorer causing malfunction

I have designed a confirmation dialog using Bootstrap 4 with the following code snippet: <div class="modal fade show" id="completeAlertDialogue" role="dialog" style="display: block;"> <div class="modal-dialog"> <div class="modal-co ...

Safari on iOS 11.4 ignoring the 'touch-action: manipulation' property

I ran into an issue while developing a React web app that I want to work seamlessly across different platforms. My goal was to allow users to interact with a div element by double-clicking on desktop and double-tapping on mobile devices. However, when tes ...

Tips for dynamically retrieving a CSS file for an HTML page

With multiple html pages containing the same navbar, I made the decision to place the navbar in its own file called navbar.html and use ajax with jquery.get() to dynamically load it onto each page. This prevents redundant code across all pages. Currently, ...

HTML - Align 3 tables horizontally with text below

I have successfully floated 3 tables next to each other using <table style="float:left;">. However, I am facing an issue where some text is being wrapped to the right side of the right-most table instead of being left-justified at the very ...

Conceal the initial modal beneath the overlay when the second modal is activated

I have a situation where I am using a modal that contains a button to trigger a second modal. The issue is that the overlay of the second modal does not hide the first modal, it simply darkens the background. How can I make the overlay of the second modal ...

Challenge with the Bootstrap 3 grid structure involving nesting

On my .aspx page, I have the code snippet below: .cDiv { background-color: gray; } .tColor { background-color:yellow; } .tColor2 { background-color:blue; } <div class="container"> <div class="row"> <div class="col-sm-9 tColor" ...

When it comes to choosing between CSS and Angular Animations for supporting animations on both browsers and NativeScript mobile applications, which approach is more effective?

Are angular animations my only option or can I also utilize css animations with native elements on mobile devices? How are css animations from an angular application translated to native mobile, and is this method less effective than angular animations? I ...

adjust the size of a form field to match the background image dimensions

I'm encountering an issue while trying to solve this particular challenge: My goal is to integrate a login box onto a full-screen image. Although I've come across numerous methods for incorporating an image into a form field, my task requires me ...

Simple Design Techniques for HTML Tables

I seem to be having trouble with the table styles when I include text in the <td> section: <td rowspan="3"> 30th May 2014 17:35... What could be the issue? <style type="text/css"> .tftable {font-size:12px;color:#333333;width:100%;borde ...

Expand a section of the background picture

I am working with an image that contains multiple flag images: https://i.stack.imgur.com/xg0iM.jpg I have a div element with the dimensions: width:100px; height:50px. My goal is to resize each flag to fit inside it: .flag{ wi ...

Hover over this area to reveal the hidden text as it slides into view

My goal is to hover over truncated text and reveal the full length of the dynamic text. However, I am struggling with removing extra space at the end of shorter text when hovered and displaying all the text for longer ones. I have a solution that almost w ...

"Troubleshooting a matter of spacing in HTML5 body

I've been struggling to eliminate the gap between the top of my webpage and the <div> element. Here's the code snippet causing the issue: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="v ...

Maintaining the aspect ratio of images in Bootstrap Carousel: A complete guide

Using the default carousel from Bootstrap template has been working well for me. However, I've encountered an issue where resizing the browser window distorts the image aspect ratio by squishing it horizontally. I've tried various solutions to m ...

Left-aligned arrow for Material UI select dropdown

Just starting out with material ui and I'm trying to grasp a few concepts. I have a basic select component but encountering two issues. Firstly, I'd like to move the arrow icon of the select to the left side instead of the right. Additionally, ...

Make a <div> element that has a fixed size, but allows for scrolling text inside

I have tried searching the forum for a solution to my problem, but haven't found one yet. I am trying to create two "divs" with fixed height and internal scrolling. However, whenever I add text inside one of them, its height increases accordingly. Wha ...

Top method for establishing multiple aliases for disabled elements in CSS

Is there a way to have two different disabled states for checkboxes, one with a gray background and the other with a blue background, while maintaining the same functionality? Code within file.css .overall-example input[type=checkbox]:checked + label { ...

The toggle feature of the Bootstrap responsive navbar is not functioning properly

I am currently implementing Twitter Bootstrap in a Rails project, but I'm encountering issues with the responsive navbar. When I resize the screen, the menu toggle button appears along with the navbar options open below it. Upon further shrinking, the ...

What is the best way to add a darker tint to the background behind my overlay panel in jQuery

After grabbing the panel code from the jQuery Mobile website and integrating it into my file, I am interested in changing the appearance. I want to either darken or blur the background content when the panel is displayed. I attempted to use filter:blur(5px ...

What is the best way to fit the text into a DIV row as efficiently as possible?

Looking for a solution to prevent a span from dropping down to the next line when text is too long within a fixed width and height row. The row consists of three elements, two with fixed widths and the third containing a span and text. Constraints include ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...