The implementation of pre-wrap styling resulted in a significant increase in the indentation of the initial

I am currently developing a book blogging platform that allows users to submit book reviews through a form with a textarea field.

To ensure that the user-entered paragraph breaks are displayed properly on the post's show page, I included a pre-wrap class to the paragraph element that will showcase the review text.

However, upon adding the pre-wrap class, an unexpected deep indent was introduced at the beginning of the first paragraph on the show page. This indent does not appear in the textarea field before form submission. I am seeking a solution to eliminate this large indent while maintaining the display of user-entered paragraph breaks.

Below is the CSS styling for pre-wrap:

.preWrap {
    white-space: pre-wrap;
}

Here is the textarea form field:

<label for="review" class="form-label">Review</label>
    <br>
    <textarea class="form-control" name="book[review]" rows="7" placeholder="Enter your review here" required></textarea>

Here is the structure of the show page (as an li element to accommodate the use of Bootstrap cards for displaying each post):

<li class="list-group-item ">
    <p class="card-text preWrap">
        <%= book.review %>
    </p>                   
</li>

Below is how the show page renders the text:

https://i.sstatic.net/bx9PF.jpg

I have attempted to add text-indent: 0 to the CSS style sheet, but the indent persists. The indent is not present in the review stored in the database and only emerged after applying the pre-wrap style. I am puzzled by this indent and seek guidance on how to eliminate it.

Thank you.

Answer №1

It appears that the issue lies in the whitespace within your HTML, specifically between the closing bracket > of the

<p class="card-text preWrap">
tag and the beginning of the rendered book review (<%= bo...).

The visible whitespace and blue line in the HTML code directly correspond to the whitespace and blue line visible on the rendered page:

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

This is due to the use of white-space: pre-wrap, which renders all whitespace in the HTML as is in the browser's viewport, unlike normal behavior where whitespace is collapsed.

You have a couple of options to address this:

  1. Modify your CSS to utilize white-space: pre-line, which preserves line breaks in raw HTML (e.g. \r\n) while collapsing other whitespace.
  2. Alternatively, remove white-space: pre-wrap and use a method like
    <%= htmlEncode( book.review ).replace( "\r\n", "<br />" ) %>
    .
    • Ensure that you are properly HTML-encoding the book.review content to prevent security risks such as XSS attacks.

Here are examples of the two approaches:

Using pre-line:

li {
    outline: 1px solid red;
}

p {
    outline: 1px solid blue;
}

li > p.card-text {
    white-space: pre-line;
}
<li class="list-group-item">
    <p class="card-text">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu lorem non lorem dapibus euismod et vel felis. Suspendisse ipsum nulla, pellentesque pretium facilisis eget, posuere quis est. Nam condimentum tincidunt mauris, eu hendrerit massa auctor vel. Cras ut turpis vitae nulla vehicula pulvinar at quis augue. Sed ante magna, ullamcorper ut tempor non, ultricies non diam. Cras tristique erat ante, ut pretium dolor tincidunt id. Donec quis ornare est. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>                   
</li>

Using <br />:

While the first HTML example has two line breaks between each block of text, the second HTML example only includes a single <br />, which may require additional adjustments for desired aesthetics, such as adding more <br /> tags or modifying the CSS line-height.

li {
    outline: 1px solid red;
}

p {
    outline: 1px solid blue;
}
<li class="list-group-item">
    <p class="card-text">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu lorem non lorem dapibus euismod et vel felis. Suspendisse ipsum nulla, pellentesque pretium facilisis eget, posuere quis est.<br />Nam condimentum tincidunt mauris, eu hendrerit massa auctor vel. Cras ut turpis vitae nulla vehicula pulvinar at quis augue. Sed ante magna, ullamcorper ut tempor non, ultricies non diam.<br />Cras tristique erat ante, ut pretium dolor tincidunt id. Donec quis ornare est. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>                   
</li>

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 there a way to replicate the Vista Command Link in HTML?

This particular dialog mimics the design of a Windows Vista Command Link: View image here: http://i.msdn.microsoft.com/Aa511455.commandlinks01(en-us,MSDN.10).png I'm curious if anyone has created HTML code that replicates the appearance and function ...

MatDialog displaying no content

I found this tutorial on how to implement a simple dialog. The issue I'm facing is that the dialog appears empty with no errors in the console. Even after making changes to my dialog.component.html or dress-form.ts, nothing seems to reflect in the o ...

Gitbook is facing a unique challenge with the Chinese language in its HTML code - an issue with excessive spacing

Currently, I am utilizing gitbook and github pages to construct my personal webpage with the main language being Chinese. However, I have noticed that gitbook is adding an extra space in some places where it is unnecessary. Below is the text found in the m ...

Using Selenium with C# to input text into an HTML <input> tag is not functioning properly

I need help figuring out how to fill in an input field on a website using HTML. Here is what the section of the website () looks like: <div class="flex-item-fluid"> <input autocomplete="username" autocapitalize="off" ...

Determine the time variance in hours and minutes by utilizing the keyup event in either javascript or jquery

There are 6 input fields, with 5 input boxes designated for time and the result expected to display in the 6th input box. See the HTML below: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input ty ...

Unveiling the method to retrieve XML tag based on the attribute of its parent element

This snippet is a segment of XML code: <?xml version="1.0"?> <menu> <pizzas> <pizza number="0"> <title>Tomato &amp; Cheese</title> <small>550</small> <medium></medium> <large> ...

Enable the ability to upload multiple images on a single page using droparea.js

I am currently utilizing droparea js for uploading images. However, I have encountered an issue where if I try to upload an image in one of the upload menus, it ends up changing all four upload menus simultaneously. <div class="col-md-12"> ...

Implementing a switch to trigger a JavaScript function that relies on a JSON object retrieved from a GET request

Having some trouble using a toggle to convert my incoming Kelvin temperature to Celsius and then to Fahrenheit. It loads properly as default Celsius when the page first loads, but once I try toggling the function outside of locationLook, it doesn't se ...

What is the best way to create an impact?

Need help fixing the parallax effect on this page. I attempted to implement a parallax effect but encountered some issues. Here is the JavaScript code: $objWindow = $(window); $('section[data-type="background"]').each(function(){ var $bgOb ...

"Angular File Upload Made Easy with Drag-and-Drop Functionality and Cleverly Positioned

Encountering an issue with Angular File upload in conjunction with relatively positioned elements. The drop target is set to 100% width and height, absolutely positioned. While dragging a file over any non-relatively positioned element, the overlay functio ...

Tips for preventing Bootstrap 4 from automatically adjusting the height of all columns equally

I am receiving text from a loop that I am formatting into lists inside columns. I am attempting to float all my columns one behind the other without any space, but Bootstrap is applying the same height to all of them. Here is an example of what it currentl ...

Activate animation while scrolling the page

I am using a progress bar with Bootstrap and HTML. Below is the code snippet: $(".progress-bar").each(function () { var progressBar = $(this); progressBar.animate({ width: progressBar.data('width') + '%' }, 1500); }); <body> & ...

Expand the div width to the specified measurement horizontally

Is there a way to horizontally collapse a div (container) to a specific width that I can adjust, effectively hiding its content? The collapse effect should move towards the left. <div id="container"> <button type="button" id="myButton"> ...

What are some effective methods for overriding materialui styles?

Here is the CSS style for a checkbox in Material-UI that I captured from the console: .MuiCheckbox-colorPrimary-262.MuiCheckbox-checked-260 I attempted to override this style using the following code, but it was unsuccessful: MuiCheckBox: { colorP ...

Using jQuery to append a string to a CSS property

My current task involves modifying a series of divs with background images named Look-1.jpg to Look-6.jpg. I am looking to add "-cut" to all of them simultaneously to transform them into Look-6-cut.jpg. I have two collections of background images and I ai ...

Adjust the angle of an object precisely using a transform upon hovering over a designated button

I am looking to design a menu that always keeps the arrow pointing at the button I hover over with my cursor. If I don't hover on any button, then it should stay in the position of the last button I hovered over. HTML: <html lang="en"> <hea ...

When using the .after() method to add jQuery elements, keep in mind that they will not trigger any

Here is the snippet of code provided: <s:file name="upload" id="upload"></s:file> $('input[id^="upload"]').change(function(){ alert("aa"); $(this).after('<input type="file" name="upload_3" id="upload_3"/> ...

Layered parallax scenery

I'm interested in creating a parallax effect similar to this example. https://medium.com/@PatrykZabielski/how-to-make-multi-layered-parallax-illustration-with-css-javascript-2b56883c3f27 However, I find the use of HAML and Coffeescript in the mentio ...

Utilize GeoJSON to showcase multiple features in a convenient table format

I am currently facing a challenge in displaying all the features from a GeoJSON file in a tabular format on a website. Despite creating a Leaflet map that successfully shows all the locations from the GeoJSON file, I am struggling to proceed further. My g ...

Is it possible to incorporate a <div> element within a PHP code?

After successfully writing some PHP/SQL code, I encountered an issue when trying to create a new div called "content" along with applying a CSS class. The recommended approach was suggested as follows: $mydiv = '<div name="content">'; $myn ...