LECTURE 4 - 1 Introduction to CSS
LECTURE 4 - 1 Introduction to CSS
<!DOCTYPE html>
<html>
<head>
<style>
h2,p{
background-color: #b0d4de;
}
</style>
</head>
<body>
<h2>My first CSS page.</h2>
<p>This is an example of CSS background-color</p>
</body>
</html>
Ruaha Catholic University
CSS Properties
Background Image
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url(“image.jpg");
margin-left:100px;
}
</style>
</head>
<body>
<h1> This is an example of CSS background-image </h1>
</body>
</html>
Background Repeat
By default, the background-image property repeats the
background image horizontally and vertically.
Some images are repeated only horizontally or vertically.
Property Description
Set the border-radius for the top-left
border-top-left-radius
corner
Set the border-radius for the top-right
border-top-right-radius corner
Set the border-radius for the bottom-
border-bottom-right-radius right corner
Set the border-radius for the bottom-
border-bottom-left-radius left corner
#roundedBox {
border-radius: 20px;
background-color: #7fd7e7;
}
#fancyBox {
border-radius: 15% 10%;
background-color: #ffa07a;
}
#customBox {
border-radius: 20px 30px 10px;
background-color: #98fb98;
}
Ruaha Catholic University
CSS Properties
CSS Float
• Example (try it yourself)
!DOCTYPE html>
<html>
<head>
<style>
img {
float: right;
}
</style>
</head>
<body>
<p>The following paragraph contains an image with style float to the right in the paragraph.</p>
<img src=“image.jpg" alt="Good Morning Friends"/>
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</p>
</body>
</html> Ruaha Catholic University
CSS Properties
CSS float
• Is a positioning property.
• It is used to push an element to the left or right, allowing other
element to wrap around it.
• It is generally used with images and layouts.
<style>
p{
margin-top: 50px;
margin-bottom: 50px;
margin-right: 100px;
margin-left: 100px;
}
</style>
Ruaha Catholic University
CSS Properties
Margin: Shorthand Property
• It specifies all the margin properties in one property.
• Example (try it yourself)
<style>
p{
margin: 50px 100px 150px 200px;
}
</style>
It identifies that:
• top margin value is 50px
• right margin value is 100px
• bottom margin value is 150px
• left margin value is 200px
Ruaha Catholic University
CSS Properties
CSS Images
• Images are an important part of any web application.
• It is important to use the images wherever they required.
• CSS helps us to control the display of images in web
applications.
• CSS properties such
as border property, height property, width property, etc.
helps to style an image.
Transparent image
• We use the opacity property to make an image transparent. The value of
this property lies between 0.0 to 1.0, respectively.
• Example (try it yourself)
<style>
img{
border: 2px solid red;
border-radius:5px;
padding:10px;
opacity:0.3;
}
</style>
Responsive Image
• It automatically adjusts to fit on the screen size.
• It is used to adjust the image to the specified box
automatically.
Property Description
set all the padding properties in one
padding
declaration.
padding-left set left padding of an element.
padding-right set right padding of an element.
padding-top set top padding of an element.
padding-bottom set bottom padding of an element.
<!DOCTYPE html>
<html>
<head>
<style>
p{
padding-top: 50px;
padding-right: 100px;
padding-bottom: 150px;
padding-left: 200px;
}
</style>
</head>
<body>
<p>This is a paragraph with specified paddings.</p>
</body>
</html>
Ruaha Catholic University
CSS Properties
• CSS Colors
• This property is used to set the color of HTML
elements.
• Typically, this property is used to set the
background color or the font color of an element.
• There are three different formats to define a color:
By a color name, example “white”
By hexadecimal value, example “#9000A1”
By RGB, example “rgb(0, 220, 98)”
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
-webkit-animation: myfirst 6s; /* Chrome, Safari, Opera */
}
@keyframes myfirst {
from {background: red;}
to {background: green;}
}
</style>
</head>
<body>
<p><b>Note:</b> The IE 9 and earlier versions don't support CSS3 animation property. </p>
<div></div>
</body>
</html> Ruaha Catholic University
CSS Properties
• Moving Rectangle
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: black;
position: relative;
-webkit-animation: myfirst 5s; /* Chrome, Safari, Opera */
animation: myfirst 5s;
}
@keyframes myfirst {
0% {background:black; left:0px; top:0px;}
25% {background:yellow; left:100px; top:0px;}
50% {background:blue; left:300px; top:200px;}
75% {background:green; left:0px; top:200px;}
100% {background:black; left:0px; top:0px;}
}
</style>
</head>
<body>
<p><b>Note:</b> The Internet Explorer 9 and its earlier versions don't support this example.</p>
<div></div>
</body>
</html>
Ruaha Catholic University
CSS Properties
• CSS Grid Layout
• CSS grid is a two-dimensional layout that helps web designers
build complex and flexible web pages.
• The grid container and grid items make up the CSS Grid
Layout model
• Grid Container: The element that houses the grid layout.
• Grid Items: The grid container's child elements.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px); /* 3 columns with 100px width each */
grid-template-rows: repeat(3, 100px); /* 3 rows with 100px height each */
gap: 10px; /* Gap between grid items */
justify-content: center; /* Center the grid horizontally */
align-items: center; /* Center the grid vertically */
height: 100vh; /* Set grid container height to full viewport height */
}
.grid-item {
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
} Ruaha Catholic University
Implementing Responsive Web Design
• Implementing responsive web design involves a combination
of practices and modern techniques to ensure that websites
provide an optimal experience across a variety of devices.
• These includes:
– Media queries: to adapt layouts based on screen size and
device orientation.
– Mobile-first approach: to prioritize design for smaller
screens
– Fluid layouts: to scale gracefully across different devices
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Layout</title>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html> Ruaha Catholic University
Adopting a Mobile-First Approach
<style>
/* Base styles for mobile (default) */
.container {
padding: 10px;
display: flex;
flex-direction: column;
background-color: #f0f0f0;
}
.item {
background-color: #4CAF50;
color: white;
margin: 5px;
padding: 20px;
border-radius: 5px;
text-align: center;
}
/* Large screens */
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}
</style> Ruaha Catholic University
Adopting a Mobile-First Approach
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
</body>
</html> Ruaha Catholic University
Creating Fluid Layouts
<style>
.container {
display: flex; /* Enable flexbox */
flex-wrap: wrap; /* Allow items to wrap onto the next line */
width: 100%; /* Fluid layout */
max-width: 1200px; /* Maximum width for larger screens */
margin: 0 auto; /* Center container */
}
.item {
flex: 1 1 100%; /* Allow items to grow and shrink */
padding: 20px;
box-sizing: border-box; /* Ensure padding doesn't affect width */
background-color: #f0f0f0; /* Light background color */
border: 1px solid #ccc; /* Light border */
margin: 10px; /* Space between items */
text-align: center; /* Center text */
}