CSS - Learning Out Loud
Posted: Mon Oct 21, 2024 5:31 am
Received this question recently:
"How to apply rounded-pill to all my buttons?"
rounded-pill is a Bootstrap class that can be added to elements to give, what some might consider, a modern rounded look to buttons, like this for example:
rounded-pill automatically applies a large 50rem border-radius to all 4 corners to achieve that look.
Of course, this class name could be added to copies of all the relevant tpl_ files and saved in a template to achieve this.
However, CSS Rules could be used in the template user.css file to achieve the same with much less effort.
Sharing here in case it's useful to others.
In a current default install, we have this on product page, for example:
All the relevant buttons in a default install have, within their class attribute, class names like btn-info, btn-success etc.
For example, something like <a class="btn btn-success">
This makes it easier to target those classes with rules that change their border-radius.
Let's start with:
So, what does that mean?
The whole piece of code is a CSS Rule, the first part before the curly braces is the Selector (multiple comma separated Selectors could be used) and the part within the curly braces is the Declaration of the Properties, ending with a colon and their Values, ending with a semi-colon (multiple Declarations could be used within the curly braces).
The square brackets in this case mean it is an Attribute Selector, so we have: [class] which means "target the class attribute of all elements".
The next part *= is called a substring operator, basically it means "contains", so we have: [class*=] which means "target the class attribute of all elements that contains".
The next part " btn-" defines what we're looking for, so we finally have: [class*=" btn-"] which means "target the class attribute of all elements that contains btn-". The space before the btn- is important so we are targeting names like btn-xyz and not somenamebtn-xyz.
The Declaration here is border-radius: 50rem; which is what rounded-pill does.
Let's save this to the user.css file on the server and hard refresh the page.
Yay! It works!
Browsing around the site shows it has changed the buttons everywhere as expected.
If it had not worked, first check would be that the user.css file on the server has been updated. We use FTP file transfer software and it (and us) are fallible
Next, there might be other CSS that is taking priority so change the Declaration to border-radius: 50rem !important; to give it priority.
This CSS Rule works with Bootstrap v.4 and v.5.
Caveat: this is using a default installation.
"How to apply rounded-pill to all my buttons?"
rounded-pill is a Bootstrap class that can be added to elements to give, what some might consider, a modern rounded look to buttons, like this for example:
rounded-pill automatically applies a large 50rem border-radius to all 4 corners to achieve that look.
Of course, this class name could be added to copies of all the relevant tpl_ files and saved in a template to achieve this.
However, CSS Rules could be used in the template user.css file to achieve the same with much less effort.
Sharing here in case it's useful to others.
In a current default install, we have this on product page, for example:
All the relevant buttons in a default install have, within their class attribute, class names like btn-info, btn-success etc.
For example, something like <a class="btn btn-success">
This makes it easier to target those classes with rules that change their border-radius.
Let's start with:
Code: Select all
[class*=" btn-"] {
border-radius: 50rem;
}The whole piece of code is a CSS Rule, the first part before the curly braces is the Selector (multiple comma separated Selectors could be used) and the part within the curly braces is the Declaration of the Properties, ending with a colon and their Values, ending with a semi-colon (multiple Declarations could be used within the curly braces).
The square brackets in this case mean it is an Attribute Selector, so we have: [class] which means "target the class attribute of all elements".
The next part *= is called a substring operator, basically it means "contains", so we have: [class*=] which means "target the class attribute of all elements that contains".
The next part " btn-" defines what we're looking for, so we finally have: [class*=" btn-"] which means "target the class attribute of all elements that contains btn-". The space before the btn- is important so we are targeting names like btn-xyz and not somenamebtn-xyz.
The Declaration here is border-radius: 50rem; which is what rounded-pill does.
Let's save this to the user.css file on the server and hard refresh the page.
Yay! It works!
Browsing around the site shows it has changed the buttons everywhere as expected.
If it had not worked, first check would be that the user.css file on the server has been updated. We use FTP file transfer software and it (and us) are fallible
Next, there might be other CSS that is taking priority so change the Declaration to border-radius: 50rem !important; to give it priority.
This CSS Rule works with Bootstrap v.4 and v.5.
Caveat: this is using a default installation.