Enable automatic indentation for wrapped text

I'm still learning the ropes here, but I'm looking to have text automatically indent when it's wrapped. Instead of this:

Peter piper picked a peck
of pickled peppers.

It should look like this:

Peter piper picked a peck
    of pickled peppers.

This is what I've attempted so far:

span.profile{ display:block; text-indent: -35px; /*this pulls the first line to the left*/ padding-left:35px; /*this pushes the paragraph to the right*/ padding-right:0px; text-align:justify; }

After implementing this, I wrap the entire text with span class="profile", but the issue is that I want it to "reset" after each line break. Is there a way to achieve that?

Appreciate any assistance!

Answer №1

I decided to showcase a compact JSFiddle example.

Does this meet your requirements?

div {
    width: 500px;
}
div p {
    text-indent: -20px;
    padding-left: 25px;
}

Although this resembles the CSS you provided, I have implemented it for every paragraph individually.

Please inform me of your thoughts.

Answer №2

It appears from the feedback that when you mention wanting it to 'reset' after each line break, you are looking for a way to prevent the next line from being indented following a <br>. Unfortunately, CSS does not have a mechanism to target the line immediately after such breaks.

To achieve the desired outcome, consider revising your HTML structure. If you apply display: block to a span, it may indicate the need for a block-level element like div or p instead. Instead of relying on <br>, opt for block elements to encapsulate sections of text.

Answer №3

A new addition to CSS is the inclusion of the hanging and each-line options for the text-indent property. These options allow for easy indentation of each line after the first, eliminating the need for negative values and padding. Additionally, they apply the indent even to lines following hard breaks like <br/>.

At present (April 2024), only Firefox and Safari have implemented support for these new options. While they are part of the official CSS specification, they are still relatively recent additions. It is hoped that Chrome and other browsers will add support for them in the near future.

/* (NOTE: Only works on Firefox and Safari as of writing!) */
.indent {
  text-indent: 3em hanging each-line;
}
<p class="indent">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lectus vestibulum mattis ullamcorper velit sed ullamcorper. Cursus mattis molestie a iaculis at erat pellentesque adipiscing commodo. Nunc id cursus metus aliquam eleifend. Odio facilisis mauris sit amet massa vitae.
</p>

<p class="indent">
Elit sed vulputate mi sit amet mauris commodo. Sed risus ultricies tristique nulla aliquet enim tortor. Eu turpis egestas pretium aenean pharetra magna.<br/>
Metus aliquam eleifend mi in nulla posuere sollicitudin. Risus quis varius quam quisque id diam. Facilisi etiam dignissim diam quis enim. Cras fermentum odio eu feugiat pretium nibh ipsum. Ornare lectus sit amet est placerat in egestas erat imperdiet.<br/>
Vel risus commodo viverra maecenas accumsan lacus vel facilisis volutpat. In tellus integer feugiat scelerisque varius. Ac turpis egestas sed tempus urna et. In mollis nunc sed id semper risus in hendrerit gravida. Et tortor consequat id porta. Feugiat in ante metus dictum. Non consectetur a erat nam. Netus et malesuada fames ac turpis egestas.
</p>

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

Centering an image on a webpage using CSS

Here is the code I am using for displaying my image: return <div class="imgContainer"><img src={broomAndText} alt="broomAndText" /></div> In my css file, I have included the following styling: .imgContainer { tex ...

What is the best way to prevent content from spilling over into the div on the right

I'm having trouble with a two column div where the text in the left column is overflowing into the right column. How can I prevent this? http://example.com <div id="wrapper-industry"> <div id="wrapper-form"> <div id="form_row"> ...

When resizing, the image problem does not transition to the next column

Just started learning how to code and I've been stuck on this issue for a while now. Whenever I resize the browser window, the image doesn't move down to the next line after the .text class like I want it to. Instead, it overflows off the page to ...

Text cannot be highlighted within DIV element

Check out this page and this page on my website. Text within the advert DIV tag cannot be selected, and some text in the Incinerator product page is unselectable in the topic-div above the iframe element containing a YouTube video. Here is the relevant HT ...

Drawer component from Material-UI placed on top of the sidebar

Currently, I am working on a project where I am using React TypeScript along with Material-UI. The issue that I am encountering is related to the width of the Drawer component provided by Material-UI. By default, the Drawer takes up the entire screen wid ...

The "html" element fails to achieve a full height when applying a 100% height setting

I am facing a height problem of 100% <!doctype html> <html> <head> <meta charset="utf-8"> <title>My Dreamweaver Website</title> <link rel="stylesheet" href="style.css"> </head> <body> <section clas ...

The table contained within a row-spanning cell fails to fully occupy the cell's height across different browsers

Chrome rendering and code issue I'm struggling to understand why the nested table (highlighted in red) is not taking up the full height of the outer table cell (highlighted in green) which spans 8 rows. This used to work fine, but newer browsers are ...

Struggling with implementing a materialize modal?

I am encountering a problem with Materialize. This time, I am trying to create a modal div, but it doesn't seem to be working. The button is created, but when I click on it, nothing happens. I have made sure to link all the necessary Materialize files ...

Methods for concealing the "image not found" icon in Internet Explorer and Chrome through CSS

I have images displayed on a webpage. Currently, when an image is not available, both Chrome and IE display a broken image indicator. I want to hide this indicator and only show the alternative text. Is there a CSS solution to achieve this? ...

Positioning an immovable element beneath a fixed element that can vary in height

I am working on a webpage that features a fixed menu at the top-left corner, followed by the main content. My goal is to ensure that the content appears below the menu, but since I do not know the exact height of the menu in advance (as it can vary based o ...

Ways to integrate a CSS file into XSLT

I have a CSS file that looks like this: .table_class1DeffCell { border-top-width : 1; border-left-width : 1; border-right-width : 1; border-bottom-width : 1; } .table_class11DeffCell { border-bottom-color : 000000; border-top-color : 000000; border-right- ...

Difficulty encountered in aligning items within neighboring table cells vertically

Currently, I am facing a styling issue with a few table rows. In this particular screenshot: https://i.sstatic.net/FcmJW.png The cells in the blue and red circles are part of a table row (with a height of 50px). Both are set to "vertical-align:top". The ...

The web font fails to display on different computers

Recently, I discovered that my website only displays the font "Open Sans" correctly on my personal computer. When I tried to access it from two other PCs, the font was not displaying as intended. Strangely, the font appears fine on smartphones. What could ...

Angular controller utilizing the `focusin` and `focusout` events from jQuery

Can anyone help me figure out why this piece of code is generating syntax errors in my AngularJS controller? $(".editRecur").focusin(function() { $(.recurBox).addClass("focus"); }).focusout(function() { $(.recurBox).removeClass("focus"); }); ...

Identifying the moment when attention shifts away from an element

Is it possible to detect when focus occurs outside an element without relying on global selectors like $(document), $(body), or $(window) for performance reasons? If achieving this without global selectors is not feasible, provide a provable reason expla ...

I'm trying to create a transparent v-card, but for some reason it's not turning out the way I want

How can I make the v-card transparent while keeping its content opaque using CSS? card.vue <v-card class="cardColor"> <v-card-text> TEXT </v-card-text> <v-card-actions> <v-btn color="prima ...

Bootstrap4: What could be causing the adjacent row cell to remain left-justified instead of being centered as intended with mx-auto?

I've been working on creating a responsive header using Bootstrap4. The main goal is to have the logo displayed as left-justified text and the menu items as right-justified when the viewport is large, then transition to both being centered in a "small ...

Tips for including a permanent button at the bottom of a material ui dialog box

I want to implement a dialog popup in my react js application. When the user clicks on a button, the dialog should open up. Inside the dialog, there is a form with input fields. After the user fills out all the inputs, they can submit the information by cl ...

Creating a JQuery statement to conditionally change CSS values

Is there a way to determine if a div element with a CSS class of "x" has a height set to "auto"? If so, I would like a jQuery script to remove the CSS class "a" from all elements with the class "y". If not, the script can remain unchanged. Thank you. ...

Is there a way to modify the background color of ::before when hovering over it?

I have a ul-li list and when i hover last li ::before element looks white and not so good. I want to change it when i hover the last li element. How can I achieve this? Here are my codes: <div className="dropup-content"> <ul> & ...