Just wanted to share a nice way to (as far as I can tell) consistently adhere styles on a component with custom CSS. So far, it hasn't steered me wrong in adhering through feature updates in Retool.
Here's a quick step by step:
- Drag and drop any component onto your main canvas in Retool.
- Give it a name (or don't, but you probably should). For example, the first button you drop onto a new app will be called
button1
. Change it to whatever is fitting for your scenario. For this example we'll call itcustomCssButton
. - Go to the section in the GUI where you can write your custom css styles.
- Here's the secret -- you use an attribute selector to target your css. Start by targeting your component by ID (the name you gave your component i.e.
customCssButton
) like this:
[id*="customCssButton"] {
/* ...your styles here */
}
- Using the syntax
[id="idName"]
is what is referred to as an attribute selector. We then add the asterisk symbol '*
' so that we can get matches anywhere in the value name. This is what is referred to as substring matching, which there are many different uses for. You can get more detailed information on attribute selectors and substring matching at the following links:
MDN - Attribute Selectors
WW3 - CSS Attribute substring Matching
StackOverflow - Detailed Example Usage
- Placing the asterisk after your 'id' attribute selector will find id names that match anywhere in the substring value of that id. So, using
[id*="customCssButton"]
will find any and all id names that contain 'customCssButton' within their name, aka their attribute value.
There should absolutely always be an id that contains the exact name you designated for your component. In this case, a button. And if you gave your component a clear and unique name to your app, there almost certainly will not be another html element that contains the exact name and casing that you designated for any of your components.
This should ensure that your custom css will always adhere to your styling conventions. You can do anything here from styling more deeply nested elements within your component, adding complex css like animations to your component, etc.
Please let me know if you have done something similar or if you need a more clear explanation! I hope some people in the community find it useful