I would like to reuse the following CSS for multiple content
labels (top-right corner). For example, Foo
, Bar
, Baz
, etc.
Is it possible to re-use as much of the existing style without duplicating the full code? In the example below foo
and bar
are identical with the exception of content
string in .foo:after
and .bar:after
.
.foo {
position: relative;
border: 1px solid #ccc;
padding: 1em 1em 0.5em 1em;
border-radius: 4px 4px 4px 4px;
}
.foo:after {
content: "Foo";
position: absolute;
top: 0px;
right: 0px;
padding: 3px 7px;
font-size: 12px;
font-weight: bold;
background-color: #f5f5f5;
border: 1px solid #ccc;
color: #9da0a4;
border-radius: 0px 4px 0px 4px;
border-width: 0px 0px 1px 1px;
}
.bar {
position: relative;
border: 1px solid #ccc;
padding: 1em 1em 0.5em 1em;
border-radius: 4px 4px 4px 4px;
}
.bar:after {
content: "Bar";
position: absolute;
top: 0px;
right: 0px;
padding: 3px 7px;
font-size: 12px;
font-weight: bold;
background-color: #f5f5f5;
border: 1px solid #ccc;
color: #9da0a4;
border-radius: 0px 4px 0px 4px;
border-width: 0px 0px 1px 1px;
}
<div class="foo">
Hello
</div>
<p/>
<div class="bar">
Mama!
</div>
Best way! use attr(data-content)
to reduce CSS classes
.x {
position: relative;
border: 1px solid #ccc;
padding: 1em 1em 0.5em 1em;
border-radius: 4px 4px 4px 4px;
}
.x:after {
position: absolute;
top: 0px;
right: 0px;
padding: 3px 7px;
font-size: 12px;
font-weight: bold;
background-color: #f5f5f5;
border: 1px solid #ccc;
color: #9da0a4;
border-radius: 0px 4px 0px 4px;
border-width: 0px 0px 1px 1px;
}
.x:after {
content: attr(data-content);
}
<div class="x" data-content="foo">
Hello
</div>
<p/>
<div class="x" data-content="bar">
World!
</div>
<p/>
<div class="x" data-content="seomthing else">
Something!
</div>