CSS3 is the latest version of CSS (Cascading Style Sheets), which is the style sheet language used to describe the presentation and layout of a web page written in HTML or XML. CSS allows web developers to separate content (HTML) from the visual design and layout, which makes websites more maintainable and accessible.
CSS3 is an update to the original CSS specification and introduces a variety of new features and capabilities, including better control over layout, animations, transitions, and responsive design.
CSS3 introduces several new selectors, allowing developers to target HTML elements more precisely.
a[href^="https"] {
color: green; /* Selects links starting with 'https' */
}
li:nth-child(2) {
color: red; /* Selects the second list item */
}
p:not(.special) {
font-size: 16px;
}
::before and ::after.
p::after {
content: " (End of Paragraph)";
}
CSS3 provides enhanced control over layout and presentation through new properties and enhancements to the box model.
box-sizing: This property allows developers to control how width and height are calculated in relation to padding and borders.
div {
box-sizing: border-box;
}
This ensures that padding and border are included in the element's total width and height.
Flexbox: The Flexible Box Layout (Flexbox) is a powerful layout model for creating flexible and responsive layouts. It provides tools to distribute space dynamically and align elements.
.container {
display: flex;
justify-content: space-between;
}
Flexbox can handle complex layouts with ease, including vertical centering and distributing space between elements.
Grid Layout: The CSS Grid Layout system provides a 2D grid-based layout system that allows for more complex designs and precise control over rows and columns.
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
}
CSS3 offers advanced options for controlling the background and borders of elements.
Multiple Backgrounds: You can now apply multiple background images to an element.
div {
background: url('image1.jpg'), url('image2.jpg');
background-position: top left, bottom right;
}
Border Radius: CSS3 introduces the border-radius property, which allows for rounded corners on elements.
div {
border-radius: 15px;
}
Box Shadows: You can apply shadows to elements to create depth or highlight them.
div {
box-shadow: 10px 10px 20px rgba(0,0,0,0.5);
}
Text Shadows: Similarly, you can apply shadows to text.
h1 {
text-shadow: 2px 2px 5px gray;
}
CSS3 provides tools to animate changes in style and create more dynamic, interactive web pages.
Transitions: The transition property allows for smooth transitions when property values change.
div {
transition: all 0.3s ease;
}
div:hover {
background-color: blue;
}
When the user hovers over the div, the background color will change smoothly over 0.3 seconds.
Animations: The @keyframes rule lets you create complex animations with multiple steps.
@keyframes slide {
from {
left: 0;
}
to {
left: 100px;
}
}
div {
position: relative;
animation: slide 2s infinite alternate;
}
CSS3 improves how text is styled and displayed on web pages.
Web Fonts: Using the @font-face rule, you can load custom fonts on a web page, making it easier to use unique typefaces.
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
body {
font-family: 'MyFont', sans-serif;
}
Text Overflow: CSS3 introduces the text-overflow property, which can be used to handle text that overflows its container.
.container {
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Responsive web design is the approach to designing websites that work well across a variety of devices, including desktops, tablets, and smartphones. CSS3 introduces several features that make responsive design easier.
Media Queries: These allow you to apply styles based on the device's characteristics, such as width, height, or orientation.
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
Viewport Units: CSS3 introduces vw (viewport width), vh (viewport height), vmin, and vmax, which allow you to size elements relative to the viewport size.
.container {
width: 100vw; /* 100% of the viewport width */
height: 100vh; /* 100% of the viewport height */
}
CSS3 allows you to apply transformations to elements, enabling rotation, scaling, skewing, and translation.
Transform Property: This property enables you to change the position, size, and shape of elements.
div {
transform: rotate(45deg);
}
3D Transforms: CSS3 also supports 3D transformations, enabling elements to be rotated in 3D space.
div {
transform: rotateX(45deg) rotateY(45deg);
}
CSS3 introduced CSS variables, which allow developers to define reusable values in their stylesheets.
:root {
--main-color: #3498db;
}
div {
background-color: var(--main-color);
}
Better Control Over Layout: CSS3 introduces powerful layout modules like Flexbox and Grid, which allow for easier and more flexible layout management, especially when dealing with complex designs.
Rich Visual Effects: With features like animations, transitions, shadows, and gradients, CSS3 provides a wide range of visual effects that were previously only achievable through JavaScript or images.
Mobile-First Design: CSS3’s support for media queries and viewport units makes it easier to design responsive websites that work across various screen sizes, promoting the mobile-first approach.
Performance and Accessibility: By using CSS3 for animations and transformations, developers can offload tasks to the GPU, improving performance. Additionally, CSS3 improves accessibility with better control over styling and positioning of elements.
Cleaner and More Efficient Code: CSS3 reduces the need for JavaScript-heavy approaches or large image files for styling and effects. This leads to cleaner and more maintainable code.
CSS3 has significantly enhanced the capabilities of web design, providing developers with powerful tools for layout control, animations, and responsive design. It enables the creation of visually appealing and interactive websites with smoother user experiences, all while keeping code more maintainable and efficient. As web design continues to evolve, CSS3 remains an essential technology for creating modern, dynamic, and mobile-friendly websites.
Open this section to load past papers