To achieve the .nfo-style boxed headings while keeping the font size consistent with the body text, you can use CSS pseudo-elements (::before and ::after) to create the box. Here’s how to do it:
CSS Solution
h1, h2, h3, h4, h5, h6 {
font-size: 1em; /* Same as body */
font-weight: normal; /* Optional: Remove default bold */
position: relative; /* Required for pseudo-elements */
display: inline-block; /* Prevents full-width box */
margin: 1em auto; /* Center the box */
padding: 0 0.5em; /* Space between text and box */
}
h1::before, h2::before, h3::before, h4::before, h5::before, h6::before {
content: "+------------+";
position: absolute;
top: -1em; /* Adjust vertical position */
left: 0;
transform: translateX(-50%); /* Center horizontally */
white-space: nowrap; /* Prevent wrapping */
}
h1::after, h2::after, h3::after, h4::after, h5::after, h6::after {
content: "+------------+";
position: absolute;
bottom: -1em; /* Adjust vertical position */
left: 0;
transform: translateX(-50%); /* Center horizontally */
white-space: nowrap; /* Prevent wrapping */
}
/* Center the text inside the box */
h1, h2, h3, h4, h5, h6 {
text-align: center;
}
Improved Solution with Dynamic Box Width
For a more flexible box that adjusts to the heading text length:
h1, h2, h3, h4, h5, h6 {
font-size: 1em;
font-weight: normal;
position: relative;
display: inline-block;
margin: 1.5em auto;
padding: 0 0.5em;
text-align: center;
}
h1::before, h2::before, h3::before, h4::before, h5::before, h6::before {
content: "+" attr(data-content) "+"; /* Use data attribute for dynamic dashes */
display: block;
margin-bottom: -0.5em; /* Overlap with text */
font-size: 0.8em; /* Smaller font for the box */
opacity: 0.7; /* Optional: Faded look */
}
h1::after, h2::after, h3::after, h4::after, h5::after, h6::after {
content: "+" attr(data-content) "+";
display: block;
margin-top: -0.5em; /* Overlap with text */
font-size: 0.8em;
opacity: 0.7;
}
/* Add data-content attribute to headings in HTML */
h1, h2, h3, h4, h5, h6 {
content: ""; /* Fallback */
}
/* Example usage in HTML */
<h1 data-content="----------------">Heading 1</h1>
Screen Reader Accessibility
To ensure screen readers treat the headings correctly:
h1::before, h1::after,
h2::before, h2::after,
h3::before, h3::after,
h4::before, h4::after,
h5::before, h5::after,
h6::before, h6::after {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
width: 1px;
height: 1px;
overflow: hidden;
white-space: nowrap;
}
Final Touches
-
Dynamic Dashes: Use JavaScript to automatically set data-content based on text length:
document.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(heading => {
const length = heading.textContent.length + 4; // Adjust for padding
heading.setAttribute('data-content', '-'.repeat(length));
});
-
Example HTML:
<h1>Scene Group</h1>
<h2>Release Title</h2>
This will give you .nfo-style boxed headings with consistent font size, dynamic box width, and screen reader accessibility. Let me know if you need further tweaks!