Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Add CSS stylesheets to a module (CKEditor)

By editor , 18 November, 2015
Question

How to add a css file / libraries in a module.

There are several ways to do that.

1. Styling a programmatically created block.
1.1 Add a librarie.
 -> Create a file modulename.libraries.yml
-> Add css file, Exemple:
my-block-styling:
  version: 1.x
  css:
    theme:
      css/style.css: {}

1.2 Create CSS file and put it in to the module_folder/css/style.css
1.3 Attach librarie to the block
In your build() methode, add to the output array,
    $output[]['#attached']['library'][] = 'modulename/my-block-styling';

Add / Attaching a library from a Twig template

{{ attach_library('MODULENALE/my-style') }}

To remove a style sheet

stylesheets-remove:
  - '@classy/css/components/tabs.css'
  - core/assets/vendor/normalize-css/normalize.css

Add to a form:
$form['#attached']['library'][] = '<YOUR_THEME_NAME>/<library_name>';

Add CSS Style to CKEditor WYSIWYG

Example:
<?php
use Drupal\editor\Entity\Editor;
/**
 * @param array $css
 * @param Editor $editor
 */
function mymodule_ckeditor_css_alter(array &$css, Editor $editor) {
  $css[] = drupal_get_path('module', 'mymodule') . '/css/ckeditor.css';
}

 

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme

Tags

  • Drupal 8
  • Theming
  • Code
  • Drupal 9
Basic CSS Styling tips
CSS Styling tips for Print
Test a webpage for Retina display
CSS Fluid Width Video

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer