Content Marketing
CSS3 Features You May Not Be Aware Of: Flexbox, Grid Layouts, Custom Properties, Transitions, Animations, and Multiple Backgrounds
Cascading Style Sheets (CSS) continue to evolve and the latest versions may have some features that you may not even be aware of. Here are some of the major improvements and methodologies introduced with CSS3, along with code examples:
- Flexible Box Layout (Flexbox): a layout mode that allows you to create flexible and responsive layouts for web pages. With flexbox, you can easily align and distribute elements within a container. n this example, the
.container
class usesdisplay: flex
to enable flexbox layout mode. Thejustify-content
property is set tocenter
to horizontally center the child element within the container. Thealign-items
property is set tocenter
to vertically center the child element. The.item
class sets the background color and padding for the child element.
HTML
<div class="container">
<div class="item">Centered Element</div>
</div>
CSS
.container {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}
.item {
background-color: #ddd;
padding: 20px;
}
Result
Centered Element
- Grid layout: another layout mode that allows you to create complex grid-based layouts for web pages. With grid, you can specify rows and columns, and then place elements within specific cells of the grid. In this example, the
.grid-container
class usesdisplay: grid
to enable grid layout mode. Thegrid-template-columns
property is set torepeat(2, 1fr)
to create two columns of equal width. Thegap
property sets the spacing between grid cells. The.grid-item
class sets the background color and padding for the grid items.
HTML
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
</div>
CSS
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.grid-item {
background-color: #ddd;
padding: 20px;
}
Result
Item 1
Item 2
Item 3
Item 4
- Transitions: CSS3 introduced a number of new properties and techniques for creating transitions on web pages. For example, the
transition
property can be used to animate changes in CSS properties over time. In this example, the.box
class sets the width, height, and initial background color for the element. Thetransition
property is set tobackground-color 0.5s ease
to animate changes to the background color property over half a second with an ease-in-out timing function. The.box:hover
class changes the background color of the element when the mouse pointer is over it, triggering the transition animation.
HTML
<div class="box"></div>
CSS
.box {
width: 100px;
height: 100px;
background-color: red;
transition: background-color 0.5s ease;
}
.box:hover {
background-color: blue;
}
Result
Hover
Here!
Here!
- Animations: CSS3 introduced a number of new properties and techniques for creating animations on web pages. In this example, we’ve added an animation using the
animation
property. We’ve defined a@keyframes
rule for the animation, which specifies that the box should rotate from 0 degrees to 90 degrees over a duration of 0.5 seconds. When the box is hovered over, thespin
animation is applied to rotate the box. Theanimation-fill-mode
property is set toforwards
to ensure that the final state of the animation is retained after it finishes.
HTML
<div class="rotate"></div>
CSS
.rotate {
width: 100px;
height: 100px;
background-color: red;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
/* Add animation properties */
animation-duration: 0.5s;
animation-timing-function: ease;
animation-fill-mode: forwards;
}
/* Define the animation */
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(90deg); }
}
.rotate:hover {
animation-name: spin;
}
Result
Hover
Here!
Here!
- CSS Custom Properties: Also known as CSS variables, custom properties were introduced in CSS3. They allow you to define and use your own custom properties in CSS, which can be used to store and reuse values throughout your stylesheets. CSS variables are identified by a name that starts with two dashes, such as
--my-variable
. In this example, we define a CSS variable called –primary-color and give it a value of#007bff
, which is a blue color commonly used as a primary color in many designs. We’ve used this variable to set thebackground-color
property of a button element, by using thevar()
function and passing in the variable name. This will use the value of the variable as the background color for the button.
:root {
--primary-color: #007bff;
}
button {
background-color: var(--primary-color);
color: white;
padding: 10px 20px;
}
- Multiple Backgrounds: CSS3 allows you to specify multiple background images for an element, with the ability to control its positioning and stacking order. The background is composed of two images,
red.png
andblue.png
, which are loaded using thebackground-image
property. The first image,red.png
, is positioned at the right bottom corner of the element, while the second image,blue.png
, is positioned at the left top corner of the element. Thebackground-position
property is used to control the positioning of each image. Thebackground-repeat
property is used to control how the images repeat. The first image,red.png
, is set to not repeat (no-repeat
), while the second image,blue.png
, is set to repeat (repeat
).
HTML
<div id="multibackground"></div>
CSS
#multibackground {
background-image: url(red.png), url(blue.png);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;
height: 200px;
width: 200px;
}