unit 3 CSS (2)
unit 3 CSS (2)
Inline Styles – Embedded Style Sheets – Conflicting Styles – Linking External Style
Sheets – Positioning Elements – Backgrounds – Element Dimensions – Box Model and
Text Flow – Media Types and Media Queries – Drop-Down Menus – Text Shadows –
Rounded Corners – Color – Box Shadows – Linear Gradients – Radial Gradients –
Multiple Background Images – Image Borders – Animations – Transitions and
Transformations – Flexible Box Layout Module – Multicolumn Layout.
The most common way to add CSS, is to keep the styles in external
CSS files. However, in this we will use inline and internal styles, because
this is easier.
EXAMPLE:
<link rel="stylesheet" href="style.css">
Note With an external style sheet, you can change the look of an entire web site, by changing one file!
If some properties have been defined for the same selector (element) in different style sheets, the value from the
last read style sheet will be used.
#center {
color: red;
text-align: center;
#id #center }
Selects the element with id=“center“
Eg. select a specific <p id=“center”> element, not to all <p> elements
.center {
color: red;
text-align: center;
}
.class .center Selects all elements with class=“center“
Eg. different HTML elements with a specific class attribute class=“center“
<h1 class="center">
<p class="center">
11
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC
The CSS element Selector The CSS id Selector
The element selector selects HTML elements based The id selector uses the id attribute of an HTML element to select a specific
on the element name. element, not to all <p> elements
The id of an element is unique within a page, so the id selector is used to select
Here, all <p> elements on the page will be center- one unique element!
aligned, with a red text color: To select an element with a specific id, write a hash (#) character, followed by the
HTML, CSS CODE: id of the element. The CSS rule below will be applied to the HTML element with
<!DOCTYPE html> id="para1"
<html> HTML, CSS CODE:
<head> <!DOCTYPE html>
<style> <html>
p{ <head>
text-align: center; <style>
color: red; #center {
} text-align: center;
</style> color: red;
</head> }
<body> </style>
<p>paragraph will be affected by the style.</p> </head>
<p>Me too!</p> <body>
<p>And me!</p> <p id=“center">paragraph will be affected by the style.</p>
</body> <p>Me too!</p>
</html> <p>And me!</p>
OUTPUT </body>
</html>
OUTPUT
12
The CSS class Selector
The class selector selects different 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:
HTML, CSS CODE:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head> OUTPUT
<body>
<h1 class="center">paragraph will be affected by the style.</h1>
<p class="center">Me too!.</p>
</body>
</html>
If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will "win",
and its style declaration will be applied to that HTML element.
Think of specificity as a score/rank that determines which style declaration is ultimately applied to an element.
Specificity Hierarchy
There are four categories which define the specificity level of a selector:
Inline styles - Example: <h1 style="color: pink;">
</body>
</html>
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 24
Positioning Elements
The position property in CSS tells about the method of positioning for an element or an HTML entity.
There are five different types of positioning the elements available in CSS:
Static
Relative
Absolute
Fixed
Sticky
The positioning of an element can be done using the top, right, bottom, and left properties.
These specify the distance of an HTML element from the edge of the viewport.
To set the position by these four properties, we have to declare the positioning method.
Syntax:
position: positioning elements;
.parent{
position:absolute;
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 30
}
position: sticky; HTML, CSS CODE
<style> <!DOCTYPE html>
An element with position: sticky; is positioned .parent { <html>
<head>
background-color: blue;
based on the user's scroll position. position:relative;
<title>CSS Positioning elements</title>
<style>
height:200vh;
}
A sticky element toggles between relative and fixed, .child-one { </style>
background-color: green; </head>
depending on the scroll position. It is positioned position:sticky; <body>
<div class="parent">
top:0px;
relative until a given offset position is met in the }
Parent
<div class="child-one">One</div>
.child-two { <div class="child-two">Two</div>
viewport - then it "sticks" in place (like background-color: red; <div class="child-three">Three</div>
} </div>
position:fixed). .child-three { </body>
background-color: pink; </html>
}
</style>
OUTPUT
33
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
background (shorthand property) specify all the background properties in one single property.
div {
body { background-color: lightblue;
}
background-image: url("paper.gif");
p{
background-color: #cccccc; background-color: yellow;
}
</style>
} </head>
<body>
You can set the background color for <h1>CSS background-color example!</h1>
any HTML elements: <div>
This is a text inside a div element.
<p>This paragraph has its own background color.</p>
We are still in the div element.
</div> OUTPUT
</body>
</html>
The border-image property takes the image and slices <h1>The border-image Property</h1>
it into nine sections, like a tic-tac-toe board. It then
places the corners at the corners, and the middle <p>Here, the middle sections of the image are repeated to create the
border:</p>
sections are repeated or stretched as you specify. <p id="borderimg">border-image: url(border.png) 30 round;</p>
OUTPUT <p>Here is the original image:</p><img src="border.png">
<p><strong>Note:</strong> Internet Explorer 10, and earlier versions,
do not support the border-image property.</p>
</body>
</html>
39
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC https://www.w3schools.com/css/css3_border_images.asp
HTML, CSS CODE
Element Dimensions <!DOCTYPE html>
<html>
CSS Setting height and width <head>
The height and width properties are used to set the <style>
height and width of an element. div {
The height and width properties do not include width: 500px; /*change to max-width */
padding, borders, or margins. It sets the height/width height: 100px;
of the area inside the padding, border, and margin of background-color: powderblue;
}
the element.
</style>
The height and width properties may have the following </head>
<body>
values:
auto - This is default. The browser calculates the height and width
<h2>Set the max-width of an element</h2>
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block <div>This div element has a height of 100px and a
initial - Sets the height/width to its default value max-width of 500px.</div>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: pink;
}
Syntax:
background-image : linear-gradient(direction, color-stop1, color-stop2,………..)
directions – by default top to bottom
to left, to right, to left top, to left bottom etc.
https://www.w3schools.com/css/css3_gradients.asp
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 52
Example - Linear Gradients – Top to Bottom by default
HTML, CSS CODE
<html>
<head>
<style>
#grad1 {
height: 200px;
width: 200px;
background-image: linear-gradient(red, yellow);
OUTPUT
}
</style>
</head>
<body>
<h1>Linear Gradient - Top to Bottom</h1>
<div id="grad1"></div>
</body>
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 53
</html>
Example - Linear Gradients – Left to Right
<html> HTML, CSS CODE
<head>
<style>
#grad {
height: 200px;
width: 200px;
background-image: linear-gradient(to right, red , yellow);
}
</style> OUTPUT
</head>
<body>
<h1>Linear Gradient - Left to Right</h1>
<div id="grad"></div>
</body>
</html> UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 54
Example - Linear Gradients – Multiple Colors
HTML, CSS CODE
<html>
<head>
<style>
#grad2 {
height: 200px;
width: 200px;
background-image: linear-gradient(red, orange, yellow,
green, blue, indigo, violet);
}
OUTPUT
</style>
</head>
<body>
<h1>Linear Gradients - Multiple Color Stops</h1>
<div id="grad2"></div>
</body> UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 55
HTML, CSS CODE
2) Radial Gradients
A Radial Gradient is defined by its center
To create a Radial Gradient you must also define at least two color steps
Syntax:
background-image : radial-gradient(shape size at position, start-color,……….., last-color);
By default, shape is ellipse, size is farthest-corner, and position is center.
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
OUTPUT
background-image: radial-gradient(red, yellow, green);
}
</style>
</head>
<body>
<h1>Radial Gradient</h1>
<div id="grad1"></div>
</body></html>
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 56
Example - Radial Gradients
HTML, CSS CODE
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
background-color: red;
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
} OUTPUT
</style>
</head>
<body>
<h1>Radial Gradient</h1>
<div id="grad1"></div>
</body></html> UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 57
HTML, CSS CODE
CSS Animation The example binds the "example" animation to the <div> element. The
animation will last for 4 seconds, and it will gradually change the
Animation is step by step movements of object
on the screen, an animation lets an element background-color of the <div> element from "red" to "yellow":
gradually change from one style to another. <!DOCTYPE html> </head>
To use CSS animation, you must first specify <html> <body>
some keyframes for the animation. <head>
Keyframes is what styles the element will have <style> <h1>CSS Animation</h1>
at certain times. div { <div></div>
width: 100px; <p><b>Note:</b> When an
height: 100px; animation is finished, it goes back
The @keyframes Rule
background-color: red; to its original style.</p>
When you specify CSS styles inside animation-name: example; </body>
the @keyframes rule, the animation will
gradually change from the current style to the animation-duration: 4s; </html>
new style at certain times. }
OUTPUT
To get an animation to work, you must bind the
@keyframes example {
animation to an element. from {background-color: red;}
to {background-color: yellow;}
Note:CSS animations are for more complex
series of movements. }
</style>
tranformation
UNIT III SDP (CSS), MEENAKSHI, DEPT OF S&H, RMKEC 64