Css
Css
Introduction
➢CSS (Cascading Style Sheets) is used to style and lay out web pages.
➢For example, to alter the font, color, size, and spacing of your content, split it into multiple
columns, or add animations and other decorative features.
➢Uses:
➢You can easily apply same style rules on multiple elements.
➢You can control the presentation of multiple pages of a website with a single style sheet.
➢You can present the same page differently on different devices.
➢You can style dynamic states of elements such as hover, focus, etc
Advantages of Using CSS
➢CSS Save Lots of Time
➢Easy Maintenance
➢Pages Load Faster
➢Superior Styles to HTML
➢Multiple Device Compatibility
Including CSS in HTML Documents
CSS can either be attached as a separate document or embedded in the HTML document
itself.
There are three methods of including CSS in an HTML document:
Inline styles — Using the style attribute in the HTML start tag.
Embedded styles — Using the <style> element in the head section of a document.
External style sheets — Using the <link> element, pointing to an external CSS file.
INLINE STYLE SHEETs
❑Inline styles are used to apply the unique style rules to an element by putting the CSS rules
directly into the start tag.
❑The style attribute includes a series of CSS property and value pairs. Each "property:
value" pair is separated by a semicolon (;)
Example
<h1 style="color:red; font-size:30px;">This is a heading</h1>
<p style="color:green; font-size:22px;">This is a paragraph.</p>
<div style="color:blue; font-size:14px;">This is some text content.</div>
Output - INLINE STYLE SHEETs
This is a heading
This is a paragraph.
This is some text content.
Disadvantages - INLINE STYLE SHEETs
❑Adding CSS rules to every HTML element is time-consuming and makes your HTML
structure messy.
Example:
<!DOCTYPE html>
<html>
<body style="background-color:black;">
</body>
</html>
Embedded or Internal style sheets
❑Embedded or internal style sheets only affect the document they are embedded in.
❑Embedded style sheets are defined in the <head> section of an HTML document using
the <style> element.
Example - Embedded style sheets
<!DOCTYPE html>
<h1>Welcome to SECE</h1>
<html lang="en"> <p>Department of IT</p>
<head> </body>
</html>
<meta charset="utf-8">
<title>Example of CSS Embedded Style Sheet</title>
<style>
body { background-color: YellowGreen; }
p { color: #fff; }
</style>
</head>
<body>
External style sheets
❑An external style sheet holds all the style rules in a separate document that you can link
from any HTML file on your site.
❑External style sheets are the most flexible because you can change the look of an entire
website by changing just one file.
❑You can attach external style sheets in two ways — linking and importing.
❑An external style sheet can be linked to an HTML document using the <link> tag.
❑The <link> tag goes inside the <head> section.
Linking External Style Sheets
Style.css
body {
background: lightyellow;
font: 18px Arial, sans-serif;
}
h1 {
color: orange;
}
Linking External Style Sheets
<!DOCTYPE html>
<html lang="en">
<head>
<title>My HTML Document</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</body>
</html>
Importing External Style Sheets
❑The @import rule is another way of loading an external style sheet.
❑The @import statement instructs the browser to load an external style sheet and use its
styles.
❑Two Ways:
❑Other CSS rules may still be included in the <style> element.
❑Another way , you can use the @import rule to import a style sheet within another style
sheet.
Importing External Style Sheets
<head>
<style>
@import url("css/style.css");
p{
color: blue;
font-size: 16px;
}
</style>
</head>
Importing External Style Sheets
<body>
<h1>The styles for this heading are defined in the imported style sheet</h1>
<p>The styles for this paragraph are defined in the embedded style sheet.</p>
</body>
Output
Importing External Style Sheets
<style>
@import url("css/layout.css");
@import url("css/color.css");
body {
color: blue;
font-size: 14px;
}
</style>
Importing External Style Sheets
<body>
<div>
<h1>Importing External Style Sheet</h1>
<p>The layout styles of these HTML element is defined in 'layout.css' and
colors in 'color.css'.</p>
</div>
</body>
</html>
CSS selectors are part of a CSS
rule that allows you to select
the contents you want to style.
CSS Selectors
• A CSS stylesheet consists of a set of rules that are interpreted by the web
browser.
• Then applied to the corresponding elements such as paragraphs, headings,
etc. in the document.
• A CSS rule have two main parts, a selector and one or more declarations:
CSS Selectors Types
• Basic Selectors
• Advanced Selectors
• Combinators
Basic Selectors Types
• Element Selector
• Class Selector
• ID Selector
• Universal Selector
• Group Selector
Element Selector
• Selects all elements of a specific type.
Class Selector
Class selectors are designated with a . (period)
before the class name when being used in a selector
ID Selector
• The id selector is used to define style rules for a single or unique element.
• The id selector is defined with a hash sign (#) immediately followed by the id
value.
Universal Selector
• Universal selectors will select all elements within a tag
Group Selector
• Selects multiple elements at once.
Advanced Selector Types
• ::after
• ::before
• ::first-letter
• ::first-line
• ::selection
Pseudo-Classes
Pseudo classes are added to selectors to specify a certain state of an element.
List of Pseudo- Classes:
• :link
• :visited
• :active
• :hover
• :focus
• :empty
• :target
• :checked
• :enabled
• :disabled
Attribute-Classes
Used to target elements based on the presence of and/or the value of HTML
attributes associated with the element.
Example
a[href]
a[href="http://www.google.com/"]
a[title~="sece"]
a[href^="http"]
a[href$=".com"] and a[href*="sece"
Combinators
Used to target elements based on their relationship with other elements.
• Descendant selector
• Child selector
• Adjacent sibling selector
• General sibling selector
Descendant Selector
A descendant selector can target all elements that are a descendant of a
specified element.
Child Selectors
A child selector is similar to a descendant, except it selects only immediate
children.
Adjacent Sibling Selectors
❑ The adjacent sibling selector is denoted using the plus symbol (+).
❑ This selector is used in cases where we want to target elements only when
the element is immediate proceeded by another targeted element.
General Sibling Selectors
A general sibling selector is much like an adjacent sibling selector, with the only
difference being that it selects all elements proceeding a targeted element
rather than one.
CSS Border
• The CSS border properties allow you to define the border area of an
element's box.
• Borders appear directly between the margin and padding of an element.
Border - Style
• The border-style property sets the style of a box's border such as: solid, dotted, etc.
h1 { h1 {
color: #ffa500; color: rgb(255, 165, 0);
} }
p{ p{
color: #00ff00; color: rgb(0, 255, 0);
} }
Example
<style> <body>
p.one { <p class="one">The border color of this paragraph is same
as the element's text color.</p>
color: #0000ff; <p class="two">The outline color of this paragraph is same
as the element's text color.</p>
border: 2px solid;
</body>
} </html>
p.two {
color: #00ff00;
outline: 2px solid;
}
</style>
</head>
Output
CSS Units – relative lengths
Relative length units specify a length relative to another length property.
Relative length units scales better between different rendering mediums.
CSS Background
The background CSS property is a shorthand property for setting the individual background
properties
✓background-image
✓background-position
✓background-size
✓background-repeat
✓background-attachment
✓background-origin
✓background-clip
✓background-color
CSS Background - Properties
Value Description
background- Specifies whether the background scrolls with the document, or remains fixed to
attachment the viewing area.
background-color Sets the background color of an element.
background-clip Specifies the area within which the background is painted.
background-image Sets one or several background images for an element.
background-origin Specifies the positioning area of the background images.
background- Sets the initial position of the background image.
position
background-repeat Specifies how background images are tiled after they have been sized and
positioned.
background-size Specifies the size of the background images.
CSS Background-Clip
The background-clip CSS property sets whether an element's background extends
underneath its border box, padding box, or content box.
background-clip: border-box;
The background extends to the outside edge of the border
background-clip: padding-box;
The background extends to the outside edge of the padding.
background-clip: content-box;
The background is painted within (clipped to) the content box.
background-clip: text;
The background is painted within (clipped to) the foreground text.
Example – background.html
<p class="border-box">The background extends behind the border.</p>
<p class="padding-box">The background extends to the inside edge of the border.</p>
<p class="content-box">The background extends only to the edge of the content box.</p>
<p class="text">The background is clipped to the foreground text.</p>
Example – clip.css
p{
border: .8em darkviolet; .text {
border-style: dotted double; background-clip: text;
margin: 1em 0; color: rgba(0,0,0,.2);
padding: 1.4em; }
background: linear-gradient(60deg, red, yellow, red,
yellow, red);
font: 900 1.2em sans-serif;
text-decoration: underline;
}
.border-box { background-clip: border-box; }
.padding-box { background-clip: padding-box; }
.content-box { background-clip: content-box; }
Output
Background Properties - Example
Background.html
<body>
<h3>This is heading</h3>
<p id="firstPara">This is a paragraph</p>
h1 {
text-shadow: 5px 5px 10px #666;
}
h2 {
text-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
}
CSS Text Shadows - Output
h1 {
text-shadow: 5px 5px 10px #666;
}
h2 {
text-shadow: 5px 5px 10px red, 10px 10px 20px yellow;
}