CSS Exam 2: Box Model and Intermediate (13 Exercises)
CSS Exam 2: Box Model and Intermediate
This exam contains 13 comprehensive exercises. Each exercise includes multiple requirements
that should be completed together in one code snippet.
Exercises 1-10: Beginner |
Exercises 11-13: Intermediate
Exercise 1
Box Model
Set box properties for a div:
- Width 300px and height 200px.
- Padding 20px on all sides.
- Border 5px solid black.
- Margin 50px.
Solution
div {
width: 300px;
height: 200px;
padding: 20px;
border: 5px solid black;
margin: 50px;
}
Exercise 2
Box Sizing
Control how element size is calculated:
- Make all elements (*) use `border-box`.
- Explain in a comment why `border-box` is often preferred over the default `content-box`.
Solution
* {
box-sizing: border-box;
}
/*
border-box includes padding and border in the declared width/height,
which makes layouts easier to reason about.
*/
Exercise 3
Display Property
Change how these elements display:
- Make links (a) behave like block elements (full line).
- Make list items (li) appear next to each other (inline-block).
- Hide elements with class (.hidden) completely (no space reserved).
Solution
a {
display: block;
}
li {
display: inline-block;
}
.hidden {
display: none;
}
Exercise 4
Flexbox Basics (Container)
Create a flex container:
- Make `.container` use Flexbox.
- Set direction to column.
- Allow wrapping if there isn't enough space.
Solution
.container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
Exercise 5
Flexbox Alignment
Inside a flex container, align items:
- Center horizontally (justify-content).
- Center vertically (align-items).
- Make the container fill the viewport height (100vh).
Solution
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Exercise 6
Positioning
Use different positioning techniques:
- Make the nav fixed at the top with width 100%.
- Position .btn absolutely in the bottom-right corner of a relative parent.
Solution
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
.parent {
position: relative;
}
.btn {
position: absolute;
bottom: 0;
right: 0;
}
Exercise 7
Float & Clear
Use floats to layout an image and text:
- Float the image (img) to the left.
- Add 15px right margin to the image.
- Ensure the next element (.clearfix) clears floats (clear: both).
Solution
img {
float: left;
margin-right: 15px;
}
.clearfix {
clear: both;
}
Exercise 8
Overflow
Control content overflow:
- Set a div size to 100x100px.
- Add a scrollbar if content exceeds the size.
- Hide overflow only on the x-axis.
Solution
div {
width: 100px;
height: 100px;
overflow: scroll; /* or auto */
overflow-x: hidden;
}
Exercise 9
Z-Index
Control stacking order:
- You have two overlapping elements (.box1 and .box2).
- Make .box1 appear on top using z-index (make sure position is set).
Solution
.box1 {
position: relative; /* required for z-index */
z-index: 2;
}
.box2 {
position: relative;
z-index: 1;
}
Exercise 10
Units
Use appropriate measurement units:
- Set container width to 50% of the page.
- Set font size to 1.2rem (relative to root).
- Set element width to 30vw (30% of viewport width).
Solution
.container {
width: 50%;
}
p {
font-size: 1.2rem;
}
.box {
width: 30vw;
}
Exercise 11
Responsive Design
Intermediate
Create responsive styles using media queries:
- Set container width to 90% on small screens (under 768px).
- Switch Flexbox direction from row to column on small screens.
- Hide elements with class (.desktop-only) on small screens.
- Use 14px font on small screens and 16px on large screens.
Solution
/* Default styles (large screens) */
.container {
width: 70%;
}
.flex-container {
display: flex;
flex-direction: row;
}
body {
font-size: 16px;
}
/* Media query for small screens */
@media (max-width: 768px) {
.container {
width: 90%;
}
.flex-container {
flex-direction: column;
}
.desktop-only {
display: none;
}
body {
font-size: 14px;
}
}
/* Media query for very large screens */
@media (min-width: 1200px) {
.container {
max-width: 1140px;
margin: 0 auto;
}
}
Exercise 12
Animations & Transitions
Intermediate
Create motion effects:
- Add a smooth background-color transition on hover (0.3s).
- Create an animation named "fadeIn" that goes from opacity 0 to 1.
- Make the animation last 2 seconds and run only once.
- Add a hover scale effect using transform.
Solution
/* Transition for background color */
.button {
background-color: #007bff;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #0056b3;
}
/* Define animation */
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Apply animation */
.fade-element {
animation: fadeIn 2s ease-in-out 1;
}
/* Hover scale effect */
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: scale(1.05);
}
/* Advanced example: rotation animation */
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.spinner {
animation: rotate 2s linear infinite;
}
Exercise 13
CSS Transforms
Intermediate
Use advanced CSS transforms:
- Rotate an element by 45 degrees.
- Skew an element by 10 degrees on the X axis (skewX).
- Translate an element 50px right and 30px down.
- Combine multiple transforms in one line (rotate + scale + translate).
- Use transform-origin to change the transform origin point.
Solution
/* Rotate */
.rotate-element {
transform: rotate(45deg);
}
/* Skew */
.skew-element {
transform: skewX(10deg);
}
/* Translate */
.translate-element {
transform: translate(50px, 30px);
}
/* Combine transforms */
.multi-transform {
transform: rotate(15deg) scale(1.2) translate(20px, 10px);
}
/* Change origin */
.custom-origin {
transform: rotate(45deg);
transform-origin: top left; /* instead of center */
}
/* Advanced example: flip card */
.flip-card {
perspective: 1000px;
}
.flip-card-inner {
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
transform: rotateY(180deg);
}
/* 3D transforms */
.3d-element {
transform: rotateX(30deg) rotateY(45deg) translateZ(50px);
}