Adding Custom CSS to WordPress Themes

Pavel R. Dec 29, 2024 Custom Styling
How can I change the look of my website without messing with the main design?
What is the procedure for enqueueing custom CSS in WordPress themes to ensure styles are applied correctly without altering theme files directly?
Andy answered Dec 29, 2024

Best Practices for Adding Custom CSS to WordPress

Method 1: Using functions.php (Recommended Custom Solution)

Add this code to your child theme's functions.php file to properly enqueue your custom CSS file:

function add_custom_stylesheet() {
    wp_enqueue_style(
        'custom-styles',
        get_stylesheet_directory_uri() . '/css/custom.css',
        array(),
        '1.0.0'
    );
}
add_action('wp_enqueue_scripts', 'add_custom_stylesheet');

Create a new file at wp-content/themes/your-child-theme/css/custom.css and add your custom CSS there.

Method 2: Using wp_add_inline_style

If you prefer adding CSS directly in PHP, use this code in functions.php:

function add_inline_custom_styles() {
    $custom_css = "
        .your-class {
            color: #333;
            background: #fff;
        }
        /* Add more CSS rules here */
    ";
    wp_add_inline_style('theme-style-handle', $custom_css);
}
add_action('wp_enqueue_scripts', 'add_inline_custom_styles');

Replace 'theme-style-handle' with your theme's main stylesheet handle (usually found in theme documentation).

Method 3: Using the WordPress Customizer API

Add this code to functions.php to create a custom CSS section in the WordPress Customizer:

function custom_theme_customizer($wp_customize) {
    $wp_customize->add_section('custom_css_section', array(
        'title' => 'Custom CSS',
        'priority' => 200
    ));

    $wp_customize->add_setting('custom_css_setting', array(
        'default' => '',
        'sanitize_callback' => 'wp_strip_all_tags'
    ));

    $wp_customize->add_control('custom_css_control', array(
        'label' => 'Add Custom CSS',
        'section' => 'custom_css_section',
        'settings' => 'custom_css_setting',
        'type' => 'textarea'
    ));
}
add_action('customize_register', 'custom_theme_customizer');

function output_custom_css() {
    $custom_css = get_theme_mod('custom_css_setting');
    if (!empty($custom_css)) {
        echo '<style type="text/css">' . wp_strip_all_tags($custom_css) . '</style>';
    }
}
add_action('wp_head', 'output_custom_css');

Plugin Solutions

If you prefer using plugins, here are reliable options:

  1. Simple Custom CSS - Lightweight solution for adding custom CSS
  2. Custom CSS Pro - Advanced features including CSS editor with syntax highlighting
  3. Elementor - Page builder with built-in custom CSS options

Important Notes

  • Always use a child theme when making custom modifications
  • Ensure CSS specificity is appropriate to override existing styles
  • Use browser developer tools to inspect elements and test CSS changes
  • Keep CSS organized and commented for maintainability
  • Consider using CSS preprocessors for larger projects

Your custom CSS will persist through theme updates when using any of these methods, with the child theme approach being the most maintainable long-term solution.