Professional Documents
Culture Documents
CSS is an acronym for Cascading Style Sheets. CSS is used to separate presentation and style from document markup content. It works by targeting your web page elements. CSS can target and style native elements like your body tag or all tags on the page. It can also be made to target specific individual page elements or a group of labeled elements, as long as you give those elements the appropriate class name or identifier. CSS is primarily used in HTML web documents, but can also work for any kind of XML document, allowing you the ability to create XML based data structures that are well organized and styled. As a webmaster and web applications creator I use external CSS files to style my web pages and help me to keep web site maintenance or changes easy. Separating content from style becomes more important the larger your websites become.
We are going to be demonstrating external CSS techniques since it is the ultimate way to work with CSS. Inline CSS methods work for quick stuff, but if you want the best and most organized way to manage a full site, use external styling. Set up your project folder like this for most of the CSS tutorials on this site. This structure is going to be very helpful for staying organized and adding/editing styles as we build on. Since the goal for using external CSS is to make design changes and site management easier, we will want to keep a nice tidy directory structure as we build on.
Tip: We will now add our images into our pages like this. Put your images into their own folder.
Notice how we reference the images folder and images now --> "images/my_pic.jpg"
1. Inline styling using the "style" property of the tag to apply CSS. 2. Place CSS in the <head> tag of the document you want it to affect. 3. Using a separate style sheet(Adam's preference when site making). Adam focuses more on method 3 throughout these lessons. Here are examples of each method: 1. Inline Styling
<p style="color:#F00; fontsize:18px;">Styling tags using inline styling with the <u>style</u> property.</p>
3. Using a separate style sheet(Adam's preference when site making) Click here to see the 3rd and most prefered method
CSS TUTORIALS
Getting Started With CSS What is CSS? Web Site Directory Structure 3 Ways to Style HTML Tags Us... Create and Link to CSS Style... Style Classes and IDs CSS Attributes and Values Re... Create Backgrounds for your ... Add Backgrounds to Tables an... CSS Layers and Positioning Customize HTML Link Appearan... Easy Site-wide Changes Using... Strategically Placing Conten... CSS Examples, Tips, Tricks... Pure CSS Drop Down Menu Exam... Opacity Settings For Transpa... CSS z-layer with Opacity Tut...
2.
Fire up your HTML editor and create a new blank file named "main.css"
3.
Save main.css into the new folder you just created named style
4.
Open index.html and add the following line of code inside of your head tag, directly under your title tag is a good spot.
Full Code Example for you to reference where to place the line above:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>My Web Page</title> <link href="style/main.css" rel="stylesheet" type="text/css" /> </head> <body> Hello World! </body> </html>
Each web page you have can use the same or different style sheets.
Run a quick test to check CSS style sheet linkage
1. Open your index.html file and place the following code within your <body> tag. Then save the file.
2. Open main.css and place the following code into it, then save the file.
3. Open index.html using your web browser. If you see Hello World! inside of a bright green 100x100px square, you have successfully linked to your new style sheet! If you do not see the bright green square, you do not have style sheet linkage.
If you happen to fail in the linkage test
1. Be sure that you have saved both files before testing. 2. Check that your directory structure matches this Click Here 3. Make sure that your index.html file looks exactly like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>My Web Page</title> <link href="style/main.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="style1">Hello World!</div> </body> </html>
all of them have the same style. In the example below we have given the <h2> tag the id attribute to make it behave slightly different from the other tags by applying "mystyle2" from the style sheet to it as well as the class style "mystyle1". index.html <html> <head> <title>My Web Page</title> <link href="style/main.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="mystyle1">Here is content in a div tag</div> <hr class="mystyle1" / > <p class="mystyle1">Here is content in a p tag</p> <h2 class="mystyle1" id="mystyle2">Here is content in a H2 tag</h2> <img src="images/my_pic.jpg" class="mystyle1" /> </body> </html> And this is what your style sheet would look like defining mystyle1 and mystyle2. main.css /* We define a class style using a [.] */ .mystyle1 { background-color:#333366; color:#00FFFF; padding: 8px; letter-spacing:.5em; border-width: thick; border-style: solid; border-color: #990000 #0000FF #996600 #009900; } /* We define an ID style using a [#] */ #mystyle2 { width:350px; height:150px;
margin-left:100px; }
You can create as many styles in your style sheet as you like. Use them or not.
You can add the class or id attributes to most HTML tags. Experiment with any tags you want. Below we have given some examples for reference. <div class="mystyle1">Text</div> <p class="mystyle1">Text</p> <table class="mystyle1">Text</table> <hr class="mystyle1" / > <h3 class="mystyle1">Text</h3> <img class="mystyle1" src="images/my_pic.jpg" />
Define the color of the background. background-color: #999999; Define an image to Define as a background on your web page. background-image: url(myBG.jpg); Define the position of your background when using an image. background-position: top center;
color value
URL of Image bottom center left right top repeat repeat-x repeat-y no-repeat thin thick solid double groove dotted dashed inDefine outDefine thin ridge hidden four-sides number color value
backgroundrepeat
Define whether or not your background will render repeat image tiling or not, and in what direction. background-repeat: no-repeat;
border
borderbottom
Define what the bottom border of the HTML element will look like. Each edge of the border can look different if you like. border-bottom:#090 1px solid;
borderbottom-color borderbottom-style
Define what the bottom border color will be for the element. border-bottom-color:#F00; Define what the bottom border style will be for the element. border-bottom-style:dashed;
color value
dashed inDefine outDefine ridge hidden borderbottom-width border-color Define the width of the bottom border. border-bottom-width:2px; Define the color of a border. border-color: #F00; border-left Define the left border of the element. Each edge of the border can look different if you like. border-left: #06C thin groove; border-leftcolor border-leftstyle Define the left border color. border-left-color: #06C; Define the left border style. border-left-style: double; solid double groove dotted dashed inDefine outDefine ridge hidden number color value number color value color value number
border-leftwidth border-right
Define the left border's width. border-left-width: thick; Define the right border of the element. Each edge of the border can look different if you like. border-right: #F90 thick outset;
border-rightcolor border-right-
Define a color for the right border. border-right-color: #06C; Define the right border style.
color value
solid
border-rightwidth border-style
Define the width of the right border. border-right-width: thick; Define a border style. border-style: ridge;
solid double groove dotted dashed inDefine outDefine ridge hidden four-sides number color value color value
border-top
border-topcolor border-topstyle
Define a color for the top border. border-top-color: #06C; Define a style for the border top. border-top-style: double;
solid double groove dotted dashed inDefine outDefine ridge hidden number
border-topwidth
border-width
number
clear
Clear space on the side of an element. Causes any floating elements in that space to display the next line down. clear: left;
color
display
Some HTML elements will take up a block(full display line) by default. But they can be set to inline where needed. And tags that do not block by default can render as a block if you define it for the element's display attribute. The <IMG> and <SPAN> tags are inline by default, while <DIV> and <P> elements display as blocks by default. Try putting two raw <P> tags on the same display line. Not possible unless you set the display value correctly. <p style="display: inline;">Hello</p> <p style="display: inline;"> World</p> Try the example above, then remove the styling and view it. Or change "inline" to "block" and view.
block inline
float
Float an element to the left or right. Other content will wrap around a floated element. float:right;
left right
font-family
Define the font family you want to use in an element. <div style="font-family:Arial, Helvetica, sansserif;">Hello</div>
font-name
font-size
font-style
font-variant
font-variant:small-caps; font-weight Define the thickness of text using values from 100 to 900. 100 is thinnest, and 900 is thickest. font-weight:900; height Define the height of an HTML element. height:340px; letter-spacing Define the value of the space between your letters. letter-spacing:18px; or letter-spacing:2.5em; line-height Defines the vertical space between lines of text in an HTML element. line-height:1.8em; or line-height:24px; or line-height:20%; list-style Define all of the characteristics of HTML list elements in one decleration. list-style:upper-roman inside; circle square disc upperalpha loweralpha upperroman lowerroman decimal inside outside url none url percentage number number number auto weightvalue
list-styleimage
Define an image file in place of the normal list styled bullets. Small images work best.
list-style-image:url(myBullet.gif); list-styleposition Define the placement of your bullets points or numbers within your list items. <ul> <li style="list-style-position:inside;">Hello</li> <li style="list-style-position:outside;">World</li> </ul> list-style-type Define the style for your list items. <ul> <li style="list-style-type: upper-roman;">Hello</li> <li style="list-style-type: upper-roman;">World</li> </ul> circle square disc upperalpha loweralpha upperroman lowerroman decimal percentage number auto inside outside
margin
Define the margin space around the outside of an HTML element. This will set all margins uniformaly to a value if used. Use the attributes below for fine tuning your margins and setting different values for each side. margin:30%;
marginbottom
Define the bottom margin of an HTML element. Margins add space directly around the outside of an HTML element. margin-bottom:40%;
margin-left
Define the left margin of an HTML element. Margins add space directly outside of an HTML element. margin-left:10px;
margin-right
Define the right margin of an HTML element. Margins add space directly outside of an HTML element. margin-right:50%;
margin-top
Define the top margin of an HTML element. Margins add space directly outside of an HTML element. margin-top:30px;
number percentage auto number percentage four-sides number percentage number percentage number percentage number percentage absolute relative percentage number right center left justify linethrough overline underline none number percentage
padding
Define the padding all around the content inside of an HTML element. padding:8px;
paddingbottom padding-left
Define the bottom padding of an HTML element. padding-bottom:8px; Define the left padding of an HTML element. padding-left:12px;
padding-right
padding-top
position
Define the position type of an element and how it relates to other elements around it. Use the Top, Right, Bottom, and Left attributes to define where the object will be placed exactly. position:fixed;
text-align
textdecoration
Define the text's decoration. Usually used in styling links on a web page. text-decoration:underline;
text-indent
texttransform vertical-align
Change text from uppercase to lowercase, and capitalize. text-transform:uppercase; Defines the vertical alignment of your element. vertical-align:top;
white-space
Prevent your text from wrapping with nowrap. Normally text will automatically break line to the next line down if the words reach a barrirer inside an element. Use this to prevent the auto-wrap. Usually results in a horizontal slider appearing unless you set the overflow attribute to the "hidden" value. white-space:nowrap;
width
word-spacing
Define the value of the space between words in your text. You can define a negative value in this attribute. It may not render visually in your HTML editor, but the browser software will render it, not to worry. word-spacing:48px;
number
z-index
Define the z-index of an HTML element. Used in layering elements, or stacking elements. The element with the lowest zindex would be on the bottom of the stack(layers). z-index:5;
wholenumber
Copy+Paste code for each example will be on the page when it opens.
Example 1 - Centered Image Strip that repeats vertically Example 2 - Image Strip that repeats horizontally Example 3 - Repeating Tiles The page you are on now has a horizontally repeating Strip (22px wide and 905px high)
Take slightly longer to load but can be more fun and creative! Example 1 - Centered custom large image background
<div class="mydiv"> <h5>The beauty of this is that you can type <br /><br /> HTML text into your table after you stick <br /><br /> your nifty custom background image in it.</h5> </div>
Now the CSS to put in main.css style sheet: (set width and height to match image)
.mydiv { background-image:url(divBG.jpg); width:380px; height:258px; border: #666666; border-style:solid; padding-left:12px; } Be sure to set your width and height to match the dimensions of your image. Else it will autorepeat unless you tell it not to using another CSS property. You can use this method to dress up most any HTML element(tag). Use it for tables, paragraphs, list items, headings and much more.
First right click and save each colored square above into your images folder.
HTML Code: <table width="100%" border="3" bgcolor="#FFFFFF"> <tr><td height="110"> <img src="images/layerbox1.jpg" alt="square1" width="100" height="100" class="layer1" /> <img src="images/layerbox2.jpg" alt="square2" width="100" height="100" class="layer2" /> <img src="images/layerbox3.jpg" alt="square3" width="100" height="100" class="layer3" /> <img src="images/layerbox4.jpg" alt="square4" width="100" height="100" class="layer4" /></td> </tr> </table>
CSS which goes in your style sheet: .layer1{ z-index:1; position: relative; left: 0px; bottom: 0px; } .layer2{ z-index:2; position: relative; right: 25px; bottom: 25px; } .layer3{ z-index:3; position: relative; right: 50px; bottom: 50px; } .layer4{ z-index:4; position: relative; right: 75px; bottom: 75px; }
a{ font-size: 12px; color: #990000; } a:link { text-decoration: underline; } a:visited { color: #990000; text-decoration: underline; } a:hover { color: #FF0000; font-weight:bold; text-decoration: none; } a:active { color: #990000; text-decoration: underline; } These will style all default links on your pages. You can set any values you like inside of the above definitions, and also add new attributes like resize the text on "hover".
link - allows you to style what the link looks like in its initial regular state. visited - defines what the link looks like after it's been visited.
hover - allows for a hover(mouse over) style to be set. active - defines what it looks like in its active page state.
What if you need a set of links to look different from all others?
.linkStyle2 { color: #999; text-decoration: none; } .linkStyle2 a:link { color: #999; text-decoration: none; } .linkStyle2 a:visited { color: #666; text-decoration: none; } .linkStyle2 a:hover { color: #CCC; text-decoration: none; } .linkStyle2 a:active { color: #999; text-decoration: none; }
Then you must give those links that class name in your HTML. You can also give any element containing these links the class name instead of giving each link the class name.
/* This automatically affects a page <body> tag without the need for class or id identifier */ body { background-color: #FF9900; margin-top: 5px; } /* This automatically affects all text fonts on the page without the need for class or id identifier */ body,td,th { font-family:Arial, Helvetica, sans-serif; font-size: 12px; } /* This automatically affects all paragraph tags on the page without the need for class or id identifier */ p{ font-size: 12px; } /* This automatically affects all list tags on the page without the need for class or id identifier */ li { font-size: 14px; } 2. View all of your HTML pages with the style sheet link in your browser.
So now we know that we can target native types of tags and batch style them. And we can also use class and id identifiers to target specific tags to style them individually.
<div style="position:absolute;top:0px;left:0px;padding:0px;margin:0px;"> <img src="images/somePicture.jpg" border="0" /> </div> You can change the number values to place it exactly where you need it. "left" can become "right", "top" can become "bottom", and so on. Negative numeric values work. Strategic placement is sometimes the only way to get the design you want on a web page that is HTML based.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pure CSS Drop Down Menu</title> <style type="text/css"> /* -------------------- Main body tag styling ---------------------- */ body,td,th { font-family: Arial, Helvetica, sans-serif; } /* -------------------- What the entire dropdown backround will look like and its positioning --------------------- */ ul { margin:0; padding:0; } .dc{ display:inline; position: relative; z-index: 0; margin:0; padding:0; } .dc:hover{ background-color: transparent; z-index: 50; } .dc ul{ position: absolute; width:120px; background-color: #999; left: -1000px; list-style-type:none; visibility: hidden; } .dc:hover ul{ visibility: visible; top: 16px; left:0px; } /* -------------------- What the list items will look like inside the dropdown ---------------------- */ .dc ul li{ margin:0; padding:0;
background-color: #EBEBEB; margin:1px; } .dc ul li:hover { background-color: #FFF; margin:1px; } /* -------------------- What the links look like inside the dropdown ---------------------- */ .dc ul li a { display:block; padding:4px; font-size:12px; } .dc ul li a:link { color: #333; text-decoration: none; } .dc ul li a:visited { text-decoration: none; color: #000; } .dc ul li a:hover { text-decoration: none; color: #333; } .dc ul li a:active { text-decoration: none; color: #333; } /* END PURE CSS DROP MENU */ </style> </head> <body> <div class="dc"> <a href="#">Drop Menu 1</a> <ul> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li>
</ul> </div> <div class="dc"> <a href="#">Drop Menu 2</a> <ul> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li> <li><a href="#">Menu Item</a></li> </ul> </div> </body> </html>
<!-body { background-image: url(myBG.jpg); background-position:top left; background-repeat:no-repeat; background-attachment:fixed; margin-top:48px; } #element1 { width:900px; color:#FFF; text-align:left; background-color:#000; border: #F90 1px solid; padding:24px; /* start opacity settings */ filter:alpha(opacity=50); -moz-opacity:.50; opacity:.50; /* end opacity settings */ } body,td,th { font-family: Verdana, Geneva, sans-serif; font-size: 14px; } --> </style> </head> <body> <div align="center"> <div id="element1"> Put your content for this container here </div> </div> </body> </html> Be sure you have a background file in place for your web page element... then you can place another element inside of it and use this. Or use the method above to use full page background and make elements on the page transparent.
CSS z-layer with Opacity Tutorial For Text That Will Not Fade
Learn how to use the z-index and opacity CSS properties together to achieve content that will not fade inside of a transparent element. We'll simply lay one on top of the other. This will give you #element1 with faded background... with #content1 right inside of it that will not fade, which works in all major browsers of today. This code's Z-index logic is explained in depth in this video tutorial:
CSS z-index Video Tutorial for CSS Opacity Tutorial
<html> <head> <style type="text/css"> <!-body { background-image: url(myBG.jpg); background-position:top left; background-repeat:no-repeat; background-attachment:fixed; margin-top:48px; } body,td,th { font-family: Verdana, Geneva, sans-serif; font-size: 14px; } #element1 { position:relative; z-index:1; width:900px; height:240px; text-align:left; background-color:#000; border: #F90 1px solid; /* start opacity settings */ filter:alpha(opacity=50); -moz-opacity:.50; opacity:.50; /* end opacity settings */ } #content1 { position:relative; z-index:2; top:-240px; width:840px; color:#FFF; text-align:left; margin:24px; }
--> </style> </head> <body> <div align="center"> <div id="element1"></div> <div id="content1"> <p>Put your content for this container here </p> <p>Put your content for this container here </p> <p>Put your content for this container here </p> </div> </div> </body> </html>