Solution:1
Firstly, this will probably be awkward as you should be building responsive sites. Your text should readjust based on the screen reading the website, and if you are trying to define changes to make the text fit on one line you could be tweaking this over and over again.
I advise that you design around this and customise your layout rather than trying to change size based on text length, over and over again.
However, you could do something as follows within PHP/CSS.
Check the strlen
on the get_title
function. If the strlen
is more than a specified amount (say 20) you’d add a class, otherwise ignore it. Then, you’d style that class responsively based on how you wanted to change the font.
You’d need to edit your theme to replace the Title with the following:
<?php
$title = get_the_title();
if (strlen($title) >= 20) {
<h1 class="longer-text-modifier">
} else {
<h1>
}
<?= $title; ?>
</h1>
With your CSS being
.longer-text-modifier {
font-size: 16px; // or em or rem
}
Edit: This could also be done on the fly with JavaScript.