Week 12
Week 12
1. CSS Basics
Cascading Style Sheets (CSS) is a stylesheet language used to describe the
presentation of a document written in HTML or XML. CSS describes how elements
should be rendered on screen, on paper, in speech, or on other media.
Syntax
CSS has a simple syntax and uses a number of English keywords to specify the names
of various style properties.
A CSS rule consists of a selector and a declaration block:
css
selector {
property: value;
property: value;
}
- The selector points to the HTML element you want to style
- The declaration block contains one or more declarations separated by semicolons
- Each declaration includes a CSS property name and a value, separated by a colon
For example:
css
h1 {
color: blue;
font-size: 12px;
}
Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
Here are the main types of selectors:
1. Element Selector: Selects all elements with the specified element name.
css
p{
color: red;
}
5. Grouping Selector: Selects all the HTML elements with the same style definitions.
css
h1, h2, p {
text-align: center;
color: red;
}
7. Child Selector: Selects all elements that are the direct children of a specified
element.
css
div > p {
background-color: yellow;
}
Properties
CSS properties are used to specify the styling of an element. There are numerous CSS
properties, but here are some of the most commonly used ones:
1. Color Properties:
- `color`: Sets the color of text
- `background-color`: Sets the background color of an element
2. Font Properties:
- `font-family`: Specifies the font for text
- `font-size`: Sets the size of the font
- `font-weight`: Sets how thick or thin characters in text should be displayed
3. Text Properties:
- `text-align`: Specifies the horizontal alignment of text
- `text-decoration`: Specifies the decoration added to text
4. Box Model Properties:
- `width`: Sets the width of an element
- `height`: Sets the height of an element
- `padding`: Sets the padding (space inside the element)
- `margin`: Sets the margin (space outside the element)
- `border`: Sets the border of an element
5. Display Properties:
- `display`: Specifies how an element should be displayed
- `position`: Specifies the type of positioning method used for an element
6. Flexbox Properties:
- `flex-direction`: Specifies the direction of the flexible items
- `justify-content`: Aligns the flexible container's items when the items do not use all
available space on the main-axis
7. Grid Properties:
- `grid-template-columns`: Specifies the number (and the widths) of columns in a
grid layout
- `grid-template-rows`: Specifies the number (and the heights) of the rows in a grid
layout
- Content - The content of the box, where text and images appear
- Padding - Clears an area around the content (inside the box)
- Border - A border that goes around the padding and content
- Margin - Clears an area outside the border
Here's a visual representation:
Padding
Padding is the space between the content and the border. You can set padding for all
four sides of an element:
css
div {
padding-top: 20px;
padding-right: 10px;
padding-bottom: 20px;
padding-left: 10px;
}
Margins
Margins are the space outside the border. Like padding, you can set margins for all
four sides:
css
div {
margin-top: 20px;
margin-right: 10px;
margin-bottom: 20px;
margin-left: 10px;
}
Borders
Borders go around the padding and content. The `border` property is a shorthand for:
- `border-width`
- `border-style` (required)
- `border-color`
css
div {
border: 2px solid black;
}
You can also set these individually:
css
div {
border-width: 2px;
border-style: solid;
border-color: black;
}
3. Typography in CSS
Typography plays a crucial role in web design, affecting readability, accessibility, and
overall user experience. CSS provides a wide range of properties to control text
appearance.
Font Properties
Text Properties
1. color: Sets the color of text.
css
p{
color: #333333;
}
2. text-align: Specifies the horizontal alignment of text.
css
h1 {
text-align: center;
}
3. text-decoration: Specifies the decoration added to text.
css
a{
text-decoration: none;
}
4. text-transform: Controls the capitalization of text.
css
h2 {
text-transform: uppercase;
}
5. letter-spacing: Increases or decreases the space between characters in text.
css
h1 {
letter-spacing: 2px;
}
6. word-spacing: Increases or decreases the white space between words.
css
p{
word-spacing: 4px;
}
7. text-shadow: Adds shadow to text.
css
h1 {
text-shadow: 2px 2px 4px #000000;
}
Using these properties effectively can greatly enhance the readability and visual
appeal of your web pages. Remember to consider accessibility when choosing fonts
and colors, ensuring sufficient contrast and readability for all users.
4. CSS Layouts
CSS provides powerful tools for creating complex layouts. Two of the most popular
and powerful layout systems in CSS are Flexbox and Grid.
Flexbox
Flexbox, or the Flexible Box Layout, provides an efficient way to layout, align, and
distribute space among items in a container, even when their size is unknown or
dynamic.
Example:
css
.container {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
}
.item {
flex: 1 1 auto;
}
Grid
CSS Grid Layout is a two-dimensional layout system that allows you to lay out
content in rows and columns.
Key concepts of Grid:
1. Grid Container: The element on which `display: grid` is applied.
2. Grid Items: The children of the grid container.
3. Grid Lines: The dividing lines that make up the structure of the grid.
4. Grid Tracks: The space between two adjacent grid lines (rows or columns).
5. Grid Cell: The space between four grid lines.
6. Grid Area: The total space surrounded by four grid lines.
Basic usage:
css
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
Important Grid properties:
1. For the container:
- `grid-template-columns`: Defines the columns of the grid.
- `grid-template-rows`: Defines the rows of the grid.
- `grid-template-areas`: Defines grid template areas.
- `grid-gap`: Sets the gap between grid items.
2. For the items:
- `grid-column`: Specifies which column(s) the item will span.
- `grid-row`: Specifies which row(s) the item will span.
- `grid-area`: Specifies a grid item's size and location in a grid.
Example:
css
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
grid-gap: 20px;
}
.item1 {
grid-column: 1 / 3;
grid-row: 1;
}
.item2 {
grid-column: 3;
grid-row: 1 / 3;
}
Both Flexbox and Grid are powerful tools for creating layouts. Flexbox is generally
better for one-dimensional layouts (either rows or columns), while Grid excels at two-
dimensional layouts (rows and columns together). Often, the best designs use a
combination of both.
5. Responsive Design
Responsive web design is an approach to web design that makes web pages render
well on a variety of devices and window or screen sizes. It's a crucial aspect of
modern web development.
Media Queries
Media queries are a key component of responsive design. They allow you to apply
CSS styles based on device characteristics, such as width, height, or orientation.
Basic syntax:
css
@media screen and (max-width: 600px) {
.class {
/ styles to apply when screen width is 600px or less /
}
}
Common breakpoints:
- Mobile: 320px - 480px
- Tablet: 481px - 768px
- Desktop: 769px and above
Example:
css
/ Base styles /
body {
font-size: 16px;
}
/ Tablet styles /
@media screen and (min-width: 481px) and (max-width: 768px) {
body {
font-size: 18px;
}
}
/ Desktop styles /
@media screen and (min-width: 769px) {
body {
font-size: 20px;
}
}
Flexible Layouts
Flexible layouts use relative units like percentages, em, or rem instead of fixed units
like pixels. This allows elements to resize based on the viewport size.
Example:
css
.container {
width: 80%;
max-width: 1200px;
margin: 0 auto;
}
.column {
width: 50%;
float: left;
}
Responsive Images
Images play a crucial role in web design, and it's important to make them responsive
as well.
1. Using max-width:
css
img {
max-width: 100%;
height: auto;
}
This ensures that images never exceed their container's width while maintaining
their aspect ratio.
2. Picture Element: HTML5 introduced the `<picture>` element, which allows you to
specify different images for different screen sizes:
html
<picture>
<source media="(min-width: 650px)" srcset="img_large.jpg">
<source media="(min-width: 465px)" srcset="img_medium.jpg">
<img src="img_small.jpg" alt="Image description" style="width:auto;">
</picture>
3. Background Images: For background images, you can use media queries to change
the image based on screen size:
css
.header {
background-image: url('small-image.jpg');
}
@media screen and (min-width: 768px) {
.header {
background-image: url('large-image.jpg');
}
}
html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
This ensures that the browser renders the width of the page at the width of its own
screen.
Mobile-First Approach
A popular strategy in responsive design is the "mobile-first" approach. This means
designing for mobile devices first, then progressively enhancing the design for larger
screens.
Example:
css
/ Base styles for mobile /
.container {
width: 100%;
padding: 10px;
}
/ Tablet styles /
@media screen and (min-width: 768px) {
.container {
width: 750px;
margin: 0 auto;
}
}
/ Desktop styles /
@media screen and (min-width: 1024px) {
.container {
width: 960px;
}
}
This approach ensures that the most essential content and functionality are available to
all users, regardless of their device.