When using percentages, the element width is used to calculate the radius. To achieve a capsule-shaped element, it is necessary to provide units like rem
or px
for the border-radius
property (the reason behind this is not clear, but it does work). This explains why specifying 500px
works. You can apply the same value to the line-height
and border-radius
properties if desired.
.capsule {
line-height: 48px;
border-radius: 48px;
}
An example can be found in CodePen. Try adjusting the $label-height
variable to observe how the shape remains consistent while the button's height changes.
In this scenario, specifying the element's width or height is unnecessary. Focus on adjusting the content's height
and padding
instead.
The padding
property effectively creates a separation between the content and the component border. Just setting the left padding can yield interesting results.
https://i.sstatic.net/TqPMH.png
By setting the line-height
property of the container, not only does the container's height get established automatically, but it also centers the content within the container.
To match the component's width with the content width, altering the component's display property to inline-block
, and employing FlexBox for column alignment, can be effective. Adding left and right margins set to auto prevents the element from expanding to the parent width.
https://i.sstatic.net/viYfX.png
For spacing between components, utilizing the margin-top
property between consecutive components is advisable.
.capsule + .capsule {
margin-top: 15px;
}
Hopefully, this explanation proves helpful :)