Cascading Style Sheets
p {
color: green;
}
.intro {
font-size: 20px;
}
#main-heading {
text-align: center;
}
CSS properties to style text include font-family
, font-size
, font-weight
, text-align
, and text-decoration
.
p {
font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;
text-align: justify;
text-decoration: underline;
}
Goal: Introduce basic properties and demonstrate how they impact different elements.
color
– Sets the text color.font-size
– Adjusts the text size.font-family
– Sets the font.text-align
– Aligns text (e.g., center, left, right).
h1 {
color: teal;
font-size: 2em;
text-align: center;
}
p {
font-family: Georgia, serif;
line-height: 1.5;
color: gray;
}
Apply quick styles directly within HTML tags to see how CSS affects the elements instantly.
<h1 style="color: navy; text-align: center;">Your Name</h1>
<p style="color: gray; font-size: 16px;">A short bio paragraph about yourself.</p>
Here, we styled the <h1>
and <p>
tags by adding the style
attribute directly to them.
color: navy;
sets the text color.text-align: center;
centers the text.font-size: 16px;
adjusts the text size.<head>
Apply CSS within the HTML <head>
to style elements without repeating code.
<head>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: darkblue;
text-align: center;
}
p {
color: gray;
font-size: 16px;
}
</style>
</head>
Explanation:
<style>
tag within the HTML <head>
.body
selector applies a font to the entire page.h1
styles all headings in dark blue and centers them.p
makes all paragraph text gray and slightly larger.Advantages of Internal CSS:
Goal: Move CSS into an external file to simplify and organize code for larger projects.
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
color: #333;
}
h1 {
color: darkblue;
text-align: center;
}
p {
font-size: 1em;
color: #555;
}
<head>
<link rel="stylesheet" href="styles.css">
</head>
Explanation: External CSS is ideal for bigger projects since it keeps the HTML clean and easy to manage.
Change the text color of the element with id="para1" to red.
<style>
# {
: red;
}
</style>
<body>
<h1>This is a heading</h1>
<p id="para1">This is a paragraph</p>
</body>
Goal: Link an external style sheet with the URL "mystyle.css" to the HTML document.
<head>
</head>
Change the background color of the HTML document to red.
<style>
{
}
</style>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
</body>
Set the page's text to green using inline style.
<body>
<h1>This is a heading</h1>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
</body>
Goal: Correct the CSS code to set the color of paragraphs to blue.
<style>
</style>
padding
– Adds space inside the border.margin
– Adds space outside the element.border
– Adds a border around the element.
div {
margin: 20px;
border: 2px solid #000;
padding: 10px;
width: 100px;
height: 100px;
}
div {
box-sizing: border-box;
}
CSS properties display
and visibility
control the layout and visibility of elements.
div {
display: block;
}
span {
display: inline;
}
.hidden {
display: none;
}
.visible {
visibility: visible;
}
.invisible {
visibility: hidden;
}
CSS positioning properties include static
, relative
, absolute
, fixed
, and sticky
.
.relative {
position: relative;
top: 10px;
left: 20px;
}
.absolute {
position: absolute;
top: 50px;
left: 100px;
}
.fixed {
position: fixed;
bottom: 0;
right: 0;
}
.sticky {
position: -webkit-sticky; /* For Safari */
position: sticky;
top: 0;
}
Flexbox is a layout model that provides an easy and clean way to arrange items within a container.
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
display: flex;
justify-content: space-around;
align-items: center;
}
.item {
flex: 1;
padding: 10px;
}
CSS Grid Layout is a two-dimensional layout system for the web
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.grid-item {
background-color: #ddd;
padding: 20px;
text-align: center;
}
Any questions?