{"id":134067,"date":"2014-11-22T08:00:40","date_gmt":"2014-11-22T13:00:40","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=134067"},"modified":"2022-02-11T15:51:26","modified_gmt":"2022-02-11T15:51:26","slug":"image-styling-css","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/image-styling-css\/","title":{"rendered":"How to Style Images On Your WordPress Website With CSS"},"content":{"rendered":"<p>I&#8217;m sure you\u00a0already have an idea of\u00a0how important images are to the success of your website.<\/p>\n<p>But when it comes to boosting engagement (and ultimately your bottom line), it&#8217;s not just about including images on your site, it&#8217;s about\u00a0what images you include and how you present them.<\/p>\n<p>In this post I want to tackle one important piece of that puzzle: the presentation.<\/p>\n<p>WordPress comes pre-packaged with simple means of styling images with CSS. You\u00a0have the power to create different effects for\u00a0the various\u00a0CSS classes that WordPress automatically assigns to images on your site. (If it seems like I&#8217;ve started talking in a foreign language, don&#8217;t worry, I&#8217;ll explain everything in detail later in the article.)<\/p>\n<p>I&#8217;m going to start by delving into the reasons why images are so important\u00a0to your site, then move\u00a0on to cover the\u00a0default CSS classes that WordPress applies to images, and finally demonstrate how you can use CSS to apply custom styles to your images.<\/p>\n<ul>\n<li><a href=\"#why-images-are-important\">Why Images Are So Important for Websites<\/a><\/li>\n<li><a href=\"#css-crash-course\">A WordPress CSS Crash Course<\/a><\/li>\n<li><a href=\"#add-custom-css\">How to Add Custom CSS to WordPress<\/a><\/li>\n<li><a href=\"#default-css\">The WordPress Default CSS Image Classes<\/a><\/li>\n<li><a href=\"#styling\">Simple Image Styling With WordPress<\/a><\/li>\n<li><a href=\"#more-examples\">More Examples of What You Can Do With Image CSS in WordPress<\/a><\/li>\n<li><a href=\"#existing-styles\">What About Existing Styles?<\/a><\/li>\n<li><a href=\"#limit\">The Sky\u2019s the Limit<\/a><\/li>\n<\/ul>\n<h2 id=\"why-images-are-important\">Why Images Are So Important for Websites<\/h2>\n<p>What better way to explain how beneficial images are to blogs than by proving it with an image?<\/p>\n<figure id=\"attachment_134071\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-134071\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/images-infographic.png\" alt=\"Images Infographic\" width=\"1000\" height=\"3702\" \/><figcaption class=\"wp-caption-text\">Courtesy of <a href=\"http:\/\/www.mdgadvertising.com\/blog\/\" rel=\"noopener\" target=\"_blank\">MDG Advertising<\/a><\/figcaption><\/figure>\n<p>Things get even more interesting when you get down to the biology\u00a0of how we process information. Consider for example that\u00a090 percent of information transmitted to the brain is visual, and\u00a040% of people respond better to visual information that plain text\u00a0(source: <a rel=\"noopener\" class=\"blog-thumbnail\" href=\"http:\/\/ceblog.s3.amazonaws.com\/wp-content\/uploads\/2013\/04\/Infographic-of-infographics.jpg\" target=\"_blank\">Zabisco<\/a>).<\/p>\n<p>But let&#8217;s be honest: I don&#8217;t need to throw statistics at you to make my point. You know from your own habits that visually appealing websites and blogs are far more likely to grab your attention than a mass\u00a0of\u00a0text. Images add color, appeal and intrigue. They fire off a bunch of reactions in our brain before we&#8217;ve even had a chance to start reading.<\/p>\n<p style=\"color: #333333;\">Put simply, if you&#8217;re not including compelling, relevant imagery within your blog posts, you are nowhere near close to\u00a0making the most of your content. Going the extra mile when it comes to including images in your blog posts <em>and<\/em> presenting them well can make all the difference to\u00a0engagement, traffic and sales.<\/p>\n<h2 id=\"css-crash-course\">A WordPress CSS Crash Course<\/h2>\n<p>The WordPress core developers are only too aware of the importance of images for\u00a0websites, which\u00a0is why they afford theme developers (and theme tweakers) a great deal of power and flexibility in determining how images are presented.<\/p>\n<p>That power and flexibility is offered in the form of a number of CSS classes. But before we go any further, let&#8217;s take a moment to\u00a0give you an understanding of what CSS is and how you can use it.<\/p>\n<p>CSS stands for Cascading Style Sheets: a markup language\u00a0that is used to determine how web pages look.\u00a0CSS is combined with HTML (Hyper Text Markup Language), which is the\u00a0markup language used to define the semantic structure of a website. If this doesn&#8217;t make much sense to you, don&#8217;t sweat it \u2013 it&#8217;s more supplementary information than necessary.<\/p>\n<p>CSS is a simple language, once you get your head around it. In terms of being able to alter the appearance and placement of images, you&#8217;ll be able to pick up and work with the code I include in this article with relative ease.<\/p>\n<p>Here&#8217;s an example of a\u00a0CSS code snippet pertaining to the appearance of\u00a0a particular image type in WordPress:<\/p>\n<pre>.alignright {\r\n\tborder: 1px solid #c5c5c5;\r\n\tfloat: right;\r\n\tmargin: 0 0 10px 10px;\r\n\tpadding: 3px;\r\n}<\/pre>\n<p>The <code>.alignright<\/code> text is a CSS <em>class<\/em> (identifiable as a class by the period that precedes its\u00a0name).\u00a0In this case, <code>.alignright<\/code> is a class that WordPress assigns to any image which is selected within the WordPress backend to be aligned to the right of the content:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-ratio-large wp-image-134074\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/image-align-right-700x379.png\" alt=\"Image Alignment\" width=\"700\" height=\"379\" \/><\/p>\n<p>Various <em>declarations<\/em> (such as <code>border<\/code> and <code>padding<\/code>) can be applied to a CSS class, which are ultimately applied to any HTML element that the class in question has been assigned to.<\/p>\n<p>In the example above, any image in WordPress that has been aligned to the right (via the text editor) will have the <code>.alignright<\/code> class assigned to it, and thus will inherit the styling applied to that class. The image will &#8216;float&#8217; to the right, have a little margin to stop text from running into it, a gray border around it, and a little padding to separate the border from the image edge.<\/p>\n<p>That&#8217;ll all make a lot more sense with a visual representation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-ratio-large wp-image-134075\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/image-aligned-right-700x408.png\" alt=\"Image aligned right\" width=\"700\" height=\"408\" \/><\/p>\n<p>There are plenty more declarations that can be added to a CSS class; we&#8217;ll have some fun with them shortly.<\/p>\n<h2 id=\"add-custom-css\">How to Add Custom CSS to WordPress<\/h2>\n<p>WordPress themes come with their own CSS &#8216;stylesheets&#8217;, which are files filled with CSS markup like in the example above. While there are many ways to add your own custom CSS to your website, editing an existing theme&#8217;s CSS stylesheet is <em>not<\/em> the way to go. Your hard work could be overwritten the next time the theme is updated.<\/p>\n<p>The cleanest and most future-proof method is to create your own simple WordPress &#8220;child theme&#8221; with its own CSS stylesheet. The CSS stylesheet within your child theme&#8217;s directory will override any matching styles within the &#8220;parent&#8221; theme&#8217;s stylesheet.<\/p>\n<p>Creating a child theme may sound like a somewhat daunting exercise, but is easy enough if you&#8217;re familiar with FTP. If you want to go down this road, check out <a title=\"How to Create a Child Theme\" href=\"https:\/\/developer.wordpress.org\/themes\/advanced-topics\/child-themes\/\" rel=\"noopener\" target=\"_blank\">the tutorial on the WordPress Codex<\/a>.<\/p>\n<p>If you&#8217;re looking for something more straightforward, I have two plugin suggestions:<\/p>\n<ol>\n<li><a href=\"http:\/\/jetpack.me\/support\/custom-css\/\" rel=\"noopener\" target=\"_blank\">The Custom CSS module within Jetpack<\/a>:\u00a0Ideal if you already use Jetpack on your site.<\/li>\n<li><a href=\"https:\/\/wordpress.org\/plugins\/simple-custom-css\/\" rel=\"noopener\" target=\"_blank\">Simple Custom CSS<\/a>:\u00a0If you don&#8217;t already use Jetpack (and have no need for its varied functionality), this is a more lightweight option.<\/li>\n<\/ol>\n<p>Whichever option you take, adding custom CSS to WordPress will be as simple as editing the custom CSS file that has been generated (by you or one of the plugins).<\/p>\n<h2 id=\"default-css\">The WordPress Default CSS Image Classes<\/h2>\n<p>With all of that out of the way, let&#8217;s get down to the juicy stuff: WordPress&#8217; default image CSS classes.<\/p>\n<p>There are four default classes you can use to change the appearance of images in WordPress:<\/p>\n<ul>\n<li><code>.aligncenter<\/code><\/li>\n<li><code>.alignleft<\/code><\/li>\n<li><code>.alignright<\/code><\/li>\n<li><code>.alignnone<\/code><\/li>\n<\/ul>\n<p>I&#8217;m sure you can figure out what types of images these classes are assigned to.<\/p>\n<p>Every single image that you add to your website via the TinyMCE text editor on the post\/page screens will have one of these classes assigned to it, which means that you have the power to style all of these images as you see fit.<\/p>\n<p>To style a particular image type, all you need to do is follow the format you&#8217;ve already seen in the example above:<\/p>\n<pre>.class-name {\r\n\tproperty: value;\r\n\tproperty: value;\r\n\tproperty: value;\r\n}<\/pre>\n<p><strong>Important:<\/strong>\u00a0When an image has a caption, WordPress assigns one of the above default classes to a <em>div<\/em> that surrounds the image, rather than the image itself. Tackling this particular issue is outside the scope of this post, but is something to bear in mind as you experiment. I recommend that you test your chosen effects with captioned images to see how things look.<\/p>\n<p>With that said, let&#8217;s move onto the fun part: styling your images!<\/p>\n<h2 id=\"styling\">Simple Image Styling With WordPress<\/h2>\n<p>When it comes to adding simple stylistic effects to your images in WordPress, there are five common\u00a0CSS properties:<\/p>\n<ol>\n<li><code>background<\/code><\/li>\n<li><code>border<\/code><\/li>\n<li><code>float<\/code><\/li>\n<li><code>margin<\/code><\/li>\n<li><code>padding<\/code><\/li>\n<\/ol>\n<p>To gain a complete understanding of how these properties affect the appearance of an image (or more accurately, the appearance of the &#8216;frame&#8217; in which the image is placed), we need to consider the CSS &#8216;box model&#8217;:<\/p>\n<figure id=\"attachment_134175\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-134175\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/css-box-model.png\" alt=\"Courtesy of W3.org\" width=\"455\" height=\"340\" \/><figcaption class=\"wp-caption-text\">Courtesy of <a href=\"http:\/\/www.w3.org\/TR\/CSS21\/box.html\" rel=\"noopener\" target=\"_blank\">W3.org<\/a><\/figcaption><\/figure>\n<p>When it comes to styling images, the image itself is the &#8220;content.&#8221; That image is then surrounded by padding, borders and margins; all of which you can define. Images can also be &#8216;floated&#8217;, which for the purposes of this tutorial simply\u00a0means aligned. Your &#8216;float&#8217; options are left, right and none. (Centering an image is a little more complicated; we&#8217;ll get onto that shortly.)<\/p>\n<p>Let&#8217;s consider a simple example to demonstrate how these properties can be used. First, here&#8217;s an image with the <code>.alignright<\/code> class assigned to it, without any CSS markup:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-ratio-large wp-image-134176\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/image-no-css-700x598.png\" alt=\"Image in WordPress (No CSS)\" width=\"700\" height=\"598\" \/><\/p>\n<p>Now let&#8217;s add some simple CSS markup:<\/p>\n<pre>.alignright {\r\n\tbackground: gray;\r\n\tborder: 3px solid black;\r\n\tfloat: right;\r\n\tmargin: 10px;\r\n\tpadding: 3px;\r\n}<\/pre>\n<p>Here&#8217;s the end effect:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-ratio-large wp-image-134178\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/wordpress-image-with-css-700x551.png\" alt=\"Image With CSS\" width=\"700\" height=\"551\" \/><\/p>\n<p>You can probably decipher what we have done here. The image\u00a0has now been &#8220;floated&#8221; (i.e. aligned) to the right, so that the text wraps around it. We added a little bit of padding, which has been given a gray background. We applied a thick black border which appears around the padding. Finally, A 10px margin creates some room between the image and text.<\/p>\n<p>There&#8217;s a lot that you can do with the five selectors above alone. If you want to explore them further, here&#8217;s a comprehensive resource for each one:<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/background\" rel=\"noopener\" target=\"_blank\"><code>background<\/code><\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/border\" rel=\"noopener\" target=\"_blank\"><code>border<\/code><\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/float\" rel=\"noopener\" target=\"_blank\"><code>float<\/code><\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/margin\" rel=\"noopener\" target=\"_blank\"><code>margin<\/code><\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/padding\" rel=\"noopener\" target=\"_blank\"><code>padding<\/code><\/a><\/li>\n<\/ul>\n<p>I promised a solution for centering images too. For reasons that are beyond the scope of this tutorial, CSS does not simply allow you to use <code>float: center;<\/code>\u00a0(i.e. the value does not exist). Instead, you have to define the image as a <em>block<\/em> element, apply <code>margin: 0 auto;<\/code> and define a width for the image. You can read more about this technique <a href=\"http:\/\/www.w3.org\/Style\/Examples\/007\/center.en.html#block\" rel=\"noopener\" target=\"_blank\">here<\/a>. (I&#8217;ll also provide an example of the markup used to create a centered image below.)<\/p>\n<h2 id=\"more-examples\">More Examples of What You Can Do With Image CSS in WordPress<\/h2>\n<p>Now you have a better understanding of the most common CSS properties you can use, here are a few examples to give you some inspiration.<\/p>\n<p>Let&#8217;s start with\u00a0some padding and a gray background to give our images a frame:<\/p>\n<pre>.alignleft {\r\n\tbackground: #dbdbdb;\r\n\tfloat: left;\r\n\tmargin: 0 10px 5px 0;\r\n\tpadding: 5px;\r\n}<\/pre>\n<p>That markup results in this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-134222\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/css-alignleft.png\" alt=\"CSS align left\" width=\"1222\" height=\"976\" \/><\/p>\n<p>Let&#8217;s explore what&#8217;s happened here:<\/p>\n<ul>\n<li>I used an <a title=\"HTML Hex Color Picker\" href=\"https:\/\/www.w3schools.com\/colors\/colors_picker.asp\" rel=\"noopener\" target=\"_blank\">HTML hex color<\/a> for the background. These color codes give you practically limitless flexibility in picking colors for your website.<\/li>\n<li>Because the image I used is a transparent PNG, the <code>background<\/code> color has filled up all the available space not used within the image file.<\/li>\n<li>I&#8217;ve used four values within the <code>margin<\/code> property to set different margins for each side of the image.<\/li>\n<\/ul>\n<p>Let&#8217;s try something else. Instead of giving our images a background color, let&#8217;s set them in a simple border to delineate them from the content. Here&#8217;s the markup:<\/p>\n<pre>.alignright {\r\n\tborder: 1px solid #000099;\r\n\tfloat: right;\r\n\tmargin: 0 0 10px 10px;\r\n\tpadding: 3px;\r\n}<\/pre>\n<p>As you can see, we&#8217;ve played around with the <code>margin<\/code> property values to account for the image alignment, reduced the padding and added a 1px solid border. Here&#8217;s the end result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-134223\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/css-alignright.png\" alt=\"CSS align right\" width=\"1230\" height=\"984\" \/><\/p>\n<p>Finally, let&#8217;s created a centered image and give it a thick gray border. Here&#8217;s the markup:<\/p>\n<pre>.aligncenter {\r\n\tborder: 5px solid #dbdbdb;\r\n\tdisplay: block;\r\n\tmargin: 0 auto;\r\n\twidth: 300px;\r\n}<\/pre>\n<p>And the end result:<\/p>\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-ratio-large wp-image-134225\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2014\/11\/css-aligncenter-700x588.png\" alt=\"CSS align center\" width=\"700\" height=\"588\" \/><\/div>\n<\/div>\n<p>As you can see, it is possible to completely change the placement and appearance of an image in WordPress using CSS alone.<\/p>\n<p>In reality we&#8217;ve only scratched the surface here; there is <em>much<\/em> more that you can do. (If you&#8217;re interested in discovering more, I&#8217;ve supplied some useful resources for further learning below.)<\/p>\n<h2 id=\"existing-styles\">What About Existing Styles?<\/h2>\n<p>If you&#8217;re using a WordPress theme of any real quality, the developer will have already added styles to each of the default WordPress image classes. At the very least she will have aligned each image class appropriately.<\/p>\n<p>Beyond that, her choices will have been entirely subjective, and you now have the power to override their styling effects. Any CSS markup you enter \u2013 either on your child theme&#8217;s stylesheet or via a plugin \u2013 will take precedence over the theme&#8217;s default CSS. You&#8217;re in control.<\/p>\n<h2 id=\"limit\">The Sky&#8217;s the Limit<\/h2>\n<p>In reading this article, I also hope that you are inspired to experiment with your own custom CSS stylings. I didn&#8217;t want to delve too deep into more advanced selectors in this tutorial, but there is a <em>lot<\/em> more you can do with CSS than I&#8217;ve covered here.<\/p>\n<p>If you&#8217;re keen to explore more advanced effects (such as shadows, rounding and rotation), there are a huge number of free online resources that can teach you more about CSS. My personal recommendations are:<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\" rel=\"noopener\" target=\"_blank\">Mozilla Developer Network<\/a>:\u00a0Loved amongst the web developer community.<\/li>\n<li><a href=\"http:\/\/www.w3schools.com\/\" rel=\"noopener\" target=\"_blank\">W3 Schools<\/a>:\u00a0Hated by many &#8220;serious&#8221; web developers, but there&#8217;s no refuting its depth and breadth of information. Just bear in mind that it may not always be 100% correct and\/or up-to-date.<\/li>\n<li><a href=\"https:\/\/wordpress.org\/documentation\/article\/css\/\" rel=\"noopener\" target=\"_blank\">WordPress Codex<\/a>:\u00a0Learn more about how WordPress and CSS come together.<\/li>\n<\/ul>\n<p>If you have any questions, please don&#8217;t hesitate to fire away in the comments section below!<\/p>\n<p><em>Photo Credit: <a title=\"PicJumbo\" href=\"http:\/\/picjumbo.com\/girl-shooting-with-dsrl-in-sun\/\" rel=\"noopener\" target=\"_blank\">PicJumbo<\/a>.<\/em><\/p>\n<p><i data-stringify-type=\"italic\">Editor\u2019s Note: This post has been updated for accuracy and relevancy. [Originally Published: November 2014 \/ Revised: May 2023]<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure you\u00a0already have an idea of\u00a0how important images are to the success of your website. But when it comes to boosting engagement (and ultimately your bottom line), it&#8217;s not just about including images on your site, it&#8217;s about\u00a0what images you include and how you present them. In this post I want to tackle one [&hellip;]<\/p>\n","protected":false},"author":37930,"featured_media":217154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[1044,16],"tutorials_categories":[],"class_list":["post-134067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-css","tag-images"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/134067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/37930"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=134067"}],"version-history":[{"count":11,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/134067\/revisions"}],"predecessor-version":[{"id":217174,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/134067\/revisions\/217174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/217154"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=134067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=134067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=134067"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=134067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}