I have a paragraph with some text, a possible span, and some :before
and :after
psueodo-content:
<p>Normal content without a span</p>
<p>Additional content with a <span>span</span></p>
I would like to pace the pseudo-content at the very beginning and end of the paragraph:
« Normal content without a span »
« Additional content with a »
I though a simple grid with grid-template-columns
would do it, but everything I try gets confused with the additional span
element.
How can I get this effect? Should I be using flex
instead?
Here is a sample snippet:
p {
display: grid;
width: 40em;
grid-template-columns: 2rem ? ? 2rem;
span {
border: thin solid #ccc;
padding: 0 0.25rem;
}
&:before {
content: "«";
}
&:after {
content: "»";
}
}
<p>Normal content without a span</p>
<p>Additional content with a <span>span</span></p>
3
Answers
You don’t need
grid
for that, aposition: absolute
for the arrows to stick them to the sides of<p>
will fix your issue.Additionally,
grid-template-columns: 2rem ? ? 2rem;
has an invalid value,?
is an invalid value.Idealy, if changing your HTML structure is an option, wrapping the whole content in another layer would be advised. This case your paragragh won’t run into issues with arbitrary
<span>
s since they are in the same grid cell.If changing the structure is not possible. I would use flexbox, however this may cause an issue when the paragragh is wrapped into another line.
You may also use grid together with
display: contents
, so the inner<span>
is treated as a plain text. But that means you lose some styling options like borders or paddings etc. Also be aware of its coverage.CSS and HTML Code
flex
and adjust the alignment and spacing using Flexbox utilities.