How to Fix CSS not working with VS Code (2024)

Introduction

One annoying that comes up when working on front-end or web designs, the CSS does not seem to work when working with VS Code.

In this post, I will go over various problems that you can encounter and the possible fixes.

Steps to troubleshoot CSS loading issues in VS Code:

  1. Check that we have linked the right CSS file and using the correct path
  2. Check the file extension
  3. Review the link syntax and the CSS file and make sure it is valid
  4. Use browser (eg Chrome) DevTools to clear cache and check for errors

1. Check that we have linked the right CSS file and using the correct path

The most common problem with CSS not working with any editor (such as VS Code) is that we are using relative paths and got the path wrong!

Explanation of use of the relative path syntax:

  • / - This means the root directory
  • ../ - This means going up on directory
  • ./ - This means the current directory (if you are working off the current directory, you may not need this - just use the file name)
  • ../../ - This means going up two directories

Lets say we got the following basic folder structure

How to Fix CSS not working with VS Code (1)

We can reference each of the files as below:

<html> <link href="mystyles_A.css" rel="stylesheet" type="text/css"> <!-- Current directory --> <link href="./mystyles_A.css" rel="stylesheet" type="text/css"> <!-- Current directory --> <link href="/mystyles_B.css" rel="stylesheet" type="text/css"> <!-- Root directory --> <link href="../../mystyles_C.css" rel="stylesheet" type="text/css"> <!-- Go up two levels --> <link href="../mystyles_E.css" rel="stylesheet" type="text/css"> <!-- Go up one level --> <link href="./custom/mystyles_F.css" rel="stylesheet" type="text/css"> <!-- Start at current directory and go down to the custom folder --> <body> ... </body></html>

👉 Tip: Use Live Server in VS Code!

If you are coding with VS Code, then using the Live Server extension is a must. When you are just coding locally without a local server running, references to images can get a bit confusing.

For example, if you use the root directory / - this could mean your actual C:/ root folder instead of the current folder!

Live server can be downloaded here: https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer

2. Check the file extension

One issue that can trip some people up is that the file names can be case insensitive. This will be based on the serve that is hosting your website.

With most Unix based servers (Linux, or OSX), the files are case sensitive. While on Windows servers, they are not.

As an example, if you are hosting with Apache on Ubuntu it does not care about your filename/ extension

In the example below, lets say we have stylesheet file called style.CSS, the second line will not load the CSS due to the all caps CSS extension

<html> <link href="style.CSS" rel="stylesheet" type="text/css"> <!-- Will not load (based on server)--> <body> ... </body></html>

3. Review the link syntax and the CSS file and make sure it is valid

Commonly, we can link to a external stylesheet with the following <link> syntax:

<html> <head> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> ... </body></html>

A few things to keep note to make sure that we get the syntax correctly:

  • keep the <link> tag within the <head> element. We can put it in the body, but best practice is to place it in the head section
  • Check that we include the rel="stylesheet" - verify the spelling!
  • make sure to also have the correct type (type="text/css"). This helps defines the type of the content. Having incorrect type could lead to the CSS not loading!
  • Check that you are not specifiying incorrect media
<link href="desktop.css" rel="stylesheet" media="screen and (min-width: 600px)" />

It is best to ignore the media attribute and use media queries in your CSS.

👉 Tip: Use CSS validation services to check your CSS

Sometimes your CSS will not load might be due to malformed code (eg missing brackets, etc).Use tools like https://jigsaw.w3.org/css-validator/ to check!

A common reason why you cant see your CSS changes is that the browser is caching the CSS.

What I usually do is to open up dev tools (Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux)) and tick the “Disable Cache (while DevTools is open)”

How to Fix CSS not working with VS Code (2)

This way we can ensure that our code is using the most up to date CSS.

Additionally, you can check if your CSS file is loading correctly by inspecting the console in DevTools. Usually if it is not showing, we can see a 404 error:

How to Fix CSS not working with VS Code (3)

Summary

In this post, I went over some problems when loading external CSS files using VS Code and ways to fix them. The most common issue is using relative paths and getting thelocation wrong.

I would suggest to use absolute paths and use the VS Code Live Server extension to get around these issues.

We also need to check that our syntax and CSS is valid - correct spelling and not missing any brackets.

Finally we can check that the browser is not loading a cached version of our CSS. I would suggest to turn DevTools and check on the option of “Disable Cache (while DevTools is open)”!

How to Fix CSS not working with VS Code (2024)

FAQs

Why CSS is not working in vscode? ›

I think you need to create a css folder first and then add your styles. css file to that folder. Maybe this will help. You haven't write any code in your file.

How do I enable CSS in VS code? ›

To add CSS to your HTML code, you can use the following steps:
  1. Create a new CSS file.
  2. Add some CSS code to the file.
  3. Save the file.
  4. Link the CSS file to your HTML file.
  5. Open the HTML file in a web browser.
  6. You should see your HTML code displayed in the web browser, styled with CSS.
May 12, 2022

Why is my CSS code not working? ›

Check for Typos:

Ensure that there are no typos or syntax errors in your CSS code. A small mistake can prevent the entire stylesheet from working.

What causes CSS to not load? ›

From time to time the css is not loading when the cache regenerates itself. switching off/cleaning cache makes it work again. Free version. You never know when it happens so your page is “unreachable” unnoticed.

How do I fix custom CSS not working? ›

The first solution to try is to regenerate your CSS. You can easily do this by going to WP admin > Elementor > Tools > Regenerate CSS. After that, you should clear both the WP and browser cache, then refresh the page. This will help the browser load the updated CSS.

How to format CSS in Visual Studio Code? ›

To format the contents of the current file, we can either use the shortcut Ctrl+Shift+I ( Command+Shift+I on macOS) or bring up the command palette with Ctrl+Shift+P ( Command+Shift+P on macOS) and then search for and execute “Format Document”.

How to activate CSS in HTML? ›

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section.

How do I enable CSS on my computer? ›

Open the CSS Overview tool

Open DevTools by pressing F12 or Ctrl+Shift+I (Windows, Linux) or Command+Option+I (macOS). In the main toolbar, click More Tools and select CSS Overview from the list.

How do I force CSS to load? ›

Anytime you make changes to CSS, JavaScript and are viewing the page you've updated - you will see this concern. You can force a refresh by pressing CTRL+F5 on the browser and that will get the latest version.

How do I force my browser to load new CSS? ›

How to force the browser to refresh the CSS/JS files
  1. "Clear all caches".
  2. Then refresh the page source in Firefox or Chrome (Ctrl+F5).
Feb 23, 2024

How to add CSS to HTML in VSCode? ›

To link the CSS to an HTML file, we use the <link> tag inside the HTML <head> section. Your CSS file will look like the image displayed below: Let's look at another example where you add an image using CSS. Note: Make sure that the image file is in the same folder as the CSS and HTML files.

How do I add custom CSS to VSCode? ›

  1. open the command list by ( cmd + shift + p ) and run the "Enable Custom CSS and JS"
  2. open that css file in vscode.
  3. copy the css below and paste it in there and save it.
  4. hit ( cmd + shif t + p ) for command lists.
  5. type "update css" and run the "Update Custom CSS and JS" .... ...
  6. download and install the "Atom one Dark" theme.

How do I disable custom CSS and JS in VSCode? ›

CTRL + SHIFT + P. Type “Disable Custom CSS and JS” Restart VSCode. Type “Enable Custom CSS and JS”

How to create website using HTML and CSS in Visual Studio Code? ›

To build a webpage with Visual Studio Code, you will need to create an HTML file, a CSS file, and a JavaScript file. The HTML file will contain the content of your webpage, the CSS file will be used to style the content, and the JavaScript file will be used to add interactivity to your webpage.

Top Articles
Supply Chain Innovation: How Does Amazon Do It?
30 Incredibly Best Places to Visit in Kerala - Travel Blog: Travel tips, tricks & more by Cleartrip
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Acts 16 Nkjv
Apply A Mudpack Crossword
Tiger Island Hunting Club
Shariraye Update
Turning the System On or Off
Animal Eye Clinic Huntersville Nc
Michaels W2 Online
National Office Liquidators Llc
No Hard Feelings Showtimes Near Cinemark At Harlingen
Aldi Süd Prospekt ᐅ Aktuelle Angebote online blättern
Spectrum Field Tech Salary
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Icommerce Agent
Golden Abyss - Chapter 5 - Lunar_Angel
Zoe Mintz Adam Duritz
Vigoro Mulch Safe For Dogs
Nhl Tankathon Mock Draft
Busted News Bowie County
A Cup of Cozy – Podcast
Encyclopaedia Metallum - WikiMili, The Best Wikipedia Reader
Jermiyah Pryear
Danielle Ranslow Obituary
Netwerk van %naam%, analyse van %nb_relaties% relaties
Craigslist Panama City Beach Fl Pets
Walmart Pharmacy Near Me Open
Delectable Birthday Dyes
Margaret Shelton Jeopardy Age
Xxn Abbreviation List 2017 Pdf
Geico Car Insurance Review 2024
Ipcam Telegram Group
Smayperu
The Hoplite Revolution and the Rise of the Polis
Soulstone Survivors Igg
Planet Fitness Santa Clarita Photos
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
Deshuesadero El Pulpo
Telugu Moviez Wap Org
Daly City Building Division
Blackstone Launchpad Ucf
Google Flights Orlando
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Poe Self Chill
Cch Staffnet
Doe mee met ons loyaliteitsprogramma | Victoria Club
The Pretty Kitty Tanglewood
Mail2World Sign Up
Washington Craigslist Housing
March 2023 Wincalendar
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6101

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.