I've created a custom Block in Drupal 8 from the UI.
Is there a way to add a custom CSS style to this custom block?
Is it possible, like in Views, to click on CSS and type it in?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityEasiest way to add a custom class to a block would be to override the default block.twig.html twig template within your theme.
You'll need to copy the above file from /core/themes/stable/templates/block into your custom theme using the same directory structure.
** NB don't edit the file found in core directly!! This is bad practice and you'll lose any changes when you upgrade Drupal core. **
Clear the cache and you should now be able to edit the block template file and customize it.
Then, to load CSS, you must define an asset library. You can then load that asset library using {{ attach_library('mytheme/special-block') }}
in your Twig template (assuming the theme is called mytheme and the asset library special-block).
Helpful links:
{{ attach_library('mytheme/special-block') }}
(assuming the theme is called mytheme
and the asset library special-block
). See drupal.org/theme-guide/8/assets.
Feb 20, 2016 at 18:02
attributes.addClass('foo')
to do that. Let me add another suggestion. Then this answer will be complete, for both possible interpretations :)
Feb 20, 2016 at 18:18