Css Tutorial
Css Tutorial
CSS is Cascading style sheets is the language we use to style an HTML document.CSS
describes how HTML elements should be displayed. CSS saves a lot of work. It can control
the layout of multiple web pages all at once
CSS Syntax:
Example
In this example all <p> elements will be center-aligned, with a red text color:
p{
color: red;
text-align: center;
}
p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
Inline CSS
Internal or Embedded CSS
External CSS
1. Inline CSS : Inline CSS involves applying styles directly to individual HTML elements
using the style attribute. This method allows for specific styling of elements within the
HTML document, overriding any external or internal styles.
Ex:
<p style="color:#009900;
font-size:50px;
font-style:italic;
text-align:center;"> Inline CSS </p>
2. Internal or Embedded CSS: Place the CSS styles within a <style> tag
inside the HTML file, usually inside the <head> section.
<html>
<head>
<style>
h3 {
color: green;
}
</style>
</head>
<body>
<h3>Welcome To CSS tutorial </h3>
<p>This is SDC Lab </p>
</body>
</html>
3. External CSS :
External CSS contains separate CSS files that contain only style properties with the help of
tag attributes (For example class, id, heading, … etc). CSS property is written in a separate
file with a .css extension and should be linked to the HTML document using a link tag. It
means that, for each element, style can be set only once and will be applied across web
pages.
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<p>CSS Tutorial</p>
</body>
</html>
CSS Selectors
CSS selectors are used to target HTML elements on your pages, allowing you to apply
styles based on their ID, class, type attributes, and more.
1. Basic Selectors
Basic selectors in CSS are simple tools used to target specific HTML
elements for styling. These include selecting by element name (e.g., h1),
class (.class Name), ID (#idName), or universally (* for all elements).
1. Universal Selector (*): Selects all elements on the page and applies
the same style universally. For example, setting the font color for every
element.
<html>
<head>
<style>
* {
color: red;
}
</style>
</head>
<body>
<h1>Header Text</h1>
<p>Paragraph Text</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
border: 2px solid green;
background-color: beige;
}
p {
background-color: yellow;
}
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.country {
color: white;
padding: 10px;
}
.china {
background-color: black;
}
.india {
background-color: blue;
}
.usa {
background-color: red;
}
center {
padding: 20px;
}
</style>
</head>
<body>
<center>
<h2 class="country china">CHINA</h2>
<h2 class="country india">INDIA</h2>
<h2 class="country usa">UNITED STATES</h2>
</center>
</body>
</html>
To select an element with a specific id, write a hash (#) character, followed
by the id of the element.
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
CSS Text
Text Color and Background Color
Example
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
<style>
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
</style>
</head>
<body>
<h1>Hi i am using CSS test property</h1>
<div>
<h2> Hi i am not using h1</h2>
</div>
<div>
<h1> Hi i am using h1</h1>
</div>
</body>
</html>
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
The CSS box model is essentially a box that wraps around every HTML
element. It consists of: content, padding, borders and margins. The image
below illustrates the box model:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is
transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is
transparent
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of:
borders, padding, margins, and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and a 15px
green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.</div>
</body>
</html>
Solution:
CSS Flexbox
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: #f2f2f2;
padding: 10px;
}
.flex-item {
background-color: #4CAF50;
color: white;
margin: 5px;
padding: 20px;
text-align: center;
flex: 1;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
</body>
</html>
Flexbox Components
Flex Container: The parent div containing various divisions.
Flex Items: The items inside the container div.
Flexbox Axes
Flexbox operates on two axes:
1. Main Axis: Runs from left to right by default.
2. Cross Axis: Perpendicular to the main axis, runs from top to bottom.
1. Main Axis:
Main Start: The start of the main axis.
Main Size: The length between Main Start and Main End.
Main End: The endpoint of the main axis.
2. Cross Axis:
The cross axis will be perpendicular to the main axis.
Cross Start: The start of the cross axis.
Cross Size: The length between Cross Start and Cross End.
Cross End: The endpoint of the cross axis.
<head>
<style>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #4CAF50;
padding: 10px;
.navbar a {
color: white;
text-decoration: none;
.navbar a:hover {
background-color: #45a049;
</style>
</head>
<body>
<div class="navbar">
<a href="#home">Home</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</div>
</body>
</html>
In this example,
The .navbar uses display: flex with justify-content: space-between to
spread navigation links evenly and align items horizontally.
Hover effects are added to each or better interactivity, creating a
simple yet effective navigation bar.