Add a CSS File to HTML
HyperText Markup Language (HTML) defines what the different pieces of a web page are. The Cascading Style Sheets (CSS) coding language describes what those pieces should look like. A CSS file can be added to HTML as an external style sheet, CSS included in a separate file from the HTML, or an internal style sheet, CSS included within the HTML file. Learn how to add a CSS file to HTML in order to customize your website design.
Contents
Steps
Add an External Style Sheet to HTML
- Create the CSS file. Prepare and save your CSS file with the ".css" file type.
- Upload your CSS file to your website.
- Copy the URL of your CSS file. The URL might look like www.yoursite.com/stylesheet.css.
- It's good form to remove the main domain name from the link (URL). Therefore a URL of "http://mysite.com/css/default.css" would be shortened to "/css/default.css". You must include that leading slash ("/"). This is called a relative path
- Add a link to the file. Find the </head> tag in your HTML file, and create an empty line just above the tag. Add <LINK rel=stylesheet type="text/css" href="www.yoursite.com/stylesheet.css"> to that empty line, changing "www.your..." to the link to your CSS file.
- Save your HTML file, and upload it to your website.
- Verify that everything on your site looks how it is supposed to look. If it doesn't, you may need to go back and look for any corrections or adjustments you need to make.
Add an Internal Style Sheet to HTML
- Create the <style> tag. In your HTML file, find the </head> tag. Add a a few extra lines above it and type the following:
- <STYLE type="text/css">
- </STYLE>
- Add all your CSS between those two tags you just added.
- Save your HTML file (as HTML).
- Verify that your web page looks like it is supposed to look. Make changes as necessary if it doesn't.
Tips
- Always check your website's appearance in different web browsers and on different computer systems. Some browsers internet CSS a bit differently, even between the same browser's Mac and Windows editions. If your website looks different in different browsers, particularly in how certain objects like lists are spaced, the issue is the browser defaults. Find a master style sheet to add to the beginning of your CSS to override all browser defaults, so your settings aren't adding to anything that the browser already sets up.
- If you have the option, use an external style sheet. Doing so will let you adjust the site appearance by changing the code in the one named file, rather than having to change the CSS on every page in your site.
- If your website isn't responding to your CSS like expected, double-check all your code to make sure you didn't forget details. Particularly pay attention to the semicolons (";") and closing arrow brackets ("}"). It's not unusual to miss one in your CSS.
- If you save the HTML file to your computer, you can open that file in your web browsers to check the appearance on your computer before uploading it, but you'll probably need to have your CSS as an internal style sheet in the HTML file for the appearance to load.
- When a style sheet contradicts itself - for example, if it first says text will be blue and later says it'll be red - the later condition will always apply. If one command is in an external style sheet, and the other is in an internal style sheet, the internal one will apply.
Warnings
- Don't use "inline" CSS, CSS that's included in the HTML tag. (For example:, the "align='center'" is inline CSS.) It's outdated, bad syntax, and difficult to adjust when you need to update the website later.
Related Articles
- Center Web Page Content Using CSS
- Do Absolute Positioning With CSS
- Create a Simple Web Page With HTML
- Learn HTML
- Make a Box in CSS
- Include CSS Within HTML Web Pages