c2-comparison-bar splits its width between two or three amounts in any unit: start-value on the start side, end-value on the end side and, optionally, middle-value between them. The bar computes each segment’s share, and show-value prints it as a percentage: the start and end shares at their edges, the middle share centred under its segment. The printed shares always add up to 100. precision sets the fraction digits (2 by default) and locale the number format. start-label, middle-label and end-label name the segments for assistive technology, which reads the whole bar as one image (“Bid 22.43%, Ask 77.57%”), and the start, middle and end slots replace the printed text when you want counts instead of percentages.
Import
import '@c2n/components/comparison-bar'
Usage
Importimport '@c2n/components/comparison-bar'
Markup<style>
.stack {
display: grid;
gap: 16px;
width: 320px;
max-width: 100%;
}
.order-book {
width: 320px;
max-width: 100%;
padding: 16px;
border-radius: 8px;
background: #1e2329;
--c2-comparison-bar__start-segment--background-color: #2ebd85;
--c2-comparison-bar__start-value--color: #2ebd85;
--c2-comparison-bar__end-segment--background-color: #f6465d;
--c2-comparison-bar__end-value--color: #f6465d;
}
.pills {
--c2-comparison-bar__track--gap: 4px;
--c2-comparison-bar__segment--border-radius: 999px;
}
.thick {
--c2-comparison-bar--height: 12px;
--c2-comparison-bar--border-radius: 4px;
}
</style>
<div data-label="Default">
<div class="stack">
<c2-comparison-bar start-value="22.43" end-value="77.57" start-label="Bid" end-label="Ask"></c2-comparison-bar>
<c2-comparison-bar start-value="2243" end-value="7757" start-label="Bid" end-label="Ask" show-value></c2-comparison-bar>
</div>
</div>
<div data-label="Three values">
<div class="stack">
<c2-comparison-bar start-value="45" middle-value="20" end-value="35" start-label="Win" middle-label="Draw" end-label="Loss" show-value></c2-comparison-bar>
<c2-comparison-bar start-value="1" middle-value="1" end-value="1" show-value></c2-comparison-bar>
</div>
</div>
<div data-label="Separated segments">
<div class="stack">
<c2-comparison-bar class="pills" start-value="22.43" end-value="77.57" show-value></c2-comparison-bar>
</div>
</div>
<div data-label="Precision and locale">
<div class="stack">
<c2-comparison-bar start-value="3" end-value="1" precision="0" show-value></c2-comparison-bar>
<c2-comparison-bar start-value="1" end-value="2" precision="1" locale="de-DE" show-value></c2-comparison-bar>
</div>
</div>
<div data-label="Custom text">
<div class="stack">
<c2-comparison-bar class="thick" start-value="12" end-value="8" start-label="Yes" end-label="No">
<span slot="start">Yes 12</span>
<span slot="end">8 No</span>
</c2-comparison-bar>
</div>
</div>
<div data-label="Order book">
<div class="order-book">
<c2-comparison-bar start-value="22.43" end-value="77.57" locale="de-DE" start-label="Bid" end-label="Ask" show-value></c2-comparison-bar>
</div>
</div>
Update start-value and end-value as the amounts change and the segments animate to their new widths. Leave middle-value unset for a two-segment bar. Negative or missing amounts count as zero; a segment with no share collapses entirely, and while none has an amount the bar splits evenly. Shares are rounded by largest remainder, so three equal thirds print as 33.34%, 33.33% and 33.33%. The printed percentages are hidden from screen readers because the bar’s accessible name already carries them.
--c2-comparison-bar__start-segment--background-color, __middle-segment--background-color and __end-segment--background-color colour the segments, with matching __start-value--color / __middle-value--color / __end-value--color for the text. --c2-comparison-bar--height sets the thickness and --c2-comparison-bar--border-radius rounds the bar’s outer ends. By default the segments touch; set --c2-comparison-bar__track--gap to separate them, and --c2-comparison-bar__segment--border-radius to round each segment into its own pill.