The absolute positioned div on the same level is impacted by negative margin

I am facing an issue with styling elements in my HTML code. I have a div with the ID of #left that is absolutely positioned, along with two other divs on the right side. Strangely, when I apply margin to the #top div on the right side, it also affects the #left div. I understand that margin collapsing may be the cause, but does this behavior extend to elements with position:absolute as well?

The code itself is quite simple and straightforward, yet I cannot seem to pinpoint the solution.

* {
    padding:0;
    margin:0;
}

#wrapper {
    width:400px;
    height:400px;
    background:gray;
    position:relative;
    margin-left:100px;
}

#left {
    background:pink;
    width:100px;
    height:100%;
    left:-100px;
    top:0;
    position:absolute;
}

#right {
    background:red;
}

#top {
    background:green;
    height:26px;
}

<div id="wrapper">
    <div id="left">Left</div>
    <div id="top">top</div>
    <div id="right">Right</div>
</div>

Jsfiddle: http://jsfiddle.net/9thvLfe0/2/

Answer №2

Adjust the top margin and apply a negative margin to the left side.

Answer №3

It seems like the issue stems from the fact that your #wrapper has a relative position while #left is set to absolute. Due to inheritance, #top and #right are also relative. Therefore, applying a negative margin to top under these conditions will actually affect #wrapper.

If you want to address this, one option is to change #wrapper to have a "fixed" position, but keep in mind that you would need to manually adjust the margin/padding for #hide-top to prevent it from being hidden beneath #wrapper. Alternatively, you could try setting it up like this:

#hide-top {
    position: relative;
    top: 400px;
}

However, instead of modifying your CSS, I propose a solution using JQuery. You can simply hide #top using the hide() function. Check out my JSFiddle for an example ;).

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 way to position three DIVs next to each other within another DIV while aligning the last DIV to the right?

I need help formatting a simple list item with three DIVs. The first DIV should be left justified, the second should be able to grow as needed, and the third should be right justified. I currently have them stacked side by side, but can't get the last ...

Evaluation of HTML5 file upload functionality with unit testing

How can I perform JavaScript unit testing for file uploads using the HTML 5 File API? Let's consider the following code: <form method="POST" enctype="multipart/form-data"> <input type="file" id="fileselec ...

The right side alignment is malfunctioning

I need assistance with creating a section on a webpage that resembles a piece of paper, where there is content displayed on white background against a grey background. Here's what my CSS currently looks like: body { background:grey; } .page { ...

Exploring issues with jQuery

I'm encountering an issue with my code. I have multiple divs with the same classes, and when I click on (toggle#1) with the .comments-toggle class, all divs below toggle-container expand. What I actually want is for only the div directly below .commen ...

Can someone point me in the direction of the app.css file within DurandalJS that controls the modals

I am currently facing an issue with implementing a modal in my app using the DurandalJS (SPA) framework. The modal is not displaying correctly, as it opens without a blockout and stretches horizontally across the entire browser. (I expect "Feeds", the "Ne ...

Angular component: Placing icons on the right side of a mat-chip element

My mat-chip contains content and a cancel icon, but I am having trouble getting the cancel icon to float to the right consistently. Despite setting a fixed width for the mat-chip, the cancel icon is not aligning properly no matter what CSS techniques I t ...

Conceal form after submission - Django 1.6

I'm currently working on a Django 1.6 project where I have this form: <form action="/proyecto/" method="POST" id="myform"> {% csrf_token %} <table> <span class="Separador_Modulo">& ...

A dynamic 2-column website design featuring a mobile-friendly layout and an adaptable image

After searching through numerous resources on the topic, I have yet to find a solution to this particular challenge. Is it feasible to create a webpage layout with a text column on the left and an image column on the right that will seamlessly transition i ...

Determine the highest position of itself by using the calculation: 100% viewport height minus y

I'm looking to make an element fill all the space below it until the bottom of the browser window. Is there a way to determine the y offset of the element in order to achieve this? I need help defining y: .occupy-space-below { max-height: calc(10 ...

Spacing is essential between <h3>, <h4>, and <p> elements

I'm facing a simple issue with my code. I can't seem to remove the white gap between each tag, particularly noticeable between the h3 and h4 tags with background colors. How can I eliminate this space? .realEstatePricing h3 { Background: lig ...

Sleek CSS Slider with Image Transformation on HoverThe requested task has been

I am currently working with a client on the website . One of the features on the site is a slider at the top that allows for image swapping using pure CSS. However, the client recently requested that the images change when hovering over radio buttons or au ...

Is there a way to convert these bootstrap links into haml format?

<!-- Get the latest CSS file by using this link --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin ...

Enhancing interactivity with jQuery's ajax event functionality

Alright, let me share my code with you: $("#content_Div").ajaxStart(function () { $(this).css('cursor', 'wait') }).ajaxComplete(function () { $(this).css('cursor', 'default') }); I'm facing a simple is ...

Positioning the tooltip to the left side

I need help with customizing a tooltip in my code. I want the tooltip text to be displayed on the next line and positioned on the left side. You can view a demo here. <div id="demo"> <p title="The tooltip text I want this in next line"> Too ...

What are the steps to incorporate an NPM package into a basic HTML/CSS/JS project?

My website is a basic one created using HTML, CSS, and some in-line JS. I came across an interesting NPM package called Simple MDE Markdown Editor that I would like to incorporate into my site. After attempting to install the package by running npm insta ...

Arranging buttons beside an image

I had previously inquired about a similar issue, but I've since made some style changes and now I'm unsure of how to position the close button in the top-right corner of the image, along with the previous and next buttons on either side of the im ...

Change the background color of a webpage by clicking with the help of Javascript

I've been experimenting with a script that changes the CSS background color on click, but I'm having trouble adding a third color option. Can anyone provide guidance on how to make the third color work? Check out my jsfiddle here: https://jsfid ...

The ethereal image drifts to the left side of the screen against a captivating background, enhanced by high

I am facing an issue with a webpage that has a background image and floating images positioned on top of it. These floating images are linked to specific areas on the background image, but I am having trouble ensuring they stay in the correct position as t ...

"An easy way to dynamically update a select dropdown based on the data of the corresponding row in a table using jQuery

Having some trouble with dynamically populating form input fields in a table. The first row works fine, but when I change the select option in the second row, it affects the previous field instead of the current row: How can I ensure that changing the sel ...

Ensuring the next tab is not accessible until all fields in the current tab are filled

I am working on a form with a series of questions displayed one at a time, like a slide show. I need help with preventing the user from moving to the next set of questions if there is an empty field in the current set. Below is the script I am using to nav ...