Unwanted overlay issues with CSS positioning

My webpage has a header div, with two nested divs called header-left-box and header-right-box that are floated left and displayed side-by-side horizontally.

Following these divs, I want to include a receiver div that occupies the free space below.

The issue is that the receiver div is not appearing below the other divs; instead, it is overlaying them. How can I ensure that the receiver div is positioned below the other divs?

Below is the code snippet:

<html>

<head>
<style type="text/css">
#header {
    position: absolute;
    width: 100%;
    height: 30%;
    margin: 0 auto;
}
#header-left-box {
    position: absolute;
    float: left;
    width: 40%;
    height: 100%;
    margin-left: 0px;
    border: 1px solid blue;
}
#header-right-box {
    position: absolute;
    float: left;
    margin-left: 50%;
    width: 50%;
    height: 100%;
    border: 1px solid red;
}
#header-right-box-content {
    position: absolute;
    bottom: 0;
    left: 0;
}
#header-left-box p {
    position: absolute;
    bottom: 0;
    font-size: 0.9em;  
}
#receiver {
    position: relative;
    width: 98%;
    border: solid 1px green;
}
</style>
<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div id="header">
    <div id="header-left-box">
        <p>Left header box</p>
    </div>
    <div id="header-right-box">
        <div id="header-right-box-content">
            <p>Right header box</p>
        </div>
    </div>
</div>

<div id="receiver">Receiver</div>
</body>

</html>

Answer №1

To achieve the desired outcome, adjust the receiver element to be absolutely positioned with its top slightly higher than 30%.

#receiver {
    position: absolute;
    width: 98%;
    top: 35%;
    border: solid 1px green;
}

For a visual representation, refer to this http://jsfiddle.net/8wRte/1/

Answer №2

The issue is that the receiver is overlapping because the elements #header-left-box and #header-right-box are set to position:absolute, while the receiver is set to position:relative. To fix this, you can either change the position of the header left and right boxes to relative, OR change the position of the receiver to absolute and add a

top:(place height of header left and right boxes here)
.

You can view an example of this issue in action in this fiddle (http://jsfiddle.net/hRcYF/) where all elements are positioned as relative. To resolve the problem, the receiver should be set to float:left (or right) and the #header-right-box should be floated right.

Answer №3

There's a simpler way to accomplish this. Instead of using absolute positioning for the #receiver div, consider floating #header-left-box to the left and #header-right-box to the right. Absolute positioning isn't recommended in this case, so try removing unnecessary position attributes from your CSS. Check out this example for reference: http://jsfiddle.net/E7T9F/. Remember, you can position elements without relying on CSS position attributes by using techniques like padding and margin.

Answer №4

Avoid using absolute positioning unnecessarily. Remove it from the elements where it's not needed, such as the #header-right-box which should not be floated to the left. Also, ensure that you clear the #receiver element. Here's the revised code:

<style type="text/css">
#header {
    /*position: absolute;*/
    width: 100%;
    height: 30%;
    margin: 0 auto;
}

#header-left-box {
    /*position: absolute;*/
    float: left;
    width: 40%;
    height: 100%;
    margin-left: 0px;
    border: 1px solid blue;
}

#header-right-box {
    /*position: absolute;*/
    /*float: left;*/
    margin-left: 50%;
    width: 50%;
    height: 100%;
    border: 1px solid red;
}

#header-right-box-content {
    /*position: absolute;*/
    bottom: 0;
    left: 0;
}

#header-left-box p {
    /*position:absolute;*/
    bottom: 0;
    font-size: 0.9em;  
}

#receiver {
    position: relative;
    clear: both; /*Cleared previously floated elements*/
    width: 98%;
    border: solid 1px green;
}
</style>

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

Tips for creating an editable div that can also incorporate a textarea and the option to upload or delete photos

On my website, users can upload images, names, and descriptions which are saved in a MySQL database. The information is then fetched to display on the website. The code below allows users to enter this information and see it displayed when they click the s ...

Calculate the total sum by iterating through a loop and adding the values of input fields with a type of "number"

I have a Shopping Cart set up in a loop that retrieves Products and Prices from the Database. Each row contains an input field of type "number" labeled as Quantity. My goal is to multiply the price by the quantity for each row when I click on a submit butt ...

What is the constant name returned by jQuery's .css method for font-weight?

I previously defined the font-weight of an element using this code snippet $('#myid').css('font-weight', 'bold'); However, when I attempt to check the value later on $('#myid').css('font-weight') I rece ...

"Exploring the Dynamic Duo of AngularJS ngAnimate and animate.css

I've been working on getting my animation to function correctly with AngularJS and animate.css. In order to achieve this, I set up the SASS in the following way: .slide-animate { &.ng-enter, &.ng-leave{ } &.ng-enter { ...

Issue encountered when attempting to retrieve elements within an HTA's IFrame

We are currently working on automating an Intranet site using HTA and JavaScript. To bypass the browser security settings, we have implemented an Iframe within the HTA itself instead of opening the site in a browser. Despite being able to successfully logi ...

The animation came to a halt after a brief period

Here is the code I wrote. When the button is clicked, the 3 containers should start flashing indefinitely, but they stop after a while. I can't figure out why. Any ideas? <!DOCTYPE html> <html> <head> <title></title> & ...

An autocomplete CSS editor with Firebug-like features

Looking for a CSS (or HTML) editor that offers code autocompletion similar to Firebug's features. I believe it's referred to as "autocomplete as you type." Thank you! Edit: I came across a tool called Zen Coding that provides shortcuts for cod ...

Encountering a problem with the multi-page template structure in jQuery Mobile

As a newcomer to Phonegap, I am utilizing jQuery Mobile and HTML5 for the development of an Android app. The app consists of three pages: the first page, index.html, displays a list of contents; the second page holds text content as well as a header with b ...

Tips for preventing menu flickering caused by overflow during CSS animations

I recently created a vertical menu with an interesting underline effect that appears when hovering over the menu items. Even though the snippet initially failed to execute, I discovered this cool hover effect (taken from here) which I wanted to implement: ...

Specifying Size of Icons in HTML and CSS

Displayed in the image below are 3 icons - 2 of them are the same size, while one (on the left) appears larger and uneven compared to the other two. https://i.sstatic.net/JtIov.png This snippet demonstrates the HTML code related to this section: < ...

When the child content surpasses the height of the parent, you can scroll within an overflow:visible; div

My sidebar navigation menu includes children and sub-children that are revealed on hover. You can view a simplified version of it in this jsfiddle link: https://jsfiddle.net/s096zfpd/ Although the example provided is basic, my main concern arises when the ...

I'm struggling to resolve this error. Can someone please help me figure it out?

` package Xcel; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Workbook; import org.apac ...

I am attempting to implement a feature that changes the color of buttons when they are clicked within an ng-repeat loop

A problem is occurring with the ng-class directive in this HTML code, which is generating seats using the ng-repeat directive. The colors are not being added properly when a seat is selected. If you'd like to view my code, it can be found in this JSf ...

Stripping out only the position attribute using jQuery

I have implemented a code with the following characteristics: The navigation items' texts are hidden behind certain Divs, which I refer to as Navigation Divs. When the mouse hovers over these pixels (navigation Divs), the text that is behind the ...

Hide the div once it goes off screen, ensuring that the user stays in the same position on the page

On my website, I implemented an effect similar to the Airbnb homepage where there is a "How it Works" button that toggles a Div element pushing down the entire page. However, when the user scrolls to the bottom of the toggled div (#slideDown) and it disapp ...

Browser inconsistencies result in varying appearance of Bootstrap selection in Firefox and Chrome

Issue with Bootstrap select rendering in Firefox and Chrome: Firefox: https://i.sstatic.net/HWEcR.png Chrome: https://i.sstatic.net/GBuDP.png Looking for ways to make the two browsers render consistently, preferably in Chrome's style. This questio ...

Is the username you want available?

I am facing a challenge in my registration form validation process where I need to verify the username input using javascript and flask in python, but the implementation is unclear to me. The requirement is to utilize $.get in the javascript segment along ...

Execute a function once all images have finished loading

My current approach involves utilizing a function to load images from an array: for (var i = 0; i < images_list.length; i++) { var img = new Image(); img.onload = function() { images_objects.push(this); ...

The CSS3 :empty selector targets elements that have no content inside

Seeking guidance on CSS3 and the :empty pseudo-class. Currently developing a web AJAX application with a sidebar in the layout that I want to hide when empty. Here's what I have: #my_sidebar:empty { display:none;} To display it when not empty, I wr ...

Tips for aligning the contents of multiple tables in HTML to the center

Currently, I am in the process of designing a website where there will be four titles displayed above four images, with corresponding descriptions below each. I have utilized two separate tables for the titles and descriptions. While the layout appears fin ...