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
- How to set time delay in javascript
- Change Background color (css property) using Jquery
- JavaScript: filter() for Objects
- JavaScript runtime error: ‘$’ is undefined
- When and why to ‘return false’ in JavaScript?
- How to generate a simple popup using jQuery
- Use images like checkboxes
- How is JavaScript .on() method defined?
- How to make html table vertically scrollable
- Why is the jquery script not working?
- TypeError: $(…).DataTable is not a function
- Change text on hover, then return to the previous text
- How to center image in carousel
- How to get client’s IP address using JavaScript?
- Simple throttle in JavaScript
- Enable & Disable a Div and its elements in Javascript
- How can I expand and collapse a using javascript?
- Javascript : array.length returns undefined
- “Uncaught TypeError: a.indexOf is not a function” error when opening new foundation project
- The ‘describe’ keyword in javascript
- How do I remove a box-shadow effect from an element when another element is hovered?
- How to set “style=display:none;” using jQuery’s attr method?
- 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?
- RequireJS in custom theme. Working Example
- How to handle multiple instance of “send_to_editor” js function
- How to get a jQuery script to run on a page?
- Modify Custom Field in Media Library using JavaScript
- Asynchronous Javascript Loaders
- Best approach for loading a sidebar Only if the screen max-width is >900px?
- Stylizing external SVG files with jQuery and CSS
- wp_enqueuescript won’t load in footer even with true value set?
- Execution of JavaScript on save post
- Using wp_ajax and wp_ajax_nopriv hooks
- Prevent five different copies of jquery from being included on every page
- Is placing stuff in page text edit window bad form?
- javascript is not working on Event Submission?
- Add colorpicker to featured image
- jQuery Tree View and wp_list_pages
- WordPress audio player causing js error, mediaelementplayer is not a function
- Auto-detect javascript errors and warn theme user
- combining js scripts from a js newb [closed]
- Loading posts via AJAX in a hidden div with updated URL?
- Heartbeat API: How to access data already enqueued?
- Implementing lazy loading in homepage posts instead of pagination
- How to show jquery popup only in one page or once?
- Correct way to write jQuery functions in WordPress
- Add a class to posts in increments of 2
- How can I make this custom menu work?
- Remove WordPress scripts
- Using bootstrap-slider and bootstrap-switch in wordpress admin panel
- Trouble with WordPress Ajax Login System – 302
- How does WordPress handle HTML Tables interaction with jQuery
- Javascript stopped working [closed]
- Why load JS through the CMS framework? [duplicate]
- Problem passing id-specific objects to javascript via wp_localize_script
- url – ajax loaded but no JS
- Callback After WordPress Customizer Complete Loading
- Create special WordPress slideshow with “jquery.cycle.all.js”
- Close Fancybox, sumbit form data and redirect
- How to enque js script in footer that are not in .js file?
- Properly embed javascript into WP (using function.php) – doesn’t work?
- Nested Gallery Shortcode INTERMITTENTLY working
- How to keep HTML form field that is conditional hidden with javasript hidden after page reload?
- Accessing javascript in multiple files [closed]
- How to Add a Read-only Textbox With Text in it That Has a Button to Copy it?
- Issues loading Jquery on WordPress website
- Custom AJAX form not working async
- Including Older Version of jQuery for IE7/IE8
- Parse wordpress blog data using json api and ajax
- Problem adding MP3 attachments to a jPlayer playlist
- Jquery not working
- A carousel slideshow plugin in JavaScript
- JS behavior differs depending on page tree [closed]
- Add other class content with reference class value
- Theme customisation – how to store javascript externally when it utilises php variables?
- Get ajaxForm response value
- onclick jquery function not working in wordpress header file [duplicate]
- Add a jQuery snippet for sepcific user role
- Adding scripts before plugins scripts
- jQuery search function not running in WordPress but running locally (fiddle)
- Masonry – Can’t Find Variable imagesLoaded
- How to update scrollbar when using Jetpack’s Inifnite Scroller?
- Clear custom field when new tag is added
- Are you sure popup on Contact Form 7 [closed]
- change the icon of a custom post type in WordPress to use twitter boostrap
- Element works, but not on wordpress site [duplicate]
- javascript not loading in wordpress
- JS file not loading [closed]
- Carousel Hover Image
- How do I add Javascript and CSS files into WordPress?
- How to Javascript/jQuery
- can’t make jQuery work (change image on time interval) [closed]