What's the best way to create the following in just CSS3 and using the fewest containers?
At the moment I'm using 2 nested divs and an hr
which seems excessive.
Short answer no, in css you can't create content or display it multiple times. Usually this repeated content is taken care of via server side technologies (PHP, JSP, ASPX) etc. If your site is static content (no server side processing) then your best bet is just copy and past.
Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content. Version: CSS2.
You can do it simply two ways:add margin 0 in hr, like in css add hr { margin: 0; } Or, you can try after content positioning as below code (more flexible)
An easy way to duplicate an HTML element in Javascript is to use the cloneNode() function: var original = document. getElementById("ID"); var clone = original.
Pseudo-elements!
Using ::before
and ::after
you can manage this with just one container.
You'll need to adjust your values for your own environment, but the general idea is to position the pseudo-elements absolutely inside the container.
#or {
position: relative;
width: 300px;
height: 50px;
line-height: 50px;
text-align: center;
}
#or::before,
#or::after {
position: absolute;
width: 130px;
height: 1px;
top: 24px;
background-color: #aaa;
content: '';
}
#or::before {
left: 0;
}
#or::after {
right: 0;
}
<div id="or">OR</div>
Using flexbox instead of absolute positioning is another option, with worse support.
.or {
display:flex;
justify-content:center;
align-items: center;
color:grey;
}
.or:after,
.or:before {
content: "";
display: block;
background: grey;
width: 30%;
height:1px;
margin: 0 10px;
}
<div class="or"> OR </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With