0

I have the following code and using css text-transform property with a value of capitalize or php ucwords() I get the title capitalized but when the title is provided with all letters as capitals it doesn't work. How can I achieve the output to be capitalized regardless of the title provided?

  <h4 style="text-transform: capitalize;">
      <a href="{{ route('posts.show', $post )}}">{{ $post->title }}</a>
  </h4>
Alphy Gacheru
  • 489
  • 1
  • 9
  • 28
  • Using CSS or PHP? Does the title contain only one word? Or multiple? Can you post any examples? – brombeer Sep 18 '21 at 08:53
  • Does this answer your question? [Lower case all then capitalize - pure CSS](https://stackoverflow.com/questions/21827377/lower-case-all-then-capitalize-pure-css) – Florian Motteau Sep 18 '21 at 08:53
  • @brombeer Any, or all if possible! – Alphy Gacheru Sep 18 '21 at 08:55
  • 1
    In php you can use : ucfirst(strtolower("STRING")) – Nero Sep 18 '21 at 09:04
  • @FlorianMotteau Yes, it does answer my question by indicating it's not possible with css but I asked how to do it in css or php and apparently is possible and easy in php. Confused, should I still accept it as a duplicate question? Kindly advice! – Alphy Gacheru Sep 18 '21 at 09:15

3 Answers3

2

As long as it's only one line you can do it with CSS by combining lowercase and capitalize using the first-line pseudo selector:

.title-case {
  text-transform: lowercase;
  display: inline-block;
}

.title-case::first-line {
  text-transform: capitalize;
}
<h4>
  <a class="title-case" href="#">SHOUTING</a>
</h4>
dave
  • 2,750
  • 1
  • 14
  • 22
1

Twig has as capitalize filter

https://twig.symfony.com/doc/3.x/filters/capitalize.html

{{ 'my first car'|capitalize }}

{# outputs 'My first car' #}

Perfect spot to do this, the view that is.

0

A php solution would be to convert case to lower and then use ucfirst

$text = 'THIS IS A TITLE';
$normalText = ucfirst(strtolower($text));
echo $normalText;
endeavour
  • 576
  • 4
  • 15