Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal line in the middle of divs

Tags:

html

css

I want to make a line in the middle of the divs. In the following image, the line should be in the middle of the red boxes.

enter image description here

I'm trying to do that using the line height, but not able to.

Here's the code:

HTML/CSS:

.wrap {
  text-align: center;
  margin: 20px;
}
.links {
  padding: 0 10px;
  border-top: 1px solid #000;
  height: 1px;
  line-height: 0.1em;
}
.dot {
  width: 20px;
  height: 20px;
  background: red;
  float: left;
  margin-right: 150px;
  position: relative;
  top: -10px;
}
<div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>

Demo: https://jsfiddle.net/nkq468xg/

like image 220
sam Avatar asked Nov 19 '16 19:11

sam


People also ask

How do you draw a line in the middle of a div?

Similarly, you can align a DIV element vertically in the middle of a containing element by using the class d-flex in combination with the class align-items-center, like this: To make a vertical line, use border-left or border-right property.

How do you make a horizontal line with words in the middle in CSS?

We are using the css flex-box, to create a horizontal line with a text is placed in the middle. In the above code, we have used the flex-box in . sperator class, so that it creates a flex-container and aligns all three elements horizontally. The align-items: center property aligns the elements vertically center.


2 Answers

You can use Flexbox on links and for line you can use :before pseudo-element on wrap element.

.wrap {
  text-align: center;
  margin: 20px;
  position: relative;
}
.links {
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  position: relative;
}
.wrap:before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  border-top: 1px solid black;
  background: black;
  width: 100%;
  transform: translateY(-50%);
}
.dot {
  width: 20px;
  height: 20px;
  background: red;
}
<div class="wrap">
  <div class="links">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>
</div>
like image 98
Nenad Vracar Avatar answered Sep 22 '22 18:09

Nenad Vracar


Here's one where the line is actually on top, but it does add another element to the HTML:

https://jsfiddle.net/nkq468xg/2/

.wrap {
    text-align: center; 
    margin: 20px; 
}
.links { 
    height: 20px;
    position: relative;
}
hr {
    border: 0;
    height: 1px;
    background: black;
    position: absolute;
    top: 1px;
    width: 100%;
}
.dot {
    width: 20px;
    height: 20px;
    background: red;
    float: left;
    margin-right: 150px;
}
<div class="wrap">
  <div class="links">
    <hr>
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
  </div>   
</div>  
like image 28
junkfoodjunkie Avatar answered Sep 22 '22 18:09

junkfoodjunkie