May 17, 2019
I'm working on a website which has icons on file upload links, like so:
This works for people who can see the icons, but what about those who can't? Accessibility means giving all users an equivalent experience, regardless of how they're accessing the site.
If the icons were inline images, we could add alt text with the filetype. However, in this case, they're background images.
So, we want to…
Continue reading Add file type indicator to file field links for accessibility - Drupal 8-10
April 23, 2019
By default, the label for the core Search module text input in Drupal 8 is invisible, that is, it's present but visually hidden so users of assistive technology can read it. But what if you want to display it visually?
There's no way to do it through configuration, and little to no information about how to do it with code. I found where the visibility was being set, in SearchBlockForm.php in the…
Continue reading How to make core search block form label visible - Drupal 8
April 17, 2019
Data attributes on HTML elements can be very handy for targeting certain elements with CSS and/or Javascript.
Let's say, for example, we have a site with a menu whose links have a different background colour depending on what category they belong to, cats or dogs. The HTML might look like this:
<ul class="menu"> <li class="menu-item"> <a…
Continue reading How to add title as data attribute to menu links in Drupal 8 and Twig
March 26, 2019
I've been working on a website with the requirement to truncate certain text fields—display the first several words of the text, with a "show more" link which on click, expands the rest of the text. I had to dig a bit into the Twig documentation to figure it out, but it's easily done with Twig filters and a bit of JavaScript.
(Note that this method will strip any HTML in the text fields. It…
Continue reading Truncated expanding text field with "show more" link in Drupal 8 & Twig
August 4, 2016
(Note that this technique is totally deprecated by Flexbox and CSS Grid, but it's interesting for historical purposes, I guess).
If you've ever tried to do it, you know that vertically centring elements in CSS is notoriously difficult. Horizontal centring is a relative breeze, but vertical is another story.
I often have a use-case where I want to vertically centre some text on top of an image,…
Continue reading How to vertically center text in an absolutely-positioned div over an image