Okay, making the box dynamically size to the text content and appear as distinct characters (+, -, |) exclusively in CSS is a classic challenge. The most robust way involves using the <span> inside the heading (hX) as the primary element that dictates the width, and then carefully placing pseudo-elements for the box characters.
Here's a solution that achieves this:
CSS:
/* --- Global Styles (from previous requests) --- */
body {
background-color: black;
color: white;
font-family: monospace;
text-align: center;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
ul, ol {
padding-left: 0;
list-style-position: inside;
}
li {
display: inline-block;
margin-left: auto;
margin-right: auto;
/* text-align: left; /* If you want text inside li left aligned */
}
hr {
border: none;
height: 1em;
overflow: hidden;
text-align: center;
margin: 2em 0;
}
hr::before {
content: "================================================================================";
display: block;
letter-spacing: 0.1em;
font-size: 1em;
}
/* --- .NFO Style Heading Box (Dynamic) --- */
h1, h2, h3, h4, h5, h6 {
font-size: inherit; /* Make hX font size same as body */
font-weight: normal; /* Remove default bold */
line-height: 1; /* Keep line height tight for box calculation */
margin: 2em auto; /* Vertical space, auto centers the block if it had a fixed width */
padding: 0; /* No padding on the hX itself */
text-align: center; /* Ensures the inner span is centered */
position: relative; /* For positioning corner pseudo-elements if needed, though span handles most */
}
/* The span is key: it's inline-block so it sizes to its text content.
It will get the main horizontal lines (---) via its borders. */
h1 > span,
h2 > span,
h3 > span,
h4 > span,
h5 > span,
h6 > span {
display: inline-block; /* CRITICAL: Sizes to content */
position: relative; /* For positioning the side '|' and corner '+' characters */
padding: 0.3em 0.6em; /* Vertical padding for space between text and --- lines */
/* Horizontal padding for space between text and | characters */
border-top: 1px solid currentColor; /* This IS the '---' top line */
border-bottom: 1px solid currentColor; /* This IS the '---' bottom line */
line-height: 1.2; /* Give text some breathing room from the borders */
}
/* Left side: '+' character for top-left and bottom-left corners */
h1 > span::before,
h2 > span::before,
h3 > span::before,
h4 > span::before,
h5 > span::before,
h6 > span::before {
content: '+';
position: absolute;
top: -1px; /* Align with the top border of the span */
left: -0.7ch; /* Position it to the left of the span's box. Adjust '0.7ch' as needed. */
/* 'ch' unit is ~width of '0', good for monospace */
line-height: calc(1.2em + 0.6em + 2px); /* Match span height: text line-height + vertical padding + borders */
/* This helps vertically center the '+' if it's multi-line text. */
/* For single line text, simpler top/bottom alignment works. */
/* This line-height trick is to try and make the single '+' span the full height.
A simpler approach for the '+' characters is to explicitly position top/bottom ones.
Let's simplify this for clarity for the '+' and use separate ones for top/bottom.
*/
}
/* Right side: '+' character for top-right and bottom-right corners */
h1 > span::after,
h2 > span::after,
h3 > span::after,
h4 > span::after,
h5 > span::after,
h6 > span::after {
content: '+';
position: absolute;
top: -1px; /* Align with the top border of the span */
right: -0.7ch; /* Position it to the right of the span's box. Adjust '0.7ch'. */
line-height: calc(1.2em + 0.6em + 2px); /* Match span height, same reasoning as ::before */
}
/* We need distinct pseudo-elements for top/bottom corners and side pipes.
The previous approach with ::before/::after on span for '+' is problematic for the pipes.
Let's dedicate ::before/::after on the SPAN for the PIPES,
and then use ::before/::after on the HX element for the CORNERS. This is cleaner.
*/
/* Reset the above span::before and span::after for a cleaner approach */
h1 > span::before, h2 > span::before, h3 > span::before, h4 > span::before, h5 > span::before, h6 > span::before,
h1 > span::after, h2 > span::after, h3 > span::after, h4 > span::after, h5 > span::after, h6 > span::after {
content: none; /* Clear previous attempt */
}
/* Vertical pipes '|' directly on the span's sides */
h1 > span::before, /* Re-purposing for left pipe */
h2 > span::before,
h3 > span::before,
h4 > span::before,
h5 > span::before,
h6 > span::before {
content: '|';
position: absolute;
left: -0.6ch; /* Position just outside the span's padding, adjust as needed */
top: 50%;
transform: translateY(-50%); /* Vertically center the pipe relative to span's height */
padding: 0 0.1em; /* Optional small spacing around the pipe character */
}
h1 > span::after, /* Re-purposing for right pipe */
h2 > span::after,
h3 > span::after,
h4 > span::after,
h5 > span::after,
h6 > span::after {
content: '|';
position: absolute;
right: -0.6ch; /* Position just outside the span's padding */
top: 50%;
transform: translateY(-50%);
padding: 0 0.1em;
}
/* Corner '+' characters: These go on the Hx element, positioned relative to the centered span */
/* This requires knowing the span's offset, which is tricky.
A better approach is to add the '+' as part of the span's pseudo-elements that form the lines/pipes,
or use more pseudo-elements if the browser supports it (e.g., ::before::before - not standard).
The most straightforward CSS-only way for true dynamic characters:
The span creates the --- (borders) and | (pseudos).
The + characters are added "around" this. The most reliable way is to place them
using pseudo-elements on the hX element itself, and align them to the edges of the
*visible box* created by the span. This alignment is the hardest part in pure CSS.
Let's try a slightly different take: the span's pseudo-elements form the corners + pipes.
*/
/* --- Final Refined Approach for Dynamic Box Characters --- */
/* hX element: basic setup, centers the span */
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: normal;
margin: 2em auto;
padding: 0; /* Ensure no interference with box model */
text-align: center; /* This centers the inline-block span */
line-height: 1; /* To make calculations easier */
}
/* Span: core element, sizes to text, gets horizontal dash borders */
h1 > span,
h2 > span,
h3 > span,
h4 > span,
h5 > span,
h6 > span {
display: inline-block; /* Sizes to content */
position: relative; /* Anchor for pseudo-elements */
padding: 0.3em 0.5em; /* y-padding for space from border, x-padding for space from pipes */
border-top: 1px solid currentColor; /* Top '---' */
border-bottom: 1px solid currentColor; /* Bottom '---' */
line-height: 1.3; /* Ensure text doesn't touch borders, adjust as needed */
}
/* Top-left '+', and left '|' */
h1 > span::before,
h2 > span::before,
h3 > span::before,
h4 > span::before,
h5 > span::before,
h6 > span::before {
content: '+ |'; /* Note the spaces for alignment - can be tricky */
position: absolute;
top: 50%; /* Center vertically relative to the span's height */
transform: translateY(-50%);
left: -2.1ch; /* Adjust this value carefully: -(width of '+' + width of ' ' + width of '|') */
/* Example: -0.7ch for '+', -0.7ch for ' ', -0.7ch for '|' approx */
white-space: pre; /* Preserve the spaces in content */
line-height: 1; /* Match character height */
}
/* Top-right '+', and right '|' */
h1 > span::after,
h2 > span::after,
h3 > span::after,
h4 > span::after,
h5 > span::after,
h6 > span::after {
content: '| +'; /* Note the spaces */
position: absolute;
top: 50%;
transform: translateY(-50%);
right: -2.1ch; /* Adjust this value carefully */
white-space: pre;
line-height: 1;
}
/* The above ::before/::after for corners + pipes is a common attempt, but often
leads to alignment issues with the span's border-top/bottom, especially with varying text.
The MOST ROBUST pure CSS method for this distinct character box:
Use 4 pseudo-elements if possible (not standard) or a combination of
pseudo-elements on Hx and Span.
Let's try the method where span's borders are '---', span's pseudos are '|',
and Hx's pseudos are '+' at the corners. This is hard to align dynamically.
**The best approach for this specific "character art" style with dynamic width in pure CSS
often involves a slight "cheat" or a very specific structure.**
Considering the constraints, let's use the `span` for borders (`---`) and its pseudo-elements for the `|` and `+` combined, and accept that the `+` might slightly overlap the start/end of the border visually or require careful `ch` unit adjustments.
*/
/* --- Revision based on a common robust pattern --- */
/* hX element styling (as before) */
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: normal;
margin: 1.5em auto; /* Adjust margin */
padding: 0;
text-align: center;
position: relative; /* Needed for the corner '+' pseudo-elements */
line-height: 1; /* Baseline for calculations */
}
/* The span is our main content holder that defines the width */
h1 > span,
h2 > span,
h3 > span,
h4 > span,
h5 > span,
h6 > span {
display: inline-block; /* Shrink-to-fit content */
position: relative; /* For its own pseudo-elements (the pipes) */
padding: 0.2em 0.5em; /* Space around text: 0.2em top/bottom, 0.5em left/right */
border-top: 1px solid currentColor; /* The --- top line */
border-bottom: 1px solid currentColor; /* The --- bottom line */
line-height: 1.4; /* Ensure text has breathing room from borders */
}
/* Vertical pipes '|' on the sides of the span */
h1 > span::before,
h2 > span::before,
h3 > span::before,
h4 > span::before,
h5 > span::before,
h6 > span::before {
content: '|';
position: absolute;
left: -0.35em; /* Position it just to the left of the span's padding box. Adjust! */
top: 50%;
transform: translateY(-50%); /* Center vertically */
}
h1 > span::after,
h2 > span::after,
h3 > span::after,
h4 > span::after,
h5 > span::after,
h6 > span::after {
content: '|';
position: absolute;
right: -0.35em; /* Position it just to the right of the span's padding box. Adjust! */
top: 50%;
transform: translateY(-50%);
}
/* Corner '+' characters using pseudo-elements on the Hx element.
These are positioned relative to the Hx, and we "guess" where the span ends.
This is the trickiest part for perfect dynamic alignment.
A more reliable way is to have the '+' characters as part of the span's pseudo-elements,
even if it means the content string is like "+ |"
*/
/* Let's refine the span's pseudo-elements to include the '+' */
/* Clear previous span pseudos */
h1 > span::before, h2 > span::before, h3 > span::before, h4 > span::before, h5 > span::before, h6 > span::before,
h1 > span::after, h2 > span::after, h3 > span::after, h4 > span::after, h5 > span::after, h6 > span::after {
content: ""; /* Reset */
}
/* Final Attempt: Combining '+' and '|' in span's pseudos, aligned to span's borders */
/* hX element styling (as before) */
/* Span styles (as before, but line-height and padding adjusted for clarity) */
h1 > span,
h2 > span,
h3 > span,
h4 > span,
h5 > span,
h6 > span {
display: inline-block;
position: relative;
padding: 0.2em 0.8em; /* Adjusted padding: 0.8em to make space for '|' inside */
border-top: 1px solid currentColor;
border-bottom: 1px solid currentColor;
line-height: 1.3;
}
/* Top-left '+' and left '|' using ONE pseudo-element */
h1 > span::before,
h2 > span::before,
h3 > span::before,
h4 > span::before,
h5 > span::before,
h6 > span::before {
content: '+ |'; /* '+' then two spaces then '|'. Adjust spaces for alignment */
position: absolute;
white-space: pre; /* Preserve spaces */
/* Align this block of text relative to the span's content box */
top: 50%;
transform: translateY(-50%);
/* The 'left' positioning is key. We want the '+' to be AT the start of the border */
/* And '|' to be just inside the border, next to text padding */
left: calc(-1 * (0.8em + 0.7ch + 0.7ch)); /* -(padding-left of span + approx width of '| +') */
/* This needs very careful tuning with 'ch' or 'em' */
/* A simpler way: position based on border-box */
left: -1.6ch; /* Approximate: -(width of '+') - (width of ' '). Adjust this! */
line-height: 1; /* Keep characters tight */
}
h1 > span::after,
h2 > span::after,
h3 > span::after,
h4 > span::after,
h5 > span::after,
h6 > span::after {
content: '| +';
position: absolute;
white-space: pre;
top: 50%;
transform: translateY(-50%);
right: -1.6ch; /* Approximate. Adjust this! */
line-height: 1;
}