Numerous Divs Expanding to Fit Window Size

I successfully managed to make one div expand to the size of the window, filling the screen completely. However, I am facing an issue where the other divs are overlapping each other and only displaying thing 3. How can I ensure that they do not overlap and allow me to scroll through each one in order while keeping the text centered in each div?

http://jsfiddle.net/592NY/1/

My goal is to achieve:

Below is the annotated CSS:

/* Styling for each individual div with unique backgrounds */
#thing1 { 
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;           
    background: blue;
}
#thing2 {   
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;           
    background: red;
}
#thing3 {   
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;           
    background: green;
}
/* Centering the text in the div */
#text {
    width: 100px;
    height: 100px;
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

Answer №1

It appears you either have a clever logic that eludes me or you're venturing into the realm of full 3D design :D The three divs share the same z-index, with no opacity modifications, so they will simply display in the order they are listed in the HTML (moving thing 3 before thing 2 will result in thing 2 being visible). At this moment, thing 2 is positioned "on top" of thing 1, and thing 3 is on top of thing 2.
As I mentioned the 3D aspect, you can utilize Firefox's 3D view to get a better grasp of what's happening.

Update: You can achieve the desired effect by using top: 100% for the second div and top: 200% for the third, which interestingly seems to work even in IE.

http://jsfiddle.net/592NY/4/

Answer №2

http://jsfiddle.net/derekstory/592NY/2/

To achieve the desired layout without overlapping, it is recommended to remove the absolute positioning and z-index properties.

html, body {
    height: 100%;
}
#thing1 {
    top:0;
    left:0;
    width:100%;
    height:100%;
    background: blue;
}
#thing2 {
    top:0;
    left:0;
    width:100%;
    height:100%;
    background: red;
}
#thing3 {
    top:0;
    left:0;
    width:100%;
    height:100%;
    background: green;
}
#text {
    width: 100px;
    height: 100px;
    margin: auto;
    top:0;
    bottom: 0;
    left: 0;
    right: 0;
}

Answer №3

When using absolute positioning for multiple elements with the same z-index, the last one will be displayed on top of the others. By adjusting the z-index values, you can control their stacking order.

To ensure uniqueness on the page, "text" should be defined as a class rather than an ID.

http://example.com/jsfiddle123

body, html {
    height: 100%;
}
#box1 {   
            position: relative; 
            height: 100%;           
            background: blue;
}
#box2 {   
            position: relative; 
            height: 100%;   
            background: red;
}
#box3 {
            position: relative; 
            height: 100%;       
            background: green;
}
.text {
    width: 100px;
    height: 100px;
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

Setting width: 100% for DIVs is not necessary as it's the default behavior.

For a cleaner approach, assign a common class to similar boxes and style them based on their IDs:

http://example.com/anotherjsfiddle

body, html {
    height: 100%;
    margin: 0; padding: 0;
}
.boxes {
    position: relative;
    height: 100%;
}
#box1 {               
    background: blue;
}
#box2 {
    background: red;
}
#box3 {   
    background: green;
}
.text {
    width: 100px;
    height: 100px;
    margin: auto;
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0;
}

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 best method to choose the initial offspring of the primary brother or sister within a list item?

Is there a way to specifically target the nested li elements in my css selector? Check out the Demo li:first-child { background-color: rgb(236, 236, 236); list-style-type: none; margin-bottom: 3px; padding: 8px; } <ul> <li& ...

Struggling to forward JSON data via AJAX to Django without relying on jQuery

My goal is to utilize AJAX in conjunction with Django, but I want to achieve this without relying on jQuery. So far, I have made progress in sending URL encoded data from AJAX to Django successfully. However, I am currently facing an issue where I am unabl ...

Unable to configure slick carousel to a 12-column grid in Bootstrap

When attempting to set my carousel to col-xs-12, the columns do not align correctly within the slick carousel in Bootstrap. If I change it to col-xs-6, it works fine but then two grids appear in one row. I require more space for my carousel. Could I be ov ...

Display temporary image if image is not located

I attempted to utilize the onerror attribute to display a placeholder image when the desired image is not found in the folder. The image path is dynamically generated from the backend. The code snippet below shows how I implemented this: <img class=&quo ...

Struggling to perfect your CSS menu design?

For some time now, I have been experimenting with CSS menus that have three levels, but unfortunately, I am struggling to get the layout exactly how I want it. The menu structure itself is simply a series of nested unordered lists within the HTML: <ul ...

How to implement Bootstrap 5 Floating Label in Contact Form 7 for maximum impact?

I'm having trouble with the new form feature, specifically Floating labels (Bootstrap 5) in Contact Form 7 on Wordpress. The issue arises when a <p> tag is added inside the <input> tag. It seems to break the functionality, but I'm not ...

Troubleshooting problem with event triggering in Angular's ngSelect dropdown menu items

Hello, I am currently utilizing ngSelect but encountering an issue. Whenever a user hovers over an option in ngSelection, I would like to trigger an event that is created in my typescript file. I am using Angular 13, so I am unsure how to achieve this. Is ...

Building a stack of DIVs, some with fixed heights and others without, potentially using only CSS

Hello everyone! I am having trouble deciding on the best approach to achieve a layout like the one below: +------------------------------------------------------------+ | div 0 , varying height +---------------------------------------------------------- ...

Creating a full-width layout with CSS div

Seeking the most stylish method to create a layout similar to this using divs in IE7 and other modern browsers (Chrome, Firefox, etc). Appreciate your help! ...

Embed one module within another module and utilize the controller from the embedded module

I am attempting to create a module and inject it into the main module. Then, I want to inject the controller into the injected module but am facing issues. In my index.html file: <html ng-app="MenuApp"> <head> </head> <body> <d ...

Scrollable tbody in a responsive Bootstrap 4 table

Utilizing Bootstrap 4, I have implemented a responsive table that I want to have a scrollable body. The table is structured as follows: <div class="table-responsive"> <table class="table table-striped table-hover custom-table text-nowrap"> ...

Customizing the appearance of charts in AngularJS using the Chart.js

I just started experimenting with AngularJS and recently created a horizontal bar chart using Chart.js and HTML. My next step is to make the chart dynamically appear on the page with the help of AngularJS. Can someone please provide some guidance on how I ...

Utilizing Python to manipulate the 'select' tag in HTML

Currently, I am attempting to retrieve events from an HTML page located at In my efforts to choose different areas using python script, I encountered a challenge with the following snippet of HTML code: <select data-ng-options="key as value.name for ( ...

VS code showing live server as installed but failing to function properly

After installing the live server extension, I noticed that my browser is not updating when I save my HTML or other files. What could be causing this issue? ...

Ensure consistency in CSS3 background color transitions

There are multiple elements on the webpage with background transitions that change from one color to another: @-moz-keyframes backgroundTransition /* Firefox */ { 0% {background-color:#ff7b7b;} 33% {background-color:#7fceff;} 66% {backgr ...

Identify and forward to the mobile-friendly version of the site

I am currently working on a website that requires separate files for mobile view as the html layout poses challenges in making it responsive. My goal is to find code that can detect if the user is accessing the site from a mobile device, and automatically ...

Unable to load JQuery from a div element

My goal is to create a standard .html file containing the navigation, footer, and other elements that will be used across multiple pages for a small site I'm building. I want to keep it simple and avoid using php or other programming languages. I&apo ...

Sophisticated Sidebar Design using Bootstrap 5

Looking to design a responsive sidebar for navigation on my page, with selected and hover properties. Visualizing the selected option to appear like this: Selected (active) option in sidebar The goal is to have the image cover the left side of the backgr ...

What is causing my background image to move upward when I include this JavaScript code for FlashGallery?

There's an issue on my website where adding a flash gallery script is causing the background image to shift unexpectedly. You can view the affected page here: The culprit seems to be the "swfobject.js" script. I've identified this by toggling t ...

Looking for a way to transfer the value of a variable to a PHP variable using any script or code in PHP prior to submitting a form?

Within this form, the script dynamically updates the module dropdown list based on the selected project from the dropdown box. The value of the module list is captured in a text field with id='mm', and an alert box displays the value after each s ...