CSS Basic and Common Errors


Published on

Published in: Education
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

CSS Basic and Common Errors

  1. 1. Using the CSSPrepared by: Sanjay Raval | http://www.usableweb.in/
  2. 2. What is CSS?CSS is a language that’s used to define the formatting applied to a Website, includingcolors, background images, typefaces (fonts), margins, and indentation.Let’s look at a basic example to see how this is done. <!DOCTYPE html> <html> <head> <title>A Simple Page</title> <style type="text/css"> h1, h2 { font-family: sans-serif; color: #3366CC; } </style> </head> <body> <h1>First Title</h1><p>…</p> <h2>Second Title</h2><p>…</p> <h2>Third Title</h2><p>…</p> </body> </html>
  3. 3. Using CSS in HTMLlnline StylesThe simplest method of adding CSS styles to your web pages is to use inline styles.An inline style is applied via the style attribute, like this: <p style="font-family: sans-serif; color: #3366CC;"> Amazingly few discotheques provide jukeboxes. </p>Inline styles have two major disadvantages:1) They can’t be reused. For example, if we wanted to apply the style above to another p element, we would have to type it out again in that element’s style attribute.2) Additionally, inline styles are located alongside the page’s markup, making the code difficult to read and maintain.
  4. 4. Using CSS in HTMLEmbedded or Internal StylesIn this approach, you can declare any number of CSS styles by placing them between the openingand closing <style> tags, as follows: <style type="text/css"> CSS Styles here </style>While it’s nice and simple, the <style>tag has one major disadvantage: if you want to use aparticular set of styles throughout your site, you’ll have to repeat those style definitions withinthe style element at the top of every one of your site’s pages.
  5. 5. Using CSS in HTMLExternal Style SheetsAn external style sheet is a file (usually given a .css filename) that contains a web site’s CSSstyles, keeping them separate from any one web page. Multiple pages can link to the same .cssfile, and any changes you make to the style definitions in that file will affect all the pages thatlink to it.To link a document to an external style sheet (say, styles.css), we simply place a link element inthe document’s header: <link rel="stylesheet" type="text/css" href="styles.css" />
  6. 6. CSS SelectorsEvery CSS style definition has two components: A list of one or more selectors, separated by commas, define the element or elements to which the style will be applied. The declaration block specifies what the style actually does.
  7. 7. CSS SelectorsType Selectors – Tag SelectorBy naming a particular HTML element, you can apply a style rule to every occurrence of thatelement in the document.For example, the following style rule might be used to set the default font for a web site: p, td, th, div, dl, ul, ol { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 1em; color: #000000; }
  8. 8. CSS SelectorsClass SelectorsCSS classes comes in when you want to assign different styles to identical elements that occur indifferent places within your document.Consider the following style, which turns all the paragraph text on a page blue: p { color: #0000FF; }Great! But what would happen if you had a sidebar on your page with a blue background? Youwouldn’t want the text in the sidebar to display in blue as well—then it would be invisible. Whatyou need to do is define a class for your sidebar text, then assign a CSS style to that class.To create a paragraph of the sidebarclass, first add a classattribute to the opening tag: <p class="sidebar"> This text will be white, as specified by the CSS style definition below. </p>
  9. 9. CSS SelectorsClass SelectorsNow we can write the style for this class: p { color: #0000FF; } .sidebar { color: #FFFFFF; }This second rule uses a class selector to indicate that the style should be applied to any elementof the sidebar class. The period indicates that we’re naming a class—not an HTML element.
  10. 10. CSS SelectorsID SelectorsIn contrast with class selectors, ID selectors are used to select one particular element, ratherthan a group of elements. To use an ID selector, first add an id attribute to the element you wishto style. It’s important that the ID is unique within the document: <p id="tagline">This paragraph is uniquely identified by the ID "tagline".</p>To reference this element by its ID selector, we precede the id with a hash (#). For example, thefollowing rule will make the above paragraph white: #tagline { color: #FFFFFF; }ID selectors can be used in combination with the other selector types. #tagline .new { font-weight: bold; color: #FFFFFF; }
  11. 11. CSS SelectorsDescendant or Compound SelectorsA descendant selector will select all the descendants of an element. p { color: #0000FF; } .sidebar p { color: #FFFFFF; } <div class="sidebar"> <p>This paragraph will be displayed in white.</p> <p>So will this one.</p> </div>In this case, because our page contains a div element that has a class of sidebar, the descendantselector .sidebar p refers to all p elements inside that div.
  12. 12. CSS SelectorsChild SelectorsA descendant selector will select all the descendants of an element, a child selector only targetsthe element’s immediate descendants, or “children.”Consider the following markup: <div class="sidebar"> <p>This paragraph will be displayed in white.</p> <p>So will this one.</p> <div class="tagline"> <p>If we use a descendant selector, this will be white too. But if we use a child selector, it will be blue.</p> </div> </div>
  13. 13. CSS SelectorsChild SelectorsIn this example, the descendant selector we saw in the previous section would match theparagraphs that are nested directly within div.sidebar as well as those inside div.tagline. If youdidn’t want this behavior, and you only wanted to style those paragraphs that were directdescendants of div.sidebar, you’d use a child selector.A child selector uses the > character to specify a direct descendant.Here’s the new CSS, which turns only those paragraphs directly inside .sidebar (but not thosewithin .tagline) white: p { color: #0000FF; } .sidebar>p { color: #FFFFFF; } Note: Unfortunately, Internet Explorer 6 doesn’t support the child selector.
  14. 14. Most CommonCSS Mistakes
  15. 15. ID vs. Class WhatAn ID refers to a unique instance in a document, or something that will only appear once on apage. For example, common uses of IDs are containing elements such as page wrappers,headers, and footers. On the other hand, a CLASS may be used multiple times within a document,on multiple elements. A good use of classes would be the style definitions for links, or other typesof text that might be repeated in different areas on a page.In a style sheet, an ID is preceded by a hash mark (#), and might look like the following: #header { height:50px; } #footer { height:50px; } #sidebar { width:200px; float:left; } #contents { width:600px; }
  16. 16. ID vs. Class WhatClasses are preceded by a dot (.). An example is given below. .error { font-weight:bold; color:#C00; } .btn{ background:#98A520; font-weight:bold; font-size:90%; height:24px; color:#fff; }
  17. 17. Redundant Units for Zero ValuesThe following code doesnt need the unit specified if the value is zero. padding:0px 0px 5px 0px;It can be written instead like this: padding:0 0 5px 0;Dont waste bytes by adding units such as px, pt, em, etc, when the value is zero. The onlyreason to do so is when you want to change the value quickly later on. Otherwise declaring theunit is meaningless. Zero pixels is the same as zero points.
  18. 18. Avoid RepetitionWe should avoid using several lines when just one line will do.For example, when setting borders, some people set each side separately: border-top:1px solid #00f; border-right:1px solid #00f; border-bottom:1px solid #00f; border-left:1px solid #00f;But why? When each border is the same! Instead it can be like: border:1px solid #00f;
  19. 19. Excess WhitespaceUsually we have tendency to include whitespace in the code to make it readable. Itll only makethe stylesheet bigger, meaning the bandwidth usage will be higher.Of course its wise to leave some space in to keep it readable.
  20. 20. Grouping Identical Styles togetherSo when we want a same style to a couple of elements. It is good to group them together anddefine the style instead of defining the style for each element separately. h1, p, #footer, .intro { font-family:Arial,Helvetica,sans-serif; }It will also make updating the style much easier.
  21. 21. Margin vs. PaddingAs margins and paddings are generally be used interchangeably, it is important to know thedifference. It will give you greater control over your layouts. Margin refers to the space aroundthe element, outside of the border. Padding refers to the space inside of the element, within theborder.Example: No Padding, 10px Margin p { border: 1px solid #0066cc; margin:10px; padding:0; }Result:
  22. 22. Margin vs. PaddingExample: No Margin, 10px Padding p { border: 1px solid #0066cc; padding:10px; margin:0; }Result: