Overlapping divs are observed when utilizing absolute positioning in CSS

I am facing a challenge with placing the div identified by our_team below the div with the ID of services. When I use absolute positioning, the former ends up overlapping the latter.

The services div transitions to display information when hovered over due to its absolute positioning. On the other hand, the our_team div only contains header and paragraph tags but still overlaps the first div.

If I try removing the absolute positioning, the arrangement of the divs is correct, but it results in white blocks when flipped. https://i.sstatic.net/9TIaw.png

However, upon adding the absolute positioning, the divs end up overlapping each other. https://i.sstatic.net/CVRW1.png

For the HTML & CSS code, you can refer to this link: https://jsfiddle.net/dasasathyan/8rjoqdnt/

Answer №1

Struggling to follow the original format? Consider this alternative approach using flexbox in HTML:

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92ddcacc92959295">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46190616090505061728162707020244">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>

</head>

<body>
  <div id="main_container">
    <div id="services">
      <div id="row1_cards">
        <div id="left_card">
          <div class="flip-card-front">
            <h3> Some Heading 1 that flips of first DIV </h3>
            <h6>1st Heading Description</h6>
          </div>
          <div class="flip-card-back">
            <ul class="services_list">
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
            </ul>
          </div>
        </div>

        <div id="right_card">
          <div class="flip-card-front">
            <h3> Some Heading 2 that flips of first DIV </h3>
            <p>2nd Heading Description</p>
          </div>
          <div class="flip-card-back">
            <ul class="services_list">
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
              <li>Some List Item</li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    <div id="our_team">
      <div id="row2_team">
        <div class="col">
          <p>2nd Div that should come under the 1st Div</p>
        </div>
        <div class="col">
          <p>2nd Div that should come under the 1st Div</p>
        </div>
      </div>
    </div>
  </div>
</body>

Take on a new challenge with this CSS layout:

#main_container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  width: 750px;
  height: auto;
}
#row1_cards {
  display: flex;
  flex-direction: row;
  gap: 50px;
}
#row2_team {
  display: flex;
  flex-direction: row;
  gap: 50px;
}
#left_card {
  width: auto;
  height: 200px;
  transform-style: preserve-3d;
  transition: transform 0.6s;
}
#right_card {
  width: auto;
  height: 200px;
  transform-style: preserve-3d;
  transition: transform 0.6s;
}
.flip-card-front {
  position: relative;
  width: inherit;
  height: inherit;
  padding: 45px 20px 0 20px;
  background: linear-gradient(to bottom, #0068a5 0%, #003367 100%);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.flip-card-back {
  position: relative;
  top: -200px;
  width: inherit;
  height: inherit;
  background-color: #2980b9;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transform: rotateY(180deg);
}
#left_card:hover {
  transform: rotateY(180deg);
}
#right_card:hover {
  transform: rotateY(180deg);
}

Answer №2

To avoid issues with defining heights for elements, it's best to steer clear of absolute positioning (as demonstrated). A more effective approach would be to utilize a different method for positioning and flipping your cards using flex.

You can create transitions using z-index values (e.g.

transition: z-index, transform 0.6s;
) and adjust the element positions like so:

.flip-card-back {
    background-color: #2980b9;
    transform: rotateY(180deg) translate(100%, 0);
}

This way, you can eliminate the need for using position: absolute entirely. I've made some tweaks to the fiddle to demonstrate this, and you can further customize the styling to fit your requirements (such as making all cards the same height as the tallest element).

Modified Fiddle: https://jsfiddle.net/pie9/ftr6w9sj/

MDN docs for translate() MDN docs for z-index

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

I need to update the class definition of a navigation menu item that contains an "a" tag with the class "section-link". How can I dynamically add the class "popover-link-a" to this definition using JavaScript?

If the grid in the body contains no data, then I want to display a pop-up message on the menu li element. This pop-up is triggered by adding the class popover-link-a. The current setup looks like this: <li id="tecrube" runat="server" ...

How to trigger the submission of a form within the submission event of another form using jQuery

I am encountering an issue with having two forms on a single page. Whenever I click on the submit button of the parent form, the parent form's submit event is triggered. In this event, I have included logic to submit the second form using the submit m ...

Invalid argument type and beyond acceptable range: file:///c:/Tools/MYScript.hta

My task involves creating a script to query an MSSQL database and display its content. However, I keep encountering the error message stating Argument wrong type and that it is out of acceptable range. Can someone please assist me with this issue? Below i ...

Determining the true width of a span element using jQuery

I am trying to determine the actual width of a span element within a div, but when I attempt to do so, it gives me the width of the entire div instead. Here is the code I am working with: <div class="content"> <span class="type-text" id="ta ...

Tips for getting a browser to clear the cache specifically for the .html files on your website

After making updates to a client's website, I'm encountering an issue where the browser is displaying a cached version of the site. The website consists of static .html files. Although clearing my browser's cache resolves the issue for me, I ...

Tips for transforming an SQL Server query into JSON format

I need to construct a table in a view utilizing the output of this particular SQL Query SELECT f.empresaid, e.fantasia AS Company, f.filialid, f.fantasia AS Branch, u.consultorid ...

24-hour countdown tool featuring a visual progress bar

Currently, I am in the process of developing an application aimed at assisting individuals in either forming new habits or breaking old ones. In this application, I am looking to implement a countdown timer that ticks down daily; with the help of this help ...

Adding Edit and Delete Functionality to MySQL Database using PHP

Struggling to implement edit and delete buttons in an HTML table populated with values from a MySQL database. The goal is to send the "eventID" to the relevant PHP page upon clicking these buttons. Any assistance on how to resolve this issue would be highl ...

What is the best way to eliminate empty space at the bottom of a page that is being caused by a button?

I have identified the reason for the blank space at the bottom of my page - it's due to a sticky "back to top" button. However, I am unsure how to resolve this issue. Here is the layout of the page: <nav> navbar </nav> <div> car ...

Opening a PHP file without performing thorough validation in JavaScript can lead to security vulnerabilities

I am in need of some assistance. I am currently working on a form and seem to be encountering an issue. Whenever I click on the submit button without checking the full validation, it performs the PHP action. For instance, when I input a serial number into ...

The vertical alignment of the button text with SVG is not properly aligned

I'm facing a challenge in vertically centering text in a button while also using SVG. The text is centered properly when the SVG is removed. How can I maintain the SVG size and still achieve vertical text centering? https://i.sstatic.net/PJRtR.png ...

Preventing document.getElementById from throwing errors when the element is null

Is there a way to handle null values returned by document.getElementById in JavaScript without adding if statements or checks to the code? I'm looking for a solution that allows the execution of subsequent JavaScript calls even after encountering a nu ...

Creating anchor links with #id that function correctly in an Angular project can sometimes be challenging

My backend markdown compiler generates the HTML content, and I need Angular to retrieve data from the server, dynamically render the content, and display it. An example of the mock markdown content is: <h1 id="test1">Test 1<a href="#test1" title ...

Leveraging .Net ConfigurationManager.AppSettings in Cascading Style Sheets

I am facing an issue where I have a specific color key in my AppSettings for the company's brand color, which needs to be applied to a CSS class. The challenge is how to incorporate this key into my stylesheet. Is there a way to access the Configurati ...

Arrange a cluster of CSS table cells onto a fresh line

Currently, I am exploring CSS properties to create the visual appearance of a table using two levels of DOM elements. The highest-level element wraps around its child elements, which are styled to resemble a flat list. For example: <div class="t"> ...

Discrepant alignment of form group among the other elements in Bootstrap CSS

Currently, I'm working on an HTML horizontal form that includes buttons and text inputs. I'm using Bootstrap 3 for this project. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/> <fo ...

HTML Brickwork Picture Arrangement

I recently integrated this code into a WordPress theme and encountered an issue where the new images are displaying vertically instead of horizontally. I am seeking assistance in aligning the new images to appear on the green line instead of the red line a ...

Only referring to a CSS class within a particular element

I have defined the following CSS: .grade a{ color: white; } It is working, but maybe too well... Now, I have an HTML structure like this: <li class="grade"> <a><i class="fa fa-star fa-fw"></i></a> My Text < ...

Creating two columns using React and Material-UI Grid while utilizing just one map function for iteration

I am facing a challenge with Material-UI Grid. I want to display the information from my map function in two columns instead of one. I tried adding another Grid item sm={6} and using the same map function, which resulted in two columns but with identical i ...

PHP code to create a search form with a dropdown menu

I'm currently working on developing a simple advanced search feature where users can input a keyword and select a category from a dropdown menu. The search functionality will then display results that match both the selected category and keyword in th ...