-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Add a unique CSS class to body tag which uses the docs ID #4280
Comments
Hi @Dannyholt, this PR should fix your issue: It have introduced the new CC: @lex111 |
Unfortunately, this won't fix my issue as I would prefer to target specific docs via CSS whereas the What I'm after is let's imagine we have a doc called <div class="main-docs-wrapper hello">...</div> Which you could then use to style elements for that doc only. For example: #__docusaurus .hello .markdown ul {
column-count: 2;
} |
@Dannyholt Yeah, you are right, I didn't get the problem which you have correctly. As a temporary workaround, if change should apply to only content of one, selected page, you can add Here you can see the example of React Native components grid: |
This is quite related to #4322 We could add some unique classname to any doc/blog/markdown page to allow to target it more easily. |
Closing since partially it is implemented in #4511 |
Reopening, because we still can't use CSS to target a specific doc page. I think there's value in using doc ID as class name of each doc's wrapper. |
The wrapper is a nested div, it's not the most flexible way to add a class. What if the user wants to change the Docusaurus layout but only for a given doc instance, for a blog post, or a specific doc id? I know those are quite unusual edge cases, but it's better if we apply those classes to the HTML tag: it would be more flexible and always suit all use-cases. Some fancy use-cases can be: "Use a different navbar background color for docs vs blog" The way we currently apply classes to the layout (pageClassName/wrapperClassName) is not the best option. We can also see the problem in #6823 (comment) where it's not easy to split the doc routes into multiple layers and still prevent useless layout unmounts/remounts. We don't have an equivalent to I think it's worth doing a breaking change on those CSS class names, moving them at the top HTML element, so that:
|
Oh yeah, when I say "wrapper" I don't necessarily mean one particular div, but just any enclosing element. Applying it to If you are open to a breaking change, then why not 👍 |
As part of #6933, a doc route will now have the following classes on the top-level HTML element:
This means you should be able to write: .plugin-docs.docs-doc-id-introduction .label {
color: red
} Are these class names looking good to you? Do you have a better suggestion? Would using data-attribute be a better fit or a good-enough alternative? [data-docs-doc-id-introduction] .label {
color: red
} Also, the "docs version" selector |
The version html className is configurable through config so I guess it's good enough for now. We'd probably need some logic to sanitize random strings to valid classNames at some point though |
also thinking we could add a it should also be possible to add the current sidebar to HTML classes? 🤷♂️ Let me know if you have a good use-case for this |
Our pages plugin has an (undocumented) |
Our layout has also this undocumented I think it also makes sense to keep it and be able to apply a className to the layout wrapper, just above the We could also generalize a But it may be complicated because the doc layout is declared above the doc itself (to prevent unmount/remount) so it can't easily apply a custom class to the parent layout. Same problem as #6823 (comment) |
is it documented how to set a class name on the root? I am trying to add custom styles to the navbar of a page created through Layout component. |
@Samrose-Ahmed you can definitively do that. See https://docusaurus.io/tests : each section has a different navbar border, because each plugin/page has a custom HTML className to target more selectively For custom React pages you'd have to use the following: <Head>
<html className="my-custom-page-className"/>
</Head> Note with Head, one HTML className overrides attribute of parents, in case you want to "compose" and avoid overriding a className set upper in the React tree, you'd have to use our undocumented helper: import {HtmlClassNameProvider} from '@docusaurus/theme-common'; |
@slorber is it possible to do the same on |
Similarly you can copy/paste our |
🚀 Feature
Can we get some CSS classes to the
body
tag or maybemain-wrapper
which uses the docs id. This would allow us to style specific docs without impacting other docs on the site which may not want/need that styling. Similar to what WordPress does on theirbody
tag which uses the page or posts unique ID.The examples below grab the
getting-started
class from a doc with an ID of getting-started.Have you read the Contributing Guidelines on issues?
Yes
Motivation
Currently we have a sidebar category called Getting Started which has a several docs such as System Requirements, Installation, Configuration and so forth. We also have a getting-started.md doc which has a bunch of information and a list of all docs in the Getting Started category.
This list is quite long and so would benefit from some simple CSS to style the list items into two columns. At the moment, this isn't really possible without impacting other lists on the site.
Pitch
This could be used in a variety of ways not just the simple scenario I explained above. I'm sure other users would find having a unique CSS class for a particular docs helpful in other styling scenarios as well.
The text was updated successfully, but these errors were encountered: