Divs experiencing unexpected wrapping of text

I've encountered an unusual issue while working on a library I'm developing. The problem can be traced back to the following code snippet:

<html>
    <body>
        <div style="position: absolute; left: 200px;">
            <div style="position: absolute; left: 0px; top: 0px">
            Hello some text.
            </div>
            <div style="position: absolute; left: -50px; top: 50px">
            Hello some text.
            </div>
            <div style="position: absolute; left: -100px; top: 100px">
            Hello some text.
            </div>
            <div style="position: absolute; left: -150px; top: 150px">
            Hello some text.
            </div>
            <div style="position: absolute; left: -200px; top: 200px">
            Hello some text.
            </div>
        </div>
    </body>
</html>

The concept here is quite simple: we have the same text within various div elements placed differently. The only difference between them is the positioning. One would expect all five texts to appear identical, whether in a single line or split into multiple lines. However, the divs positioned to the left do not wrap text, whereas the ones towards the right do.

This behavior is consistent across Chrome 83, Firefox 77, and Edge 83.

The issue seems to stem from the negative left values, as when these are removed, the display remains consistent (though this adjustment is not feasible in the actual application due to how those offsets are determined).

<html>
    <body>
        <div style="position: absolute; left: 0px;">
            <div style="position: absolute; left: 200px; top: 0px">
            Hello some text.
            </div>
            <div style="position: absolute; left: 150px; top: 50px">
            Hello some text.
            </div>
            <div style="position: absolute; left: 100px; top: 100px">
            Hello some text.
            </div>
            <div style="position: absolute; left: 50px; top: 150px">
            Hello some text.
            </div>
            <div style="position: absolute; left: 0px; top: 200px">
            Hello some text.
            </div>
        </div>
    </body>
</html>

Is this behavior a bug present in all three browsers? Or is there some logical explanation for it?

Current rendering: https://i.sstatic.net/ZFMPp.png

Expected rendering: https://i.sstatic.net/zDGlV.png

Answer №1

The Issue with Your Code

Your code is not functioning correctly because the first child div is positioned within its parent div due to a left property of zero, while the subsequent div's are extending beyond the boundaries of the parent div as they have negative left properties.


https://i.sstatic.net/7f32Q.png

If you add another div and give it a left property value of 50px, it will display properly as it will be considered part of the parent div.

https://i.sstatic.net/O0lqN.png

Why Your Corrected Code Works

The reason your corrected code is successful is that the parent div has a left property of 0px, followed by the first child having a left property of 200px, the second child using 150px, and so on. This ensures all children are treated as part of their parent div, eliminating the need for negative values.

<div style="position: absolute; left: 0px;">
  <div style="position: absolute; left: 200px; background-color:red; top: 0px">
    Hello some text.
  </div>
  <div style="position: absolute; left: 150px; top: 50px;  background-color:red;">
    Hello some text.
  </div>
  <div style="position: absolute; left: 100px;  background-color:green; top: 100px">
    Hello some text.
  </div>
  <div style="position: absolute; left: 50px;  background-color:green; top: 150px">
    Hello some text.
  </div>
  <div style="position: absolute; left: 0px;  background-color:green; top: 200px">
    Hello some text.
  </div>
</div>

If you prefer a solution without altering the left property, using width:100% will resolve the issue:

<div style="position: absolute; left: 200px;">
  <div style="position: absolute; left: 0px; top: 0px; width:100%;">
    Hello some text.
  </div>
  <div style="position: absolute; left: -50px; top: 50px; width:100%;">
    Hello some text.
  </div>
  <div style="position: absolute; left: -100px; top: 100px; width:100%;">
    Hello some text.
  </div>
  <div style="position: absolute; left: -150px; top: 150px; width:100%;">
    Hello some text.
  </div>
  <div style="position: absolute; left: -200px; top: 200px; width:100%;">
    Hello some text.
  </div>
</div>

Answer №2

To ensure your div maintains the desired width, set it up accordingly.

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

Deactivating controls while displaying the loading bar in AngularJS

I'm currently working on a web application using AngularJS. I want to incorporate a loading bar to signify long data load times from the server. To replicate heavy data loads, I am utilizing $timeout to trigger the loadbar when the operation begins a ...

Troubleshooting Image Map Failure on Internet Explorer 10

<img src="images/imagemap.png" width="600" height="100" border="0" usemap="#map" /> <map name="map"> <!-- #$-:Image map file created by GIMP Image Map plug-in --> <!-- #$-:GIMP Image Map plug-in by Maurits Rijk --> <!-- #$-:Plea ...

What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary. Each post is composed of a title, a subheading, and a placeholder div ...

How can you style an HTML tag using esc_html in CSS?

I've encountered an issue while editing the woocommerce orders.php template. This template is responsible for displaying the user's placed orders. I have identified several variables that require secure coding, such as $date_created or $view_orde ...

Is there a way to incorporate a subtle fading effect when transitioning between images?

I find this continuously changing image every 5 seconds to be a bit overwhelming without any smooth transition. Is there a way I can implement a short fade effect like what is possible in CSS? import React, { useEffect, useState } from "react"; ...

"Exciting Changes in Color According to the Active State of vue-route-link

I am trying to find a way to customize the CSS based on whether a link is exact or active. Essentially, when a user clicks on a menu item, I want the underline to change depending on whether the link is an active router-link. Although I was able to accompl ...

Error: The request to /api/auth/login in Postman failed unexpectedly

As I am working on developing an app using node.js and express, I encountered an error while making post requests in Postman. Cannot POST /api/auth/login%0A Below are the details of my app's structure along with the files involved: app.js const ex ...

JavaScript: Can you clarify the value of this variable using five sets of double quotations?

Could you please review the code snippet below for me? <script type="text/javascript"> function recentpostslist(json) { document.write('<ul class="recommended">'); var i; var j; for (i = 0; i < json.feed.entry.length; i++) { ...

Utilize the "rel" attribute within the text currently being shown through Colorbox

Can the rel text be utilized in the current text shown by Colorbox? I have a collection of images categorized with rel="Project Name" <a href="gallery/IN01 _GSM1 450x582.jpg" rel="Project Name" title="Global Supplier Management brochure Cover" name ...

positioning the cursor at the end of a content-editable div

When working with a contenteditable div, I encountered several issues specific to FireFox. To address these problems, I found that appending a br tag to the end of the div was necessary. <div id="testDiv" contentEditable="true"> Hey, click the butt ...

Toggle the visibility of image elements depending on the input entered in the search bar using jQuery

I have a photo gallery grid and I am looking to implement a search functionality for an img tag's 'data-title' value, where only images with matching data will be displayed. For example, if I type "hay" into the search box, only the image be ...

The initial click event for the input element in Jquery is not functioning correctly

I found a jQuery date selector online and the script looked something like this... <script type="text/javascript> $(document).ready(function () { $("#date3").click(function() { $("#date3").scroller({ preset: 'datetime' }); wheels = []; whe ...

I find it frustrating that Angular consistently redirects me to the login page every time I attempt to navigate to a different page

I currently have a website with both normal user-accessible pages and an admin dashboard accessible only by admins through ngx-admin. To restrict users from accessing the admin dashboard, I've implemented an auth guard that redirects them to the logi ...

How can I use jQuery to reset a select dropdown to its initially selected option upon page load?

Can a select element be reset to its original loaded option using jQuery without the need for creating a custom cache of the initial value? ...

What causes the card to exceed the boundaries of its parent column?

My Form elements seem to be misaligned and not fitting correctly. .h-100 is used for the blue navigation background to occupy the full height of the screen. An answer explaining why my code snippet is not working would be appreciated. View Plunker here ...

Error: The specified element to insert before is not a direct descendant of this parent node

As I work on converting a jquery dragable script to plain javascript, I encountered an issue at the insertBefore point. The error message that pops up is: NotFoundError: Node.insertBefore: Child to insert before is not a child of this node. This particula ...

I attempted to separate a string containing <br> tags using the explode function, but the method was not successful

In my code, there is a string that looks like this <br> ACCEPT:YES <br> SMMD:tv240245ce <br> This string is stored in a variable called $_session['result'] My task is to extract the following information either as an array or ...

Issue encountered in Internet Explorer: Expecting an object error message with SAP UI5

Struggling with SAP UI5 as I encounter an 'object expected' error on line 347 when trying to run the index.html file in IE. <html> <head> <meta content="IE=edge" http-equiv="X-UA-Compatible"> ...

The radio button is not providing the necessary notification

I currently have the following elements on my webpage: Table rows that contain radio buttons A link labeled "Issue" that triggers a popup with one submit button A submit button labeled "details" that also triggers a popup The issue I am facing is as fol ...

Manifest-related issues causing GWT not to function correctly upon second attempt

After spending two whole days, I finally figured out why my manifest was not functioning properly with GWT in Eclipse. The issue was right in front of me all along. For some unknown reason, GWT's Jetty doesn't seem to work well with HTML5 manifes ...