Is there a way to make text overflow a div but still stay within the viewport?

Take a look at this code snippet

<div>
   <p> text goes here</p>
</div>

It works fine with short text.
But when the text gets a bit longer, I can make it stay within the div using the white-space: nowrap CSS property.
However, if the text is extremely long, some of it may be hidden beyond the viewport. Is there a way to make it wrap onto a second line instead of overflowing based on the size of the parent div?

Answer №1

To ensure the text within your div has a width of 100vw and normal white-space, you can enclose it in an inner div.

In this code snippet, the outer div is styled with a pink background to visualize its boundaries.

div.outer {
  white-space: nowrap;
  width: 50%;
  background-color: pink;
}

div.inner {
  width: 100vw;
  white-space: normal;
}
<div class="outer">
  <div class="inner">
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
  </div>
</div>

An additional (inner) element is included to accommodate various scenarios. Depending on the setup, you may opt for using the p element instead of div.inner if there are other texts outside of an inner element.

Answer №2

To resolve the issue, you can try using either: - overflow-wrap: break-word; or - word-break: break-all;

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

Is it possible to create a cross-sectional view of a lens using the HTML5 canvas element?

I am interested in creating a visual representation of the cross section of a lens element. Typically, these elements consist of one or two circular surfaces (front and back) with a rim of arbitrary shape. My goal is to simply connect the front and back su ...

HTML: Deconstruct, Analyze, and Revamp for a mobile-friendly site

I am currently working on a project for my computer science class in which we are required to choose an application that interests us and understand how it functions. I decided to focus on the technology provided by dudamobile.com This particular app is d ...

Creating a Responsive Table with HTML and CSS in Magento

I'm having trouble formatting a table on my Magento website using the HTML Code Element. Even with responsive media types, the table is still extending off the screen on mobile devices. Check out my Magento Responsive Table for Mobile <style&g ...

CSS - selecting elements that are greater than a specific criteria N

I have multiple <p> tags in the body of my HTML. I want to display only the first two paragraphs and hide all the paragraphs that come after. However, the code I'm using doesn't seem to work as expected. <html> <head> < ...

Is there a way for me to superimpose text onto an image in this instance?

I am attempting to place a "Text overlay goes here" over the profilepic.jpg image below. I believe there may be an issue with my CSS code. My approach was to create a new div class named overlay and embed that line within the imgcontainer class. Can some ...

I successfully filtered the total sum in my models property, resulting in the correct answer. However, it only provided me with each

I recently came across this code snippet that provides the correct answer, but I have a question. When I added HTML to calculate the total sum, will it write in the database how many rows I have and give me the same result as the row count? def total_ove ...

Loading indicator displayed at the top of a div using JavaScript/jQuery

My current challenge involves implementing a progress bar, similar to the pace.js progress bar. The issue arises when the browser is refreshed, as the pace.js progress bar loads on top of the body instead of within a specified div. It is important that the ...

Issue with FullPageJS: scrollOverflow feature not functioning upon click event

I am currently working on a Fullpage.js instance that needs to be initialized with a click event and then destroyed when switching to another page, and vice versa. The scrollOverflow feature works perfectly as long as the #fullpage element is not hidden up ...

php prevent unauthorized direct access to URLs

I have a variety of files such as: _main.php_, _header.html_, _page1.php_, _page2.php_,.. The header.html is included in main.php and contains links to page1.php and page2.php. I want to restrict access to page1.php and page2.php so that they can only be ...

Add a 'tag a' directly following 'img'

Check out this code snippet: <a href="#" class="list-group-item"> <span class="badge"><?=$st ?></span> <img class="small_profile_pic" src="<?=$pic ?>" /> aaa<a ...

Is there a way to import an image from a different webpage using HTML?

Is it possible to load an image from another page using the load method? Here's an example: $("#result").load(url, 'tag.attribute') However, I am facing a challenge where I need to retrieve the path of the first image from a list on anothe ...

Ordering tables in jQuery with both ascending and descending options

Trying to sort a table in ascending and descending order using jQuery? Here's the JavaScript code for it. However, encountering an error: Cannot read property 'localeCompare' of undefined If you can provide guidance on how to fix this so ...

.slideDown Not Functioning Properly on my System

After successfully linking my index.html file to jQuery, I am facing an issue with the .slideDown code. I'm not sure if there is a problem with the code itself or if I didn't attach jQuery correctly. Can anyone help me troubleshoot this? Below i ...

Achieving the minimum width of a table column in Material-UI

Currently I am in the process of developing a React website with Material-UI. One query that has come up is whether it's feasible to adjust the column width of a table to perfectly fit the data, along with some extra padding on both ends? Outlined be ...

Browser displaying a CSS error: "Invalid property name" while applying pseudo-element :after

I encountered an issue in Chrome and Explorer while attempting to set a CSS property for a pseudo element :after. (I'm trying to create a styled burger nav icon) The error message I received was: 'Unknown property name' This happened wh ...

Ways to retrieve JSON data through multiple levels of nested if statements

Currently, I am using CodeIgniter to work on a small project involving creating batch lists. When an admin attempts to create a new batch list, they must input the start date, end date, start time, and end time. The system will then check the database to s ...

JQuery: Ensuring only one panel is expanded at a time

Each time an <i> tag inside a <li> is clicked, it toggles the display of the following <ul>. To ensure that only one <ul> is expanded at a time, clicking on another <i> will collapse the previously expanded <ul>. For a ...

Twitter Bootstrap enables the creation of a responsive layout for websites

I need help creating a responsive layout with Bootstrap. I'm not sure how to code the section below to ensure it functions properly with Bootstrap. Following Twitter's guidelines, I have already used div.row and div.span12. In this section, ther ...

Why is it not achievable to dynamically update the HTML DOM structure while `<!DOCTYPE html>` is specified?

The following HTML code is causing an issue: <!DOCTYPE html> <html> <head> <title>Clear a Timer</title> <meta charset="utf-8" /> <script> var theTimer, xPosition = 0, theImage; functio ...

Using CSS to conceal an element when a different element is also invisible

My inquiry is as follows: I currently possess a code that conceals class element 1 when it possesses a specific value (which varies across different sites). Now, my objective is to also conceal class element 2 ONLY IF class element 1 is already hidden. ...