In Chrome (mobile), the DIV element fails to stretch across the entire page when a lengthy word causes overflow

Case 1: Excluding the initial-scale=1.0

The following code is presented:

<!DOCTYPE html>
<html>
  <head>
    <title>Foo</title>
    <meta name="viewport" content="width=device-width">
    <style>
      body {
        margin: 0;
      }
      .header {
        background: green;
        color: white;
        height: 2em;
      }
    </style>
  </head>
  <body>
    <div class="header">
      Header
    </div>
    <p>
      Veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongword
    </p>
</html>

Upon opening this page with Chrome on a desktop browser, selecting Inspect, and viewing it in the mobile mode using Galaxy S5, the result shows that the <div> element does not stretch to match the width of the page.

Case 2: Including initial-scale=1.0

The code provided for this case is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Foo</title>
    <meta name="viewport" content="width=device-width; initial-scale=1.0">
    <style>
      body {
        margin: 0;
      }
      .header {
        background: green;
        color: white;
        height: 2em;
      }
    </style>
  </head>
  <body>
    <div class="header">
      Header
    </div>
    <p>
      Veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongword
    </p>
</html>

Although initially the page appears fine, as we scroll horizontally to the right, it becomes evident that the <div> does not span across the entire width of the page.

The issue persists even when applying width: 50% to the .header in the CSS.

Pondering Question

What could be causing this discrepancy? Is there a way to rectify this so that the <div> extends fully across the page, ensuring the complete visibility of the lengthy word towards the right, even if horizontal scrolling is required?

Answer №1

The issue at hand is an overflow problem; the width of the element containing the text is not expanding, which means child elements inheriting this width won't expand either.

The workaround (or solution, depending on your perspective) is to enforce breaks for long words, using https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap

If you refer to the browser compatibility chart below that link, you'll notice that the value anywhere lacks support in most browsers, except for Firefox 65+. In most scenarios, using break-word should suffice.

(For additional options, you can explore a similar property here: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break Sometimes employing both properties can yield better outcomes in older browsers.)

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

Using jQuery to add a dynamic active class according to a specific data attribute

Despite my thorough research, I have yet to find a solution that works flawlessly. The HTML code I've created: <div class="option" data-toggle="tooltip" data-placement="right" data-name="Home" data-url="/" title="Home"> <p class="select ...

Background image fixed with scrolling effect

I've been struggling with a parallax effect on my website. After seeing it work smoothly on other websites, I tried to implement it myself but couldn't quite get it right. The background image keeps moving when I scroll the page and I want it to ...

The execution of jQuery was hindered due to the implementation of PHP include

Having an issue with jQuery not working in index.php when including the file header.php. The nav sidebar is included, but clicking the chevron does not trigger anything. It only works when I directly include header_user.php without checking the UserType in ...

Struggling to properly center the footer on my Django template

I'm currently facing an issue when attempting to align my footer in the center of my Django template. The specific class for the footer is Pagination. Base HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict ...

Error encountered when attempting to launch Chrome driver in Selenium Web Driver due to failed DevTools request

Whenever I attempt to launch the Chrome browser using Selenium WebDriver, I sometimes encounter an issue where it fails to open and generates the following error log. This problem occurs randomly and not consistently every time. I have ensured that I am ...

Comparison of the "Proposed Recommendation" versus the "Candidate Recommendation"

In a recent piece about HTML5, it was mentioned that the Proposed Recommendation date is set for 2022, while the Candidate Recommendation date is from 2012. I'm curious to understand the distinction between the "Proposed Recommendation" and the "Cand ...

What modifications were implemented in Bootstrap 5 to cause controls to automatically assume their maximum size?

After upgrading NodeJs to version 16.13.0 from 14.x, one of the modules that was updated was bootstrap. This caused me to remove my Jumbotron control due to the upgrade, although I don't believe that is directly related to the issue I am experiencing. ...

A PHP function with multiple parameters

I am currently in the process of updating a row on Parse using PHP and have created the following function: if (isset($_GET['updateHistory'])) { updateHistory($_GET['updateHistory']); } if (isset($_GET['ye ...

Tips for adjusting the maximum width and content-based width for columns within an HTML/CSS table

I'm working on a simple HTML/CSS code featuring a container with two adjustable slots, controlled by a handler in the middle to modify the space each slot occupies. The first slot contains a table with fixed columns, automatic columns, and limited col ...

Utilizing Bootstrap 4 to Position an Image Amidst Two Card Elements

Looking at the bootstrap 4 card documentation page in the Images > Image Caps section, you'll find two examples provided. In these examples, the image can be positioned either at the top or the bottom of the card block, with the .card-block div eit ...

Retrieve data from a different div, perform a subtraction operation, and dynamically update yet another div using jQuery

Looking to extract a specific div class value, subtract 500 from it, and display the result in another div. Unclear about the steps needed to show the subtraction outcome on the other div. Consider this scenario: <div class="main-value">6000</d ...

Numerous divs positioned above a myriad of images

Is there a way to create multiple images with a tag name overlaying each one as a link? I've been able to add a tag name on top of one image, but not on others. Every time I try to duplicate the div containing the image and tag, the tags do not show u ...

Having trouble aligning Bootstrap SVG icons to the center

I'm struggling with getting the svg icons centered on my welcome.blade.php file. Instead, they appear shifted to the right. I would really appreciate any help or advice on how to fix this issue. Check out a screenshot here My welcome.blade.php code s ...

What is the best way to adjust an element to match the height of the browser window?

I've coded a solution to ensure that the height of a section (#home) matches the height of the window, but it's causing issues. Here is the code snippet I used: // Adjust Home section height based on window height function fitHomeToScreen() { ...

What is the best way to create an HTML table using a two-dimensional list in Python?

I am currently facing an issue with the functionality of my code. The HTML is being generated as desired, but it appears at the top of the page rather than in the intended location. def fixText(self,text): row = [] z = text.find(',') ...

Button activates SVG animation

Currently, I am working on an animation for a button using SVG. The animation utilizes the classes .is-loading and .is-success. However, when I click on the button, the animation does not execute as expected. I'm having trouble identifying the error w ...

The THREE.js GLTFLoader is failing to load the 3D model

Hey there, I'm encountering an issue with displaying a 3D model using THREE.js GLTFLoader(). Below is the script I am working with: Here is the HTML file: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

Explanation: The information box does not appear when the mouse hovers over it

Welcome to the gallery showcasing a unique calendar design with tooltip functionality. This calendar features a special hover effect for New Year's Day on January 1st, displaying a tooltip text upon interaction. Feel free to explore the code below to ...

Unable to choose fields and options within the popup box

Interacting with jQuery: $("#type_name").click(function(){ $("body").append('<div class="modalOverlay">'); $("#add_form").fadeIn(1000); $("#first_name").val(""); $("#email").val(""); ...

typescript is failing to update CSSRule with the newly assigned background-color value

I am currently working on dynamically changing the CSS style (specifically background-color) for certain text on a webpage. To achieve this, I have been attempting to modify CSSRule by accessing the desired CSSRule based on selectedText. Here is the code s ...