HTML For Juniors: Part 6

Introduction

 
Before reading this article please visit the following links:
This article explains the basic parts of HTML that a junior (beginner) needs to know. From juniors I mean all those that have just passed High School or are 14 years or above.
 

HTML Comments and CSS (Cascading Style Sheets)

 
Comment tags < !- - and - -> are used to insert comments in HTML.
 
 
HTML Comments tags
 
You can add comments to your HTML page using the following syntax.
  1. < ! - - write your comment here - - >  
NOTE
 
There is an exclamation mark in the opening tag that will not be in the closing tag.
  • Comments are not displayed by the browser, but they can help document your HTML.
  • With comments, you can place notifications and reminders in your HTML.
Example
  1. <-- I am a girl -->    
  2. <p>I love to write articles..</p    
  3. <!-- Remember to add more information here -->   
Comments are also great for debugging HTML, because you can comment out HTML lines of code, one at a time, to search for errors.
 
Example
  1. <!-- Do not display this at the moment<img border="0" src="pic_girl.jpg" alt="Girl">-->    
Conditional Comments
 
You might stumble upon conditional comments in HTML.
  1. <!--[if IE 8]>     
  2. .... some HTML here ....    
  3. <![endif]-->   
Conditional comments define HTML tags to be executed by Internet Explorer only.
 
Software Program Tags
  • HTML comment tags can also be generated by various HTML software programs.
  • For example <!--webbot bot--> tags wrapped inside HTML comments by FrontPage and Expression Web.
  • As a rule, let these tags stay, to help support the software that created them.
HTML CSS
 
CSS means Cascading Style Sheets.
  1. <html>    
  2.     <head>    
  3.         <style>    
  4. body {background-color:purple}    
  5. h1 {color:white}    
  6. p {color:blue}    
  7. </style>    
  8.     </head>    
  9.     <body>    
  10.         <h1>I am a girl.</h1>    
  11.         <p>I love to write articles..</p>    
  12.     </body>    
  13. </html>  
RESULT
 
 
Styling HTML with CSS
 
Styling can be added to HTML elements in one of the following 3 ways:
  • Inline
     
    Using a style attribute in HTML elements.
     
  • Internal
     
    Using a <style> element in the HTML <head> section.
     
  • External
     
    Using one or more external CSS files.
     
    The most common way to add styling is to keep the styles in separate CSS files.
CSS Syntax
 
CSS styling has the following syntax.
 
element { property:value; property:value }
  • The element is an HTML element name. The property is a CSS property. The value is a CSS value.
  • Multiple styles are separated by semicolons.
Inline Styling (Inline CSS)
  • Inline styling is useful for applying a unique style to a single HTML element.
  • Inline styling uses the style attribute.
  • This inline styling changes the text color of a single heading.
     
    Example:
    1. <h1 style="color:blue">This is a Blue Heading</h1>  
Internal Styling (Internal CSS)
  • An internal style sheet can be used to define a common style for all HTML elements on a page.
  • Internal styling is defined in the <head> section of an HTML page, using a <style> element:
     
    Example
    1. <html>    
    2.     <head>    
    3.         <style>    
    4. body {background-color:purple}    
    5. h1 {color:white}    
    6. p {color:blue}    
    7. </style>    
    8.     </head>    
    9.     <body>    
    10.         <h1>I am a girl.</h1>    
    11.         <p>I love to write articles..</p>    
    12.     </body>    
    13. </html> 
External Styling (External CSS)
  • External style sheets are ideal when the style is applied to many pages.
  • With external style sheets, you can change the look of an entire web site by changing one file.
  • External styles are defined in an external CSS file and then linked to in the <head> section of an HTML page.
    1. <html>    
    2.     <head>    
    3.         <link rel="stylesheet" href="styles.css">    
    4.     </head>    
    5.     <body>    
    6.         <h1>I am a girl.</h1>    
    7.         <p>I love to write articles..</p>    
    8.     </body>    
    9. </html>   
    Result
     
     
CSS Fonts
  • The CSS color property defines the text color to be used for the HTML element.
  • The CSS font-family property defines the font to be used for the HTML element.
  • The CSS font-size property defines the text size to be used for the HTML element.
     
    Example
    1. <html>    
    2.     <head>    
    3.         <style>    
    4. h1 {     
    5. color:orange;     
    6. font-family:verdana;     
    7. font-size:300%;    
    8. }    
    9. p {     
    10. color:yellow;     
    11. font-family:courier;     
    12. font-size:160%;    
    13. }    
    14. </style>    
    15.     </head>    
    16.     <body>    
    17.         <h1>I am a girl.</h1>    
    18.         <p>I love to write articles..</p>    
    19.     </body>    
    20. </html>   
    Result
     
     
The CSS Box Model
 
 
Every HTML element has a box around it, even if you cannot see it.
  • The CSS border property defines a visible border around an HTML element.
     
    Example
    1. p {     
    2. border:1px solid black;    
    3. }  
  • The CSS padding property defines padding (space) inside the border.
     
    Example
    1. p {     
    2. border:1px solid black;     
    3. padding:10px;    
    4. }   
  • The CSS margin property defines a margin (space) outside the border.
     
    Example
    1. p {     
    2. border:1px solid black;     
    3. padding:10px;     
    4. margin:30px;    
    5. }   
The id Attribute
  • All the examples above use CSS to style HTML elements in a general way.
  • To define a special style for one special element, first add an id attribute to the element.
     
    Example
    1. <p id="p01">I am a girl.</p>   
     Result
     
     
  • Then define a different style for the (identified) element.
     
    Example:
    1. p#p01 {     
    2. color:blue;    
    3. }   
The class Attribute
  • To define a style for a special type (class) of elements, add a class attribute to the element.
     
    Example:
    1. <p class="error">I am different</p>    
  • Now you can define a different style for all the elements with the specified class.
     
    Example
    1. p.error {     
    2. color:blue;    
    3. }   
Deprecated Tags and Attributes in HTML5
  • In older HTML versions, several tags and attributes were used to style documents.
  • These tags and attributes are not supported in HTML5!
  • Avoid using the <font>, <center>, and <strike> elements.
  • Avoid using the color and bgcolor attributes.

Chapter Summary

  • Comment tags < !- - and - -> are used to insert comments in HTML.
  • Use the HTML style attribute for inline styling.
  • Use the HTML <style> element to define internal CSS.
  • Use the HTML <link> element to refer to an external CSS file.
  • Use the HTML <head> element to store <style> and <link> elements.
  • Use the CSS color property for text colors.
  • Use the CSS font-family property for text fonts.
  • Use the CSS font-size property for text sizes.
  • Use the CSS border property for visible element borders.
  • Use the CSS padding property for space inside the border.
  • Use the CSS margin property for space outside the border.