Ways to layer div elements in HTML

Hey there! I am currently in the process of learning how to create a simple website. So far, I have successfully created some static web pages and now I am attempting to add a dynamic element with CSS. As part of this effort, I decided to experiment with this example. My goal is to make elements overlap each other to achieve a visually appealing layout that resembles the following:

    _ ______  _ _______
   |N|De|N|De|N|Details|.....Etc
   |A|  |A|  |A|       |     
   |M|  |M|  |M|       |     
   |E|  |E|  |E|       |     
   |_|__|_|__|_|_______|

However, my current outcome looks like this:

  _ _______  _ _______
   |N|Details||N|Details|.....Etc
   |A|       ||A|       |
   |M|       ||M|       |
   |E|       ||E|       |
   |_|_______||_|_______|

I would appreciate any guidance on how to improve this design. You can view the code on jsFiddle by visiting the following link: http://jsfiddle.net/kdGCu/

Answer №1

Your CSS seems a bit messy, so let's clean it up by following the correct way. I have created an example from scratch for you to refer to.

Start by using a container with position: relative; and wrap all three divs inside it with position: absolute; assigned.

You can then utilize the top and left properties to stack them in the desired order.

View Demo (Stack Up Demo)

View Demo (Customized as per your requirements)

View Demo (Experimenting with z-index for hover effects)

In the CSS provided, I am using the nth-of-type selector to target specific types of elements, in this case, the div element.

CSS

<div class="wrap">
    <div></div>
    <div></div>
    <div></div>
</div>

.wrap {
    position: relative;
}

.wrap div {
    height: 300px;
    width: 300px;
    position: absolute;
}

.wrap div:nth-of-type(1) {
    background: #f00;
    top: 0;
    left: 0;
}

.wrap div:nth-of-type(2) {
    background: #00f;
    top: 0;
    left: 40px;
}

.wrap div:nth-of-type(3) {
    background: #0f0;
    top: 0;
    left: 80px;
}

Answer №2

After carefully reviewing the issue at hand, I believe that I have identified the problem and come up with a solution. You are very close to completion, but there is something missing. By simply adding a width attribute to the div.mainContainer>div element and making some adjustments to your .Child class, the issue should be resolved. You can see the updated code with the solution here: http://jsfiddle.net/kdGCu/2/

div.mainContainer>div {
    display:inline-block;
    float:left;
    position:relative;
    overflow:auto;
    width: 5em;
}

.Child {
    z-index:5;
}

I hope this helps resolve your issue.

Answer №3

One unique technique is to utilize negative margin in the design.

.UserProfiles {
    width:25em;
    height:350px;
    border: 3px solid blue;
    border-radius:15px;
    left:2em;
    z-index:1;
    position:absolute;
    margin-left:-25px;
}

Check out this code snippet on jsfiddle: http://jsfiddle.net/exampleLink123

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

Html flipcard malfunctioning and logo overlapping issue detected

Regarding the following code: <html> <style> .img { max-width: 100%; } .Headerstyle { color:Black; transition: transform .2s; text-align: center; margin-top: 80vh; } .Headerstyle:hover { transform: scale(1.5) ...

Hide a division when either the body or another division is clicked

<div id="container"> <div class='sub1'></div> <div class='sub2'></div> <div class='part1'></div> <div class='part2'></div> </div> When yo ...

The phenomenon of IE6 Float Drop

Having a bit of trouble with IE6; typically I'd just drop support for it, but there's a chance that many visitors will be using it on this project. If you have IE6 and want to see the issue, check out my Home Page link. The problem is that div#c ...

When the HTML and PHP code keeps running, the JavaScript text on the page updates itself

I was experimenting with threading in different languages like Java, PHP, and JavaScript. I am aware that JavaScript is single-threaded and PHP lacks robust threading capabilities. Below is the code snippet I have been working on: <body> <p id= ...

Homepage featuring a stacked image background similar to Kickstarter

I am trying to replicate the stacked image effect seen on kickstarter.com. However, I am facing an issue where the images do not overflow on the screen as desired. The arrow indicates the area that needs to be filled with the image. Check out the image he ...

My webpage layout doesn't quite accommodate my content, so I need to adjust it to be more zoom

Why does the page container load bigger than the html/body container in mobile mode, as shown in the photo? https://i.sstatic.net/fD1N4.png Here is my CSS: html { margin-top: 48px; width: 100%; } body { margin: 0; width: 100%; background: ...

Struggling to correct alignment issues with buttons within a container using Bootstrap 5

I'm struggling with a piece of html code where everything looks good except for the placement of the button. It seems to be too far away from the container. Here's the code snippet: <div class="container-fluid mt-1 d-flex align-items-cent ...

The media query for mobile is not functioning properly

Hello, I have a page with MVC that includes CSS for iPad and mobile devices. The media query for iPad (@media only screen and (min-device-width: 768px) and (max-device-width: 1024px)) works perfectly, but I am struggling to get the media query for mobile ...

Struggling to align the header of a column to the center in a mat-table that is sortable?

I am working with a mat-table that has a sortable column similar to the example shown here, but I am having trouble centering the column headers using my CSS file. Interestingly enough, I am able to achieve this when making changes in the browser debugger! ...

Conducting Node Inquiries within API Systems

If you take a look at the Reddit post here: https://www.reddit.com/r/datascience/comments/xlvr0l/who_is_applying_to_all_these_data_scientist_jobs/ Part 1: This post contains comments like the one left by "Playful_Message_7944". With the help of the Reddit ...

Arranging fixed-width <div>s in rows of four for a sequential display

Below is the code that I am working with: <div style="margin: 0 auto; display:block; width: 916px; overflow: auto;"> <?php echo ""; echo "<i>Owned: $line->phone </i><br><br>"; $query ...

jQuery toggle functioning in one scenario, but failing in another

My experience with JS/Jquery is limited which might explain why I'm struggling with this. I am attempting to hide some text on a page and then make it visible again by clicking on a toggle link. The JavaScript code below is what I have been using. The ...

What is the best way to perfectly center text within an image, maintaining both vertical and horizontal alignment, all while being contained within an <a> tag?

I've been working with this code snippet. When I set the position of .thumb-title to absolute and use bottom: 50%, it shifts upwards in relation to its own height. .project-thumb { position: relative; } .thumb-title { font: 400 1.5rem 'La ...

Dynamically linking a video URL to the <video> tag in real-time

How can I dynamically assign a video URL to the <video> tag so that the video will display in the player immediately? This is my current code: <script> jQuery( '#the-video' ).children( 'source' ).attr( 'src', &apo ...

Strategies for Updating Backgrounds on Individual WordPress Pages

I am looking to revamp a WordPress blog with 5 different pages, each requiring a unique background image. Is there a method to achieve this without altering the background element, but instead changing the background image of the #main element in my CSS? ...

Stop unauthorized access to PHP files by utilizing AJAX

Is there a way to prevent direct access to a specific PHP file named prevented.php? My approach involves a main file called index.php, which generates and stores a token in a $_SESSION variable. Additionally, there is another file called def.php that is ac ...

Define the number of columns in CSS Grid and ensure that rows wrap accordingly

tag, I need to utilize CSS grid with 12 columns while ensuring that the tables stack in a column layout when the viewport size decreases. Is it possible to accomplish this using CSS grid? My research suggests that for row wrapping, I must use either ' ...

Issue with toggling JS checkboxes selection

Seeking assistance in expanding my knowledge of Javascript. I have a form with checkboxes and a JavaScript function that allows toggling between selecting and deselecting all the boxes. Everything is functioning as intended so far. The challenge arises f ...

What is the best method for creating a header design in CSS?

My website code is pretty standard <div class="header"></div> <div class="site-inner"></div> <div class="footer"></div> How can I achieve a header background like the one shown in the image? Do I need to set the entire ...

Setting the minimum width for Cards in Bootstrap: A How-To Guide

I'm facing an issue with Bootstrap cards. When I set a minimum width for the cards, they end up overlapping as I reduce the window width. How can I ensure that the third card moves to the next row to prevent this overlapping? https://i.sstatic.net/XA ...