css (1)
css (1)
CSS stands for Cascading Style Sheets, it is a simple design language intended to
simplify the process of making web pages presentable using CSS properties. CSS
specify how an HTML element should be displayed on the web page. If you think of
the human body as a web page then CSS is styling part of the body. Like color of the
eyes, size of the nose, skin tone, etc.
With CSS, you can adjust font sizes and colors, add backgrounds, and manage the
layout, transforming a basic webpage into a visually appealing and user-friendly
experience. CSS also simplifies layout management across multiple web pages
by using external stylesheets stored in CSS files.
CSS Syntax
● A CSS Syntax rule consists of a selector, property, and its value. The selector
points to the HTML element where the CSS style is to be applied. The CSS
property is separated by semicolons. It is a combination of the selector name
followed by the property: value pair that is defined for the specific selector. let
us see the syntax and how we can use the CSS to modernize the website.
Syntax:
selector { Property: value; }
For instance, we have declared a heading tag (h1) along with having assigned
some property: value pair that is used to style the heading tag. Here, h1 is the
selector, { color: green; font-family: sans-serif; } is a declaration block and it can
contain one or more declarations separated by semicolons, color: green; is
a property: value pair that is applied to the HTML element to style them.
● Selector: Used to target and select specific HTML elements to apply styles to.
Example: This example illustrates the use of CSS Syntax for the styling of HTML
elements.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
text-align: center;
</style>
</head>
</body>
</html>
Output: In the above code, h1 is the selector of h1 tags, it select the h1 element that
you want to style. The color is a property and green is the value of that property,
similar text-align is the property and value of that property is center.
● Flexibility and Control: CSS provides precise control over the presentation of
HTML elements, allowing developers to customize layout, typography, colors,
and other visual properties.
CSS Selectors
CSS Selectors are used to select the HTML elements you want to
style on a web page. They allow you to target specific elements or
groups of elements to apply styles like colors, fonts, margins, and
more.
The element or elements that are selected by the selector are
referred to as subject of the selector.
Types of Selectors
Types of CSS Selectors
CSS selectors come in various types, each with its unique way of
selecting
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
5. CSS Group Selector
The class selector selects HTML elements with a specific class attribute. It is used
with a period character . (full stop symbol) followed by the class name.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
14. </body>
15. </html>
Universal Selector
The Universal selector (*) in CSS is used to select all the elements in an HTML
document. It also includes other elements which are inside under another
element.
Note: The following code is used in the above Example using the universal
selector. This CSS rule will be applied to each and every HTML element on the
page:
CSS:
*{
color: white;
background-color: black;
}
● Inline CSS: Inline CSS are directly applied on the HTML elements and it is the
most prioritize CSS amonth these three. This will override any external or
internal CSS.
Example
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>
● Internal CSS: Internal CSS are defined in the HTML head section inside
of <style> tag to let the browser know where to look for the CSS.
Example
Internal styles are defined within the <style> element, inside the <head> section of an
HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
● External CSS: External CSS are defined in a separate file that contains only
CSS properties, this is the recomended way to use CSS when you are working
on projects. It is easy to maintain and multiple CSS files can be created and
you can use them by improting it into your HTML document using
HTML <link> tag.
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>
An external style sheet can be written in any text editor, and must be saved with a .css
extension.
The external .css file should not contain any HTML tags.
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}