Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
For a line attached to a panel, card, or content block, add a CSS border to that existing element: border-top: 1px solid #c00;. If the line marks a meaningful thematic break, use <hr> and reset its browser styling. Use a separate decorative element only when the line needs to stand alone.
Choose the right element for the line
Tables are for tabular data, not for positioning or drawing decoration. Use ordinary document flow and CSS for layout. The best line technique depends on what the line means:
- A thematic break between sections: use
<hr>. It represents a thematic change in content and has an implicit separator role. See the MDN reference and the HTML Standard. - A visual edge belonging to a component: put a border on that component. This avoids adding a separate block whose margins and dimensions need coordination.
- A purely decorative, independent line: use a styled element or pseudo-element. Hide decorative markup from assistive technology when it conveys no information.
Best for most designs: add a border to an existing element
If a line belongs to a panel, style the panel rather than inserting a spacer:
<section class="panel">
<h2>Heading</h2>
<p>Content goes here.</p>
</section>
.panel {
border-top: 1px solid #c00;
border-bottom: 1px solid #c00;
padding-block: 0.75rem;
}
For just one edge, use border-top or border-bottom. For both, border-block is a concise logical alternative:
#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
.panel {
border-block: 1px solid #c00;
}
border-top and border-bottom name physical edges. border-block follows the block direction of the writing mode, which is generally the top and bottom in horizontal writing but adapts to other writing modes. Use a border when the line should follow the element’s edge; borders are part of the box model. See MDN’s border reference.
Thickness, color, and width
The first value sets border thickness, the second its style, and the third its color:
.thin { border-top: 1px solid #333; }
.thick { border-top: 4px solid #333; }
.dashed { border-top: 1px dashed #777; }
A percentage width is relative to the containing block. To make a shorter, centered line:
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #2
.short-rule {
width: 60%;
margin-inline: auto;
border-top: 1px solid #c00;
}
A 1px CSS line is one CSS pixel, not a promise of exactly one physical screen pixel. Device pixel ratio and zoom affect how browsers rasterize it.
Use and reset <hr> for a thematic break
<hr> is not obsolete, and it is not merely a rectangle. When it marks a meaningful break in the content, keep the semantic element and set the appearance you want:
<p>First section.</p>
<hr class="rule">
<p>Next section.</p>
.rule {
box-sizing: border-box;
width: 100%;
height: 0;
margin: 0;
border: 0;
border-top: 1px solid #c00;
}
This removes the default border and margins, then draws one border edge. The box-sizing declaration is optional for this zero-height pattern, but makes the sizing model explicit if you later change dimensions.
Rank #3
An alternative is a one-pixel content box filled with color:
.rule {
height: 1px;
margin: 0;
border: 0;
background-color: #c00;
}
With the border method, the line is an element edge. With the background method, the line fills the content box. Keep the border at zero if you intend the background itself to be the full one-pixel shape; adding borders changes the total dimensions unless the box model accounts for them.
For an independent decorative line
If there is no suitable existing element to carry a border and the line is decoration rather than a content break, use a small element:
Rank #4
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
<div class="decorative-line" aria-hidden="true"></div>
.decorative-line {
width: 100%;
height: 1px;
background-color: #c00;
}
The aria-hidden attribute keeps this non-informative decoration out of the accessibility tree. Do not use an empty hidden <div> as a substitute for <hr> when the line communicates a thematic break. A pseudo-element can also work for visual accents attached to a heading, but it should not replace semantic markup for structure:
.heading::after {
content: "";
display: block;
height: 1px;
margin-top: 0.5rem;
background: #c00;
}
Why the line has unwanted space or thickness
A line’s visible placement and thickness come from separate parts of CSS. Height controls the content box; borders draw edges; margins create outside space; padding creates inside space. Setting only height does not reset the other properties.
- Default rule styling: browsers apply user-agent styles to
<hr>, which can include margins and a border or other rendering. Reset the rule’s margin and border rather than assuming a height declaration removes them. - Margins on neighbors: a heading before the rule or paragraph after it may contribute the gap. Inspect those elements too. Margin collapsing between block elements can also affect the apparent distance.
- Height plus border: a one-pixel content height with a border is not necessarily a one-pixel total. Choose either a border edge with
height: 0, or a one-pixel height with no border and a background. - Rule order: a later shorthand can cancel a specific edge. This is wrong because
border: 0removes the preceding top border:
.rule {
border-top: 1px solid #c00;
border: 0;
}
Put the reset first instead:
.rule {
border: 0;
border-top: 1px solid #c00;
}
If the rule itself is reset but a gap remains, scope margin changes to the relevant component rather than removing margins globally:
Best Value
.section-heading,
.section-copy,
.section-rule {
margin-block: 0;
}
display: block alone is not a fix: <hr> is already block-level in ordinary browser styling, and changing display does not remove its margins or border. A decorative element may need block display, but its height or border, color, and margins still determine the result.
Responsive and layout variations
A rule with width: 100% spans its containing block. Keep it inside the content area unless it is intentionally full-bleed. For example, to extend a rule across a container’s horizontal padding:
.container {
padding-inline: 1rem;
}
.container > .rule {
margin-inline: -1rem;
}
Negative margins should match the padding only when the design calls for that edge-to-edge effect. Otherwise, leave the line within the padded content width. Prefer container-relative sizing over fixed widths when the line should adapt to screen size.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchCopy-paste patterns
/* Line attached to an existing block */
.card { border-top: 1px solid #c00; }
/* Semantic thematic break */
hr.rule {
height: 0;
margin: 0;
border: 0;
border-top: 1px solid #c00;
}
/* Standalone decoration */
.decorative-line {
height: 1px;
background: #c00;
}
The original 2006 SitePoint discussion raised browser-specific spacing concerns from its era. Those observations are historical, not current requirements for Internet Explorer-era hacks. In modern CSS, identify whether the gap comes from the rule’s own default styling or from surrounding margins, then reset the specific cause.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

