Unit 6 MWD
Unit 6 MWD
It is a style sheet language used to add styles to HTML documents displayed in browsers. CSS
enhances the user experience by making web pages more attractive and user-friendly.
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
CSS (Cascading Style Sheets) is used to style and layout web pages — for example, to
alter the font, color, size, and spacing of your content, split it into multiple columns, or add
In earlier days, web page designers used to write presentation information
within the web pages. This mechanism increased the complexity of web pages
significantly. Moreover, changing the presentation on demand required
considerable overhead. Style sheets solve these problems easily.
h1 {
color:blue; font-size:12px;
}
• h1 is the selector
• color and font size are properties
• blue and 12px are value.
The part of syntax that contains the properties and values is known
as Declaration Block.
Inline CSS
Inline CSS is a method of styling where CSS properties are directly applied
to HTML elements within the body section using the style attribute.
<html>
<head>
<title>External CSS</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
1) ADDING CSS
External
Embedded
Imported
Inline
External Style Sheets
In this case, style information is written in a separate, file and is
referenced from an HTML document An external style sheet is useful
when the same style is applied on different documents.
For example, the designer of a website may keep one style sheet file
for entire website. This helps to update and maintain the look and feel
of the entire website from a single file, without significant effort It was
mentioned that external style sheets are cached by most of the
browsers. So, browsers have to download only documents. This
implies faster response.
title="large"/>
Imported Style Sheets
Another way of importing a style sheet is to use import statement. It works in much the same
way as linking. It allows importing a style sheet from another style sheet. The import statemem
is used within the style tag in an HTML document as follows:
<style>
@import urirstylei.com
<style
This code segment imports all style rules written in the CSS file style.cas. The style elemen may
include other internal style rules, but the import statement must be the first rule within a style
tag. Even a comment before the import statement can cause it to fail. The style sheet may be
mentioned as a string as follows:
cetylee
#import "stylel.css":
</style>
This import statement will be interpreted as if it had url (...) around it.
Internal rules override the conflicting rules in the imported style sheets. For
example, the following style rule makes all paragraphs green even if a
stylel.css file contains a rule picolor: red
<style>
picolor: green:)
<style>
#import "stylel.css":
</style>
This import statement will be interpreted as if it had url (...) around it.
Internal rules override the conflicting rules in the imported style sheets. For
example, the following style rule makes all paragraphs green even if a
stylel.css file contains a rule picolor: red
<style>
picolor: green:)
<style>
selector
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.
Types of Selectors
• Universal Selectors
• Element Selectors
• Class Selectors •Adjacent Sibling Selectors
• Id Selectors •General Sibling Selectors
• Attribute Selectors
• Group Selectors
• Pseudo-element Selectors
• Pseudo-class Selectors
• Descendant Selectors
• Child Selectors
CSS Universal Selector
Universal selector, denoted by an asterisk mark (*), is a special selector that
matches all elements in an HTML document. These are generally used to
add a same length margin and padding to all the elements in document.
Syntax
*{
margin: 0;
padding: 0;
}
As per the above syntax, the universal selector is used to apply a margin and
padding of 0 to all HTML elements.
CSS
Element Selector
A element selector targets an HTML element, such as <h1>, <p>, etc. This
is used when we want to apply similar style to all the <p> tags or <h1> tags
in the document.
Syntax
/* Sets text color of all p tags to green */
p{
color: green;
}
/* Add underline to all h1 tags in document */
h1 {
text-decoration-line: underline;
}
The class selector selects HTML elements with a specific class attribute. It
CSS Class Selector
is used with a period character . (full stop symbol) followed by the class
name.
Syntax
<html>
<head>
<style>
.center {
text-align: center;
color: blue; }
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
CSS Class 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.
Syntax
<html>
<head>
<style>
.center {
text-align: center;
color: blue; }
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>
CSS Id Selector
The id selector selects the id attribute of an HTML element to select a
specific element. An id is always unique within the page so it is chosen
to select a single, unique element. It is written with the hash character
(#), followed by the id of the element.
Syntax
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
Group Selector
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to
separate each selector in grouping.
Syntax As you can see, you need to define CSS properties
h1 { for all the elements. It can be grouped in following
ways:
text-align: center;
color: blue; } h1,h2,p {
text-align: center;
h2 { color: blue;
text-align: center; }
color: blue; }
p{
text-align: center;
color: blue; }
CSS Attribute Selector
attribute Selector allows you to select elements based on the presence,
value, or specific characteristics of their attributes. They are particularly
useful for dynamic or structured content where attributes play a key role,
such as in forms or data tables.
Syntax
<html>
<head>
<style>
/* Styles all elements with a 'placeholder' attribute */
[placeholder] {
border: 2px solid blue;
}
</style>
</head>
<body>
<input type="text" placeholder="Enter your name">
<input type="password" placeholder="Enter your password">
</body>
</html>
A pseudo-element selector is used to style a specific part of an element
CSS Pseudo Element Selector
rather than the element itself.
<html>
Syntax <head>
<style>
/* Define contents before paragraph */ /* Add and style contents before
paragraph */
p::before {
p::before
content: " "; { content: "Note: ";
font-weight: bold;
} color: red; }
/* Add and style contents after
paragraph */
/* Style first letter of paragraph */ p::after
{ content: " [Read more]";
p::first-letter { font-style: italic;
font-size: 2em; color: blue; }
</style>
} </head>
<body>
<h2>Pseudo-element selector</h2>
<p>This is a paragraph.</p>
</body> </html>
It is used to style a special type of state of any element. For example- It is
CSS Pseudo Class Selector
used to style an element when a mouse cursor hovers over it. This is
commonly used for buttons and links to enhance interactivity.
Syntax h1,
h2 {
<html>
color: green;
<head> text-align: center;
}
<title>CSS :hover Pseudo Class</title>
</style>
<style> </head>
.box { <body>
<h1>Geeks For Geeks</h1>
background-color: yellow; <h2>:hover Pseudo-class</h2>
width: 300px; <div class="box">
My color changes if you hover over me!
height: 200px; </div>
margin: auto; </body>
</html>
font-size: 40px;
text-align: center;
The CSS descendant selector is utilized to match the descendant elements
CSS Descendant Selector
of a particular element. The word Descendant indicates nested anywhere in
the DOM Document Object Model tree.
Syntax
<html> <head> <style>
div p {
background-color: lightblue;
font-weight: bold; }
</style> </head>
<body> <div>
<p> This is 1st paragraph in the div. </p>
<p> This is 2nd paragraph in the div. </p>
<div> This is second div in the first div
<p> This is the paragraph in second div. It will also be affected. </p>
</div> </div>
CSS Child Selector
The child selector in css is used to target all the direct child of a particular
element. This is denoted by '>' (Greater than) symbol.
Syntax
<html><head><style>
div > p {
background-color: yellow;
}</style></head><body>
<h2>Child Selector</h2>
<p>The child selector (>) selects all elements that are the children of a
specified element.</p>
<div><p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><!-- not Child but Descendant -->
<p>Paragraph 3 in the div (inside a section element).</p>
</section> <p>Paragraph 4 in the div.</p></div>
<p>Paragraph 5. Not in a div.</p>
<p>Paragraph 6. Not in a div.</p>
</body></html>
Adjacent Sibling Selectors
div ~ p
{
background-color: yellow;
}
General Sibling Selectors
div + p
{
background-color: yellow;
}
Browser Compatibility
Not all browser supports the full CSS Specification.
Syntax:
<style>
@ import url (“style1.css”);
</style>
<style>
@import “style1.css”;
</style>