Viewers: in countries Watching now:
In addition to adding single images throughout the content of a post or a page, WordPress allows you to add image galleries in your post and pages. Image galleries are collections of small thumbnails of images with optional captions that are displayed in a grid pattern. And then when you click on an image you open a larger version of it. Out of the box underscores this knot style image galleries. So you end up with what you see here which is just a vertical list of images. So I'm just going to show you how to apply basic styling to the gallery to make it look like a grid pattern and also deal with the captions.
So here's what I want to do. I want to take each of the images and stack them one next to the other in a nice grid pattern. And I want to grab the caption, style it so that it matches the caption for our single images, and place the caption inside the image, so that when you hover over the image, it appears, and if you don't hover over the image, it disappears. That, of course, will cause us to truncate some of the captions like this really long one. But that's quite all right, because once you go and look at the whole image, you'll get the whole caption. So here, we could start in several different places.
I'm just going to start at the top. First, I need to find the captioning code inside style.css. And you'll find it right here in the media section, directly under the captioning. And the first thing I'm going to do is make sure that the captioning styling that we already created is inherited by the galleries as well. So here we have W-P caption, caption text. I'm going to add a comma here and I'm also going to apply this to gallery caption. And that way we'll at least get the same font and other content on our captioning.
Then I need to go to my code snippets. And here you see I have a list of styles that together work to create a proper gallery. So first I'm going to align each of the gallery items using this first class here, site-main.gallery-item. So I'll copy it out and paste it in directly below site-main.gallery here. So this will target each of the items within my gallery. And here you see we're setting the position to relative, we're setting the max width to 100% so we're not expanding the images beyond their size.
And I set a margin right and then I float everything to the left so that way we'll get this stacked gallery pattern. I'll save this new style and go and check that out in my browser. And now, you see were getting the image of stacking but our captions are causing a lot of problems. So, the next thing we need to do is position the captions absolutely inside the image and that way they will only take up the space of the image itself and we won't have this alignment problem anymore. Back in the code snippets, you'll see I have a huge rule just for the captions.
So I'll copy this one out and paste it in right here where it says gallery caption. And this new rule, positions the gallery caption absolutely to the bottom and left side of the image, and then sets a max height of 50% and a width of a 100%, and does some other things. Most importantly it sets the background color of the caption to black but with a .8 opacity, so you can kind of see through it, and it sets the overall opacity of the caption to zero.
That means the caption will be hidden by default. Of course, since we're hiding it by default, I need to add one more rule here. So when we're hovering over the gallery item, we set the gallery caption opacity to one so that it becomes visible. I'll save that, go back and reload the page. And now you'll see each of our images displays, and when I hover over them you see the captioning. And now we just have one problem left. As you can see, the images look really strange right now. They're kind of squished sideways and we also have this reverse L shape with the background showing through.
Now, the reason why this is happening is because of an existing style within underscores. If we go back to our styles and scroll down you'll see here we have site-main.gallery a img. So any image that's inside a link, so that would be all the images. And then you see the max width is set to 90% and that means the image gets squished in by 90% which is bizarre. I have no idea why it's doing that. So I'm going to remove that style and replace it with a style that actually makes sense. That sets the max width to 100% and also does some other things.
So I'll copy this out, replace this final rule here. So now we set the max width to 100%, set the height to auto so that it scales properly. I'll display each item as a block and I'll also set the margin inside the image to zero and auto so that it's center aligned. I'll save that and reload my page one more time. And now our image gallery looks the way it's supposed to. If I hover over an image, I see the caption and I can click on any of the images to go to that image.
There are currently no FAQs about WordPress: Building Themes from Scratch Using Underscores.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.