Skip to content

How to Use wp_enqueue_style to Load CSS Stylesheets

Stylesheets are at the heart of making HTML pages look good, in WordPress and the web. And the way that we make CSS stylesheets load into the page in WordPress is using the wp_enqueue_style function. It’s not too complicated, but there a few common places that people new to using it will get tripped up. This Quick Guide will cover those, and then get you running in a step-by-step guide.

Understanding WordPress: wp_enqueue_style in More Depth

If you’re new to WordPress development, you may not be super familiar to the concept of WordPress specific functions. wp_enqueue_style is a common example of just such a WordPress-provided function. Though the name’s a little weird, it basically just means “tell WordPress to load a stylesheet.”

Here’s the video explaining more about loading in stylesheets in WordPress:

wp_enqueue_style Example Code to Load Your Stylesheet, Nothing Else

This code if directly from the video above, but reformatted with added comments. For me in the video, this code lives is in a plugin.

If you put this code in a theme, the biggest change you’ll need to make is $file_url will probably get defined using something like get_stylesheet_directory_uri instead.

// This line runes our function when 
//   WordPress hits the wp_enqueue_script
//   milestone of loading
add_action( 
	'wp_enqueue_scripts', 
	'wpshout_enqueue_styles' 
);
// This function runs because of the add_action
function wpshout_enqueue_styles() {
	// Location of our stylesheet
	//   For me, this shared a plugin
	//   with this code
	$file_url = plugins_url(
		'stylesheet.css', // File name
		__FILE__ // Magic PHP constant that means
					// the "current file"
	);
	// Actually load up the stylesheet
	wp_enqueue_style( 
		'sp_sytlesheet', // A "name" for our file
		$file_url // Location variable
	);
}

If everything in the code made sense to you, you can probably stop reading and copy-and-paste it. It will work fine. If not, please keep reading, we’ve got a great explanation for you. 😊

Which WordPress Hook to Use with wp_enqueue_style

If you watched the video, you may have already caught it, but for wp_enqueue_style, we’ve got to use a “WordPress hook” as well. We’ve got way more details about WordPress hooks generally in this great article:

WordPress Hooks, Actions, and Filters: What They Do and How They Work

If you want even more depth about them, we’ve got a Course of free articles that goes deep. Everything from registering your own hooks to de-registering others:

https://wpshout.com/course-complete-introduction-wordpress-hooks-system/

wp_enqueue_style for Styling Your WordPress Site

  1. Your first line of code is an add_action call which is used to make sure that WordPress runs the code that really contains your use of the enqueuing function. That looks something like add_action( 'wp_enqueue_scripts', 'function_name' );.  The second function name will match function you create in #2.
  2. Create a wrapper function for your use of wp_enqueue_style. For us, if you copied the line above, it would be function_name. I recommend a more descriptive name though. 🤓
  3. Inside of that function, you’ll need to locate the URL for your CSS stylesheet. For me, that meant getting the URL of the plugins directory using plugins_url, and then passing it the right arguments for it to produce the URL. You’ll probably need a different function if you’re in a theme.
  4. Finally, we pass it all to the wp_enqueue_stylesheet function from WordPress. Pretty quick and easy. We must define two arguments, though the function can take more. The ones I do in my example code are the (1) “handle” or nickname for WordPress to use with our stylesheet, and (2) the actual URL (or web address) of our CSS file.

Can you make wp_enqueue_style Async?

Being a Quick Guide, the topic of making wp_enqueue_style asynchronous is a little too ambitious for me to give you a final answers to. We have lots of experience and a great article about making wp_enqueue_script for JavaScript files asynchronous, which is common and supported well by browsers. Check that out:

https://wpshout.com/make-site-faster-async-deferred-javascript-introducing-script_loader_tag/

For CSS, it’s less common and support to load it out-of-band. Mostly, this is because most CSS must be loaded for a browser to start “painting” the web page’s content. Here’s a useful article from CodePen about why it’s not common. You can accomplish this approximate goal with JavaScript, which this article explain. And if you only need to support cutting-edge browser, preload of links is slowly getting supported by more of them. Can I Use has the latest statistics on <link rel=”preload”> support across browsers.

David Hayes

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Thiago
October 13, 2018 8:40 am

I’m trying to include a css after the wordpress css but my code does not work.

wp_enqueue_style (‘bootstrap_mega_importer’, array (‘common’, ‘forms’), false, ‘all’);

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

25 Best Free WordPress Themes (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...