|
|
|
||
|
|||
|
|
Comp25: External Style SheetsWhat's an External Style Sheet?W3 Schools CSS Tutorial and Reference HTML Dog's Guide Beginning CSS: More reading Cascading Style Sheets: An interactive tutorial for beginners - More Reading An external style sheet lets you create a single style sheet for use throughout the entire website. Advantages to External Style SheetsUsing external style sheets has a number of advantages:
What Can Be Changed?One of the greatest things about using style sheets is that almost every element can be changed. Here's a few just to get you thinking creatively:
Creating a External Style SheetJust like an HTML page, external style sheets are plain text. The syntax is exactly like an internal style sheet without any HTML in the way. The entire text file might look like this:
And that's it. External style sheets do not have a <style> element, as with inline HTML styles. Style sheets are always named with the "css" extension. A common filename is "styles.css". CSS stands for Cascading Style Sheets Linking to a External Style SheetIn order to take advantage of your new style sheet, you must link every page in your website to it. This may sound tedious at first, but remember by doing this once you are saving yourself a great deal of time in the future. You will need to enter the following code into the <head> element of your pages (be sure the .css filename matches your style sheet filename.
Just like images, the CSS file must be in the same folder as the website pages. You must also be sure to reference it by its exact filename and extension. Font and Text StylesOne of the easiest things to change using style sheets is the way that text appears. Here are a few properties that you'll want to familiarize yourself with. Try them all and really get the hang of styles. colorThis changes the color of text. It also changes the color of a <hr> element. You generally use it in the following ways:
text-decorationThis deals with how the text is "decorated" with lines:
Decorations can also be combined like this:
letter-spacingIf you want to space out the letters in your text, use this property. It's great for headings!
font-familyChanging the font couldn't be easier. You just need to know the font's name.
WEB SAFE FONTS!! There is one major caveat regarding fonts: you must use fonts that are common on everyone's computer. If you use a font that someone does not have it will show as Times New Roman (or some other generic font). View a list of "web safe" fonts. You can specify an "order of preference" in your font-family that basically says if the first font doesn't exist, try the second, or the third, and so on. Here's how that is done:
Note that font names which span multiple words must be enclosed within apostrophes. If you absolutely have to use a particular font, consider making a graphic in an image editor with the words you want and embed the graphic as an <img>. This too has its side effects, though, such as poor accessibility and slower page load time. font-sizeYou will no doubt want to change the size of letters on the screen. This is especially true for headings.
By specifying font size as a percentage, you allow the user to adjust their display through their browser preferences (on Internet Explorer, under View and Text Size). This is important to having an accessible website for those who may have difficulty reading small text. font-styleBasically this lets you specify italic text.
font-weightThis lets you specify how bold text appears.
For more help on fonts check out this fantastic website. Paragraph and Margin Stylestext-alignIf you want to change the alignment of text within its container, use this property. When combined with other properties like background-color or border, it can create a great effect on headings.
marginSpecifying the margin of an element creates empty space around that element. This takes some trial and error.
The above example creates the same space on all four sides of the element you are modifying. margin-left (-right, -top, and -bottom)This allows you more control over the four sides of an element:
In this example we have specified that all paragraphs will be indented on the left by 1em, and all <h1> elements will have a 2em gap separating them from whatever might be above them.. paddingPadding is very similar to margins, but there is a very important distinction. When you use margin, you are providing space on the outside of the element. Padding, on the other hand, specifies space in the inside of the element. This is best demonstrated when you have an element with a background color and/or border.
padding-left (-right, -top, and -bottom)Much like the margin property, you can specify the padding on all four sides individually.
The <td> element is used when creating tables. list-style-typeBy default, ordered lists count in cardinal numbers (1, 2, 3...) and unordered lists display round bullets. You can change this behavior using the list-style-type property.
Choices for an ordered list include: lower-roman (i, ii, iii...), upper-roman (I, II, III...), lower-alpha (a, b, c...), upper-alpha (A, B, C...), and decimal (1, 2, 3...). Choices for an unordered list include: circle, square, and none. list-style-imageIf you really want to make things more exciting, consider adding images as bullets instead of the choices above.
Just like an <img> tag, make sure that the file you are referencing is in the same folder as your HTML file and that you are using exact same filename with extension. Want to find a great bullet image? Try Google Image Search and change the search query for something specific to what your site is about. line-heightThis property changes the spacing between lines of a paragraph. You might want to double-space your <p> elements, for example.
100% is the default. 200% would be double-spaced. A number smaller than 100% can make the text difficult to read but will create an interesting layout. This property has very little effect if the element does not wrap to multiple lines. Remember that this property controls the spacing within a paragraph. For spacing between neighboring paragraphs use the margin property (or margin-top and/or margin-bottom). Border Stylesborder-styleEvery element can have a border applied to it, and you can specify the style of that border through this property.
Values for border-style include: dotted, dashed, solid, double, groove, ridge, inset, and outset. Borders are black unless otherwise specified. border-colorAs we have already covered, you can change the color of the border of any element.
For some interesting effects, apply this property to the <body> tag, <hx> tags, or <table> and <td> tags. Remember that for border-color to work, you must also specify a border-style. border-widthWant your border to be thicker? Use this property.
Background Stylesbackground-colorYou can change the background color of any element, including <hx> and <body>.
This also works with <table> and <td> and <blockquote>. In fact, each of the examples you see here have the <blockquote> background-color specified to a light gray. background-imageIf there is a picture you would rather use as a background instead of a color, you can load it with this parameter.
This isn't limited to the <body> element. It can create a great effect to give a <td> or <table> a particular background image, or a <h1>. By default, the image will "tile" horizontally and vertically throughout the entire element you attached it to. Generally you should specify both a background-image and a background-color. This will help the appearance of the page while the image is still loading, and give you a backup in case the image does not load or if the user has images turned off. background-attachmentNormally a background image stays in place with the page when the user scrolls up and down. You can change this behavior and make the background "float" behind the element.
You will typically apply this only to the <body> element. background-repeatNormally a background will "tile" both horizontally and vertically. You might want the image to only tile one way or the other, or not tile at all.
The above example will disable tiling for the background image. As an alternative you can specify repeat-x or repeat-y to only let the image tile horizontally or vertically, respectively. cursorOne of the more catchy properties you can change is the cursor property, which displays the specified cursor when the user's mouse is hovering over that particular element.
Applying this property to the body will affect the entire web page. It might be interesting to apply it to just the <img> or <a> elements. Some cursor choices are: crosshair, default, pointer, move, wait, progress, all-scroll, no-drop, not-allowed, and help. TablesStyles can control the appearance of tables, table headers, rows, and cells (td):
table th{
Anchor tags - Special Note about LinksWe haven't talked much about the <a> tag throughout this reading until now. You can use all of the described style properties in conjunction with the <a> tag, however this tag has something that makes it very special. Because the <a> tag can change appearances when it is hovered over or clicked on, each of those states (called psuedo classes) should be defined in your style sheet. Consider this example:
With this in your style sheet, all links will appear with white lettering. If the user has already visited the page that the anchor links to, then it will appear in grey. And, when the user hovers the pointer over the link it will change to bold, blue text with lines above and below the words. You can even change the background color or border of the <a> element to react when it is hovered-over. This order of priority is said to cascade from external to internal to inline. Thus, we call the entire concept Cascading Style Sheets (CSS). Sample Style Sheets
|
| Copyright © 2010-10 University of the Pacific, Stockton, California. All rights reserved. |