-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I would like to be able to add my own class to a label #26
Comments
You can add a class to the form by adding a
https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html contains a list of all the attributes you can set within a form's You can also add classes to each individual field within the layout: |
Hey, Thanks for the info - my question wasn't worded very clearly. I meant that I know already how to add classes to form element and to any form field, but what I can't do is add a class to the label element. It's that label class I'd love to be able to add without having to create a custom template. Is there any way to add this as an option in Thanks. |
Thanks for the extra info :). Could you explain the use case so I can better understand why adding a class to a label would be helpful? Right now, you could do something like:
Would that be enough? I would advise against creating a custom template as much as possible - this option should only be used in unique circumstances if you are doing something with an unsupported field, such as a new widget. Creating field templates will make your app harder to maintain and more prone to errors. |
Hey @kbayliss, Apart from the fact that we'd never ever use an id for styling 😛 , adding a class to a parent div and using that to target the label would be an option, but it's not ideal. The reason a class directly on the label is preferable is because we try to avoid nested CSS wherever feasible - it makes it harder to override, and in more complex builds it can quickly lead to quite tangled CSS code. |
Haha, true! How often do you need to override the CSS for a particular label? I've just realised that in most cases you can avoid wrapping the
|
Sorry, I realise I never responded to this! And now I'm styling crispy forms again... basically we really don't want to be nesting CSS - we always want to avoid the cascade. So it would be really handy to style a label directly without nesting |
There was a discussion about this on the #tech torchbox slack channel, as someone else was asking for the same thing. Apparently this is possible in the bootstrap theme, so ought to be possible for "Depending on the theme you are using, you can use a helper and the
As you might have guessed from the class names, this is one of the bootstrap theme (bootstrap4 to be precise) |
Apologies, Helen. I thought you wanted to set a class for an individual label, not all labels for a form. Have you tried using Let me know how you get on! Also, a minor point of clarification - each template pack ( |
Ah thanks @kbayliss - I was really looking for something that will set a class on an individual label field - but for the whole form is better than no class! |
At the moment I can add my own classes to the form itself and to any form field, if I use a
Layout
I can also add arbitrary attributes if needed. But it seems I can't change the label class unless I write a custom template.It would be good to have a way to do this - a lot of the headaches with form styling comes when we need to nest CSS, so anything we can do to avoid this is good.
The text was updated successfully, but these errors were encountered: