3 CSS Basics
3 CSS Basics
Properties specify
what aspect of the
Property: font-
element to style. Property: color
size
• Value: red • Value: 16px
Values determine the
specific style to apply
to the property.
CSS Declaration Block
Selects elements
based on their HTML
tag names. h2 {
color: #ff6600;
It targets all instances }
of the specified HTML
tag.
Class Selector (.class)
Selects elements by
their class attribute.
.button {
background-color: #007bff;
}
Can be applied to
multiple elements
with the same class.
ID Selector (#id)
Selects a specific
element based on its
id attribute. #header {
font-size: 32px;
}
IDs should be unique
on a page, so it targets
only one element.
Attribute Selector ([attribute="value"])
Internal
Inline CSS (Embedded)
CSS
External CSS
Inline CSS
<div> is a block-level
container element.
<div id="header">
<h1>My Website</h1>
It is used to group and style <nav>...</nav>
larger sections of content. </div>
Define font h2 {
/* Font family */
font-family: "Arial", sans-serif;
img {
border: border-width border-style border-color /* Border style and color */
border-top, border-right, border-bottom, border: 2px solid #007bff;
border-left }
border-style:
none|hidden|dotted|dashed|solid|double|gro
ove|ridge|inset|outset
Background
Set body {
/* Background image */
background-image:
background url("background.jpg");
/* Background color */
images and }
background-color: #f2f2f2;
colors.
Margin & padding
• Inner area of an
Padding element (inside
the border)
• Inner area of an
Margin element (outside
the border)
Margin
p { p {
margin-top: 100px; margin: 100px 150px
margin-right: 150px; 100px 80px;
margin-bottom: 100px; }
margin-left: 80px; /*shorthand property
} margin*/
Padding
p { p {
padding-top: 50px; padding: 50px 30px
padding-right: 30px; 50px 80px;
padding-bottom: 50px; }
/*shorthand property
padding-left: 80px;
padding*/
}
Width and Height
indentation }
ul {
as needed. }
list-style: square;
List Item Styles
li {
Style the list /* Change text color */
color: #333;
items /* Make text bold */
font-weight: bold; /*}
themselves for
consistent
presentation.
List Hover Effects
ul:hover li {
Add hover /* Change background on
hover */
effects for background-color:
#f0f0f0;
interactive /* Change cursor on hover
*/
styling, including td {
font-family: Arial, sans-
font family, size, serif;
font-size: 14px;
and color within }
color: #333;
table cells.
Table Width and Alignment
table {
Adjust the width: 100%;
}
width of table
th, td {
columns and text-align: center;
align content }
within cells
Striped Tables
classes.
Hover Effects
Apply hover
effects to tr:hover {
background-color:
highlight rows #ffcc00;
}
when a user
hovers over them.
Styling Form
HTML
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Your name">
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your email">
elements to input[type="email"] {
background-color: #e0e0e0;
improve visual }
appeal
Text and Font Styles
feedback
Styling Labels
Customize the
appearance of label {
font-weight: bold;
labels to color: #333;
improve the }
form's structure.
Customizing Buttons
button,
Style buttons to input[type="submit"] {
background-color: #333;
make them color: #fff;
padding: 10px 20px;
more visually border: none;
appealing and }
cursor: pointer;
user-friendly.
Form Layout
Organize form
form {
elements and width: 250px;
apply layout margin: 0 auto;
text-align: center;
styles to improve }
user experience.
CSS Combinators: Selecting
Elements with Precision
Introduction to CSS Combinators
Combinators can be
nav > ul li a {
combined to create /* Selects <a> elements
more complex selectors. that are descendants of
<li> elements, which are
direct children of <ul>
This allows for precise elements within <nav> */
element selection within text-decoration: none;
your HTML structure. }
CSS combinators
Check src4/04_activity2/instructions.txt
Exercice: Recipe website
Check src4/05_exercice/instructions.txt
Challenge: CSS Diner