How can I put a vertical line down the center of a div?

CssHtml

Css Problem Overview


How do I put a vertical line down the middle of a div? Maybe I should put two divs inside the div and put a left border on one and a right border on the other? I have a DIV tag and I need to put one ascx on the left (that will get swapped out from time to time with another ascx) and then a static ascx on the left. Any ideas on how I should do this? Maybe I answered my own question.

Any pointers would be great

Css Solutions


Solution 1 - Css

Maybe this can help you

<div class="here">Content</div>

.here:after {
    content:"";
    position: absolute;
    z-index: -1;
    top: 0;
    bottom: 0;
    left: 50%;
    border-left: 2px dotted #ff0000;
    transform: translate(-50%);
}

div {
    margin: 10px auto;
    width: 60%;
    height: 100px;
    border: 1px solid #333;
    position:relative;
    text-align:center
}

Here's is a JSFiddle demo.

Solution 2 - Css

Although this question was asked 9yrs ago and a lot of the answers would "work". CSS has evolved and you can now do it in a single line without using calc.

One liner (2018) answer:

background: linear-gradient(#000, #000) no-repeat center/2px 100%;

How this works
  1. linear-gradient(#000, #000) this creates a background-image so we can later use background-size to contain it.
  2. no-repeat this stops the gradient from repeating when we do put background-size on it.
  3. center - this is the background-position this is where we put the "diving line"
  4. /2px 100% - this is making the image 2px wide and 100% of the element you put it in.

This is the extended version:

  background-image: linear-gradient(#000, #000);
  background-size: 2px 100%;
  background-repeat: no-repeat;
  background-position: center center;

Solution 3 - Css

Here's a more modern way to draw a line down a div. Just takes a little css:

.line-in-middle {
    width:400px;
    height:200px;
	  background: linear-gradient(to right, 
	                              transparent 0%, 
	                              transparent calc(50% - 0.81px), 
	                              black calc(50% - 0.8px), 
	                              black calc(50% + 0.8px), 
	                              transparent calc(50% + 0.81px), 
	                              transparent 100%); 
	}

<div class="line-in-middle"></div>

Works in all modern browsers. http://caniuse.com/#search=linear-gradient

Solution 4 - Css

Just tested this; works:

<div id="left" style="width:50%;float:left;background:green;">left</div>
<div id="right" style="margin-left:50%;border-left:solid 1px black;background:red;">right</div>

Solution 5 - Css

This is my version, a gradient middle line using linear-gradient

.block {
      width:400px;
      height:200px;
      position:relative;
 }
.block:before {
      content:"";
      width:1px;
      height:100%;
      display:block;
      left:50%;
      position:absolute;
	    background-image: -webkit-linear-gradient(top, #fff, #000, #fff);
      background-image: -moz-linear-gradient(top, #fff, #000, #fff);
      background-image: -ms-linear-gradient(top, #fff, #000, #fff);
      background-image: -o-linear-gradient(top, #fff, #000, #fff);
      background-image: linear-gradient(top, #fff, #000, #fff);
	}

<div class="block"></div>

Solution 6 - Css

I think you need a wrapper div with a background image. If not, then you are not guaranteed to have the border go all the way from the top to the bottom.

<div class="wrapper">
    <div>Float this one left</div>
    <div>float this one right</div>
</div>

*be sure to leave the space between the left and right for the image to show up.

you'll need a style that looks like:

.wrapper{background:url(img.jpg) 0 12px repeat-y;}

Solution 7 - Css

I think your multiple DIV approach is going to be the sanest way to approach this:

<DIV>
   <DIV style="width: 50%; border-right: solid 1px black">
      /* ascx 1 goes here */
   </DIV>
   <DIV style="width: 50%">
      /* ascx 2 goes here */
   </DIV>
</DIV>

Solution 8 - Css

Three divs?

<div>
   <div>
      /* ascx 1 goes here */
   </div>
   <div style="width:1px; background-color: #000"></div>
   <div>
      /* ascx 2 goes here */
   </div>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHcabnettekView Question on Stackoverflow
Solution 1 - CssddjikicView Answer on Stackoverflow
Solution 2 - CssjnowlandView Answer on Stackoverflow
Solution 3 - CssSamuel NeffView Answer on Stackoverflow
Solution 4 - Cssuser1228View Answer on Stackoverflow
Solution 5 - CssAG_View Answer on Stackoverflow
Solution 6 - CsseasementView Answer on Stackoverflow
Solution 7 - CssRyan BrunnerView Answer on Stackoverflow
Solution 8 - CssSupertuxView Answer on Stackoverflow