A CSS file is a plain text document that holds styling rules for your website
A CSS file is a text file with a .css extension that contains all the styling instructions for how your web pages look — colors, fonts, spacing, layout, and more. You create one by opening a text editor, writing CSS code, and saving the file with a .css name. Your HTML file then links to this CSS file, and the browser reads both together to display the styled page.
The process takes less than a minute once you have a text editor open. You do not need special software — any plain text editor works, whether that is Notepad on Windows, TextEdit on Mac, or a code editor like Visual Studio Code.
Key Takeaways
- Open any plain text editor and write your CSS code, then save the file with a .css extension in the same folder as your HTML file.
- Link your CSS file to your HTML document using a <link> tag in the <head> section so the browser knows to use those styles.
- CSS file names should use lowercase letters and hyphens instead of spaces — for example, styles.css or main-styles.css.
- You can test your CSS by opening the HTML file in a browser and checking that colors, fonts, and spacing appear as you intended.
Creating a CSS file in a plain text editor
Start by opening a text editor on your computer. On Windows, you can use Notepad (search for "Notepad" in the Start menu). On Mac, open TextEdit from Applications > Utilities, then go to Format menu and select "Make Plain Text" before you start typing. If you have a code editor like Visual Studio Code installed, open that instead — it highlights CSS syntax and makes errors easier to spot.
Type your CSS rules into the editor. A basic example looks like this:
body { background-color: lightblue; font-family: Arial, sans-serif; } h1 { color: darkblue; text-align: center; }
Once you have written your CSS, save the file. Go to File > Save As. Choose a folder on your computer — ideally the same folder where your HTML file lives. Name the file something clear like styles.css or main.css. Use lowercase letters and hyphens instead of spaces. Make sure the file extension is .css, not .txt.
Linking your CSS file to your HTML document
Your HTML file needs to know where to find the CSS file. Open your HTML document in a text editor and locate the <head> section — this is the part at the top of the file between <head> and </head> tags. Add this line inside the head:
<link rel="stylesheet" href="styles.css">
Replace styles.css with whatever you named your CSS file. If your CSS file is in a subfolder, include the folder name: href="css/styles.css". The rel="stylesheet" part tells the browser this is a style sheet. The href tells it where to find the file.
A complete head section might look like this:
<head> <title>My Website</title> <link rel="stylesheet" href="styles.css"> </head>
Organizing your CSS file structure
As your CSS grows, organizing it makes it easier to find and change rules later. Group related styles together with comments. A comment in CSS starts with /* and ends with */:
/* Header styles */ header { background-color: navy; padding: 20px; } /* Navigation styles */ nav a { color: white; text-decoration: none; }
Put general styles like body and html at the top, then group styles by section or component. This makes it faster to locate a rule when you need to change it later. Many developers also create separate CSS files for different parts of the site — one for navigation, one for forms, one for the footer — and link all of them in the HTML head.
Testing your CSS file in a browser
Save both your HTML and CSS files. Open the HTML file in a web browser by double-clicking it or right-clicking and selecting "Open with" and choosing your browser. The page should display with your CSS styles applied — colors, fonts, spacing, and layout should all match what you wrote in the CSS file.
If the styles do not appear, check three things: First, make sure the CSS file name in the href matches the actual file name exactly, including capitalization. Second, confirm both files are in the same folder (or adjust the path if they are not). Third, open the browser's developer tools by pressing F12 or right-clicking and selecting "Inspect," then look at the Console tab for error messages that might tell you what went wrong.
Common mistakes when creating CSS files
The most frequent error is a mismatch between the file name and the link. If you save the file as Styles.css but link to styles.css, the browser will not find it on systems that treat capitalization as different (like Linux servers). Always use lowercase and check that the name in the href matches exactly.
Another common issue is saving the file as plain text but with the wrong extension. If you use Notepad on Windows, it may automatically add .txt even if you type .css. To prevent this, type the full name in quotes: "styles.css" instead of just styles.css. In the Save As dialog, also change "Save as type" from "Text Documents" to "All Files."
Forgetting to save the CSS file after making changes is also easy to do. If you edit the CSS and the browser does not show the changes, save the file, then refresh the browser page (press Ctrl+R on Windows or Cmd+R on Mac, or click the refresh button).
Where to store your CSS file
For a small project, you can keep the CSS file in the same folder as your HTML file. For larger projects, create a subfolder called css and put all your stylesheets there. If you do this, update the link in your HTML to point to the subfolder:
<link rel="stylesheet" href="css/styles.css">
Some developers also create folders for images, JavaScript files, and other assets. A typical folder structure looks like this: a main folder for the project, with subfolders for css, images, js, and an index.html file at the root level. This keeps everything organized and makes it easier to move the project to a web server later.
Frequently Asked Questions
Can I write CSS directly in my HTML file instead of a separate file?
Yes. You can put CSS inside a <style> tag in the HTML head, or use inline styles on individual elements. However, a separate CSS file is better practice because you can reuse the same styles across multiple HTML pages, and it keeps your code organized and easier to maintain.
What text editor should I use to create a CSS file?
Any plain text editor works. Notepad and TextEdit are free and built into Windows and Mac. Code editors like Visual Studio Code, Sublime Text, or Atom are free or low-cost and offer features like syntax highlighting and error checking that make writing CSS faster and less error-prone.
Do I need to upload my CSS file to the internet for it to work?
No. You can test CSS files on your own computer by opening the HTML file in a browser. When you are ready to publish your website, you upload both the HTML and CSS files to a web server, and they work the same way there.
What happens if the browser cannot find my CSS file?
The HTML page will display without any styling — just plain text and default browser formatting. Check the file name and path in the href attribute, make sure both files are saved, and use the browser's developer tools to see if there are error messages about the missing file.
Can I link to multiple CSS files in one HTML document?
Yes. Add multiple <link> tags in the head section, one for each CSS file. This is useful when you want to organize styles by purpose — for example, one file for layout, one for typography, and one for colors. The browser loads them all and combines the rules.