Centering Category Feature Images in WordPress

English

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.

Adding Category Feature Images to Category Top Pages — Implementation Steps 1 & 2

I 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 …

Current HTML

<div style="position: relative; display:inline-block;">
  <img src="http://static.the-world-star.com/wp-content/uploads/2022/07/20173139/20220720_172737.jpg" />
  <div style="position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)">
     <p><h1 class="page-header-title">サーバー</h1></p>
  </div>
</div>

The text "Server" is centered over the image, but should the alignment be applied to the first div, or directly to the <img> tag itself?

To find out, I pasted the code into a WordPress custom HTML block and ran some tests.

サーバー

In the initial state shown above, the image is aligned to the left.

Updated HTML

<div style="text-align: center;">
   <div style="position: relative; display:inline-block;">
      <img "="" src="http://static.the-world-star.com/wp-content/uploads/2022/07/31115522/server_eyecatch.jpg">
      <div style="position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)">
           <p></p><h1 class="page-header-title">サーバー</h1><p></p>
      </div>
   </div>
</div>

サーバー

I added another <div></div> block and applied centering to it using style="text-align: center;".

Applying the center alignment directly to the original <div> or the <img> 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 <div> to group and position elements, I had a sudden realization: why not just wrap it in an outer <div> block? That ended up working like a charm.

PHP Modifications

/var/www/html/wp-content/themes/lightning/_g3/template-parts/page-header.php

At the bottom of this file, the changes made since the last edit are highlighted in pink.


<!-- //カテゴリー独立プロジェクトのための修正、プラグインのCategory Imagesを利用している
<!-- //categoryだったらの条件はz_taxonomy_image()に入っていたのでそこもいじる -->
<?php
if (function_exists('z_taxonomy_image')){
  echo '<div style="text-align: center;">';
  echo '<div style="position: relative; display:inline-block;">';
  $taximage = z_taxonomy_image(); //これで画像[show]のecho、成功時にもhtmlコードを返すようにした
  if(!empty($taximage)){           //画像があったらカテゴリー名を[show]
    echo '<div style="position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)">';
    echo '<p>';
    echo wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html ); //カテゴリー名
    echo '</p>';
    echo '</div>';
  }
  echo '</div>';
  echo '</div>';
 }
?>

Previous Article

Adding Category Feature Images to Category Top Pages — Implementation Steps 1 & 2

I 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 …

Leave a Reply