HTML style using CSS

HTML style using CSS Tutorial

1. HTML style using CSS

Let's suppose we have created our web page using a simple HTML code, and we want something which can present our page in a correct format, and visibly attractive. So to do this, we can style our web page with CSS (Cascading Stylesheet) properties.

CSS is used to apply the style on the web page which is made up of HTML elements. It describes the look of the webpage.

CSS provides various style properties such as background color, padding, margin, border-color, and many more, to style a webpage.

Each property in CSS has a name-value pair, and each property is separated by a semicolon (;).

2. Example:

  1. <body style="text-align: center;">  
  2.       <h2 style="colorred;">Welcome to samtpoint</h2>  
  3.       <p style="colorblue; font-size: 25px; font-style: italic ;">This is a great website to learn technologies in very simple way. </p>  
  4. </body>  

3. Output:

4. Three ways to apply CSS

To use CSS with HTML document, there are three ways:

  • Inline CSS: Define CSS properties using style attribute in the HTML elements.
  • Internal or Embedded CSS: Define CSS using <style> tag in <head> section.
  • External CSS: Define all CSS property in a separate .css file, and then include the file with HTML file using tag in section.

5. Inline CSS:

Inline CSS is used to apply CSS in a single element. It can apply style uniquely in each element.

To apply inline CSS, you need to use style attribute within HTML element. We can use as many properties as we want, but each property should be separated by a semicolon (;).

6. Example:

  1. <h3 style="color: red;  
  2.             font-style: italic;  
  3.             text-align: center;  
  4.             font-size: 50px;  
  5.             padding-top: 25px;">Learning HTML using Inline CSS</h3>  

7. Output:

8. Internal CSS:

An Internal stylesheets contains the CSS properties for a webpage in <head> section of HTML document. To use Internal CSS, we can use class and id attributes.

We can use internal CSS to apply a style for a single HTML page.

9. Example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.                   <style>  
  5.       /*Internal CSS using element name*/  
  6.             body{background-color:lavender;  
  7.              text-align: center;}  
  8.              h2{font-style: italic;  
  9.               font-size: 30px;  
  10.               color: #f08080;}  
  11.             p{font-size: 20px;}  
  12.         /*Internal CSS using class name*/  
  13.             .blue{color: blue;}  
  14.             .red{color: red;}  
  15.             .green{color: green;}  
  16.       </style>  
  17.     </head>  
  18.   <body>  
  19.    <h2>Learning HTML with internal CSS</h2>  
  20.     <p class="blue">This is a blue color paragraph</p>  
  21.     <p class="red">This is a red color paragraph</p>  
  22.     <p class="green">This is a green color paragraph</p>  
  23.   </body>  
  24. </html>  

10. Output:

11. External CSS:

An external CSS contains a separate CSS file that only contains style code using the class name, id name, tag name, etc. We can use this CSS file in any HTML file by including it in HTML file using <link> tag.

If we have multiple HTML pages for an application and which use similar CSS, then we can use external CSS.

There are two files need to create to apply external CSS

  • First, create the HTML file
  • Create a CSS file and save it using the .css extension (This file only will only contain the styling code.)
  • Link the CSS file in your HTML file using the tag in the header section of HTML document.

12. Example: HTML File

  2. <html>  
  3. <head>  
  4.     <link rel="stylesheet" type="text/css" href="style.css">  
  5.     head>  
  6.   <body>  
  7.    <h2>Learning HTML with External CSSh2>  
  8.     <p class="blue">This is a blue color paragraphp>  
  9.     <p class="red">This is a red color paragraphp>  
  10.     <p class="green">This is a green color paragraphp>  
  11.   <body>  
  12. <html>  

13. CSS File

text-align: center;
: italic;
size: 30px;
color: #f08080;
: 20px;

: blue;
: red;
: green;

14. Commonly used CSS properties:

Properties-name Syntax Description
background-color background-color:red; It defines the background color of that element.
color color: lightgreen; It defines the color of text of an element
padding padding: 20px; It defines the space between content and the border.
margin margin: 30px; margin-left: It creates space around an element.
font-family font-family: cursive; Font-family defines a font for a particular element.
Font-size font-size: 50px; Font-size defines a font size for a particular element.
text-align text-align: left; It is used to align the text in a selected position.