SDC Day 2 Workbook Css
SDC Day 2 Workbook Css
Pages
Table of Contents
. Hands-On Exercises
. Step-by-Step Solutions
User: Alex, a computer science undergraduate student who has successfully built the
basic structure of their personal portfolio website using HTML.
The Challenge
Alex has a functional HTML portfolio website, but it looks quite plain. All the text is black,
the background is white, and everything is aligned to the left. It lacks visual appeal and
doesn't stand out. Alex wants to make the website look professional and engaging for
potential internship employers.
Alex approaches Expert again, eager to learn how to add style and flair to their website.
The goal is to transform the basic HTML structure into a visually appealing and well-
designed online portfolio.
Alex: Expert, my HTML portfolio is working, but it looks... well, very basic. It's just plain
text on a white background. How can I make it look good? I want to add colors, change
fonts, and arrange things nicely.
Expert: That's a fantastic next step, Alex! This is where CSS comes into play. CSS stands for
Cascading Style Sheets. If HTML is the skeleton of your website, CSS is the skin, clothes,
and makeup. It controls the presentation, layout, and appearance of your web pages.
Expert: Exactly! With CSS, you can control colors, fonts, spacing, positioning, and even
create animations. It allows you to separate the content (HTML) from its presentation
(CSS), which makes your code cleaner, easier to maintain, and more flexible.
Alex: That sounds powerful! But how do I actually use CSS? Where do I write it, and how
does it connect to my HTML?
Expert: Great question! There are three main ways to include CSS in your HTML
document:
. Inline CSS: Applying styles directly to an HTML element using the style attribute.
This is generally discouraged for larger projects because it mixes content and
presentation.
. Internal CSS: Placing CSS rules within a <style> tag in the <head> section of your
HTML document. This is useful for single-page styles.
. External CSS: Linking an external .css file to your HTML document. This is the
most common and recommended method for larger websites, as it allows you to
style multiple HTML pages from a single CSS file.
Expert: For sure! It promotes reusability and makes your website easier to manage.
Imagine if you had pages on your portfolio and wanted to change the main heading
color. With external CSS, you'd change it in one file, and it would update across all
pages. With inline or internal CSS, you'd have to change it times!
Alex: Wow, that's a huge time-saver. So, how do I link an external CSS file?
Expert: You use the <link> tag inside the <head> section of your HTML. It looks like this:
<head>
<link rel="stylesheet" href="styles.css">
</head>
Expert: The rel="stylesheet" attribute tells the browser that this is a stylesheet, and
href="styles.css" points to the location of your CSS file. You'd typically name your
main CSS file styles.css or main.css .
Alex: Okay, I think I understand how to connect them. But once I have my styles.css
file, what do I write inside it? What does CSS code look like?
Expert: Excellent question! Let's dive into the basic syntax of CSS rules. This is where you
tell the browser what to style and how to style it.
Expert: A CSS rule consists of a selector and a declaration block. The selector points to the
HTML element you want to style, and 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.
h1 {
color: blue; /* This is a declaration */
font-size: 24px; /* This is another declaration */
}
p {
text-align: center;
color: black;
}
The entire block within the curly braces {} is the declaration block.
Alex: So, I pick an HTML element, and then I tell CSS what properties of that element I
want to change and what values I want them to have?
Expert: Exactly! You've got the core idea. Now, let's talk more about selectors, because
they are incredibly powerful for targeting specific elements.
Expert: Selectors are how you tell CSS which HTML elements to apply styles to. There are
many types of selectors, but let's focus on the most common ones:
. Element Selector: Selects HTML elements based on their name (e.g., p , h1 , body ).
css p { color: green; }
. Universal Selector: Selects all HTML elements on the page. It's represented by an
asterisk ( * ). ```css
{ margin: ; padding: ; } ``` (This is often used to reset default browser styles.)
Expert: Every HTML element on a web page can be thought of as a rectangular box. This
box is governed by the CSS Box Model, which describes how element dimensions are
calculated, including content, padding, borders, and margins.
Expert: Exactly! And understanding how these boxes interact is key to controlling your
page layout. The box model consists of four layers, from the innermost to the outermost:
. Content: The actual content of the element, like text or images. Its dimensions are
determined by width and height properties.
. Padding: The space between the content and the border. It pushes the border away
from the content. You can control padding with padding-top , padding-right ,
padding-bottom , padding-left , or the shorthand padding .
. Border: A line that goes around the padding and content. You can style its width ,
style , and color .
. Margin: The space outside the border, separating the element from other elements.
It pushes other elements away from the border. You can control margins with
margin-top , margin-right , margin-bottom , margin-left , or the shorthand
margin .
Expert: Precisely! That's a common point of confusion for beginners, but you've got it.
The default box-sizing property is content-box , meaning padding and border are
added outside the specified width and height . However, many developers prefer
border-box , where width and height include padding and border, making layout
calculations more intuitive.
/* Example of box-sizing */
.my-box {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid black;
margin: 10px;
box-sizing: border-box; /* This makes width/height include padding and border */
}
Alex: That box-sizing: border-box; sounds really useful. What about arranging these
boxes on the page? Like, if I want things side-by-side or in a grid?
Expert: Excellent question, Alex! That brings us to modern CSS layout techniques: Flexbox
and CSS Grid. These are incredibly powerful tools for creating complex and responsive
layouts.
CSS Flexbox: One-Dimensional Layout
Expert: Flexbox, or the Flexible Box Module, is designed for one-dimensional layout. This
means you can arrange items in a single row or a single column. It's perfect for
distributing space among items in an interface and aligning them.
Alex: So, if I have a navigation bar and I want the links to be evenly spaced horizontally,
Flexbox would be good for that?
parent) and its direct children become flex items. Then you can use properties like
justify-content (for alignment along the main axis) and align-items (for alignment
along the cross axis).
<div class="flex-container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
.flex-container {
display: flex;
justify-content: space-around; /* Distributes items evenly with space around
them */
align-items: center; /* Aligns items vertically in the center */
height: 100px;
border: 1px solid black;
}
.flex-container div {
padding: 10px;
background-color: lightblue;
border: 1px solid blue;
}
Expert: Flexbox is incredibly versatile for component-level layouts. Take a look at this
visual guide to Flexbox properties:
CSS Grid: Two-Dimensional Layout
Expert: While Flexbox is for one-dimensional layouts (rows or columns), CSS Grid Layout
is for two-dimensional layouts. It allows you to design complex, responsive web page
layouts with rows and columns simultaneously.
Alex: So, if I want to create a full page layout with a header, sidebar, main content area,
and footer, Grid would be better?
Expert: Absolutely! Grid is perfect for overall page layouts. You define rows and columns
on a container element using properties like grid-template-columns and grid-
template-rows , and then you place items into those grid cells.
<div class="grid-container">
<header>Header</header>
<nav>Sidebar</nav>
<main>Main Content</main>
<footer>Footer</footer>
</div>
.grid-container {
display: grid;
grid-template-columns: 1fr 3fr; /* One column takes 1 fraction, the other 3
fractions */
grid-template-rows: auto 1fr auto; /* Header and footer auto-size, main content
takes remaining space */
gap: 10px; /* Space between grid items */
height: 300px;
}
.grid-container header,
.grid-container nav,
.grid-container main,
.grid-container footer {
background-color: lightgray;
padding: 10px;
border: 1px solid gray;
}
.grid-container header,
.grid-container footer {
grid-column: 1 / -1; /* Spans across all columns */
}
Expert: CSS Grid gives you precise control over where elements are placed on your page.
Here's a visual guide to CSS Grid:
Alex: This is amazing! It feels like I can finally build any layout I can imagine. So, Flexbox
for components and Grid for overall page structure?
Expert: That's a great rule of thumb, Alex! They often work together beautifully. Now that
you have a solid understanding of CSS syntax, selectors, the box model, Flexbox, and Grid,
are you ready to apply these concepts to your portfolio?
Your goal is to create a styles.css file and link it to your index.html (from the HTML
workbook). Then, apply some basic styling:
Now, let's enhance Alex's index.html by adding a simple navigation bar and styling it
with Flexbox. Modify your index.html and styles.css :
In index.html :
. Inside the <nav> section, add an unordered list ( <ul> ) with three list items ( <li> ).
. Each list item should contain an anchor tag ( <a> ) with placeholder links (e.g.,
href="#" ). The text for the links should be "Home", "Projects", and "Contact".
In styles.css :
padding: 0;
margin: 0;
align-items: center;
text-decoration: none;
Step-by-Step Solutions
Expert: Alright, Alex, let's start with Exercise . First, you need to create your styles.css
file and link it to your index.html . You remember how to link it, right?
Expert: Perfect! Now, open your styles.css file. The first task is to set the background
color of the <body> . How would you write that CSS rule?
Expert: Excellent. Next, let's style the <h1> . We need to set its color, center it, and add
padding and margin. What do you think comes next?
Expert: Fantastic, Alex! You've correctly applied all the properties. The margin: 0 auto;
is a classic trick to center block-level elements horizontally when they have a defined
width . Here's what your styles.css should look like:
/* styles.css */
body {
background-color: #f4f4f4;
}
h1 {
color: #333366;
text-align: center;
padding: 20px;
margin-bottom: 30px;
}
p {
font-family: Arial, sans-serif;
font-size: 16px;
width: 80%;
margin: 0 auto;
border: 1px solid #cccccc;
padding: 15px;
}
Expert: And remember to ensure your index.html has the <link> tag in the <head>
and a basic <h1> and <p> in the <body> .
Alex: It looks so much better already! The centering and the border make a huge
difference.
Solution for Exercise : Implementing Flexbox for Navigation
Expert: Great! Now for Exercise , we're going to add a navigation bar and style it with
Flexbox. First, let's update your index.html to include the navigation links. What would
you put inside the <nav> tags?
Alex: I'd add an unordered list with three list items, each containing an anchor tag:
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
Expert: Perfect! Now, let's jump into styles.css to make that navigation bar look good
with Flexbox. What's the first thing you'd do to the <ul> to remove its default styling?
Expert: Excellent. Now, to make the list items behave like flex items and distribute
themselves, what property do you apply to the <ul> within the <nav> ?
Expert: Spot on! And what about the background color and padding for the <nav> itself?
Expert: You're doing great, Alex! Finally, how would you style the individual links ( <a>
tags) to make them white, remove underlines, and add some padding?
Expert: Absolutely correct! The display: block; is a subtle but important touch,
making the entire padded area of the link clickable. Here's the complete styles.css for
Exercise :
/* styles.css (for Exercise 2) */
body {
background-color: #f4f4f4;
}
h1 {
color: #333366;
text-align: center;
padding: 20px;
margin-bottom: 30px;
}
p {
font-family: Arial, sans-serif;
font-size: 16px;
width: 80%;
margin: 0 auto;
border: 1px solid #cccccc;
padding: 15px;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
justify-content: space-around;
align-items: center;
}
nav {
background-color: #007bff;
padding: 10px 0;
}
nav ul li a {
color: white;
text-decoration: none;
padding: 5px 15px;
display: block;
}
Alex: It's incredible! The navigation bar looks so professional, and it was much easier than
I thought with Flexbox. I'm really starting to see how CSS makes websites come alive.
Expert: Beyond the basics, CSS offers a vast array of properties to fine-tune the
appearance of your elements. Let's explore a few that can significantly enhance your
design:
Expert: The font-weight property sets how thick or thin characters in text should be
displayed. You can use keywords like normal , bold , lighter , bolder , or numerical
values from 100 to 900 (where 400 is normal and 700 is bold ).
Alex: So, font-weight: bold; makes text bold, but I can also use numbers?
Expert: Exactly! Numbers give you more granular control, especially with fonts that offer
many weight variations. For example:
.my-heading {
font-weight: 700; /* Equivalent to bold */
}
.light-text {
font-weight: 300; /* Lighter than normal */
}
Expert: The text-shadow property adds shadow to text. It takes values for horizontal
offset, vertical offset, blur radius, and color. You can even apply multiple shadows for cool
effects.
Alex: Like a drop shadow in a design program?
h1 {
text-shadow: 2px 2px 4px #000000; /* Horizontal, Vertical, Blur, Color */
}
.neon-text {
color: #fff;
text-shadow:
0 0 7px #fff,
0 0 10px #fff,
0 0 21px #fff,
0 0 42px #0fa,
0 0 82px #0fa,
0 0 92px #0fa,
0 0 102px #0fa,
0 0 151px #0fa;
}
Expert: The border-radius property allows you to round the corners of an element's
border. You can apply a single value for all corners, or separate values for each corner
(top-left, top-right, bottom-right, bottom-left).
Alex: So I can make a square box into a circle?
Expert: You absolutely can! If you have a square element, setting border-radius: 50%;
will turn it into a perfect circle. For rectangles, it creates rounded rectangles.
.rounded-box {
border-radius: 10px; /* Applies to all corners */
border: 2px solid blue;
padding: 10px;
}
.pill-button {
border-radius: 25px; /* Creates a pill shape for a rectangular button */
background-color: #007bff;
color: white;
padding: 10px 20px;
}
.circle {
width: 100px;
height: 100px;
background-color: red;
border-radius: 50%; /* Makes it a circle */
}
Expert: These properties, while seemingly small, can dramatically change the aesthetic of
your website. Ready to dive into making your website adapt to different screen sizes?
Alex: Yes! I've heard about responsive design, and it sounds really important for mobile
phones.
Expert: You\'re right, Alex. With so many different devices accessing the internet today ‒
from tiny smartwatches to large desktop monitors ‒ responsive web design is no longer
optional; it\'s essential. Responsive design is an approach to web design that makes web
pages render well on a variety of devices and window or screen sizes.
Alex: How does a website know what size screen it's on?
Expert: That's where Media Queries come in! Media queries are a CSS module that
allows you to apply CSS styles depending on a device's characteristics, such as screen
width, height, orientation, and resolution. They let you write different CSS rules for
different screen sizes.
Expert: The most common use of media queries is to create breakpoints. A breakpoint is
a point at which a website's content and design will adapt to provide the best possible
user experience. For example, you might have one layout for desktops, another for
tablets, and a third for mobile phones.
Here's the basic syntax:
/* Styles for screens wider than 768px (e.g., tablets and desktops) */
body {
font-size: 18px;
}
Expert: The @media rule specifies the type of media (e.g., screen for computer screens)
and conditions (e.g., max-width: 768px ). When these conditions are met, the CSS rules
inside the media query block are applied.
Alex: So, I can change layouts, font sizes, and even hide elements based on screen size?
Expert: Exactly! You can adjust almost any CSS property. This allows you to create a single
website that looks great on any device, rather than building separate versions for mobile
and desktop. It's a cornerstone of modern web development.
Expert: Now, let's talk about making things move and grab attention: CSS Animations!
Expert: CSS animations allow you to create smooth, interactive, and engaging visual
effects without relying on JavaScript. They involve two main components: keyframes and
the animation properties.
Alex: What are keyframes?
Expert: Think of keyframes like the critical poses in a traditional animation. You define
what an element should look like at specific points during the animation (e.g., at %, %,
and % of the animation duration). The browser then smoothly transitions between
these keyframes.
@keyframes slideIn {
0% {
transform: translateX(-100%);
opacity: 0;
}
100% {
transform: translateX(0);
opacity: 1;
}
}
Expert: Once you define your keyframes, you apply the animation to an element using
various animation properties:
animation-fill-mode : What styles are applied to the element before and after the
animation.
Expert: You can combine these into a shorthand animation property. For example, to
make a text element slide in:
h1 {
animation: slideIn 1s ease-out forwards;
}
Alex: So, I can make text appear with a cool effect, or even make images move?
Expert: Absolutely! You can animate almost any CSS property. For images, you might
make them fade in, slide across the screen, or even rotate. For example, a simple image
hover effect:
.my-image {
transition: transform 0.3s ease-in-out; /* Smooth transition for transform
property */
}
.my-image:hover {
transform: scale(1.1); /* Zooms in the image on hover */
}
Expert: This uses transition , which is simpler for single-state changes, while
animation with @keyframes is for more complex, multi-state sequences. Both are
powerful tools for adding dynamic flair to your portfolio.
Alex: This is incredible! I can't wait to try these out. My portfolio is going to look so much
more professional and engaging.
Hands-On Exercises
Let's apply some of the new CSS properties to your index.html and styles.css :
. font-weight : Make the <h1> element even bolder by setting its font-weight to
900 .
. text-shadow : Add a subtle text-shadow to your <h1> element: 2px 2px 5px
rgba(0,0,0,0.5) .
Now, let's make your navigation responsive and add a simple animation:
text-align: center;
font-size: 2em;
margin-top: 50px;
Expert: Alright, Alex, let's enhance your existing styles.css with these new properties.
First, for the <h1> element, how would you make it even bolder and add a text shadow?
h1 {
color: #333366;
text-align: center;
padding: 20px;
margin-bottom: 30px;
font-weight: 900; /* New */
text-shadow: 2px 2px 5px rgba(0,0,0,0.5); /* New */
}
Expert: Perfect! Now, for the <p> element, how would you add rounded corners?
p {
font-family: Arial, sans-serif;
font-size: 16px;
width: 80%;
margin: 0 auto;
border: 1px solid #cccccc;
padding: 15px;
border-radius: 8px; /* New */
}
Expert: Excellent, Alex! You've successfully applied these new properties. Here's the
updated styles.css :
/* styles.css (Updated for Exercise 1) */
body {
background-color: #f4f4f4;
}
h1 {
color: #333366;
text-align: center;
padding: 20px;
margin-bottom: 30px;
font-weight: 900;
text-shadow: 2px 2px 5px rgba(0,0,0,0.5);
}
p {
font-family: Arial, sans-serif;
font-size: 16px;
width: 80%;
margin: 0 auto;
border: 1px solid #cccccc;
padding: 15px;
border-radius: 8px;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
justify-content: space-around;
align-items: center;
}
nav {
background-color: #007bff;
padding: 10px 0;
}
nav ul li a {
color: white;
text-decoration: none;
padding: 5px 15px;
display: block;
}
Alex: The text shadow looks really cool, and the rounded corners make the paragraph
look softer!
Expert: Fantastic! Now for the more advanced Exercise . First, let's make your navigation
responsive using a media query. How would you add the media query and the styles for
smaller screens?
Expert: Excellent! That will make your navigation stack vertically on smaller screens. Now,
let's add the animated text. First, you need to add the div to your index.html . Where
would you put it?
<main>
<p>This is a simple page to showcase my projects and skills.</p>
</main>
<div class="animated-text">My Awesome Projects</div> <!-- New -->
<footer>
<p>© 2025 Alex. All rights reserved.</p>
</footer>
</body>
</html>
Expert: Perfect. Now, in styles.css , how would you define the fadeInUp keyframes
and apply the animation to the .animated-text div?
@keyframes fadeInUp {
0% {
opacity: 0;
transform: translateY(20px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
.animated-text {
animation: fadeInUp 1s ease-out 0.5s forwards;
text-align: center;
font-size: 2em;
margin-top: 50px;
}
Expert: You've nailed it, Alex! The forwards value for animation-fill-mode is
important here, as it ensures the element retains the styles from the last keyframe ( %)
after the animation finishes. Here's the complete updated styles.css and index.html :
/* styles.css (Final for Exercise 2) */
body {
background-color: #f4f4f4;
}
h1 {
color: #333366;
text-align: center;
padding: 20px;
margin-bottom: 30px;
font-weight: 900;
text-shadow: 2px 2px 5px rgba(0,0,0,0.5);
}
p {
font-family: Arial, sans-serif;
font-size: 16px;
width: 80%;
margin: 0 auto;
border: 1px solid #cccccc;
padding: 15px;
border-radius: 8px;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
justify-content: space-around;
align-items: center;
}
nav {
background-color: #007bff;
padding: 10px 0;
}
nav ul li a {
color: white;
text-decoration: none;
padding: 5px 15px;
display: block;
}
@keyframes fadeInUp {
0% {
opacity: 0;
transform: translateY(20px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
.animated-text {
animation: fadeInUp 1s ease-out 0.5s forwards;
text-align: center;
font-size: 2em;
margin-top: 50px;
}
Alex: This is incredible! The responsive navigation is so important, and the animation
adds a really professional touch. I feel like I can build anything now!
Expert: Today, Alex, you've truly elevated your web development skills. You've learned:
What CSS is and its role in controlling the presentation of web pages.
The three ways to include CSS in HTML (inline, internal, external) and why external is
preferred.
The CSS Box Model and how content, padding, border, and margin affect element
dimensions.
The fundamentals of Flexbox for one-dimensional layouts and its key properties.
The fundamentals of CSS Grid for two-dimensional layouts and its key properties.
How to use advanced CSS properties like font-weight , text-shadow , and border-
radius to refine your designs.
The critical importance of responsive web design and how to implement it using CSS
Media Queries to adapt your site to various screen sizes.
Alex: I'm so excited to apply all of this to my actual portfolio. Thank you, Expert, for
guiding me through this!
Expert: You're very welcome, Alex. Your progress has been remarkable. For your
continued learning, I recommend:
. Delving deeper into advanced responsive design techniques, such as min-width vs.
max-width strategies and mobile-first design.
. Starting to think about user experience (UX) and user interface (UI) design principles
to make your visually appealing website also highly usable.
We'll meet again soon to discuss JavaScript, which will add interactivity to your portfolio.
Keep practicing!
References