CSS Styled Content Boxes
This example demonstrates how to create reusable content boxes using HTML and CSS. Explore different backgrounds, borders, shadows, gradients, notification styles, and hover effects that can be used to organize and highlight information on a webpage.
Basic Content Box
A basic content box uses padding, a background color, and a subtle border to separate information from the surrounding page.
This style works well for general content sections.
Bordered Content Box
A thicker border creates a stronger visual boundary around the content.
Use borders to emphasize important sections without changing the background color.
Rounded Content Box
The border-radius property creates rounded corners.
Larger radius values produce a softer appearance suitable for modern interface designs.
Shadow Content Box
The box-shadow property adds depth by creating a shadow around the content box.
Subtle shadows help distinguish content from the page background.
Left Border Accent
A thick left border provides a simple way to highlight information.
This style is commonly used for notes, tips, quotations, and informational messages.
Gradient Content Box
The linear-gradient() function creates a smooth transition between background colors.
Gradient boxes can highlight featured content or important announcements.
Notification and Alert Boxes
Notification boxes use different background and border colors to communicate the purpose of a message. The following examples demonstrate informational, success, warning, and error messages.
Information Box
Information: This box provides additional details, instructions, or helpful information.
Success Box
Success: This box indicates that an operation or process has been completed successfully.
Warning Box
Warning: This box highlights information that requires attention or caution.
Error Box
Error: This box indicates a problem or an action that could not be completed.
Decorative Content Boxes
Additional CSS properties can create more distinctive content box designs.
Hover Effect Box
Move your mouse over this box to see it lift slightly and display a larger shadow.
The transition property creates a smooth visual effect.
FEATURED
Featured Content Box
A positioned label identifies this box as featured content.
The position property controls the placement of the label within the box.
i
Icon Content Box
An icon or symbol can help visitors quickly identify the purpose of a content box.
Flexbox aligns the symbol beside the text.
Top Accent Border
A colored top border creates a decorative accent without surrounding the entire box with a thick border.
This style works well for features and informational sections.
Dashed Border Box
The border-style: dashed declaration creates a dashed outline.
Dashed borders can identify examples, optional information, or special content areas.
Try It: Compare the different content box styles and resize the browser window to see how the grid adjusts. Move your mouse over the Hover Effect Box to see its animation. Change the background colors, border widths, padding, and shadows to experiment with different designs.
How Styled Content Boxes Work
Styled content boxes use CSS properties to visually separate and organize information.
The base content-box class defines common properties such as padding, margins, borders, backgrounds, and rounded corners.
Additional classes modify individual properties to create different visual styles.
For example, the following CSS creates a basic content box:
.content-box {
padding: 25px;
margin: 20px 0;
border: 1px solid #d5dfe5;
border-radius: 6px;
background-color: #f1f5f8;
}
Using reusable classes makes it easier to maintain consistent styling throughout a website.
Understanding the CSS Box Model
Every HTML element is represented as a rectangular box consisting of content, padding, borders, and margins.
The padding property controls the space between the content and its border.
The border property creates a visible boundary around the element.
The margin property controls the space outside the border.
The box-sizing: border-box declaration includes padding and borders within an element's specified width and height.
Using Backgrounds and Borders
The background-color property applies a solid background color to a content box.
The background property can also apply gradients and other background effects.
The border property controls the width, style, and color of the border.
The border-radius property creates rounded corners.
For example, the following CSS creates a content box with a thick left border:
.box-left-border {
background-color: #e8f2f8;
border: none;
border-left: 6px solid #146c94;
border-radius: 0;
}
Individual border properties allow you to create accents along the top, right, bottom, or left side of an element.
Adding Box Shadows
The box-shadow property creates a shadow around an element.
The following example adds a subtle shadow to a content box:
.box-shadow {
background-color: #fff;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
}
The first value controls horizontal offset, the second controls vertical offset, and the third controls blur radius.
The rgba() color function allows the shadow to use partial transparency.
Shadows can make boxes appear elevated above the surrounding page.
Creating Gradient Backgrounds
The linear-gradient() function creates a smooth transition between two or more colors.
The following example creates a diagonal gradient:
.box-gradient {
background: linear-gradient(135deg, #146c94, #263746);
color: #fff;
}
The 135deg value controls the gradient direction.
Gradient backgrounds can be useful for featured sections, promotional content, and decorative elements.
Adding Content Box Hover Effects
The :hover pseudo-class applies styles when the pointer moves over a content box.
The transform property can move or scale the box, while box-shadow can increase its visual depth.
The following CSS creates a lifting effect:
.box-hover:hover {
transform: translateY(-5px);
border-color: #146c94;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}
The transition property makes the visual changes appear smooth.
Hover effects should be decorative and should not be required to access important information.
Responsive Content Box Design
The examples use CSS Grid to arrange content boxes into flexible columns.
The auto-fit keyword automatically adjusts the number of columns based on the available width.
The minmax() function defines flexible column sizes.
The following CSS creates the responsive layout:
.boxes-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
gap: 25px;
}
On wider screens, multiple boxes appear side by side. On smaller screens, the layout adjusts to fewer columns.
A media query at 600 pixels or less reduces padding, margins, and spacing for smaller devices.
Content Box Accessibility
Use meaningful headings and descriptive text to organize information within content boxes.
Do not rely on color alone to communicate the purpose of an alert or notification.
The notification examples include text labels such as Information, Success, Warning, and Error.
Use sufficient color contrast between text and background colors to maintain readability.
The prefers-reduced-motion media query removes transitions and movement effects for visitors who request reduced motion.
For dynamically updated notifications, appropriate ARIA live regions may be needed to announce changes to assistive technology.
Customizing Styled Content Boxes
You can customize the appearance of each box by modifying its CSS properties.
Change the background-color property to use different background colors.
Adjust the padding property to increase or decrease the internal spacing.
Modify the border and border-radius properties to change the border appearance and corner shape.
Use box-shadow to add depth or emphasize selected content.
To create another content box style, define a new CSS class and combine it with the existing content-box class.
For example, class="content-box box-info" combines the base content box styling with the informational notification appearance.