"Offspring div popping up beyond the bounds of its parent div

I've been working on a project where I need to insert a div containing an image into another div. The parent div already has two other child divs set up and functioning perfectly. Here is the HTML code for the parent and child divs:

<div id="contentsWrapper">
    <div><ul id="sideNav">
        <li><a href="linkone.html" id="linkone">One</a></li>
        <li><a href="linktwo.html" id="linktwo">Two</a></li>
    </ul></div>

    <div id="text">
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, <br />
        sed diam nonummy  nibh euismod tincidunt ut laoreet dolore magna.
    </div>

    <div id="picture">
        <img src="picture.jpg" alt="picture" height="200" width="200" />
    </div>
</div>

Here is the CSS code:

#contentsWrapper {
    width: 800px;
    margin-left: 10px;
}

#sideNav {
    padding: 0;
    list-style: none;
    width: 160px;
    margin-top: 0px;
    border-bottom: 1px dashed #999;
    position: absolute;
}
#text {
    width: 375px;
    margin-left: 180px;
    font-weight: bold;
}
#picture {
    position: absolute;
    width: 225px;
    margin-left: 575px;
    background-color: #FFFF00;
}

After implementing this code, the picture appears correctly aligned with the left side of the screen but ends up below the contentsWrapper div. I have attempted adjusting the width and margin-left values for the picture div without success.

Answer №1

When using absolute positioning, the element is taken out of the container's flow. You can achieve a similar effect with the following code:

.image {
width: 200px;
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
background-color: #00FF00;
}

}

Answer №2

To make the #text style work properly, include float:left;.

#text {
    width: 375px;
    margin-left: 180px;
    font-weight: bold;
    float:left;
 }

Answer №3

It is suggested to either remove the position:absolute or apply position:relative to the parent div.

Also, why are there such large margins?

#contentsWrapper {
    width: 800px;
    margin-left: 10px; background:red; position:relative; overflow: hidden; height:auto
}

#sideNav {
    padding: 0;
    list-style: none;
    width: 160px;
    margin-top: 0px;
    border-bottom: 1px dashed #999;
    float:left
 }
 #text {
    width: 375px;
    font-weight: bold; float:left
 }
 #picture {
    width: 225px;
    background-color: #FFFF00; float:left
 }

View DEMO here

Answer №4

He is simply following your instructions, but it's possible that I am not grasping your overall vision (perhaps including an image would help clarify?)

First query: what is the reason for utilizing position:absolute on the image? This removes it from the normal flow.

Second inquiry: why opt for position:absolute along with margin-left instead of using left?

If you do choose to use left, keep in mind that it will be calculated relative to the parent element if you apply position:relative to the parent.

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

Why is it that every time I write this code, the localhost gets redirected and a message pops up saying

<?php include("connect.php"); if(isset($_POST['submit'])) { $qonax=@$_POST['qonax']; $subject=@$_POST['subject']; $insert="INSERT INTO subject(ID,Qonax,Subject)VALUES('','$qonax', ...

Use Bootstrap to effortlessly arrange columns horizontally in a row

I'm facing a scenario where I need to arrange the columns in a row horizontally. The challenge is to ensure that the row expands horizontally to fit the columns. I attempted to use white-space: nowrap on the row, remove floats on columns, and set the ...

What causes a span with absolute positioning to not be correctly positioned within a span with relative positioning in Firefox, unless it is displayed as inline-block or similar?

Can you explain why the code below doesn't display correctly in Firefox unless the parent span is set to display as inline-block? When inspecting the layout of the absolutely positioned span, it shows the number 184 instead of 197 on the right side. A ...

IE compatibility with CSS2 selectors

Is there a plugin, preferably jQuery, that can enable the use of CSS2 selectors like 'parent > child' and 'element:first-child' in my stylesheet for IE6, which doesn't seem to support them natively? ...

When using jqueryprint.js to print from Firefox browser, an additional blank page appears before the content

Seeking assistance in printing a section of an HTML page using PHP with a large header space for empty space after printing. Despite multiple attempts, achieving the desired result in Chrome, but facing issues with Firefox, which adds an additional page be ...

Design a big-sized button using Bootstrap framework

Is there a way to design a big, responsive button using Bootstrap? Similar to the example image linked below https://i.sstatic.net/xEBwc.png ...

Returning a button to its original state

Currently, I am working on styling the video controls on my website and have run into a small issue. The video section consists of a video list and a video player. When an li element from the list is clicked, the source of the video player changes to the ...

Angular router link circles back to its originator

I've been working on a basic login page that transitions to a homepage once the user successfully logs in. So far, I've focused solely on the HTML structure of the login component without implementing any code in the corresponding TypeScript file ...

Why isn't my margin: auto code centering correctly?

My current struggle involves centering a div within the document. While I have successfully achieved horizontal centering, vertical centering remains elusive: width: 950px; height: 630px; background: #FFFFFF; margin: auto; Is it not expected that margin: ...

Unlimited webpage scrolling downward

Recently, I was given a project to enhance a webpage and encountered an unusual issue. When the screen resolution drops below 900px (such as on small laptops and tablets) and you reach the bottom of the page, you can continue scrolling endlessly after seei ...

Updating the content within a div following the submission of a contact form 7

I'm struggling with my contact form. I want the content of my contact form div to change after clicking submit on the form. I've been searching for a solution, but so far, no luck. Here is what I have: Form (div1) with input fields, acceptance c ...

Ways to incorporate CSS design into Django input pop-up when the input is invalid

Looking to enhance the error message styling for a Django Form CharField when an incorrect length is entered, using CSS (Bootstrap classes preferred). I have successfully styled the text input itself (check the attrs section in the code), but I am unsure ...

Implementing key strokes in an HTML input field within a geckoWebBrowser

I am currently using the "geckoWebBrowser1" component to navigate to a URL that displays a login textbox with the ID: login-email Although I have successfully inserted "[email protected]" into the aforementioned textbox, it is essential to simulate k ...

Update the CSS styles

html document <ul id="navbar"> <li class="selected"> <a href="#"> HOME</a> </li> <li> <a href="#"> ABOUT US</a> </li> ...

Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side. My initial attempt involved using flex layout, but I have ...

Issue with PageSpeed and Lighthouse showing NOT_HTML error post recent update

Our team decided to utilize the tool at for assessing the speed of our website. This tool has been effective in analyzing our site's URLs for some time now. However, recently we have encountered an issue where we receive the following error message: ...

retrieve data with the help of DOMDocument

I'm currently working on extracting a specific value from the HTML snippet below using DOMDocument: <h3> <meta itemprop="priceCurrency" content="EUR">€ <meta itemprop="price" content="465.0000">465 </h3> The value ...

Using identical CSS for two separate layouts

Imagine having to work with an html page that cannot be altered in any way. The only tool at your disposal is CSS. The page is loaded and shown through an iframe from a different domain. This particular page serves as a payment gateway, displaying either ...

"Troubleshooting the issue of consistently receiving null values when uploading files with

I'm facing an issue with uploading a file using jQuery AJAX. Although I can see all the details of the file object such as its name and size, when I check the console with formdata.get("files"), the context.request.files size always shows zero. It see ...

What is the JavaScript mouseenter/out event all about?

My goal is to create a hover effect using JavaScript. I am utilizing the mouseenter and mouseout events to display the content when the mouse hovers over a button and remove the content when the mouse moves out. However, I am facing an issue where the cont ...