Absolute Positioning Problem

When I attempt to insert elements as static html text, everything functions correctly. However, when I try to insert elements at runtime (using arrays of data from the server), I encounter positioning issues with absolute. Here is the simplest example to replicate this problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Positioning</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
        body { font-family: Verdana, Tahoma, sans-serif; font-size: 14px }
        div { border: 1px solid black }
        .div5 { float: left; width: 300px; height: 300px; }
        .div6 { width: 100px; height: 100px; position: absolute; top: 50px; left: 50px; background-color: #F5D8C1 }
        .div9 { width: 20px; height: 20px; position: absolute; bottom: 20px; left: 20px; background-color: #D8F5C1 }
    </style>
</head>
<body>
    <div id="content"></div>
    <script type="text/javascript" language="JavaScript">
        function setDivValue($divId, $value)
        {
            var divL = document.getElementById($divId);
            if (divL)
                divL.innerHTML = $value;
        }

        var rtext = "<h1>Position absolute test</h1>";
        for (var i = 0; i < 10; i++){
            rtext +=    "<div class='div5' id='" + i + "'>" +
                            "<div class='div6'>div6</div>" +
                            "<div class='div9'>div9</div>" +
                        "</div>";
        }
        setDivValue("content", rtext);
    </script>
</body>
</html>

Instead of positioning div6 and div9 relative to the parent element div5, they are being positioned relative to the window in both Firefox 20 and Internet Explorer 8 (indicating that this issue is not browser-specific).

Is there a solution to this problem? I am looking to place pictures within div6 and place text within div9 at the top left corner of the picture (inside).

Answer №1

Here are some CSS styles to improve your layout:

#content{position:relative;}

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

How to position a fixed element in relation to a wrapper div using HTML and CSS

I need assistance with positioning the floating box on the right side of the window on this particular page. My goal is to have this box relative to the white div containing all the text, so instead of it sticking to the window edge, it should float aroun ...

The attribute 'checked' is not a valid property for the 'TElement' type

Learning about typescript is new to me. I have a functional prototype in fiddle, where there are no errors if I use this code. http://jsfiddle.net/61ufvtpj/2/ But in typescript, when I utilize this line - if(this.checked){ it presents an error [ts] Pro ...

What is the best way to navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

Align an image with text using CSS and HTML

I'm having trouble with my CSS and HTML code. I want to align the text to the right of an image in a grid format, but it keeps appearing below the image instead. Here is the code snippet: <html> <head> <style type="text/css"> #conta ...

Clustering in an environment requires merging and minifying granules

While Granule is effective for minifying and merging CSS/JS files on a local environment, it presents challenges in clustered environments. The issue arises when each node of the cluster computes its own file during runtime, causing discrepancies when a us ...

Is it possible to incorporate Google icons within Freemarker?

https://i.stack.imgur.com/Pv2T4.pngI'm having trouble using Google icons in my project. Can anyone help me figure out how to fix this? UPDATE: Here is a snippet of my HTML template: <?xml version="1.0" encoding="UTF-8"?> < ...

"Seamlessly transition between items in a ng-repeat loop

My list of names in the scope is pretty straightforward: $scope.friends = [ {name:'John'}, {name:'Jessie'}, {name:'Johanna'}, {name:'Joy'}, {name:'Mary'}, {name:'Peter'}, ...

How can you make the jQuery popup bar hidden by default?

Recently, I came across a tutorial on creating a popup bar similar to the one you see at the top of many websites. Interested in learning how it's done, I followed this tutorial: Being new to jQuery, I had a question in mind - is it possible to have ...

Beginner tips for adding padding in CSS

Could anyone clarify a discrepancy in the material provided here: w3schools.com code example According to the code : th,td { padding:5px; } Also, based on their explanation here: http://www.w3schools.com/cssref/pr_padding.asp The explanation states tha ...

What is the best way to crop an image to focus solely on the center portion?

Using Bootstrap 3, how can I display an image cropped in a .col-sm-6 grid column? The image is 720px wide. Typically, with the .img-responsive class, the image will resize proportionally when the display size changes. However, I want to crop the left and ...

Tips for programmatically relocating the slider handle in HTML 5 range input without relying on Jquery UI

INQUIRY: I am facing an issue with an HTML5 range input slider in my project. When I try to change the value of the slider using jQuery, the handle's position remains unchanged. While I am aware that this can be resolved using the .slider() method in ...

creating an animated loop within an Angular template

How can we display a dynamic loop in Angular template using an array of object data? [ { "name": "name", "text": "text", "replies": [{ "name": "Reply1", "t ...

Error: The function 'document.getsElementsByClassName()' is not defined when evaluating 'undefined'

Actual Error TypeError: undefined is not a function (evaluating 'ActiveElem[i].hasClass('active'); HTML <div class = 'Carousel-Inner'> <div class="morningSlide active"> <img src="/Users/KO527/Sites/TarasDeli ...

The sticky HTML table header feature is functional, however, the header's border disappears while scrolling

Utilizing the react-bootstrap-table2 library in my React project, I added custom CSS to create a sticky top row effect: .table-container { overflow-y: auto; max-height: 500px; } .react-bootstrap-table th { position: sticky; top: -1px; background- ...

Tips on choosing the initial N website elements that match a specific CSS selector using Python Selenium

Currently, I am utilizing phantomJS in a python/selenium configuration to scrape the screen. My main objective is to extract the first N elements that match a specified CSS selector. An issue I am encountering is that there are significantly more matching ...

Node.js and MongoDB Login Form Integration with Mongoose

I am relatively new to web development and currently working on a simple web page for user login authentication. My goal is to verify user credentials (username & password) on the LoginPage from a mongoose database, and if they are correct, redirect them t ...

Using *ngFor to iterate through elements with distinct styles

Is there a way to assign different classes to buttons generated using *ngFor without changing the class for all previously created buttons when toggled? I have 2 search bars adding text to the same array, displayed as buttons. I want to differentiate betwe ...

Having trouble with your HTML iFrame not functioning properly?

My code is not working as expected and I'm puzzled by it. It was working fine yesterday but now it's giving me trouble. <iframe allowfullscreen="" allowtransparency="true" frameborder="0" height="2000" scrolling="no" src="http://www.google.co ...

Using PHP, HTML, and JavaScript to generate a dropdown list from a database

I am looking to create a dynamic dropdown list populated from a database using JavaScript. My goal is to design a form where users can add multiple rows by clicking the "add more" button. The form will include both a text input field and a dropdown list. ...

Transformation of visuals with alteration of status

I am attempting to change the image of a door from closed to open when the status changes in my home automation dashboard. I need help with this task. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu ...