You can style a horizontal line in CSS using the <hr> tag with properties like color, width, height, and effects such as gradients or shadows. This simple element divides content on a webpage, making it easier to read. With CSS, you can change its look to match your site’s design. In this guide, you’ll learn step-by-step methods to style horizontal lines, see examples with code, and get answers to common questions. Whether you want a bold red line or a subtle dashed divider, this article covers it all.
What Is a Horizontal Line in CSS?
A horizontal line in CSS is an <hr> tag that creates a straight line across a webpage. It separates sections like paragraphs or headings. By default, it’s a thin, gray line that spans the full width of its container. CSS lets you customize it with properties like border, width, and background. For example, you can make it blue, dashed, or even add a shadow.
Horizontal lines help organize content. They’re used in blogs, forms, and portfolios. With over 10 styling options available, this guide shows you how to make them stand out.
Why Should You Style a Horizontal Line?
Styling a horizontal line improves a webpage’s look and readability. A plain gray line works, but a styled one can match your site’s colors or theme. Here are 5 key reasons to style it:
- Separates content clearly – A thick or colorful line shows where one section ends and another begins.
- Matches your design – Use your brand colors, like blue or green, for consistency.
- Adds creativity – Effects like gradients or dots make it eye-catching.
- Guides the reader – A styled line draws attention to important sections.
- Enhances user experience – A clean, styled divider makes pages easier to scan.
Studies show well-organized pages keep visitors engaged longer. Let’s explore how to do it.
How Can You Change the Color of a Horizontal Line?
You can change the color of a horizontal line using the border-color or background-color property in CSS. The default gray isn’t always appealing, so picking a new color is a quick fix.
Steps to Change the Color
- Select the
<hr>tag in your CSS. - Set
border: noneto remove the default style. - Add
border-topwith a size, style, and color.
Example Code
hr {
border: none;
border-top: 2px solid #ff5733; /* Red-orange color */
}
This makes a 2px thick, red-orange line. You can use hex codes like #00ff00 for green or names like purple.
Variations
- Thin line: Use
1px solid blue. - Thick line: Try
5px solid #ffcc00for a bold yellow. - Transparent: Set
opacity: 0.5for a faded look.
Over 16 million color options exist with hex codes, giving you endless choices.
How Do You Adjust the Width and Height of a Horizontal Line?
You adjust the width with the width property and the height with border-top or height In CSS. This controls how long or thick the line appears.
Changing the Height
- Use
border-topfor thickness, like3px solid black. - Or use
heightwithbackground-colorfor a solid block.
Changing the Width
- Set
width: 50%to make it half the container’s size. - Use pixels, like
width: 200px, for a fixed length.
Example Code
hr {
border: none;
border-top: 4px solid #3498db; /* Blue, 4px thick */
width: 75%; /* 75% of container width */
}
This creates a 4px thick, blue line that’s 75% wide.
Tips
- Test widths like 25%, 50%, or 100% to fit your layout.
- Combine with
margin: 0 autoto center it (more on that later).
Adjusting these makes the line fit any design, from minimal to bold.
How Can You Make a Dashed or Dotted Horizontal Line?
You can make a dashed or dotted horizontal line using the border-style property. This adds variety, like breaking the line into segments or dots.
Steps
- Select the
<hr>tag. - Remove default styling with
border: none. - Set
border-topwithdashedordotted.
Example: Dashed Line
hr {
border: none;
border-top: 3px dashed #2ecc71; /* Green dashed line */
}
Example: Dotted Line
hr {
border: none;
border-top: 2px dotted #e74c3c; /* Red dotted line */
}
Variations
- Spacing: Use
border-top: 3px dashedfor wider gaps or2px dottedfor tighter dots. - Colors: Try
#f1c40f(yellow) or#9b59b6(purple).
Dashed and dotted lines are great for playful or modern designs.
How Do You Add a Gradient to a Horizontal Line?
You add a gradient to a horizontal line using the background property with linear-gradient. This blends two or more colors for a stylish effect.
Steps
- Set
border: noneto clear defaults. - Use
heightto define thickness. - Apply
background: linear-gradient()with colors.
Example Code
hr {
border: none;
height: 6px;
background: linear-gradient(to right, #8e44ad, #3498db); /* Purple to blue */
}
This creates a 6px thick line fading from purple to blue.
Variations
- Direction: Use
to left,to top, or45degfor angles. - Colors: Try
#e67e22(orange) to#27ae60(green). - Multi-color: Add more stops, like
red, yellow, blue.
Gradients work well for creative sites, with over 100 possible combinations.
How Can You Center a Horizontal Line?
You center a horizontal line by setting a width and using margin: 0 auto. By default, it stretches fully across, but this shortens and centers it.
Steps
- Set a width, like
width: 40%. - Add
margin: 0 autoto center it.
Example Code
hr {
border: none;
border-top: 3px solid #1abc9c; /* Turquoise */
width: 40%;
margin: 0 auto;
}
This makes a 3px thick, turquoise line, 40% wide, centered.
Tips
- Use percentages (e.g., 30%) or pixels (e.g., 150px).
- Add padding, like
padding: 10px 0, for spacing.
Centering is perfect for narrow dividers in articles or forms.
How Do You Add Shadows to a Horizontal Line?
You add shadows to a horizontal line using the box-shadow property. This gives it a 3D or raised look.
Steps
- Clear defaults with
border: none. - Set
heightandbackground-color. - Add
box-shadowwith offset, blur, and color.
Example Code
hr {
border: none;
height: 5px;
background-color: #34495e; /* Dark blue */
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.3); /* Soft shadow */
}
Variations
- Light shadow: Use
0 2px 4px rgba(0, 0, 0, 0.1). - Colorful: Try
0 4px 8px #e74c3cfor a red glow. - Double-sided: Add
-3pxfor a shadow above and below.
Shadows add depth, making lines pop on flat designs.
How Can You Style Horizontal Lines with Borders?
You style horizontal lines with borders using border-top and border-bottom for effects like double lines. This creates unique looks beyond a single line.
Example: Double Line
hr {
border: none;
border-top: 1px solid #7f8c8d; /* Gray */
border-bottom: 1px solid #7f8c8d;
height: 6px; /* Gap between lines */
}
Example: Rounded Edges
hr {
border: none;
height: 5px;
background-color: #f39c12; /* Orange */
border-radius: 3px; /* Rounded ends */
}
Variations
- Thicker borders: Use
2px solidfor bolder lines. - Mixed colors: Try
border-top: 1px solid redandborder-bottom: 1px solid blue.
Borders offer 8+ ways to customize lines for any style.
What Are the Best Practices for Styling Horizontal Lines?
The best practices for styling horizontal lines include keeping them simple and matching your theme. Here are 6 tips:
- Avoid over-styling – Too many effects can distract readers.
- Use consistent colors – Match your site’s palette, like blues or greens.
- Test responsiveness – Check how it looks on phones and desktops.
- Keep it subtle – A faint line often works better than a loud one.
- Space it out – Add
margin: 20px 0for breathing room. - Preview changes – Test in browsers like Chrome or Firefox.
These ensure your lines enhance, not overpower, your content.
What Tools Can Help You Style Horizontal Lines?
Tools like CSS editors and color pickers help you style horizontal lines easily. Here are 5 useful ones:
- CodePen – Test CSS styles live.
- Google Chrome DevTools – Edit styles in real-time on your site.
- Coolors – Pick colors like
#2980b9(blue) or#e67e22(orange). - W3Schools – Learn CSS properties with examples.
- Canva Color Wheel – Find matching shades for gradients.
These tools save time and improve your designs.
How Do You Combine Multiple Styles for a Horizontal Line?
You combine multiple styles by applying properties like color, width, and effects together. This creates a unique look.
Example Code
hr {
border: none;
height: 4px;
width: 60%;
margin: 0 auto;
background: linear-gradient(to right, #e74c3c, #3498db); /* Red to blue */
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
border-radius: 2px;
}
This makes a 4px thick, centered line with a red-to-blue gradient, shadow, and rounded edges.
Tips
- Start with one style, like color, then add others.
- Test combinations to avoid clashing effects.
Mixing styles offers over 20 creative possibilities.
FAQ: Common Questions About Styling Horizontal Lines in CSS
Can You Use Images for Horizontal Lines?
Yes, you can use images with the background-image property. This works for complex patterns but needs extra CSS for responsiveness.
Is It Possible to Animate a Horizontal Line?
Yes, you can animate it with CSS transitions. For example, transition: width 0.5s makes the width grow smoothly.
Can You Remove the Default <hr> Style?
Yes, use border: none to clear it. Then add your own styles like border-top or background.
Does Styling Work on All Browsers?
Yes, basic styles like color and width work everywhere. Test advanced effects like gradients in Chrome, Firefox, and Safari.
Can You Make a Vertical Line Instead?
Yes, use a <div> with width: 2px and height: 100px. Horizontal lines stay as <hr>.
Conclusion
Styling a horizontal line in CSS is an easy way to improve your webpage’s design and structure. You can change its color, adjust its size, or add effects like gradients and shadows. This guide covered 10+ methods, from simple tweaks to advanced combinations, with clear examples. Use these techniques to match your site’s theme and keep readers engaged. Experiment with the code snippets today and see how a small line can make a big impact.
