Do you want to bulk edit featured images in WordPress?
It’s easy to add, change, and remove featured images from your WordPress posts and pages. However, changing the featured images for multiple pages can take a lot of time and effort.
In this article, we will show you how you can save time by bulk editing featured images in WordPress.
Why You Might Need to Bulk Edit Featured Images in WordPress
Featured images, also known as post thumbnails, are an important part of many websites.
Almost all modern WordPress themes come with built-in support for featured images and display them across different areas of your WordPress website including your blog archives.
As an important part of your WordPress blog or website, you’ll want to make sure your featured images look good and represent your brand.
With that in mind, at some point you may need to bulk edit your featured images in WordPress. For example, you might install a new WordPress theme and decide to design new featured images that better compliment your new theme.
You can change the featured image for an individual page or post in the standard WordPress editor. However, changing the featured image for lots of different pages and posts can be frustrating and time-consuming.
With that in mind, let’s see how you can bulk edit featured images in WordPress.
How to Bulk Edit Featured Images in WordPress
The easiest way to bulk edit featured images in WordPress is by using the Quick Featured Images plugin.
This plugin makes it easy to change every single featured image across your website. It also has detailed filters that allow you to change the featured images for specific categories, tags, post types, and more.
Before you start using the Quick Featured Images plugin, just be aware that this plugin doesn’t allow you to undo your bulk edits. This can cause problems if you change your featured images and are unhappy with the results.
With this in mind, it’s a good idea to backup your WordPress website before using this plugin. If you don’t already have a backup solution, then there are several great WordPress backup plugins that allow you to automatically backup your website.
Upon activation, go to Featured Images » Overview in the left-hand menu. On this screen, click on the following link: ‘Bulk set, replace and remove featured images for existing posts.’
This screen allows you to bulk edit your featured images in a few different ways.
Your first option is the ‘Set the selected image as a new featured image’ radio button.
This setting finds all of the posts and pages that don’t currently have a featured image. You can then choose a new featured image and apply it to all of these pages and posts.
Another option is ‘Replace featured images by the selected image.’ You can use this to replace any existing featured images with a new image.
Next up is ‘Remove the selected image as featured image.’ This lets you choose a specific image, and then remove that featured image wherever it appears on your website. This is perfect for removing an outdated featured image, such as any image that features your site’s old logo.
After selecting one of these radio buttons, click on the Choose Image button.
You can now choose the image that you want to use in the bulk edit. You can either select an image from your WordPress media library or upload a new file.
Once you’ve done that, scroll to the bottom of the screen and click on the ‘Next’ button.
On this screen, you’ll be able to configure how WordPress performs the bulk edit.
The options you see may vary depending on whether you’re adding, changing, or removing a featured image. For example, if you selected the ‘Set the selected image as new featured image’ radio button then you can choose whether to override all existing featured images, or ignore them.
To select which posts your bulk changes will apply to, scroll to the ‘Add a filter’ section.
By default, Quick Featured Images will apply its bulk edit to all pages and posts, across all of your site’s categories and tags.
If you don’t want to bulk edit every page and post, then you can create filters. These filters let you bulk edit featured images that meet a specific criteria.
You can check more than one box. For example, you might check the ‘Post Type Filter’ and ‘Tag Filter’ boxes if you want to bulk edit posts that have a specific tag.
After checking one or more boxes, go ahead and click on the ‘Next’ button.
You will now see some settings that you can use to create your filter. For example, if you checked the ‘Post Type Filter’ box then you can choose between the ‘Posts’ or ‘Pages’ box.
If you checked either the ‘Category Filter’ or ‘Tag Filter’ box, then you’ll have access to a dropdown menu.
You can open this dropdown and then choose the category or tag where you want to make the bulk edit.
When you’re happy with how your filter is set up, scroll to the bottom of the screen.
You can then go ahead and click on the ‘Preview filtering’ button.
The Quick Featured Images plugin will now show a preview of all the pages and posts that will be affected by the bulk edit.
If you’re happy with the preview, then you can go ahead and click on the ‘Apply’ button.
Quick Featured Images will now bulk edit all of the relevant featured images across your WordPress website.
How to Set Multiple Images Randomly as Featured Images
When you bulk edit featured images in WordPress, you can sometimes end up using the same featured image for lots of different posts. These duplicate images can make your website look boring and repetitive.
With that in mind, you may want to set your featured images at random. You can use the Quick Featured Images plugin to select different images from your WordPress media library. The plugin will then add these featured images to your pages and posts at random.
In this way, you can bulk edit your images without creating lots of duplicate featured images.
To set your featured images at random, go to Featured Images » Overview and again click on ‘Bulk set, replace and remove featured images for existing posts’.
Once you’re here, click on ‘Set multiple images randomly as featured images.’
You can then click on the ‘Choose Images’ button to open the WordPress media library.
Here, hold down the Shift key and then click all the images that you want to randomly use as your featured images.
Once you’ve done that, click on the ‘Choose Images’ button. Then, scroll to the bottom of the screen and click on ‘Next.’
By default, Quick Featured Images will apply these images to all your WordPress pages and posts at random.
Do you just want to use these images for certain content types, categories, or tags? Then you can create a filter by following the same process described above.
After adding a filter, Quick Featured Images will show a preview of all the pages and posts that it plans to update. You can check the thumbnail to see which image the plugin has randomly assigned to each page or post.
If you’re happy with the preview, go ahead and click on ‘Apply.’ Quick Featured Images will now add the new images to your pages and posts.
How to Bulk Remove Featured Images in WordPress
Sometimes you may prefer not to use featured images on your website. For example, you might be trying to speed up your WordPress performance, or you may be using a theme that simply doesn’t look good with featured images.
You can use Quick Featured Images to easily remove all the featured images from your pages and posts.
You can also use filters to remove the featured images from pages, posts, or content that has a particular tag or category.
To bulk remove featured images in WordPress, simply click on the ‘Remove all featured images without existing image files’ radio button. You can then go ahead and click on ‘Next.’
Now, you can apply filters by following the instructions above.
Once you’ve created one or more filters, go ahead and click on the ‘Next’ button.
As always, the plugin will show a preview of all the pages and posts that it will change as part of this bulk edit. Since you’re deleting the featured image, it will show ‘No image set’ in the ‘Future Featured Image’ column, rather than a thumbnail preview.
If you’re happy to go ahead and remove the featured image for these pages and posts, then just click on the ‘Apply’ button.
Our 5-star free form creating plugin, Forminator, has a few advanced features that you can implement today for your WordPress site.
For example, he has pagination for Knowledge & Personality Quizzes! This allows you to show quiz questions one at a time, or all the questions at once. The pagination feature helps further determine how your quiz will be displayed and used.
Plus, Forminator also has Bulk Editing, image support in Checkbox and Radio fields, and reCAPTCHA badge positioning!
This article will show you pagination support and rundown other features so that you can implement them for your WordPress site today.
By the time you read through this, you’ll hopefully have some ideas to put these features immediately to work!
Creating a New Paginated Quiz
If you want to display several questions for your knowledge quizzes at a time, then you’ll want to use Quiz Pagination. As you’ll see, creating a quiz with pagination is as easy as ever with Forminator.
You’ll start just like usual by making a new quiz. Just hit Create…
…then, give it a Quiz Name. For this example, we’ll create a Knowledge Quiz.
At this point, Forminator will ask you how you want to present your quiz. Since we’re focused on pagination, we’ll choose the Paginated Quiz option and hit Continue.
Once you continue, you’ll also have the option to collect leads. That’s up to you and won’t have any effect on your pagination options.
After creating the quiz, you can set it up like a regular quiz (e.g. adding a title, featured images, etc.). Additionally, add your questions.
If you need help getting a quiz set up in detail, check out our Getting the Most Out of Forminator article to walk you through the process.
Editing Pagination
I’ve set up some questions and am now ready to edit the pagination (and again, if you need help setting up questions, please refer to this article).
Editing pagination is done from the Behaviour tab.
Once you’re here, you have the option No Pagination or Paginated Quiz. This is so you can remove pagination if you decide not to use it in the future.
For this article, we’re going with Paginated Quiz.
Paginated Quiz then lets you determine the number of questions per page, the Start Quiz Button text, and the text for previous & next buttons. You can also check the box if you want to show a page indicator.
It’s as simple as filling in the details in the spaces provided.
When the pagination details are entered, the rest can be created like any other knowledge quiz. That includes:
Results Display Method (real-time or on submission)
Option to Show Evaluation Loader
Correct Answer Message
Incorrect Answer Message
Social Sharing Options
Rendering Options (load quiz using AJAX and prevent page caching on quiz pages)
Set the quiz up as you feel appropriate, and then you’re on to…
Previewing the Quiz
When you have your quiz configured to your standards, click Preview to see what it looks like. Let’s check it out!
I uploaded a cover image and titled it Forms and Superheros, and added a description. This is how the quiz begins…
And here’s a quick walkthrough where I have a correct — and incorrect — answer. Plus, it’ll show me the results. Keep in mind that this is a two-question quiz that I wanted on the same page.
As you can see, Forminator lets you know when you have the answer right or wrong immediately.
When you click View Results, Forminator lets you know exactly how many you got right and wrong. Whew! This was a tough one.
If the preview looks good, like any quiz, hit Publish, and Forminator will deliver a shortcode that can be used on any page, post, or acceptable widget.
And that’s it!
If you need to, you can always edit the quiz at any time through Forminator’s dashboard. Otherwise, your paginated knowledge quiz is ready to go.
Bulk Editing
Another great advanced editing feature in Forminator is Bulk Editing. Bulk editing lets you add a large number of options at once. This makes the process of editing more streamlined, efficient, and quick.
This feature works with the Radio, Checkbox, and Select fields.
When it comes to bulk editing, you have three options. You can bulk edit:
Manually
Via CSV File
Predefined Options
The process to get this implemented is the same in all of the fields. Bulk Edit is located in the Labels tab in each respective field.
To get started, simply click the Bulk Edit option.
One-click is all it takes to get moving.
Once implementing Bulk Edit, any existing options you have manually created will automatically populate in the Bulk Edit section.
Let’s take a look at each option when it comes to bulk editing.
Manually Entering Bulk Options
This option is great if you want to manually enter or paste your list of options into the Bulk Edit area. When doing this, enter them one per line with each term separated by a semicolon in this order:
Option Label;
Option Value;
Selected;
Image URL (this can be blank if you have no image)
Or, you can enter Label only. By doing this, Forminator will generate the value automatically and set selected to zero. Plus, it won’t add an image URL.
An example of this might look like this…
Manually entering options is best for adding just several options at a time in bulk.
Adding Options Via CSV File
When you have a ton of options on a CSV file, you can quickly bulk upload these to Forminator.
What if you don’t have a CSV file? Download Forminator’s CSV template. It has a list of examples that you can edit to include your own options.
The example file is fundamental, with just a few pieces of information. Edit however you want, and then click Upload File once you have edited and saved the CSV file. Then, hit Import to add all of your options into the Bulk Edit section.
Keep in mind that uploading will replace any existing options that you have in place.
Predefined Options
Forminator has quite a few predefined options you can use in an instant. They’re all available via the dropdown menu.
The dropdown menu has them all.
Choose from the dropdown menu:
Age
Gender
Educational Attainment
Employment Status
Occupation
Marital Status
Continents
Countries
American States
Canadian Provinces
Plus, you can add New Option, which will allow you to input your data.
The predefined options make it quick and easy to bulk edit whatever field you’re working in.
Additional Advanced Features
There are several more advanced features for Forminator that you may find helpful. These were all included in the release of 1.14.12, and include ImageSupport for Checkbox & Radio Field, HTML Quiz Description Field, and Setting reCAPTCHA Badge Position.
We’ll quickly check out each one of these and demonstrate how they work.
Image Support in Checkbox and Radio Field
Forminator now gives you the option to upload an image to the Checkbox and Radio fields. This can help forms become much more visually appealing and easier to set up than uploading an image separately for a form.
Go into the form field, and you’ll see that under the Labels tab, there’s a checkbox you can click to Enable Images.
Once enabled, you simply go to the Options, click the dropdown by any option you want an image in, and upload a photo.
Once you have your images uploaded, click Apply, and you’re all set! Your images will be included with your Checkbox or Radio field.
You can always edit and adjust your images at any time.
HTML Quiz Description Field
Add descriptions for each individual quiz answer in Forminator’s HTML Quiz Description Field. This feature helps clarify any answer for your users and can help make for a more engaging quiz.
All you do is type it in the Description area in each individual question field.
Once you have the description entered, click Apply. Your description is now added!
Set reCAPTCHA Badge Position
This is a fast adjustment that can be made regarding the reCAPTCHA position. It puts you in control of where your reCAPTCHA is displayed.
You just go into your reCAPTCHA field, click Settings, and choose where you want your badge to appear on your site.
Choose from these specific areas:
Bottom Right
Bottom Left
Inline in Form
Once you select where you want your badge to appear, click Apply.
Keep in mind that this only works with V2 Invisible and reCAPTCHA V3 options.
And for even more about using reCAPTCHA, please read our article covering incorporating it with our security plugin, Defender.
Pagination is Quite the Sensation!
If you think features like pagination are sensational, Forminator is constantly adding new beneficial features and enhancements. Be sure to follow our Roadmap to see what’s next!
And now that you know how, implement these advanced features (for free!) and give them a try today.
Dark website designs are where it’s at. Don’t believe me? They’ve become increasingly popular over the past year. Dark designs are thought to be easier on eye strain and make for a more pleasant viewing experience. They can also add atmosphere to your site.
As more and more websites have shifted to dark mode in 2020, we thought it imperative to highlight some designs that really capture the concept of dark designs well. What follows is a list of over 20 delightfully dark website designs you’ll definitely want to check out.
The Serge Thoraval: Atelier website doesn’t just offer a dark design — it also features some really interesting effects. From a mouse hover effect when the site is loading to transition effects when you scroll down the page, this site sets itself apart.
This site offers a dark design that makes each portfolio piece really stand out. Plus, when you hover over each item, they color shift and descriptive text scrolls across it from right to left.
DevArt is another site with a dark design that helps to make the artwork it features stand out. It’s also easier on the eyes than sites with brighter backgrounds.
The Creative Park website offers up a delightfully dark design that allows the subject of photos to really stand out. It also has some really interesting transition effects for changing slides, clicking links, and opening menus.
The 3D Hubs website leverages its dark design to make the components it features stand out. This also works well for displaying video and individual items the company offers.
Welford Media is a web design firm that uses a dark design on its website to engage visitors. This design choice allows the company’s tagline stand out at a glance and builds immediate interest.
The website for the Meaning 2020 Conference has a dark design as well. The gray background allows the colorful logo to stand out and offers a subdued backdrop onto which videos and photos are placed.
This is a website dedicated to the work of Rich Brown, an art director and UX/UI designer. The site itself features a dark design onto which video backgrounds play seamlessly.
The design on this website is super interesting. Artem Pivovarov has a dark design with a prominent photo background. It also has interactive elements in this background that react on hover.
Formigari Srl is another site you should check out for dark design inspiration. It highlights videos and photos on a deep gray background that makes the entirety of the content feel immersive.
Still another site you should check out is Canvas United. This site is for a New York-based digital agency that provides some cool transition effects and an immersive experience.
The Resoluut website offers a dark purple background with images that appear as though they’re hovering on top of it. There is a lot of play with depth of field here.
Still another option is the Fantassin website, which features a dark background and tons of cool scroll effects that draw the eye further down the page.
The Cody Petts Studio website highlights the work of Cody Petts, including print and packaging designs as well as photography. It’s dark, gritty, and extremely compelling.
The design for the Kazuma Kurata website offers a dark background that’s intimately inviting. It makes the colorful Aspects of featured photos stand out even more. The design and function of the site make it clear why it’s deserving of its AWWWARDs Nominee status.
The Bachoy website highlights a creative and tech studio. The dark design featured here uses a cool spotlight hover effect and interesting interactions with photos as you scroll down the page.
Last on our list is the website for Arara, which offers battery-free bicycle lights. This site is dark but not brooding, and instead uses the darker background to make the bright colors featured in images and videos “pop.”
Dark Website Designs Can Serve You Well
As you can see from the examples listed above, dark website designs can really take the overall look of your website to the next level — whatever that means for you. Whether you want to make colors look more vibrant, ease eye strain, or provide a mood, dark designs offer flexibility in both form and function. Have fun playing with them!
I was recently working on a modern take of the blogroll. The idea was to offer readers a selection of latest posts from those blogs in a magazine-style layout, instead of just popping a list of our favorite blogs in the sidebar.
The easy part was grabbing a list of posts with excerpts from our favorite RSS feeds. For that, we used a WordPress plugin, Feedzy lite, which can aggregate multiple feeds into a single time-ordered list — perfect for showcasing their latest offerings. The hard part was making it all look awesome.
The plugin’s default list UI is rather bland, so I wanted to style it to look like a newspaper or magazine website with a mixture of smaller and larger “featured content” panels.
This seems like an ideal case for CSS Grid! Create a grid layout for different layouts, say, one five-column layout and one three-column layout, then use media queries to switch between them at different break points. Right? But do we actually need those media queries — and all the hassle of identifying break points — when we can use grid’s auto-fit options to automatically create a fluid responsive grid for us?
The approach sounded tempting, but when I started introducing column-spanning elements, I ran into trouble with the grid overflowing on narrow screens. Media queries appeared to be the only solution. That is, until I found a workaround!
After looking at several tutorials on CSS Grid, I found that they largely fall into two camps:
Tutorials that show you how to create an interesting layout with spanned elements, but for a fixed number of columns.
Tutorials that explain how to make a responsive grid that resizes automatically, but with all of the grid items the same width (i.e. without any spanned columns).
I want to make the grid do both: create a fully responsive fluid layout that includes responsively resizing multi-column elements as well.
The beauty is that once you understand the limitations of responsive grids, and why and when column spans break grid responsiveness, it is possible to define a responsive magazine/news style layout in just a dozen lines of code plus one simple media query (or even with no media queries if you are willing to limit your span options).
Here’s a visual showing the RSS plugin right out of the box and what it’ll look like after we style it up.
This magazine-style grid layout is fully responsive with the colored featured panels adjusting dynamically as the number of columns change. The page displays around 50 posts, but the layout code is agnostic as to the number of items displayed. Ramp up the plugin to show 100 items and the layout stays interesting all the way down.
All of this is achieved using only CSS and with only a single media query to deal with a single column display on the narrowest of screens (i.e. smaller than 460px).
Incredibly, this layout only took 21 lines of CSS (excluding global content styling). However, to achieve such flexibility in such a few lines of code, I had to dig deep into the more obscure parts of some of CSS Grid and learn how to work around some of its inherent limitations.
The essential elements of the code that produce this layout is incredibly short and a testament to the awesomeness of CSS Grid:
The techniques in this article could be used equally well to style any dynamically generated content such as the output from a latest posts widget, archive pages or search results.
Creating a responsive grid
I have set up seventeen items displaying a variety of mock content — headlines, images and excerpts — which are all contained in a wrapper
The code that turns these items into a responsive grid is remarkably compact:
.archive {
/* Define the element as a grid container */
display: grid;
/* Auto-fit as many items on a row as possible without going under 180px */
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
/* A little spacing between articles */
grid-gap: 1em;
}
Notice how the heights of the rows automatically adjust to accommodate the tallest content in the row. If you change the width of the Pen, you will see the items grow and shrink fluidly and the number of columns change from one to five, respectively.
The CSS Grid magic at play here is the auto-fit keyword that works hand-in-hand with the minmax() function that’s applied to grid-template-columns.
How it works
We could have achieved the five-column layout alone using this:
However, this would create five columns that grow and shrink with different screen widths, but always stay at five columns, resulting in them becoming ridiculously narrow on small screens. The first thought might be to create a bunch of media queries and redefine the grid with different numbers of columns. That would work fine, but with the auto-fit keyword, it is all done automatically.
For auto-fit to work the way we want, we need to use the minmax() function. This tells the browser how small the columns can be squeezed down to followed by the maximum width they can expand to. Any smaller, and it will automatically reduce the number of columns. Any larger, and the number of columns increases.
In this example, the browser will fit in as many columns as it can 180px wide. If there is space left over the columns will all grow equally by sharing the remaining space between them — that’s what the 1fr value is saying: make the columns equal fractions of the available width.
Drag the window out and as the available space increases the columns all grow equally to use up any additional space. The columns will keep growing until the available space allows for an additional 180px column, at which point a whole new column appears. Decrease the screen width, and the process reverses, perfectly adjusting the grid all the way down to a single column layout. Magic!
And you get all this responsiveness out of just one line of code. How cool is that?
Creating spans with “autoflow: dense”
So far, we have a responsive grid but all items the same width. For a news or magazine layout we need some content to be featured by spanning two or more columns or even, perhaps, to span all the columns.
To create multi-column spans we can add the column-span feature to the grid items we want to take up more space. For example, if we want the third item in our list to be two columns wide we can add:
.article:nth-child(3) {
grid-column: span 2;
}
However, once we start adding spans a number of problems can arise. First, gaps may appear in the grid because a wide item may may not fit on the row, so grid auto-fit pushes it onto the next line, leaving a gap where it would have been:
The easy fix is adding grid-auto-flow: dense to the grid element which tells the browser to fill in any gaps with other items, effectively making the narrower content flow around the wider items like this:
Note that the items are now out of order, with the fourth item coming before the third item, which is double the width. There is no way round this as far as I can tell, and it is one of the limitations you have to accept with CSS Grid.
There are several ways to indicate how many columns an item should span. The easiest is to apply grid-columns: span [n] to one of the items, where n is the number of columns the element will span. The third item in our layout has grid-column: span 2, which explains why it is double the width of other items that only span a single column.
Grid lines can be specified from left-to-right using positive values (e.g. 1, 2, 3) or negative values (e.g. -1, -2, -3) to go from right-to-left. These can be used to place items on the grid using the grid-column property like this:
So, this gives us additional ways to specify a spanned item. This is especially flexible as either the start or end value can be replaced with the span keyword. For example, the three-column blue box in the example above could be created by adding any of the following to the eighth grid item:
grid-column: 3 / 6
grid-column: -4 / -1
grid-column: 3 / span 3
grid-column: -4 / span 3
grid-column: span 3 / -1
Etc.
On a non-responsive (i.e. fixed columns) grid, these all produce the same effect (like the blue box above), however, if the grid is responsive and the number of columns changes, their differences start to become apparent. Certain column spans break the layout with an auto-flowing grid, making the two techniques appear incompatible. Fortunately, there are some solutions which allow us to combine the two successfully.
First, however, we need to understand the problem.
Overflow side-scrolling problems
Here are some featured areas created using the notation above:
It all looks good at full-width (five columns) but when resized to what should be two columns, the layout breaks like this:
As you can see, our grid has lost its responsiveness and, although the container has shrunk, the grid is trying to maintain all five columns. To do so, it has given up trying to keep equal-width columns, and the grid is breaking out of the right-hand side of its container, causing horizontal scrolling.
Why is this? The problem comes about because the browser is trying to honor the explicit grid lines we named. At this width, the auto-fit grid should implicitly be displaying two columns, but our grid line numbering system contradicts this by explicitly referring to the fifth grid line. This contradiction leads to the mess. To display our implicit two-column grid correctly, the only line numbers allowed are 1, 2 and 3 and -3, -2, -1, like this:
But if any of our grid items contains grid-column references that lie outside this, such as grid line number 4, 5 or 6 (or -4, -5 or -6), the browser is getting mixed messages. On the one hand, we have asked it to automatic create flexible columns (which should implicitly give us two columns at this screen width) but we have also explicitly referred to grid lines that don’t appear in a two-column grid. When there is a conflict between implicit (automatic) columns and an explicit number of columns, grid always defers to the explicit grid; hence the unwanted columns and horizontal overflow (which has also been aptly named CSS data loss). Just like using grid line numbers, spans can also create explicit columns. So, grid-column: span 3 (the eighth grid item in the demo) forces the grid to explicitly adopt at least three columns, whereas we want it, implicitly display two.
At this point it might seem like the only way forward is to use media queries to change the grid-column values at the width where our layout breaks — but not so fast! That’s what I assumed at first. But after thinking it though a bit more and playing around with various options, I found there are a limited set of workarounds that work all the way down to two columns, leaving just one media query to cover a single column layout for the narrowest screens.
The solutions
The trick, I realized, is to only specify spans using grid lines that appear in the narrowest grid you intend to display. That is a two-column grid in this case. (We will use a media query to cover the single column scenario for very narrow screens.) That means we can safely use grid lines 1, 2 and 3 (or -3, -2 and -1) without breaking the grid.
I initially thought that meant limiting myself to a maximum span of two columns, using combinations of the following:
grid column: span 2
grid-column: 1 /3
grid-column: -3 / -1
Which remains perfectly responsive right down to two columns:
Although this works, it is rather limiting from a design perspective, and not particularly exciting. I wanted to be able to create spans that would be three, four or even five columns wide on large screens. But how? My first thought was that I would have to resort to media queries (OMG old habits die hard!) but I was trying to get away from that approach and think differently about responsive design.
Taking another look at what we can do with just 1 to 3 and -3 to -1, I gradually realized that I could mix positive and negative line numbers for the grid column’s start and end values ,such as 1/-3 and 2/-2. At first glance, this does not seem very interesting. That changes when you realize where these lines are located as you resize the grid: these spanned elements change width with the screen size. This opened up a whole new set of possibilities for responsive column spans: items that will span different numbers of columns as the screen gets wider, without needing media queries.
The first example I discovered is grid-column: 1/-1.This makes the item act like a full-width banner, spanning from the first to the last column at all column numbers. it even works down to one column wide!
By using grid-column: 1/-2, a left-aligned nearly-full-width span could be created that would always leave a one column item to the right of it. When shrunk to two columns it would shrink responsively to a single column. Surprisingly, it even works when shrunk to a single column layout. (The reason seems to be that grid will not collapse an item to zero width, so it remains one column wide, as does grid-column: 1/1.) I assumed grid-column: 2/-1 would work similarly, but aligned with the right-hand edge, and for the most part it does, except at one column display when it causes overflow.
Next I tried 1/-3 which worked fine on wider screen, showing at least three columns, and smaller screens, showing one column. I thought it would do something weird on a two-column grid as the first grid line is the same as the grid line with -3. To my surprise, it still displays fine as a single-column item.
After a lot of playing around, I came up with eleven possible grid column values using grid line numbers from the two-column grid. Surprisingly, three of these work right down to single-column layouts. Seven more work down to two columns and would only need a single media query to deal with single column display.
Here is the full list:
As you can see, although this is a limited subset of every possible responsive span, there are actually a lot of possibilities.
2/-2 is interesting as it creates a centered span which works all the way down to one column!
3/-1 is least useful as it causes overflow even with two-columns.
3/-3 was a surprise.
By using a variety of grid-column values from this list, it is possible to create an interesting and fully responsive layout. Using a single media query for the narrowest single-column display, we have ten different grid-column span patterns to play with.
The single-column media query is generally straightforward as well. The one on this final demo reverts to using flexbox at smaller screens:
Here is the final grid which, as you can see, is fully responsive from one to five columns:
Using :nth-child() to repeat variable length displays
The last trick I used to get my code down to two dozen lines was the :nth-child(n) selector which I used to style multiple items in my grid. I wanted my span styling to apply to multiple items in my feed, so that the featured post boxes appeared regularly throughout the page. To start with I used a comma-separated selector list, like this:
But I soon found this cumbersome, especially as I had to repeat this list for each child element I wanted to style within each article — such as the title, links and so on. During prototyping, if I wanted to play around with the position of my spanned elements, I had to manually change the numbers in each of these lists, which was tedious and error-prone.
That’s when I realized that I could use a powerful feature :nth-child pseudo-selector instead of a simple integer as I had used in the list above. :nth-child(n) can also take an equation, such as :nth-child(2n+ 2), which will target every second child element.
Here is how I used the :nth-child([formula]) to create the blue full-width panels in my grid which appear at the very top of the page, and is repeated just over half way down:
The bit in the brackets (31n + 1 ) ensures that the 1st, 32nd, 63rd, etc. child is selected. The browser runs a loop starting with n=0 (in which case 31 * 0 + 1 = 1), then n=1 (31 * 1 + 1 = 32), then n=2 (31 * 2 + 1 = 63). In the last case, the browser realizes that there is no 63rd child item so it ignores that, stops looping, and applies the CSS to the 1st and 32nd children.
I do something similar for the purple boxes which alternate down the page from right-to-left:
The first selector is for the right-hand purple boxes. The 16n + 2 makes sure that the styling applies to every 16th grid item, starting with the second item.
The second selector targets the right-hand boxes. It uses the same spacing (16n) but with a different offset (10). As a result, these boxes appear regularly on the right-hand side for grid items 10, 26, 42, etc.
When it comes to the visual styling for these grid items and their contents, I used another trick to reduce repetition. For styles that both boxes share (such as the background-color, for example) a single selector can be used to target both:
This will target items 2, 10, 18, 26, 34, 42, 50, and so forth. In other words, it selects both the left- and right-hand featured boxes.
It works because 8n is exactly half of 16n, and because the offsets used in the two separate selectors have a difference of 8 (i.e. the difference between +10 and +2 is 8)
Final thoughts
Right now, CSS Grid can be used to create flexible responsive grids with minimal code, but this does come with some significant limitations on positioning elements without the retrograde step of using media queries.
It would be great to be able to specify spans that would not force overflow on smaller screens. At the moment, we effectively tell the browser, “Make a responsive grid, please,” which it does beautifully. But when we continue by saying, “Oh, and make this grid item span four columns,” it throws a hissy-fit on narrow screens, prioritizing the four-column span request rather than the responsive grid. It would be great to be able to tell grid to prioritize responsiveness over our span request. Something like this:
.article {
grid-column: span 3, autofit;
}
Another issue with responsive grids is the last row. As the screen width changes the last row will frequently not be filled. I spent a long time looking for a way to make the last grid item span (and hence fill) the remaining columns, but it seems you can’t do it in Grid right now. It would be nice if we could specify the item’s start position with a keyword like auto meaning, “Please leave the left-hand edge wherever it falls.” Like this:
.article {
grid-column: auto, -1;
}
…which would make the left-hand edge span to the end of the row.
When Spotify launched their colorful new brand identity, featuring hip duo-toned imagery, it went hand-in-hand with a new Colorizer tool that allows artists to apply a variety of filters to images. This solved a problem in which Spotify needed a way to present the thousands of images uploaded, all with a different look and feel, […]
One of the challenges of coding premium WordPress themes is the unpredictable nature of how they will be used. Compared to coding a custom website, especially one using static HTML documents where you have complete control over the markup, you have to solve problems creatively and ensure flexibility. In these cases, CSS selectors make all […]