Bootstrap Helper Classes
Learn how to style elements quickly using Bootstrap utility and helper classes.
What are Bootstrap Helper Classes?
Bootstrap helper classes allow developers to apply common styles directly to HTML elements without writing custom CSS. They help control colors, spacing, borders, shadows, alignment, display, and element sizes.1. Text Color Classes
Bootstrap provides predefined classes to change the color of text.
Program Code
<p class="text-primary">Primary Text</p>
<p class="text-success">Success Text</p>
<p class="text-danger">Danger Text</p>
<p class="text-warning">Warning Text</p>
<p class="text-info">Information Text</p>
<p class="text-secondary">Secondary Text</p>
Program Output
Primary Text
Success Text
Danger Text
Warning Text
Information Text
Secondary Text
2. Background Color Classes
Use background utility classes to change an element's background color.
Program Code
<div class="bg-primary text-white p-3">
Primary Background
</div>
<div class="bg-success text-white p-3">
Success Background
</div>
<div class="bg-warning text-dark p-3">
Warning Background
</div>
Program Output
3. Margin and Padding Classes
Margin creates space outside an element. Padding creates space inside an element.
m-3— margin on all sidesmt-3— margin on the topmb-3— margin on the bottomp-3— padding on all sidespx-4— horizontal paddingpy-2— vertical padding
Program Code
<div class="bg-primary text-white p-3 mb-3">
Element with Padding and Bottom Margin
</div>
<div class="bg-success text-white px-4 py-2">
Horizontal and Vertical Padding
</div>
Program Output
4. Border Classes
Border classes add or remove borders and can also change border colors.
Program Code
<div class="border p-3">Default Border</div>
<div class="border border-primary p-3">
Primary Border
</div>
<div class="border border-success p-3">
Success Border
</div>
<div class="border-0 p-3">No Border</div>
Program Output
5. Rounded Corner Classes
Rounded classes add curved corners to elements.
Program Code
<div class="border rounded p-3">Rounded</div>
<div class="border rounded-3 p-3">
Rounded 3
</div>
<div class="border rounded-pill p-3">
Rounded Pill
</div>
Program Output
6. Shadow Classes
Shadow utilities create visual depth around elements.
Program Code
<div class="shadow-none p-3 mb-3">
No Shadow
</div>
<div class="shadow-sm p-3 mb-3">
Small Shadow
</div>
<div class="shadow p-3 mb-3">
Regular Shadow
</div>
<div class="shadow-lg p-3">
Large Shadow
</div>
Program Output
7. Text Alignment Classes
Text alignment classes align text to the left, center, or right.
Program Code
<p class="text-start">Left Aligned Text</p>
<p class="text-center">Center Aligned Text</p>
<p class="text-end">Right Aligned Text</p>
Program Output
Left Aligned Text
Center Aligned Text
Right Aligned Text
8. Display Classes
Display classes control how an element appears in the page layout.
For example, d-none hides an element, while
d-block makes it a block element.
Program Code
<div class="d-block bg-primary text-white p-3 mb-2">
Block Element
</div>
<span class="d-inline bg-success text-white p-2">
Inline Element
</span>
<div class="d-none">
Hidden Element
</div>
<div class="d-none d-md-block bg-warning p-3 mt-3">
Visible on Medium Screens and Larger
</div>
Program Output
Note: The last example appears only at the medium breakpoint and above. Resize the browser to test responsive visibility.
9. Width Classes
Bootstrap provides width utilities such as w-25,
w-50, w-75, and w-100.
Program Code
<div class="bg-primary text-white w-25 p-2 mb-2">
Width 25%
</div>
<div class="bg-success text-white w-50 p-2 mb-2">
Width 50%
</div>
<div class="bg-warning text-dark w-75 p-2 mb-2">
Width 75%
</div>
<div class="bg-info text-dark w-100 p-2">
Width 100%
</div>
Program Output
10. Flex Alignment Classes
Flex utilities help align and arrange items inside a container.
Use d-flex to enable flexbox and
justify-content-between to place items at opposite ends.
Program Code
<div class="d-flex justify-content-between
align-items-center border p-3">
<span>Student</span>
<span class="badge bg-primary">Active</span>
</div>
Program Output
11. Practical Example: Student Status Card
Combine multiple helper classes to create a clean student information card.
Program Code
<div class="border rounded-3 shadow p-4">
<h4 class="text-primary mb-3">Student Details</h4>
<p class="mb-2">Name: Rahul Sharma</p>
<p class="mb-3">Course: Full Stack Development</p>
<span class="badge bg-success">Active Student</span>
</div>
Program Output
Student Details
Name: Rahul Sharma
Course: Full Stack Development
Active StudentCommon Mistakes
- Using
text-greeninstead of a supported Bootstrap text color class. - Confusing margin (
m-3) with padding (p-3). - Using
justify-content-betweenwithout enabling flexbox usingd-flex. - Expecting
d-noneelements to be visible. - Forgetting that responsive display classes behave differently at different screen sizes.
Summary
- Text color classes control text appearance.
- Background classes change element backgrounds.
- Margin and padding utilities control spacing.
- Border, rounded, and shadow classes improve visual design.
- Display and flex utilities help create responsive layouts.
- Width utilities control the width of elements.