Indentation in html paragraph

Asked

Viewed 112 times

-1

Does anyone know how to make a indentation in the first lines of each paragraph of a text typed through a textarea?

Using php’s nl2br function I can even save and display line-breaking text, but I can’t indent the first line of the paragraph.

Doing so

 @php 
   echo '<p style="text-indent: 1.25cm;">'.nl2br($oficio->texto).'</p>'; 
 @endphp

it gives the indentation only in the first paragraph, the remainder of the paragraphs is without the indentation.

2 answers

2

Put text-indent in the container father and not directly in the <p>

div {
  border: 1px solid #000;
  width: 200px;
}
<div style="text-indent: 30px;">

  <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. </p>
  <p>Quam corrupti ipsam molestias consectetur in at exercitationem et porro aut, sint aperiam libero?</p>

</div>

OBS: Based on the example above, if you want to use CSS vc you can directly attack each <p>, the result will be the same as the code above.

div > p {
  text-indent: 30px;
}

1

User line returns are stored as \n in the database and modified to <br /> for nl2br.

\n\n usually means a new paragraph (enter). The white space between paragraphs is CSS and is actually the default browser style. \n is a <br>(shift-enter).

So don’t use the nl2br() and do it yourself:

$texto = '<p>' . htmlspecialchars($text) . '</p>'; // HTML ENCODE
$texto = preg_replace('#\n\n\n*#', '</p><p>', $text); // 2 ou mais \n
$texto = preg_replace('#\n#', '<br />', $text); // tudo sobrando \n
$texto = preg_replace('#><#', ">\n<", $text); // Se você gosta de </p>\n<p> com uma nova linha entre eles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.