Log in

View Full Version : CSS should be put WHERE?



dondred
10-11-2011, 10:56 PM
okaye, im use to working with html codes by themselves.

but where do you put the actual CSS code in the Source page?

And how exactly does the HTML code correspond with the CSS code if i paste both of them in my document?

People have different methods of using CSS but which is the easiest way?:cool:

jscheuer1
10-12-2011, 12:48 AM
CSS code corresponds to HTML code via selectors, ex:


p {font-weight: bold}

In this case p is the CSS selector. All p elements in the HTML will have bold text.



In the long run the easiest way is to put the CSS code in an external stylesheet. That way it can be used by all of your pages and be changed in one place to affect all of your pages.

djr33
10-12-2011, 12:52 AM
There are several way to include styles in HTML.

CSS actually stands for Cascading Style Sheet, so that is actually a very specific kind: the sort stored in a .css file that is NOT embedded in the page.

Regardless, the term CSS or "CSS Styles" is used more generally just referring to the code that changes how things look in HTML.


There are three ways:

1. Inline styles are part of an HTML tag.
<img style="width="100px;">

2. <style> tags in the <head> section can hold general styles for a page.
These styles can be for specific tags (for example, for <img> tags), or for classes or IDs (for specific elements, not types of tags):
img { width:100px; }
img.myclass { width:200px; }

3. An external stylesheet (.css file) can be used, with exactly the same code as in <style> tags.
<link type="text/css" href="mystyle.css">



Inline styles are very lazy and become very messy because you must write them out the long way for EVERY instance that you want to use them. It's much better to use either of the other two methods to apply styles more generally. And if needed you can use a class or an ID to apply a style to a specific element, or to several specific elements.


Including <style> tags and placing your code inside the page is better when it is specific to that page.

But generally it is much better to share a stylesheet between similar-looking pages because that will mean only downloading the styles one time instead of as part of every page and repeating the same information.


As a designer, though, the best part of generalizing and sharing styles between pages (or even just between elements on a single page) is that you save yourself work when you want to update something. Instead of changing EVERY paragraph's color, you can just change the color for all paragraphs on your page, or even all paragraphs on your website. It's great.


You ask about the "easiest" way, but it really depends on what you're trying to do. The different methods have different purposes. As I explained above, though, I'd recommend in general that you avoid inline markup as much as possible and try to share it throughout or between pages.
And even if it seems like the right way (in this case, using external stylesheets) is harder, then I still recommend it-- in the end it will usually be easier. This is what John said too.


The second part of your question:

Inline styles are applied directly to the element they modify. See (1) above.

Other styles (either in the head section or in an external file) are related to certain elements in the page by, as I said, class or ID, or by type of element.

If you want ALL images on your page to be 100px wide, then use this:
img { width:100px; }
Note that this can be overridden in more specific cases as needed-- this just becomes the default.

Next, for a type of element that you want to group, use a class:
HTML: <img class="myclass">
CSS: img.myclass { width:100px; }

And for one (and only one) specific item, you can give it an ID:
HTML: <img id="myimage">
CSS: img#myimage { width:100px; }


Those are the three general methods. Very often, you won't need to use ID at all, and in fact you can always use a class instead if you want. (The only cases where you do often use ID involve Javascript, because ID also works in JS, so you end up with cases where you already have an ID, so using it also in the CSS is convenient, but not technically required.)

There are a couple other ways to refer to elements in the CSS, but I won't go into those because they are for very specific cases.

However, do realize that you can combine these items:
div.layout #footer p { color:green; }
That code will locate any <p> tags in the element with ID 'footer' inside a <div> tag with a class of 'layout'.



But honestly, you need to start with some basic CSS tutorials if you want more information. Even what I've posted is VERY easily available if you look around a bit.