Tips for preventing overlap between two divs when utilizing position: absolute in CSS

In this CodePen link [https://codepen.io/anon/pen/qvQpaY], there is an issue with overlay between the two divs that have a class name of box-container. The black rectangle is being counted as the overall height by box-container because the red rectangle is positioned absolutely.

To ensure the red rectangle appears above the black one, absolute positioning must be used. However, how can I make sure that box-container considers the red rectangle in its overall height?


        body {
          background-color: pink;
          display: flex;
          justify-content: center;
        }

        .container {
          width: 500px;
        }

        .box-container {
          margin: 10px 0;
          width: 100%;
          position: relative;
        }

        .box-text {
          width: 300px;
          height: 200px;
          background-color: black;
        }

        .box-image {
          width: 300px;
          height: 200px;
          background-color: red;
          position: absolute;
          right: 0;
          top: 60px;
          z-index: 1
        }
      
    

        <div class="container">
          <div class="box-container">
            <div class="box-text"></div>
            <div class="box-image"></div>
          </div>
          <div class="box-container">
            <div class="box-text"></div>
            <div class="box-image"></div>
          </div>
        </div>
      
    

Answer №1

If you utilize position: relative, the layout will resemble this; now the red box contributes to the total height as well; feel free to adjust its position and appearance according to your requirements.

body {
  background-color: pink;
  display: flex;
  justify-content: center;
}

.container {
  width: 500px;
}

.box-container {
  margin: 10px 0;
  width: 100%;
  position: relative;
}

.box-text {
  width: 300px;
  height: 200px;
  background-color: black;
}

.box-image {
  width: 300px;
  height: 200px;
  background-color: red;
  position: relative;
  right: 0;
  top: -100px;
  left: 150px;
  z-index: 1
}
<div class="container">
  <div class="box-container">
    <div class="box-text"></div>
    <div class="box-image"></div>
  </div>
  <div class="box-container">
    <div class="box-text"></div>
    <div class="box-image"></div>
  </div>
</div>

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

What is the method of generating an HTML tag solely using CSS?

Looking to style HTML tags without altering them in any way? Consider this example: <div id="Code_G" class="editor-group"> editor-group<br /> <div id="Code_L" class="editor-label "> editor-label </div> < ...

Backdrop behind of Bootstrap modal located back of other page contents

I'm facing some challenges after transferring a website I developed locally to a live server. The modal windows are appearing behind other content on the live server, although they work perfectly fine on the local version. Despite my attempts to adju ...

Missing ghost image appears when you drag and drop the file

New to JavaScript... As a rookie in coding, I often get interesting requests from my partner who is a kindergarten teacher. Recently, she asked me to create a "Function Machine" for her classroom activities. With some trial and error, I managed to put tog ...

Problem Alert: Click Event Not Functioning on Generated Links

Take a look at these two code snippets. Despite other jQuery functions in the same JS file working fine on the UL element, nothing seems to be happening with these. Is there something obvious that I am missing? <ul id="activityPaganation" class="paga ...

What is the best way to ensure my hover caption spans the entire size of the image?

I'm currently working on creating a hover caption that adjusts itself to fit the full width and height of an image even when it is resized. I attempted to implement a jQuery solution that I came across, but unfortunately, I am struggling to get it to ...

"Adjusting the size of a jQuery dialog for optimal viewing on

I am currently working on designing an HTML page that is responsive for both mobile and desktop browsers. One issue I am encountering involves a jQuery dialog that I need to scale properly to fit the page. I believe the problem lies within these lines of ...

Is there a way to modify the appearance of a text box to suit individual preferences?

Is it possible to alter the shape of a text box, creating a rectangle with a portion removed from one side? I have included an example image for reference. I came across clip-path: polygon in CSS, but it seems to only accept four coordinates. Thank you i ...

CSS grid is organizing itself neatly on larger screens

I'm currently facing an issue with displaying a CSS grid on my page. When viewed on desktop, the grid appears stacked up instead of in separate columns. I've tried various methods like dividing it into 4 columns and using spans for width but noth ...

Modifying a Flash Video through JavaScript

Similar Question: How can I change the source of a flash object using jQuery? UPDATE I have resolved the initial loading issue, so I removed that part from the code. I've created a flash player like this: <div id="video_content"><div i ...

Tips for extracting the chosen value from a dropdown list within a table cell using JavaScript

Here is an example of an HTML element: <td> <select> <option value="Polygon 47">Polygon 47</option> <option value="Polygon 49">Polygon 49</option> </select> </td> I am looking for a ...

What is the best way to choose an Animatedmodal that can showcase various demos while using just one ID?

I am currently customizing my website and utilizing the AnimatedModal.js framework. I have successfully displayed content in one modal, but encountered difficulty when attempting to create multiple modals as they all share the same ID. My question is, how ...

How to use JQuery to parse an external JSON file with array elements in Javascript

My goal is to extract information from an external JSON file using JavaScript, specifically an array and other elements. The JSON file I am working with is called 'TotalUsers.json' {"@version":"1.0", "@generatedDate":"12/20/10 5:24 PM", "day":[{ ...

What causes the vuetify tag not to render as HTML in the browser?

I ran into a styling issue while using Vuetify in my Nuxt 2 project. Upon inspecting the element in dev tools, I noticed that some Vuetify tags were not being converted to HTML as expected (they were displayed as div class="tag_name"). These unco ...

In a two-column layout, ensure that the text in the left box is aligned to the left and the text in the right box is aligned to the

I'm working on a flexbox layout with two columns, each containing text. The text in the left column is aligned to the left, while the text in the right column is aligned to the right. However, I want the text in the right column to switch to left al ...

Ways to adjust the select element to match the width of its current option

As I work on building a form, I'm looking for a way to make the select element adjust its width based on the length of its current option. Essentially, I want the select element to expand or contract depending on the width of its longest option. I&ap ...

Images within the signature are not appearing as intended

Trying to find a way to display a company logo in an email signature without it being blocked by default on different email clients. Initially, I attempted uploading the image to the company website, but encountered the following error: So, I decided to t ...

Tips for appending the id of an active element to a URL

My goal was to include the id value of the active element in a URL and then redirect to that URL with a button click. HTML <div class="icon" tabindex="0" id="company"> <img src="company.png"> </div> <button type="submit" onclick= ...

Ways to tackle my code's linked dropdown issue without the use of extensions

When I selected the Id Province, I couldn't select the Id City. How can I fix this issue? Controller code snippet to address this: View code snippet for the same: ...

ReactJs: Struggling to programmatically set focus on an element with React.createRef()

Looking to detect when a user clicks on an arrow using the keyboard? I stumbled upon the Arrow Keys React react module. To make it work, you need to call the focus method on a specific element. Manually clicking on an element does the trick: https://i.s ...

How can I properly integrate jQuery libraries, along with CSS, in a create-react-app project?

I'm currently working on incorporating the select2 jquery library into my React application, which was created using create-react-app. After adding jquery and select2 to my package.json file, I was able to get the javascript functionality to work pro ...