7

I am doing a project which is building a website for my CS 205 class. I have made it using notepad++ for the html files and notepad for the css files. My site has a home/index.html page as well as 5 other content pages. What I did was created each each page in notepad++, with each page having its own css file.

What I'm having trouble with is it must have 1 css file that maintains a consistent look across your site / or link all pages to the same css external file. I'm not totally sure if those two mean the same thing that's why I list both of them.

I already have a style sheet in each html page that links to its css file. But I must have one css file for the entire site. Could I just copy and past each css file into one without it changing how each page looks? I would really appreciate it if someone could explain how I do this without it messing up the setup I have for each page.

Greg
  • 71
  • 1
  • 1
  • 3
  • Can you merge the content of all css files into one or you think there are similar css selectors with different styles across multiple css files? – prasun Apr 22 '15 at 05:08
  • How about using @import to link all your css files in the main file? http://stackoverflow.com/questions/10036977/best-way-to-include-css-why-use-import – Sandeep Nayak Apr 22 '15 at 05:11
  • you should check css file path or your css extension, it should be save with .css – Mukul Kant Apr 22 '15 at 05:18

8 Answers8

7

Having all of your styles be consistent across the website is ideal. To achieve this, you have a single stylesheet and link all your pages to it using the <link> tag in the <head>.

It's a good practice to reuse as much CSS as you can, it'll save you time in the future and that's kinda the goal of a stylesheet versus inline styles.

To answer your question, yes you can combine all of our stylesheets together into a single stylesheet provided you do not have any duplicate class names. Notice in my example how I have a .class-for-index that is used in index.html but not in page.html and similarly for .class-for-page.

styles.css (your single stylesheet with all your classes)

body {
    background-color: cyan;
}

.class-for-index {
    color: red;
}

.class-for-page {
    color: blue;
}

index.html (link to the single stylesheet)

<html>
    <head>
        <link href="styles.css" rel="stylesheet">
    </head>

    <body class="class-for-index">
        Page 1
    </body>
</html>

page.html (link to the single stylesheet)

<html>
    <head>
        <link href="styles.css" rel="stylesheet">
    </head>

    <body class="class-for-page">
        Page 2
    </body>
</html>
allejo
  • 2,076
  • 4
  • 25
  • 40
3

I am assuming you aren't using PHP at all. Maintaining consistent look across all your webpages is quite easy if done correctly.

basically you have two options:

1. Put all CSS blocks into a single file and link it to all pages

For example: add this to all HTML pages, this single style.css file has rules for all the HTML pages as well as the overall layout.

<head>
  <link href="style.css" rel="stylesheet">
</head>

This style.css file can get messy and large if you have a lot of HTML pages.

2. Put CSS blocks that are related to overall design in one file; add individual page-specific CSS rules into new files and link these to their respective pages

For example: add this to a login page, the main.css file will give the overall layout like body background-color, font-family, font-size etc. etc. while the login.css is specifically tailored to the login.html page.

<head>
  <link href="css/main.css" rel="stylesheet">
  <link href="css/login.css" rel="stylesheet">
</head>

I prefer the 2nd approach because it's more easy to maintain and I have more control over my CSS without breaking other styles.

However if you decide to follow the 1st technique, it is advisable to separate strictly page specific CSS (styles that are being only used by as single page) by comment lines. This makes the file more readable.

Ashesh
  • 3,499
  • 1
  • 27
  • 44
2

You've learnt an important lesson of the DRY principle - Don't Repeat Yourself. Maintaining many CSS files creates overhead - how many places do you want to define/change the styling for H1 for example? This is why you've been asked to have a single file.

I'd recommend taking the largest of your css files and making it the master. For each of the other files, add those elements that are missing from the master. It's tedious, but that's the problem you created ;)

You could just copy and paste each file into a single master file and it would work (this is css and the last definition will win), but it's poor practice and you'll just have problems editing it when you have to find the actual definition you are using.

Others have already explained how to link to a single css file from many pages.

Romski
  • 1,912
  • 1
  • 12
  • 27
0

I think a single css file to be created and linked to all pages. You can create multiple css files too but one css file would be easy to maintain and once your index.html loads the css file would get cached in the browser.

SM Ahmed
  • 128
  • 7
0

Each file within your solution just needs to link to that one unified external stylesheet via a link tag in the head of the document:

<link rel="stylesheet" type="text/css" href="/path-to-mystyle.css">

Google "create external stylesheet" for many resources on this!

Brian John
  • 609
  • 3
  • 12
  • I already have a style sheet in each html page that links to its css file. But I must have one css file for the entire site. Could I just copy and past each css file into one. – Greg Apr 22 '15 at 05:15
  • Yes, all CSS could be merged into one common css file, but you will have to be careful of reused or common classes and IDs. – Brian John Apr 22 '15 at 05:25
0

You can create a separate CSS file and put all of your "common" CSS into that, call it main.css for example. This is CSS for tags such as p, h1, h2, ul, li etc to set fonts and margins etc across the whole site since these should not really change between different pages.

You can include that file on all of your pages.

Then beneath that file you can include a page specific CSS file with CSS for that page only. That will have CSS which is for the layout of that specific page like background-images etc.

Josh
  • 847
  • 9
  • 17
0

This is creating external css file:

In Index.html:

 <head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>

Other pages,

Page-1.html:

Put the same css file,

 <link rel="stylesheet" type="text/css" href="mystyle.css">

Same as put css file for page-2.html and likewise..

Note: Latest of html, no need to put type="text/css" for create external css file.

pcs
  • 1,864
  • 4
  • 25
  • 49
0

It isn't fantastic practice to have 1 CSS file for all pages in a site, especially if you are styling selectors like h1, a, p etc... very differently per page.

But allejo has a great, simple approach to it.

Since the project calls for 1, just make sure you don't override the styles of elements on pages you want styled differently. If it means adding some additional divs to encompass tags on multiple pages to not lose points then go for that.

IE:

.about_page h1{
     ...}

.home_page p{
     ...}

etc...

Jason
  • 211
  • 1
  • 10