{"id":82983,"date":"2023-04-06T08:02:33","date_gmt":"2023-04-06T08:02:33","guid":{"rendered":"\/tutorials\/?p=82983"},"modified":"2025-04-26T02:57:37","modified_gmt":"2025-04-26T02:57:37","slug":"wp_enqueue_style","status":"publish","type":"post","link":"\/in\/tutorials\/wp_enqueue_style","title":{"rendered":"How to Enqueue CSS Stylesheet to WordPress Using wp_enqueue_style&nbsp;"},"content":{"rendered":"<p>When developing WordPress themes or plugins, it&rsquo;s essential to enqueue stylesheets to make them load correctly.<\/p><p>To do so, we recommend using the wp_enqueue_style() function. It&rsquo;s a powerful tool for adding custom stylesheets to your WordPress theme or plugin. This function also ensures that stylesheets are loaded only when necessary and helps avoid conflicts with other plugins or themes.<\/p><p>In this tutorial, we&rsquo;ll explore how you can use the <strong>wp_enqueue_style()<\/strong> function to improve your WordPress site&rsquo;s appearance and overall user experience.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all in one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-wp-enqueue-style-to-load-css-stylesheets-to-wordpress\">How to Use wp_enqueue_style to Load CSS Stylesheets to WordPress<\/h2><p>We&rsquo;ll start with a few basic examples to help you better understand how the <strong>wp_enqueue_style()<\/strong> function works.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-enqueue-main-style-css-stylesheet\">How to Enqueue Main style.css Stylesheet<\/h3><p>To enqueue the main theme <strong>style.css<\/strong> stylesheet, use the <strong>wp_enqueue_style() <\/strong>function in your theme&rsquo;s <a href=\"\/in\/tutorials\/functions-php-wordpress\">functions.php file<\/a>.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/in\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"854\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted-1024x854.png\" alt=\"The functions.php location in the twentytwentytwo theme directory on the File Manager\" class=\"wp-image-82986\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted-300x250.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted-150x125.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-functionsphp-highlighted-768x641.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Here is an example of how it looks:<\/p><pre class=\"wp-block-preformatted\">function my_theme_enqueue_styles() {\n    wp_enqueue_style( 'my_theme_style', get_stylesheet_uri() );\n}\nadd_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );<\/pre><p>In the code, <strong>my-theme-style<\/strong> is a unique name for the stylesheet you are enqueueing, while the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_stylesheet_uri\/\" target=\"_blank\" rel=\"noopener\">get_stylesheet_uri()<\/a> function handles the URL of the main theme&rsquo;s <strong>style.css<\/strong> file.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/in\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"795\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted-1024x795.png\" alt=\"The twentytwentytwo theme directory on the file manager. The style.css file is highlighted\" class=\"wp-image-82987\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted-300x233.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted-150x117.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/04\/filemanager-wpcontent-themes-stylecss-highlighted-768x596.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Next, the <strong>wp_enqueue_style() <\/strong>function registers the style and adds it to the queue. Finally, the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_action\/\" target=\"_blank\" rel=\"noopener\">add_action()<\/a> function adds your custom <strong>my_theme_enqueue_styles()<\/strong> function to the <a href=\"\/in\/tutorials\/wp_enqueue_script\">wp_enqueue_scripts<\/a> hook, which will ensure that the stylesheet is enqueued properly.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-enqueue-other-stylesheets\">How to Enqueue Other Stylesheets<\/h3><p>You can also use the <strong>wp_enqueue_style()<\/strong> function to enqueue additional styles on top of the main stylesheet. For example, add extra styling options in a separate file.<\/p><p>As for the code, you can reuse most of it from the previous example with a few extra additions:<\/p><pre class=\"wp-block-preformatted\">function my_theme_enqueue_styles() {\n    wp_enqueue_style( 'my-theme-style', get_stylesheet_uri() );\n wp_enqueue_style('my-theme-extra-style', get_theme_file_uri('extra.css') );\n}\nadd_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );<\/pre><p>In this method, we&rsquo;ve used the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_theme_file_uri\/\" target=\"_blank\" rel=\"noopener\">get_theme_file_uri()<\/a> function, which returns the file&rsquo;s URL in the current theme&rsquo;s directory. In our case, it&rsquo;s <strong>extra.css<\/strong>. This way, the function will enqueue the main stylesheet first and then move to the additional styles.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-load-external-stylesheets-to-wordpress\">How to Load External Stylesheets to WordPress<\/h3><p>It&rsquo;s possible to use the <strong>wp_enqueue_style()<\/strong> function to enqueue a stylesheet from an external source. The process can be beneficial if you want to use custom fonts or a stylesheet hosted on a content delivery network (CDN).<\/p><p>The code is very similar to previous examples:<\/p><pre class=\"wp-block-preformatted\">function theme_files() { \n    wp_enqueue_style('theme_custom', 'INSERT URL'); \n} \n\nadd_action('wp_enqueue_scripts', 'theme_files');<\/pre><p>Remember to replace the<strong> INSERT URL<\/strong> part with an actual stylesheet URL.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-add-script-files-to-wordpress-using-wp-enqueue-script\">How to Add Script Files to WordPress Using wp_enqueue_script<\/h3><p>WordPress has a built-in function called <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_script\/\" target=\"_blank\" rel=\"noopener\">wp_enqueue_script()<\/a> that enables you to enqueue JavaScript or similar scripts. Add the following code to your theme&rsquo;s <strong>functions.php<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">function theme_scripts() {\n    wp_enqueue_script( 'my-script', get_template_directory_uri() . '\/js\/my-script.js', array(), '1.0', true );\n}\nadd_action( 'wp_enqueue_scripts', 'theme_scripts' );<\/pre><p>Note that this function uses more parameters:<\/p><ul class=\"wp-block-list\">\n<li><strong>my-script <\/strong><em>&ndash;<\/em><strong> <\/strong>the name of your script. You can choose any name you want.<\/li>\n\n\n\n<li><strong>\/js\/my-script.js<\/strong> &ndash; location of your script. In this case, it&rsquo;s in the <strong>js<\/strong> directory of the WordPress theme.<\/li>\n\n\n\n<li><strong>array()<\/strong> &ndash; defines dependencies your script may have.<\/li>\n\n\n\n<li><strong>1.0 <\/strong>&ndash; the<strong> <\/strong>stylesheet<strong> <\/strong>version number.<\/li>\n\n\n\n<li><strong>true<\/strong> &ndash; determines whether to load the script in the footer.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-useful-examples-of-wp-enqueue-style\">Useful Examples of wp_enqueue_style<\/h2><p>Check out a few practical use cases of the <strong>wp_enqueue_style()<\/strong> function to improve your <a href=\"\/in\/tutorials\/launch-a-wordpress-site\">WordPress site<\/a>.<\/p><h3 class=\"wp-block-heading\" id=\"h-loading-css-only-on-specific-pages\">Loading CSS Only on Specific Pages<\/h3><p>Loading CSS on specific pages can provide several benefits for a WordPress website:<\/p><ul class=\"wp-block-list\">\n<li><strong>Faster page load times <\/strong>&ndash; when you load CSS only on the pages where it is necessary, you avoid having unnecessary code. This will improve your overall site&rsquo;s speed.<\/li>\n\n\n\n<li><strong>Easier maintenance<\/strong> &ndash; you can change CSS files without affecting other pages.<\/li>\n<\/ul><p>In the example below, we will register and load CSS to the <strong>Contact Us<\/strong> page with the help of the <strong>wp_enqueue_scripts<\/strong> WordPress hook and the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/is_page\/\" target=\"_blank\" rel=\"noopener\">is_page()<\/a> function:<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nadd_action('init', 'register_custom_styles');\nfunction register_custom_styles() {\n    wp_register_style( 'custom-design', '\/wp-content\/design.css' );\n}\nadd_action( 'wp_enqueue_scripts', 'conditionally_enqueue_styles_scripts' );\nfunction conditionally_enqueue_styles_scripts() {\n    if ( is_page('contactus') ) {\n        wp_enqueue_style( 'custom-design' );\n    }\n}\n?&gt;<\/pre><h3 class=\"wp-block-heading\" id=\"h-loading-css-file-in-the-footer\">Loading CSS File in the Footer<\/h3><p>By moving CSS to the bottom of the page, the browser can prioritize loading the HTML and other important resources first. As a result, loading CSS in the footer improves your page load time.<\/p><p>The best way to load CSS in the footer is with the<strong> get_footer()<\/strong> <a href=\"\/in\/tutorials\/what-are-wordpress-hooks\/\">WordPress hook<\/a>:<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nfunction footer_style() {\n    wp_enqueue_style('custom-design', '\/wp-content\/design.css');\n};\nadd_action( 'get_footer', 'footer_style' );\n?&gt;<\/pre><p>Remember that loading CSS in the footer can cause rendering issues and make the page look broken or unstyled. For this reason, load the most important CSS in the head section first, then move to the footer part.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-dynamic-inline-styles\">Adding Dynamic Inline Styles<\/h3><p>Dynamic inline styles allow you to add custom styles to individual elements on a web page. The easiest way to add inline styles is with the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_add_inline_style\/\" target=\"_blank\" rel=\"noopener\">wp_add_inline_style()<\/a> function, which loads them after a specific CSS file.<\/p><p>In the following example, we will combine both the <strong>wp_enqueue_style() <\/strong>and <strong>wp_add_inline_style() <\/strong>functions to apply style from the<strong> design.css<\/strong> file and bold headlines:<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nfunction theme_style() {\n\twp_enqueue_style( 'custom-style', get_template_directory_uri() . '\/wp-content\/design.css' );\n\t$bold_headlines = get_theme_mod( 'headline-font-weight' ); \n\t$inline_style = '.headline { font-weight: ' . $bold_headlines . '; }';\n\twp_add_inline_style( 'custom-style', $inline_style );\n}\nadd_action( 'wp_enqueue_scripts', 'theme_style' );\n?&gt;<\/pre><p>Keep in mind that inline style will only work after the <strong>design.css <\/strong>is properly enqueued.<\/p><h3 class=\"wp-block-heading\" id=\"h-checking-the-stylesheet-s-enqueue-status\">Checking the Stylesheet&rsquo;s Enqueue Status<\/h3><p>Use the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_style_is\/\" target=\"_blank\" rel=\"noopener\">wp_style_is()<\/a> function if you want more information about the stylesheet state. This function can check whether a CSS stylesheet file is in the queue, enqueued, registered, or ready to be displayed.<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nfunction check_styles() {\n\tif( wp_style_is( 'main' ) {\n    \n\t\twp_enqueue_style( 'my-theme', '\/custom-theme.css' );\n\t}\n}\nadd_action( 'wp_enqueue_scripts', 'check_styles' );\n?&gt;<\/pre><h3 class=\"wp-block-heading\" id=\"h-inserting-metadata-into-the-stylesheet\">Inserting Metadata into the Stylesheet<\/h3><p>You can also use the <strong>wp_enqueue_style <\/strong>function with the following code snippet to enqueue a CSS stylesheet with title metadata:<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nfunction theme_extra_styles() {\n\twp_enqueue_style( 'add-metadata', '\/wp-content\/design.css' );\n\twp_style_add_data( 'add-metadata', 'title', 'My Awesome Title' );\n\t}\nadd_action( 'wp_enqueue_scripts', 'theme_extra_styles' );\n?&gt;<\/pre><p>In this example, we&rsquo;ve used the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_style_add_data\/\" target=\"_blank\" rel=\"noopener\">wp_style_add_data()<\/a> function and added a title to the CSS stylesheet.<\/p><h3 class=\"wp-block-heading\" id=\"h-deregistering-style-files\">Deregistering Style Files<\/h3><p>It&rsquo;s important to deregister CSS-style files you no longer use. When multiple plugins or themes enqueue the same style file, it can lead to conflicts and issues on the website.<\/p><p>The following code will deregister and dequeue a specific style and replace it with a new stylesheet:<\/p><pre class=\"wp-block-preformatted\">add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', 20 );\nfunction remove_default_stylesheet() {\n    wp_dequeue_style( 'original-enqueue-stylesheet-handle' );\n    wp_deregister_style( 'original-register-stylesheet-handle' );\n\n    wp_register_style( 'new-style', get_stylesheet_directory_uri() . '\/new.css', false, '1.0.0' ); \n    wp_enqueue_style( 'new-style' );\n}<\/pre><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1024x300.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-768x225.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1536x450.png 1536w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The <strong>wp_enqueue_style()<\/strong> function is an essential part of WordPress development. It provides an easy and efficient way to load stylesheets to your website.<\/p><p>In this tutorial, we&rsquo;ve covered the <strong>wp_enqueue_style() <\/strong>function and provided some examples of how you can use it to customize your website&rsquo;s appearance and improve its speed.<\/p><p>We hope you found this tutorial useful and will be able to use the <strong>wp_enqueue_style() <\/strong>function with your WordPress projects successfully. If you have any questions or insights, refer to our <a href=\"\/in\/tutorials\/wordpress\">WordPress guide<\/a> or leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing WordPress themes or plugins, it&rsquo;s essential to enqueue stylesheets to make them load correctly. To do so, we recommend using the wp_enqueue_style() function. It&rsquo;s a powerful tool for adding custom stylesheets to your WordPress theme or plugin. This function also ensures that stylesheets are loaded only when necessary and helps avoid conflicts with [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/wp_enqueue_style\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-82983","post","type-post","status-publish","format-standard","hentry"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-wp-enqueue-style-wordpress-function","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp_enqueue_style","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/wp_enqueue_style","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/wp_enqueue_style","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-menggunakan-wp_enqueue_style","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp_enqueue_style","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-enqueue-css-stylesheet-to-wordpress-using-wp_enqueue_style","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-enqueue-css-stylesheet-to-wordpress-using-wp_enqueue_style","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/wp_enqueue_style","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/wp_enqueue_style","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/wp_enqueue_style","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp_enqueue_style","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wp_enqueue_style","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-use-wp-enqueue-style-wordpress-function","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-use-wp-enqueue-style-wordpress-function","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-use-wp-enqueue-style-wordpress-function","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/82983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=82983"}],"version-history":[{"count":12,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/82983\/revisions"}],"predecessor-version":[{"id":96311,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/82983\/revisions\/96311"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=82983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=82983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=82983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}