After grappling with my textbox dilemma, I finally triumphed in maintaining a logical heading order while balancing aesthetics and accessibility—without the need to constantly update my textbox’s headings. Through extensive experimentation and research, I discovered the perfect solution: structuring my textboxes with the <figure> element. Now, the next challenge awaits: styling the <figcaption> element to seamlessly match the <h4> element in my theme.
h4 {
color: #333;
font-size: 16px;
font-size: 1rem;
font-weight: 800;
}
At first glance, this might seem like a simple task. And it is—if you deal with CSS on a daily basis. However, as regular readers of this blog know, my CSS skills have become a bit rusty since the summer of 2020. Ideally, I should have revisited my CSS tag archive to brush up on my skills, but, like many of you, I find myself perpetually short on time.
Quick and dirty
Given my rustiness in CSS and the lack of time to refresh my skills, I initially opted for a quick and dirty solution: in-text styling. This approach allowed me to style the <figcaption> element without diving deep into my CSS files. Here’s the code I used:
<figcaption><span style="font-size: 1.125rem;font-weight: 800;color: #333;">Box 1: TITLE HERE </span></figcaption>
While the in-text styling method may achieve the desired visual effect, it’s fundamentally flawed for several reasons:
Maintainability: In-text styling scatters CSS rules across the HTML file, making it cumbersome to manage and update styles. If I ever need to change the styling of my <figcaption> elements, I would have to hunt down each instance and manually update the code.
Reusability: By embedding styles directly in the HTML, I’m preventing the reuse of these styles across multiple elements. This contradicts the DRY (Don’t Repeat Yourself) principle, which is crucial for efficient coding.
Separation of concerns: One of the core tenets of web development is to separate content (HTML) from presentation (CSS). In-line styles blur this line, making the codebase harder to read and maintain.
Specificity and overrides: In-line styles have higher specificity than CSS classes, which can lead to unexpected behavior and difficulties when trying to override styles with external or internal CSS.
Performance: Embedding styles directly in the HTML increases the file size, which can slow down the loading time of the page.
After applying in-text styling to the <figcaption> element and comparing it with my old textbox code, I noticed an issue: the spacing between the figcaption and the first paragraph was not as generous as it was with the <h4> element. Determined to fix this quickly, I added an extra space with the following code:
<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>
This seemingly effective solution was, in fact, my second mistake. Had I taken a moment to examine the style.css file, I would have realized that the extra space was due to the <h4> element’s margin, a common attribute for all headings.
h1,
h2,
h3,
h4,
h5,
h6 {
clear: both;
line-height: 1.4;
margin: 0 0 0.75em;
padding: 1.5em 0 0;
}
Getting it right—almost
If you don’t have time to do it right, when will you have time to do it over?John R. Wooden
Returning to my textbox the following day, I quickly realized that my initial ‘quick and dirty’ attempt hadn’t quite hit the mark. Despite my efforts, the figcaption still didn’t match the <h4> element’s style perfectly. Recognizing that this ‘quick and dirty’ approach went against the very principles of CogitActive, I resolved to do it right this time.
Armed with determination, I dove into the style.css file of the Twenty Seventeen theme, meticulously combing through the CSS rules. I knew I hadn’t made any tweaks to the <h4> styles in my child theme, which meant the solution lay within this file. I just had to identify the properties that gave the <h4> element its distinctive appearance and apply those same attributes to a new class that I would add to my own stylesheet (i.e., the style.css file of my child theme).
.box {
color: #333;
font-size: 16px;
font-size: 1rem;
font-weight: 800;
clear: both;
line-height: 1.4;
margin: 0 0 0.75em;
padding: 1.5em 0 0;
}
And I added class="box" to the <figcaption> element in my textbox.
However, even with these improvements, the figcaption still looked off. The approach was better, but the appearance was far from perfect. Clearly, I was missing something. But what?
The third time the charm?
Going through my style.css file was a good start, but I decided to leverage the power of the Inspect Element tool 1. By using this tool, I could see how my <h4> element looked within the old textbox and quickly realized that it wasn’t matching the CSS rules I had set. How could this be?
Determined to find the root of the discrepancy, I went back to the style.css file and discovered something else: the first-child pseudo-class. Here’s the extra code I found:
h1:first-child,
h2:first-child,
h3:first-child,
h4:first-child,
h5:first-child,
h6:first-child {
padding-top: 0;
}
The first-child pseudo-class targets the first element of its type within its parent, in this case, the first <h1> through <h6> elements. By setting padding-top: 0, it overrides any default padding that these headings might have at the top, ensuring they start flush against their parent element. This explained why, in my case, the H4 in my textbox wasn’t matching the expected appearance; it was the first child within its container and thus had no top padding.
Further investigation and refreshing my knowledge on the subject matter revealed another important discovery. I realized that creating a box class was unnecessary. Instead, I could simply replace .box in the style.css file with .textbox figcaption. The key here was that I had already assigned the textbox class to the parent <figure> element. By using the parent-child relationship in CSS, I could style the <figcaption> element directly without needing an additional class. Here’s a quick breakdown:
- .box targets elements with the box class.
- .textbox figcaption targets <figcaption> elements that are children of elements with the textbox class.
This approach not only simplified my CSS but also ensured that styles were applied consistently and efficiently, maintaining a cleaner and more organized codebase.
Despite these adjustments, there was still something not quite right; the <figcaption> element still wasn’t matching the <h4> element’s style perfectly.
The final hurdle
Determined to find the missing piece, I continued my investigation with the Inspect Element tool and noticed another discrepancy. My CSS indicated the font size was 1rem, but the Inspect Element tool showed it as 1.125rem. How could this be? Going back to the style.css file, I discovered another occurrence of H4 (buried far down the stylesheet):
@media screen and (min-width: 30em) {
h4 {
font-size: 18px;
font-size: 1.125rem;
}
}
The code above utilizes a media query to apply specific styles only when the screen width is at least 30em (equivalent to 480px). This rule adjusts the font size of all <h4> elements to 1.125rem (18px) under these conditions. To ensure my figcaption matched the <h4> element’s style under the same conditions, I needed to add a similar rule to my style.css file:
@media screen and (min-width: 30em) {
.textbox figcaption {
font-size: 18px;
font-size: 1.125rem;
}
}
Despite applying the media query fix, the <figcaption> element stubbornly refused to match the <h4> element’s style. I couldn’t understand why it wasn’t working. The solution seemed so straightforward, yet it was evading me. My frustration grew with each failed attempt. This should be fixed now! What was I missing?
Completely lost, I started to wonder if my changes in the style.css files were being applied at all. I had recently experienced issues with caching, and although caching turned out not to be the problem before, I suspected it again this time. Perhaps the style.css file wasn’t updating because I was receiving a cached version. To test my hypothesis, I changed the figcaption text color to a vivid red in the first CSS rule, not the media query, to see if the change would be applied. Sure enough, the text remained as dark gray as a #333 could be!
I went to the caching section of my hosting provider and flushed the cache. But to no avail! Could it be another cache issue? Browser cache, perhaps? Curious, I checked the source code of my page and looked for style.css. There I found it, opened it, and… it was still the old version. After carefully examining the file, I discovered the root of the problem: I had forgotten to update the version. Despite my changes in the file, without updating the version, I was receiving the old file! 2
With the version updated, my changes finally took effect. The <figcaption> element now matched the <h4> element’s style perfectly. This journey, filled with trial and error, taught me invaluable lessons about the intricacies of CSS and the significance of versioning. In the end, persistence and a meticulous approach paid off, resulting in a cohesive and visually appealing design. Ironically, if I had taken the time to do it the right way and read my posts tagged as CSS in the first place, I would have been faster due to the lost time trying to fix my mistakes.
1 The Inspect Element tool is an invaluable resource for web developers, allowing them to view and modify the HTML and CSS of a webpage in real time. It provides insights into the structure and styling of web elements, making it easier to diagnose and fix design issues. To access the Inspect Element tool, right-click on the element you wish to inspect and click on Inspect from the context menu. ^
2 In web development, updating the version number of a stylesheet is crucial for ensuring that the latest changes are applied. Browsers often cache CSS files to improve load times, which means they may serve an old, cached version of the file even after updates are made. By updating the version number, you force the browser to load the new version of the CSS file, ensuring that all recent changes are reflected on the webpage. ^

