Prevent overlapping of divs

Having some trouble with my portfolio page. I've got a section set up with nested divs, but when I try to adjust the position of the divs, they end up on top of each other. Can anyone offer some guidance?

#portfolio {
  width: 650px;
  background-color: white;
  margin-left: 75px;
  margin-top: 120px;
  margin-right: 40px;
  margin-bottom: 200px;
  padding: 15px;
  float: left;
  border: 1px solid #dddddd;
  overflow: auto;
  clear: both;
}

#blog {
  position: relative;
  float: left;
  width: 40%;
}
#blog img{
  float: left;
  width: 100%;
  margin-right: 10px;
  position: absolute;
}
#blog p {
  margin: 0;
  position: absolute;
  top: 125px;
  color: white;
  background-color: #41AAA5;
  width: 100%;
  padding-top: 7px;
  padding-bottom: 7px;
  text-align: center;
  font-size: 20px;
}
#hangman {
  position: relative;
  float: right;
  width: 40%;
}
#hangman img{
  float: left;
  width: 100%;
  position: absolute;
}
#hangman p {
  margin: 0;
  position: absolute;
  top: 125px;
  color: white;
  background-color: #41AAA5;
  width: 100%;
  padding-top: 7px;
  padding-bottom: 7px;
  text-align: center;
  font-size: 20px;
}
#playlist {
  position: relative;
  float: left;
  width: 40%;
}
#playlist img {
}
#playlist p {
}
<section id="portfolio">
  <div id="blog">
    <img src="assets/images/icon1.jpg">
    <p>Blog</p>
  </div>
  <div id="hangman">
    <img src="assets/images/icon2.jpg">
    <p>Hangman Game</p>
  </div>
  <div id="playlist">
    <img src="assets/images/icon3.jpg">
    <p>Playlist</p>
  </div>
  <div id="maps">
    <img src="assets/images/icon4.jpg">
    <p>Map Page</p>
  </div>
    <div id="pets">
    <img src="assets/images/icon5.jpg">
    <p>Pets</p>
  </div>
</section>

Answer №1

The issue with the positon:absolute styling on the img tags in both the blog and hangman divs was causing overlapping elements. When using position:absolute, height is not accounted for which leads to layout problems like this.

Below is the updated code:

#portfolio {
  width: 650px;
  background-color: white;
  margin-left: 75px;
  margin-top: 120px;
  margin-right: 40px;
  margin-bottom: 200px;
  padding: 15px;
  float: left;
  border: 1px solid #dddddd;
  overflow: auto;
  clear: both;
}

#blog {
  position: relative;
  float: left;
  width: 40%;
}

#blog img {
  float: left;
  width: 100%;
  margin-right: 10px;
}

#blog p {
  margin: 0;
  position: absolute;
  top: 125px;
  color: white;
  background-color: #41AAA5;
  width: 100%;
  padding-top: 7px;
  padding-bottom: 7px;
  text-align: center;
  font-size: 20px;
}

#hangman {
  position: relative;
  float: right;
  width: 40%;
}

#hangman img {
  float: left;
  width: 100%;
}

#hangman p {
  margin: 0;
  position: absolute;
  top: 125px;
  color: white;
  background-color: #41AAA5;
  width: 100%;
  padding-top: 7px;
  padding-bottom: 7px;
  text-align: center;
  font-size: 20px;
}

#playlist {
  position: relative;
  float: left;
  width: 40%;
}

#playlist img {}

#playlist p {}
<section id="portfolio">
  <div id="blog">
    <img src="https://dummyimage.com/600x400/000/fff&text=blog">
    <p>Blog</p>
  </div>
  <div id="hangman">
    <img src="https://dummyimage.com/600x400/f00/fff&text=hangman">
    <p>Hangman Game</p>
  </div>
  <div id="playlist">
    <img src="https://dummyimage.com/600x400/0f0/fff&text=playlist">
    <p>Playlist</p>
  </div>
  <div id="maps">
    <img src="https://dummyimage.com/600x400/00f/fff&text=maps">
    <p>Map Page</p>
  </div>
  <div id="pets">
    <img src="https://dummyimage.com/600x400/ff0/fff&text=pets">
    <p>Pets</p>
  </div>
</section>

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

CSS - the styling properties of the class should not affect the child components

I am working on a Parent component that contains two child components. Each component utilizes accordion-groups for its functionality. I have created a specific class for styling purposes in my stylesheets, but I want this class to only apply to the parent ...

Prevent the Icon in Material UI from simultaneously changing

I'm working on a table where clicking one icon changes all icons in the list to a different icon. However, I want to prevent them from changing simultaneously. Any suggestions on how to tackle this issue? Code: import React from 'react'; im ...

retrieving data from a separate div and injecting it into the current page using an ajax request

I have a script that calls the HTML of a div from another page to display on the current page. Here is the code snippet: <script> $(document).ready(function($)) { $('#add').click( function() { $. ...

Alter the background image of a DIV based on the selected menu item

I am working on a project that involves a div element with the class "jumbotron" which currently has a background image applied to it. Additionally, I have a menu consisting of 6 items that I would like to interact with. My goal is to dynamically change ...

Generating a list containing sub-items extracted from a JSON structure

I have created a list using the code provided in this fiddle. <ul></ul> var myList = [{ "title": "Home", "sub": 0, "url": "/home", "show": 1 }, { "title": "News", "sub": 0, ...

Ensure there is a blank space preceding a table only when the table is being physically printed

I am currently in the process of developing a report card system for students in grades K-6. The report card will display specific tables based on the student's grade level. For example, a 5th grader may not have a "Reading Stage" table displayed on t ...

Prevent Repetition of Meta Descriptions and Keywords within Next.js

I'm currently working on my website using Next.js. In order to enhance the SEO performance of my site, I am making an effort to steer clear of duplicate meta tags. My Inquiry According to Next's official documentation, they suggest that using t ...

Enhance user experience by enabling clickable links in Angular comment sections

Currently, I'm developing an application that allows users to add comments to specific fields. These comments may contain links that need to be clickable. Instead of manually copying and pasting the links into a new tab, I want the ability to simply c ...

Assistance Needed with XPATH and CSS for Selenium Automation

Can anyone assist me in finding the appropriate XPATH/CSS locator to extract text from the structure below? <div class="page-header song-wrap"> <div class="art solo-art"> <div class="meta-info"> <h1 class="page-title"> Zehnaseeb I ...

Can the @font-face src URL be modified?

We have integrated FontAwesome with Bootstrap on our website. However, we encountered an issue when using FA with a custom minifier as it tries to load fonts from a relative path, leading to a 404 error due to the structure of the minified URL. To address ...

Difficulties encountered when attempting to use a background image for shadow effects within a CSS design

I have been learning from a tutorial on how to create a fixed tabless CSS layout, starting from Photoshop and ending with HTML+CSS code. Here is the final example suggested by the tutorial (how it should look like in the end): And this is my own version ...

Troubleshooting: JSColor Is Failing to Function Properly in Volusion's

Recently, I've encountered some issues with the JSColor plugin () on the Volusion e-commerce platform. Despite having successfully used it before, getting it to load correctly seems to be a challenge. My current task involves working on a test produc ...

Creating a user-friendly interface for the admin to easily upload photos by implementing a php/bootstrap/js code within the panel

I'm currently in the process of creating an online website as part of my thesis project. I've been researching this specific code, but unfortunately, I haven't been able to find a solution. In the admin section of the site, I need to enable ...

Keep JS Accordion open when links are clicked

Utilizing PHP, I am fetching items from a database and creating an HTML table to display each item. Adjacent to the HTML table, there is a side menu with an accordion function implemented in JavaScript. Within each accordion section, there are links for ...

The appearance of the Bootstrap button is not displaying correctly

My current button looks like this: https://i.sstatic.net/PyP8v.png As seen in the image above, there is something strange at the back of the button. How can I resolve this issue? I am working on a small project using Django and Bootstrap. Thank you in a ...

The server is currently pointing towards my local C drive directory instead of the desired message location

My goal is to create a functionality where, upon clicking the calculate button (without performing any calculations yet), the user will be redirected to a new screen displaying a response message that says "Thanks for posting that!". However, instead of th ...

Display properties of a JSON object using VUEJS

How can I efficiently read JSON data in my VueJS code? Here is the code snippet: <template> {{data}} </template> <script> import axios from 'axios'; export default { data() { return { data: null }; }, mou ...

Tips for properly positioning the ko validation message in case it is lengthy

I have implemented ko validation messages in order to validate input fields. Can anyone provide guidance on how to align the validation message correctly within the specified location? <input id="personName" class="form-control placeholder has-error" t ...

What is the best way to darken the background when an alert is displayed and disable the dimming effect when the alert is closed?

Here's the JavaScript snippet I'm using: reset.addEventListener("click", function(){ document.querySelector("#body").classList.add("darkenPage"); myReset(); alert("Reset Successful!!"); document.querySelector("#body").classList.re ...

The total height of the document's body in jQuery is not equal to the sum of the viewport height and the window's scroll top position at the bottom of the document

Why does the document height appear smaller than the window scroll top value plus the viewport height when I reach the end of the document? Shouldn't they be equal? I've been struggling with this issue for hours and can't seem to figure it o ...