Showing the blank lines from the uploaded information within a <p> tag

Here is some text with empty lines (retrieved from a Database) inside a paragraph element:

<p className={styles.biography}>{loaderData.cast.biography} </p>

.biography {
    font-size: 1.4rem;
    color: #333333;
    text-align: justify;
  }

Unfortunately, the empty lines are not showing up as expected. For example, this is part of the text fetched from the Database:

to stay new and fresh even after over four decades in the business.

Downey was born April 4, 1965 in Manhattan, New York....

Instead of displaying with blank lines, it appears like this:

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

I attempted changing the P tags to DIVs, but encountered the same issue. Can anyone provide advice on how to render the empty lines from the data within the output P using CSS?

Answer №1

To maintain whitespace in your text, you can utilize the CSS property white-space. Adjust the value to pre, pre-line, or pre-break depending on your desired text behavior.

Here is an overview from the MDN documentation on white-space updated on 2023-08-11.

Values

The values for the white-space property can be single keywords selected from the following list or two values representing shorthand for white-space-collapse and text-wrap properties.

  • normal

    • : White space sequences are collapsed, treating newline characters like other spaces. Lines break as needed to fit boxes.
  • nowrap

    • : Collapses white space similar to normal, but prevents line breaks within the text.
  • pre

    • : Preserves white space sequences. Text only breaks at newlines or <br/> elements.
  • pre-wrap

    • : Preserves white space sequences. Lines break at newlines, <br/>, and when necessary to fill boxes.
  • pre-line

    • : Collapses white space sequences. Lines break at newlines, <br/>, and as required to fill boxes.
  • break-spaces

    • : Behavior mirrors pre-wrap, with added features:

      • All preserved white spaces occupy space, even at the end of lines.
      • A line-breaking chance emerges after each preserved space character, including between such characters.
      • These spaces take up space without hanging, influencing box sizes (min-content and max-content).
New lines Spaces and tabs Text wrapping End-of-line spaces End-of-line other space separators
normal Collapse Collapse Wrap Remove Hang
nowrap Collapse Collapse No wrap Remove Hang
pre Preserve Preserve No wrap Preserve No wrap
pre-wrap Preserve Preserve Wrap Hang Hang
pre-line Preserve Collapse Wrap Remove Hang
break-spaces Preserve Preserve Wrap Wrap Wrap

For further insights into HTML and whitespace handling, refer to MDN - How whitespace is handled by HTML, CSS, and in the DOM.

// taken from the provided question
const textWithWhiteSpace = `to stay new and fresh even after over four decades in the business.

Downey was born April 4, 1965 in Manhattan, New York....`;

function App({ text }) {
  return <p className="biography">{text}</p>;
}

ReactDOM.render(<App text={textWithWhiteSpace} />, document.getElementById("root"));
.biography {
    font-size: 1.4rem;
    color: #333333;
    text-align: justify;
    /* preserve whitespace */
    white-space: pre;
}
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Answer №2

To ensure that spaces are counted and not trimmed, utilize the <pre></pre> tag. This tag is effective in preserving formatting by considering all spaces.

<pre className={styles.biography}>{loaderData.cast.biography} </pre>

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

The onInput feature in React 360/React VR is not working as expected

I'm currently working on a simple React 360 app to practice handling input from different sources. For more information, I've been referring to the documentation available at React 360 Input Handling. However, despite studying the content on the ...

Encountered an invalid prop type error while employing CSSTransition

Encountering an issue with the implementation of CSSTranstion for React. The purpose is to animate the mobile menu in a small application. Everything was functioning properly until the inclusion of the react-transition-group package, specifically using < ...

HTML code with embedded messages

I am interested in creating a straightforward message forum system. My goal is to organize messages in a nested structure (answering questions). For my website, I want it to be in Hebrew (dir="rtl"). I am considering generating <ol> elements dynam ...

Is there a way to set reCAPTCHA as a mandatory field using material-ui components?

How can I make a recaptcha field required in my form when using the "react-google-recaptcha" component along with "material-ui"? I want it to behave like other fields that are required, such as the description field below: <Field ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...

Creating offspring within offspring

I am currently facing a problem that I believe should be easy to solve. The issue revolves around rendering a component on a particular page. I have set a layout for all child components under the dashboard, but I am uncertain if another layout is needed f ...

Send the 'key' parameter to the React component

Attempting to pass a parameter named "key" to a react component is resulting in it not functioning properly. However, when I use "keyy" instead of "key," it works as intended. It appears that using "key" as a parameter name may be causing issues due to it ...

What is the best way to create an iframe positioned above a navigation bar using CSS?

When I click the button, an iframe pops up on my website. However, the navbar overlaps it unless I scroll down. You can view my practice site at: This is the issue I am facing: https://i.sstatic.net/TKmw7.png Oddly enough, everything looks fine when I ...

Alignment of text varies between canvas and HTML

How can I ensure that the labels in both canvas and HTML display overlap exactly, regardless of font family and size? The current code fails to center text inside an area when switching between canvas and HTML view. let canvas = document.getElementById( ...

The routing feature functions properly on localhost but encounters issues on the live server

There seems to be an issue with this code. While it works perfectly on localhost, it doesn't function as expected on a live Apache server. I have already specified the homepage in the package JSON file and included an htaccess file. However, when acce ...

After each animation in the sequence is completed, CSS3 looping occurs

I have currently set up a sequence of 5 frames, where each frame consists of 3 animations that gradually fade into the next frame over time. My challenge is figuring out how to loop the animation after completing the last sequence (in this case, #frame2). ...

What could be causing the issue with saving form data to local storage in React?

I am facing an issue with the code I have written to store data in local storage. Sometimes it works fine, but other times it saves empty data or erases the previous data when the Chrome window is closed and reopened. Any idea what could be causing this in ...

Optimizing Static File Caching in Yii

Having a frustrating issue with Yii where my local development environment caches CSS and JS files. Despite making changes to the file, the edits do not reflect in the output and sometimes causes corruption leading to broken functionality. This problem see ...

Strange fuzzy ring on Safari IOS HTML5 videos

There is a strange circular shape that keeps appearing on certain websites (image below) when viewed on an iPhone using Safari. Upon inspecting the Safari console, it seems to be related to the autoplay feature of a video, but unfortunately, I have been un ...

Issues with implementing Callouts CSS in Highcharts are causing complications

Currently, I am attempting to construct a callout, and I aim to format the callouts using CSS. Unfortunately, the CSS implementation seems to be malfunctioning for some unknown reason. Below is the HTML: <script src="https://code.highcharts.com/highch ...

What are the steps to achieve a smooth transition from a background-image to transparency?

Take a look at this related image: The goal is to replicate the design shown on the right side of the image. However, there's also a parent container with its own background image, as opposed to a solid color. Any guidance on how to achieve this? U ...

Animating the scaling of a background image with JavaScript resulted in a jittery, unstable image

I'm encountering a shaking effect when I animate a DIV container with a background image using JS. How can I make the animation smoother? $('body').on('click', '#container', function() { $("#container").animate({ ...

Effortless sliding panel that appears on hover and vanishes when mouse is moved away

I am in the process of creating a menu for my website that utilizes linkbuttons which trigger additional linkbuttons to slide down upon hover. The desired effect is a smooth sliding panel that appears when hovering over the linkbutton, and disappears when ...

The webpack production build consistently displays the message "This site is running on the development build of React."

I have been attempting to utilize webpack-4 for generating a production build of my React project (not built with Create React App), but I am facing difficulties. The project is written in TypeScript and uses ts-loader, as well as React version 15.6.2. Be ...

Problem encountered: ReactJS unable to parse JSON array

Below is my App.jsx file: import React from 'react'; class App extends React.Component { constructor() { super(); this.state = { data:require('json!./dataa.json') } } render() { return ( <body> <div> ...