CSS Basic introduction with example

 CSS Basic Introduction


CSS, which stands for Cascading Style Sheets, is a language used to describe the appearance and formatting of a document written in HTML (Hypertext Markup Language). It allows you to control the layout, colors, fonts, and other visual aspects of a webpage.






1. CSS Syntax:


   CSS uses a simple syntax consisting of selectors and declarations. Selectors target HTML elements, and declarations specify the styles to be applied to those elements. Here's an example:
  

   selector 
{
     property: value;
 }


2. Linking CSS to HTML:


   You can link an external CSS file to your HTML document using the `<link>` tag. Place the following line inside the `<head>` section of your HTML document:
  
   <link rel="stylesheet" href="styles.css">
 
   This assumes your CSS file is named "styles.css" and located in the same directory as your HTML file.


3. Selectors:


   CSS provides various types of selectors to target specific HTML elements. Some common ones include:
   - Element selector: Targets all instances of a particular HTML element.
   - Class selector: Targets elements with a specific class attribute.
   - ID selector: Targets a single element with a specific ID attribute.
   - Attribute selector: Targets elements with a specific attribute value.


4. Properties and Values:

 
   CSS properties define the specific aspect of an element you want to style, such as color, font-size, margin, etc. Each property is assigned a value. Here are a few 

example

   h1 
{
     color: blue;
     font-size: 24px;
     margin-top: 10px;
   }

   


5. Cascading and Specificity:
   CSS follows a cascading rule, which means that styles can be inherited from parent elements and overwritten by more specific rules. Specificity determines which styles take precedence when conflicting rules exist.

6. Inline, Internal, and External CSS:
   CSS can be applied in three ways:
   - Inline CSS: Applied directly to HTML elements using the "style" attribute.
   - Internal CSS: Placed within the `<style>` tags in the HTML document's `<head>` section.
   - External CSS: Defined in a separate CSS file and linked to the HTML document using the `<link>` tag.

7. CSS Box Model:
   The CSS box model represents the structure of an element on a webpage. It consists of the content area, padding, border, and margin. You can control the size and spacing of elements using properties such as width, height, padding, margin, etc.
These are just the basics of CSS. As you delve deeper, you'll encounter more advanced concepts and techniques for styling webpages. CSS is a powerful tool that allows you to create visually appealing and responsive websites.



Post a Comment

0 Comments