Boguz Didgeridoo
Boguz Didgeridoo

Reputation: 151

change font size with jQuery when text is too long

on a wordpress theme i am trying to make i have some what narrow spaces for the post titles. Normally it is ok when the words aren't too big (because a word can always go to the next line), but when there is a very long word (like "ANTIDISESTABLISHMENTARIANISM") then it is a problem.

I thought a solution could be to check with jQuery if on the title there is any word with more than x characters. i wanted to start be trying the total amount of characters in the title (vs. looking for long single words in side the title).

My code looks like this:

$(function(){
    var $title = $(".entry-title a").text();
    var $titlesize = $title.lenght();

    if ($titlesize > 10){
        $(title).css("color","red");
    }
});

(on this example i try to change the text color, so it is easy to see if it working or not... ;) ).

Any ideas on i could solve this? Would something like this affect ALL of the post titles or just the one which is too big?

Upvotes: 0

Views: 1702

Answers (2)

Gaucho_9
Gaucho_9

Reputation: 265

Another solution with CSS3: text-overflow: ellipsis

Example

Upvotes: 1

j08691
j08691

Reputation: 208032

Why don't you just use CSS to handle this? Use the word-break property.

Ex: word-break: break-all;

jsFiddle example

Upvotes: 2

Related Questions