Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create OR separator in CSS3? [duplicate]

Tags:

css

What's the best way to create the following in just CSS3 and using the fewest containers?

enter image description here

At the moment I'm using 2 nested divs and an hr which seems excessive.

like image 960
Hopstream Avatar asked Aug 07 '15 04:08

Hopstream


People also ask

How do you duplicate elements in CSS?

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.

What is :: before and :: after?

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.

How do I make a line separator in CSS?

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)

How do you duplicate something in HTML?

An easy way to duplicate an HTML element in Javascript is to use the cloneNode() function: var original = document. getElementById("ID"); var clone = original.


2 Answers

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.

like image 90
Oka Avatar answered Oct 19 '22 03:10

Oka


.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>
like image 23
chillvivek Avatar answered Oct 19 '22 02:10

chillvivek