iTranslated by AI
Prevent Rendering Block by Inlining External CSS into Style Tags
Hello. How do you all load CSS when building a website?
A common method is to use <link> inside the <head> to load files like style.css.
I often use this method myself, but ever since I learned that "When a browser loads an external file, it does not proceed to the next step until that file has finished loading (rendering block)," I have been trying to write CSS directly before </head> as much as possible.
Avoiding rendering blocks leads to faster display speeds.
■ BEFORE: Loading external CSS
<head>
<link rel="stylesheet" href="../common.css">
</head>
↓
■ AFTER: Writing CSS directly within <head> > <style>
<head>
<style>
〜 Write CSS here 〜
</style>
</head>
However, writing CSS inside <style> tags has the disadvantage of making management and updates difficult. This creates the following dilemma:
Outputting the contents of an external CSS file into <style>
Now we finally get to the main topic.
I will introduce a method to keep your CSS as an external file while outputting only its content inside <style> tags.
By doing this, you can achieve both "avoiding rendering blocks" and "ease of management/updating."
While this may not be applicable in every scenario, it should be useful at times.
This time, I will introduce how to do this using the world's No. 1 market share CMS, "WordPress."
Getting the content of style.css
Use PHP's file_get_contents to store the content of style.css in a variable.
I think it is fine to write this code in your theme's functions.php or similar files.
(Here, I define it as a function to make it easy to reuse.)
function my_css_minifier(){
global $post;
$css = '<style>';
$css .= file_get_contents(get_template_directory_uri().'/css/style.css');
$css .= '</style>';
// Change relative paths to absolute paths as the path relationship will change.
$css = str_replace('../', get_template_directory_uri().'/', $css);
return $css
}
After that, if you echo it just before </head>, the contents of style.css will be output.
<head>
<?php echo my_css_minifier(); ?>
<?php wp_head(); ?>
</head>
Since it's PHP, every time the site is displayed, it executes "Get the contents of style.css → Output just before </head>". Therefore, if style.css is updated, the content inside <style> will be automatically updated as well.
Now, we have achieved both "avoiding rendering blocks" and "ease of management/updating."
Further Improving Display Speed by Minifying CSS
With the work done so far, we have achieved the goal in the article title: "Copying the content of an external CSS file into <style> tags to avoid rendering blocks."
Now, let's take it a step further and perform "Minification (Optimization)".
Modify the code previously added to functions.php as follows:
function my_css_minifier(){
global $post;
$css = '<style>';
$css .= file_get_contents(get_template_directory_uri().'/css/style.css');
$css .= '</style>';
// Minifier
$css = preg_replace('/\/\*.*?\*\//s', '', $css);
$css = str_replace(array(PHP_EOL, " "), '', $css);
// Change relative paths to absolute paths as the path relationship will change.
$css = str_replace('../', get_template_directory_uri().'/', $css);
return $css
}
When outputting, it is the same as before.
Now, the CSS with line breaks and other characters removed will be output.
<head>
<?php echo my_css_minifier(); ?>
<?php wp_head(); ?>
</head>
Changing the Target CSS for Each Page Using Conditional Branching
If you want to load different CSS depending on the page, such as CSS for the top page or CSS for article pages, you can add conditional branching using if statements.
function my_css_minifier(){
global $post;
$css = '<style>';
// Common CSS
$css .= file_get_contents(get_template_directory_uri().'/css/style.css');
if(is_front_page()){
// CSS for Top page
$css .= file_get_contents(get_template_directory_uri().'/css/top.css');
}elseif(is_single()){
// CSS for Article page
$css .= file_get_contents(get_template_directory_uri().'/css/article.css');
}
$css .= '</style>';
// Minifier
$css = preg_replace('/\/\*.*?\*\//s', '', $css);
$css = str_replace(array(PHP_EOL, " "), '', $css);
// Change relative paths to absolute paths as the path relationship will change.
$css = str_replace('../', get_template_directory_uri().'/', $css);
return $css
}
That's all. This was an introduction to a little tip (or maybe a medium-sized one?).
I thought there would be some demand for this, but since there wasn't much information available, I decided to write an article about it.
Please let me know in the comments if you have any feedback or questions.
Discussion