Welcome to the CSS Website Layout lesson, where we'll learn how to structure and style your web pages using Cascading Style Sheets (CSS). By the end of this tutorial, you'll be able to create visually appealing and well-organized websites!
CSS is a styling language used for designing web pages. It separates the design of a website from its structure, allowing for cleaner and easier-to-manage code.
In CSS, selectors are used to target HTML elements to apply styles.
selector {
property: value;
}Identifies a unique element using the # symbol.
<div id="unique-id">This is a unique element.</div>
#unique-id {
color: red;
}Styles multiple elements with the same class using the . symbol.
<div class="my-class">This element has the class "my-class".</div>
<div class="my-class">This element also has the class "my-class".</div>
.my-class {
font-weight: bold;
}The Box Model is a fundamental concept in web development that helps visualize HTML elements as boxes with specific parts: margin, border, padding, and the actual content (content).
-------------------------------------------
| margin (outside) |
| |
| border |
| |
| padding |
| |
| content |
| |
-------------------------------------------
CSS provides various positioning methods to control the layout of elements.
Normal flow (static): Elements are positioned according to the normal document flow.
Relative positioning: Elements are positioned relative to their original positions.
#my-element {
position: relative;
top: 20px;
left: 30px;
}#my-element {
position: absolute;
top: 20px;
left: 30px;
}Floats allow you to move elements to the left or right, creating multi-column layouts.
#my-element {
float: left;
}Flexbox is a modern layout method that provides a more flexible and responsive way to arrange elements.
.flex-container {
display: flex;
}Grid is another modern layout method that enables creating complex and responsive layouts easily.
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}Which CSS selector is used to style multiple elements with the same class?
Apply your newfound skills by creating a simple multi-column layout using floats and absolute positioning.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Layout Practice</title>
<style>
.container {
background-color: #f2f2f2;
padding: 20px;
}
.box {
width: 200px;
height: 200px;
margin: 10px;
float: left;
background-color: #ddd;
}
.box-1 {
background-color: red;
}
.box-2 {
background-color: green;
}
.box-3 {
background-color: blue;
position: absolute;
top: 100px;
left: 300px;
}
</style>
</head>
<body>
<div class="container">
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2</div>
<div class="box box-3">Box 3</div>
</div>
</body>
</html>This code will create a simple web page with three boxes, one positioned absolutely, and the other two floating left. Try modifying the code to better understand how these properties work! 🎉
Happy coding! 👩💻💻