I’ve done something similar taking the simple approach of adding a CSS class to titles based on their character count. That CSS class then decreases letter-spacing
and/or font-size
.
// Count the characters, taking Unicode into account
$chars = mb_strlen($post->post_title);
// For every 10 characters after the first 20, add a size
$size = max(0, ceil(($chars - 20) / 10));
// Generate the CSS class: x-long, xx-long, etc.
$class = ($size) ? str_repeat('x', $size).'-long' : '';
// Output
echo '<h1 class="'.$class.'">'.esc_html($post->post_title).'</h1>';
This approach is quite naive, though. It doesn’t take into account the actual character width as rendered by the font. For that, you’d need a JavaScript approach.
Related Posts:
- JavaScript Loading Screen while page loads
- Is it OK combined wp-include js, jquery, css? [closed]
- Getting click on item to open closest relevant element (popup) with jQuery
- jQuery for Fade Preload Causes My Site Favicon to Disappear from Tab of Browser
- Site images are fixed in front of my site content [closed]
- WP Media Uploader modal conflicts with Bootstrap modal
- JavaScript sleep/wait before continuing [duplicate]
- Why does my JavaScript code receive a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error, while Postman does not?
- How to change CSS using jQuery?
- How do I link a JavaScript file to a HTML file?
- What does [object Object] mean? (JavaScript)
- Remove class using jQuery
- Why does my JavaScript code receive a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error, while Postman does not?
- How to set time delay in javascript
- JavaScript error (Uncaught SyntaxError: Unexpected end of input)
- JavaScript error (Uncaught SyntaxError: Unexpected end of input)
- How can I scroll to an element using jQuery?
- jQuery equivalent of JavaScript’s addEventListener method
- Adding an onclick function to go to url in JavaScript?
- Checking a Url in Jquery/Javascript
- Bootstrap Dropdown menu is not working
- My javascript is returning this error: $.ajax is not a function
- Best way to store a key=>value array in JavaScript?
- jQuery if div contains this text, replace that part of the text
- Javascript Runtime error: $ is undefined
- Change Background color (css property) using Jquery
- JavaScript: filter() for Objects
- JavaScript runtime error: ‘$’ is undefined
- When and why to ‘return false’ in JavaScript?
- Set timeout for ajax (jQuery)
- How to generate a simple popup using jQuery
- Use images like checkboxes
- Adding images to a Bootstrap Carousel with AJAX
- Bootstrap Dropdown menu is not working
- Setting background-image using jQuery CSS property
- Bootstrap Dropdown menu is not working
- Format date to MM/dd/yyyy in JavaScript
- What do curly braces in JavaScript mean?
- How is JavaScript .on() method defined?
- Click button copy to clipboard
- Rotate image with javascript
- How to make html table vertically scrollable
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- Javascript isnull
- Equivalent of jQuery .hide() to set visibility: hidden
- TypeError: $(…).modal is not a function with bootstrap Modal
- Why is the jquery script not working?
- TypeError: $(…).DataTable is not a function
- How to make a simple image upload using Javascript/HTML
- Change text on hover, then return to the previous text
- How to make Bootstrap carousel slider use mobile left/right swipe
- Javascript TypeError: Cannot read property ‘indexOf’ of undefined
- bootstrap 3 navbar collapse button not working
- JQuery: $.get is not a function
- How to center image in carousel
- How to get client’s IP address using JavaScript?
- Can Twitter Bootstrap alerts fade in as well as out?
- Play/pause HTML 5 video using JQuery
- javascript function wait until another function to finish
- Simple throttle in JavaScript
- Refresh/reload the content in Div using jquery/ajax
- How to read data From *.CSV file using javascript?
- Enable & Disable a Div and its elements in Javascript
- How to get height of div in px dimension
- jQueryUI dialog width
- How to display raw JSON data on a HTML page
- How can I expand and collapse a using javascript?
- How to call a webservice method from an html page [javascript] with out refresh the page
- Javascript – ERR_CONTENT_LENGTH_MISMATCH
- Javascript : array.length returns undefined
- Random background images CSS3
- “Uncaught TypeError: a.indexOf is not a function” error when opening new foundation project
- The ‘describe’ keyword in javascript
- How can I remove all CSS classes using jQuery/JavaScript?
- Why is jquery show() not working in example
- jQuery scrollTop() method not working
- How can I change the text color with jQuery?
- Fully responsive HTML5 video
- How do I remove a box-shadow effect from an element when another element is hovered?
- How to change cursor from pointer to finger using jQuery?
- Slide right to left?
- How can I remove a style added with .css() function?
- How to use aria-expanded=”true” to change a css property
- How to set “style=display:none;” using jQuery’s attr method?
- I want to enqueue a .js file to my child theme
- Is there a way to enable Cross-Origin Resource Sharing for WordPress’ ajaxurl?
- Can’t get a JSON object in response to an Ajax request with wp_ajax
- How to get attachment id as soon as it is uploaded through media uploader in jquery?
- Add tinymce buttons to caption textarea in media uploader
- How to prevent wordpress from loading old versions of jquery in wp_head();?
- Move WordPress native javascript to bottom of page?
- Add custom shortcode button to Editor
- RequireJS in custom theme. Working Example
- Infinite scroll / lazy loading with NextGen Gallery [closed]
- Ajax call does not activate callback function
- How to handle multiple instance of “send_to_editor” js function
- Using ‘$’ instead of ‘jQuery’ in WordPress
- jQuery UI Datepicker not working
- How to get a jQuery script to run on a page?
- Defer Parsing of Scripts