Featured images are an essential part of any WordPress website. They act as the visual representation of your post or page and significantly contribute to the overall user experience. However, one common issue site owners face is featured images that don’t fit properly within their containers. Either they overflow, appear too small, or don’t align perfectly with the page layout.
In this article, we’ll explain why ensuring your featured images are scaled and aligned correctly is important for your website’s design and user experience. We’ll also show you a quick CSS solution to centralise your featured images, add a clean gap around them, and ensure they fit perfectly within their container.
Why Featured Image Alignment Matters
- Aesthetic Appeal: A featured image that fits perfectly within its container, with some spacing around it, looks more professional and visually pleasing.
- Improved User Experience: If images are improperly sized or misaligned, it can make the page look cluttered or break the flow of content, which can negatively impact user experience.
- Mobile Responsiveness: Images that are not properly scaled may not adapt well to different screen sizes. Proper scaling and alignment ensure your featured images look great on all devices.
The Solution: Simple CSS to Fit and Centre Your Featured Image
To fix this issue, we can apply some simple CSS that ensures your featured images take up 100% of their container’s width, while remaining centralised and giving them a small padding for a professional, clean look.
Here’s the CSS you can use:
.post-thumbnail img,
.wp-post-image {
width: 100%;
height: auto;
object-fit: cover;
display: block;
margin: 0 auto; /* Ensures the image is centred */
padding: 10px; /* Adds a small gap around the image */
box-sizing: border-box; /* Ensures the padding doesn't affect the image width */
}
How to Add This CSS to Your WordPress Site
- Navigate to the WordPress Customiser:
- In your WordPress dashboard, go to Appearance > Customize.
- Find the Additional CSS Section:
- In the Customiser menu, select Additional CSS.
- Add the Code:
- Copy and paste the CSS code above into the editor.
- Publish Your Changes:
- After adding the CSS, click Publish to apply the changes to your site.
What This CSS Does:
- Width and Height: The
width: 100%ensures that the image takes up the full width of its container, whileheight: automaintains the aspect ratio. - Object Fit: The
object-fit: coverproperty makes sure the image covers the entire container without distortion or stretching. - Centred Alignment:
margin: 0 auto;centres the image horizontally. - Padding: The
padding: 10px;adds a clean gap around the image to avoid it sticking to the edges of its container. - Box Sizing: The
box-sizing: border-box;ensures the padding is included in the image’s total width, preventing any overflow.
Final Result
Once you’ve added this CSS, your featured images will:
- Fit perfectly within their container.
- Be centred horizontally on the page.
- Have a clean, consistent gap around them for a professional look.
- Be fully responsive, ensuring a great appearance across all screen sizes.
Conclusion
Featured images play a significant role in how your website looks and functions. With just a few lines of CSS, you can ensure that your images fit perfectly, are centred, and have a clean gap around them. This simple solution not only improves the aesthetics of your site but also enhances the overall user experience.
If you want your website to look polished and professional, this CSS tweak is a quick and easy way to achieve that! Let us know if you try it out and how it works for your site.