{"id":6671,"date":"2022-08-03T06:14:01","date_gmt":"2022-08-02T21:14:01","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6671"},"modified":"2026-11-19T14:02:57","modified_gmt":"2026-11-19T05:02:57","slug":"centering-category-feature-images-in-wordpress","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/server\/centering-category-feature-images-in-wordpress-6671\/","title":{"rendered":"Centering Category Feature Images in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I recently set up category-specific feature images to appear on category top pages and post pages. However, smaller images were aligning to the left, especially on desktop screens. I wanted to center them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read my previous article here.<\/p>\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\t\t\t<div class=\"blog-card\">\n\t\t\t\t<div class=\"blog-card-body-outer\">\n\t\t\t\t\t<div class=\"blog-card-body\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<h5 class=\"blog-card-title\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\">Adding Category Feature Images to Category Top Pages \u2014 Implementation Steps 1 &#038; 2<\/a>\n\t\t\t\t\t\t\t<\/h5>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"blog-card-text\">\n\t\t\t\t\t\t\t\tI want to display a category feature image at the top of both category archive pages and individual blog posts. Doing this helps leave a lasting impression on \u2026\t\t\t\t\t\t\t<\/p>\n\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"blog-card-site-title\">\n\t\t\t\t\t\t\t<a href=\"http:\/\/the-world-star.com\/en\/\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe\u2606World Star\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<div class=\"blog-card-image-outer\">\n\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\" class=\"blog-card-image-frame\">\n\t\t\t\t\t\t\t<img decoding=\"async\" class=\"blog-card-image-src\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\" alt=\"\">\n\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\n\t\t\t<\/div><\/figure>\n\n\n<h2 class=\"wp-block-heading\">Current HTML<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;div style=\"position: relative; display:inline-block;\"&gt;\n  &lt;img src=\"http:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20173139\/20220720_172737.jpg\" \/&gt;\n  &lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\"&gt;\n     &lt;p&gt;&lt;h1 class=\"page-header-title\"&gt;\u30b5\u30fc\u30d0\u30fc&lt;\/h1&gt;&lt;\/p&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The text \"Server\" is centered over the image, but should the alignment be applied to the first div, or directly to the &lt;img&gt; tag itself?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To find out, I pasted the code into a WordPress custom HTML block and ran some tests.<\/p>\n\n\n<div style=\"position: relative; display:inline-block;\">\n  <img decoding=\"async\" src=\"http:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\" \"=\"\"><\/p>\n<div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\">\n<h4 class=\"page-header-title\">\u30b5\u30fc\u30d0\u30fc<\/h4>\n<\/p><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In the initial state shown above, the image is aligned to the left.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updated HTML<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;div style=\"text-align: center;\"&gt;\n   &lt;div style=\"position: relative; display:inline-block;\"&gt;\n      &lt;img \"=\"\" src=\"http:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\"&gt;\n      &lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\"&gt;\n           &lt;p&gt;&lt;\/p&gt;&lt;h1 class=\"page-header-title\"&gt;\u30b5\u30fc\u30d0\u30fc&lt;\/h1&gt;&lt;p&gt;&lt;\/p&gt;\n      &lt;\/div&gt;\n   &lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n\n\n<div style=\"text-align: center;\">\n<div style=\"position: relative; display:inline-block;\">\n      <img decoding=\"async\" \"=\"\" src=\"http:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\"><\/p>\n<div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\">\n<h4 class=\"page-header-title\">\u30b5\u30fc\u30d0\u30fc<\/h4>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">I added another &lt;div&gt;&lt;\/div&gt; block and applied centering to it using style=\"text-align: center;\".<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Applying the center alignment directly to the original &lt;div&gt; or the &lt;img&gt; tag didn't work. I'm not entirely sure why, but it conflicted with the settings used to center the text over the image. Since you can use &lt;div&gt; to group and position elements, I had a sudden realization: why not just wrap it in an outer &lt;div&gt; block? That ended up working like a charm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP Modifications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\/var\/www\/html\/wp-content\/themes\/lightning\/_g3\/template-parts\/page-header.php<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the bottom of this file, the changes made since the last edit are highlighted in pink.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;!-- \/\/\u30ab\u30c6\u30b4\u30ea\u30fc\u72ec\u7acb\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u305f\u3081\u306e\u4fee\u6b63\u3001\u30d7\u30e9\u30b0\u30a4\u30f3\u306eCategory Images\u3092\u5229\u7528\u3057\u3066\u3044\u308b\n&lt;!-- \/\/category\u3060\u3063\u305f\u3089\u306e\u6761\u4ef6\u306fz_taxonomy_image()\u306b\u5165\u3063\u3066\u3044\u305f\u306e\u3067\u305d\u3053\u3082\u3044\u3058\u308b --&gt;\n&lt;?php\nif (function_exists('z_taxonomy_image')){\n<mark style=\"background-color:#f78da7\" class=\"has-inline-color\">  echo '&lt;div style=\"text-align: center;\"&gt;';<\/mark>\n  echo '&lt;div style=\"position: relative; display:inline-block;\"&gt;';\n  $taximage = z_taxonomy_image(); \/\/\u3053\u308c\u3067\u753b\u50cf\u8868\u793a\u306eecho\u3001\u6210\u529f\u6642\u306b\u3082html\u30b3\u30fc\u30c9\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u305f\n  if(!empty($taximage)){           \/\/\u753b\u50cf\u304c\u3042\u3063\u305f\u3089\u30ab\u30c6\u30b4\u30ea\u30fc\u540d\u3092\u8868\u793a\n    echo '&lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\"&gt;';\n    echo '&lt;p&gt;';\n    echo wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html ); \/\/\u30ab\u30c6\u30b4\u30ea\u30fc\u540d\n    echo '&lt;\/p&gt;';\n    echo '&lt;\/div&gt;';\n  }\n  echo '&lt;\/div&gt;';\n<mark style=\"background-color:#f78da7\" class=\"has-inline-color\">  echo '&lt;\/div&gt;';<\/mark>\n }\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Previous Article<\/p>\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\t\t\t<div class=\"blog-card\">\n\t\t\t\t<div class=\"blog-card-body-outer\">\n\t\t\t\t\t<div class=\"blog-card-body\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<h5 class=\"blog-card-title\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\">Adding Category Feature Images to Category Top Pages \u2014 Implementation Steps 1 &#038; 2<\/a>\n\t\t\t\t\t\t\t<\/h5>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"blog-card-text\">\n\t\t\t\t\t\t\t\tI want to display a category feature image at the top of both category archive pages and individual blog posts. Doing this helps leave a lasting impression on \u2026\t\t\t\t\t\t\t<\/p>\n\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"blog-card-site-title\">\n\t\t\t\t\t\t\t<a href=\"http:\/\/the-world-star.com\/en\/\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe\u2606World Star\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<div class=\"blog-card-image-outer\">\n\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\" class=\"blog-card-image-frame\">\n\t\t\t\t\t\t\t<img decoding=\"async\" class=\"blog-card-image-src\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\" alt=\"\">\n\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\n\t\t\t<\/div><\/figure>","protected":false},"excerpt":{"rendered":"<p>I recently set up category-specific feature images to appear on category top pages and post pages. However, smaller images were aligning to the left, especially on desktop screens. I wanted to center them. Read my previous article here. Current HTML &lt;div style=\"position: relative; display:inline-block;\"&gt; &lt;img src=\"http:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20173139\/20220720_172737.jpg\" \/&gt; &lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2098,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=2659","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[3],"tags":[67,68,81,1240,1241,1242],"class_list":["post-6671","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-server","tag-wordpress","tag-lightning","tag-image","tag-category-top","tag-graphic","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/comments?post=6671"}],"version-history":[{"count":2,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6671\/revisions"}],"predecessor-version":[{"id":6673,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6671\/revisions\/6673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/2098"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}