Introduction To CSS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

LEARN CSS

Introduction

CSS is the language we use to style a Web page.

❖ CSS stands for Cascading Style Sheets


❖ CSS describes how HTML elements are to be displayed on screen, paper, or in other media
❖ CSS saves a lot of work. It can control the layout of multiple web pages all at once
❖ External stylesheets are stored in CSS files

Click this link for example.


CSS Syntax

A CSS rule consists of a selector and a declaration block

The selector points to the HTML element you want to style.


The declaration block contains one or more declarations separated by semicolons.
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces..

Click this link for example.


CSS Selector
A CSS selector selects the HTML element(s) you want to style.

CSS selectors are used to "find" (or select) the HTML elements you want to style.

We can divide CSS selectors into five categories:

● Simple selectors (select elements based on name, id, class)


● Combinator selectors (select elements based on a specific relationship between them)
● Pseudo-class selectors (select elements based on a certain state)
● Pseudo-elements selectors (select and style a part of an element)
● Attribute selectors (select elements based on an attribute or attribute value)
CSS Element Selector
The element selector selects HTML elements based on the element name.

Here, all <p> elements on the page will be center-aligned, with a red text color:

p{
text-align: center;
color: red;
}

Click this link for example.


CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.

The CSS rule below will be applied to the HTML element with id="para1":

#para1 {
text-align: center;
color: red;
}

Click this link for example.

Note: An id name cannot start with a number!


CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.

In this example all HTML elements with class="center" will be red and center-aligned:

.center {
text-align: center;
color: red;
}

Click this link for example.


CSS class Selector
You can also specify that only specific HTML elements should be affected by a class.

In this example only <p> elements with class="center" will be red and center-aligned:

p.center {
text-align: center;
color: red;
}

Click this link for example.


CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.

In this example the CSS rule below will affect every HTML element on the page:

*{
text-align: center;
color: blue;
}

Click this link for example.


CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
CSS Grouping Selector
It will be better to group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

h1, h2, p {
text-align: center;
color: red;
}

Click this link for example.


How to add CSS
When a browser reads a style sheet, it will format the HTML document according to the information in
the style sheet.

Three Ways to Insert CSS

There are three ways of inserting a style sheet:

● External CSS
● Internal CSS
● Inline CSS
External CSS
With an external style sheet, you can change the look of an entire website by changing just one file!

Each HTML page must include a reference to the external style sheet file inside the <link> element,
inside the head section.
External styles are defined within the <link> element, inside the <head> section of an HTML page:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.

Internal styles are defined within the <style> element, inside the <head> section of an HTML page:

Click this link for example.


Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain any
CSS property.

Inline styles are defined within the "style" attribute of the relevant element:

<!DOCTYPE html>
<html>
<body>

<h1 style="color:blue;text-align:center;">This is a heading</h1>


<p style="color:red;">This is a paragraph.</p>

</body>
</html>
CSS Navigation Bar
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.

A navigation bar needs standard HTML as a base.

In our examples we will build the navigation bar from a standard HTML list.

A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:

<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
CSS Website Layout
A website is often divided into
headers, menus, content and a
footer:

You might also like