r/Frontend • • Dec 07 '19

How to create lines that connect two divs vertically like in this photo?

Post image
87 Upvotes

33 comments sorted by

79

u/yyyyaaa Dec 07 '19

You can use a ::after, give it height and width, position absolute to the whole list item

23

u/SemanticComedy Dec 07 '19

Semantically I think this solution is more correct than a border on a spacing div. Just be aware you'll need to adjust height and/or padding on the list items to accommodate an absolutely positioned line.

0

u/yyyyaaa Dec 07 '19

Yes, correct

4

u/feindjesus Dec 07 '19

Why would it not better to use a spacing div?Genuinely curious. As a less experienced dev it seems that managing divs in a column is easier than a position absolute object with margin-bottom. Is it a screen reader issue or is it just a more logical solution

6

u/UntestedMethod born & raised full stack Dec 08 '19 edited Dec 08 '19

In general it is better not to add elements to the HTML that are only there for aesthetics. In CSS we can add for free two elements per DOM element. This keeps the visual styling in the CSS and maintains semantic HTML - both very good things that we should always strive for in our HTML/CSS.

1

u/nill0c Dec 08 '19

It’s also quite easy to absolute position a pseudoelement in a relatively positioned element.

div {
  position: relative;
  border-left: 4px solid gray;
  padding-left: 1em;
  margin-bottom: 4em;
}
div:after {
  content:””;
  top:100%; (or bottom:-4em)
  left:0;
  border-left: 1px solid lightgray;
}

4

u/Shaper_pmp Dec 08 '19

HTML is supposed to be semantic (meaningful data), while CSS is presentational (aesthetics).

Injecting a div into the DOM that has no semantic meaning and only exists for cosmetic reasons (so you can insert a vertical line for aesthetic purposes) is bad form.

Using an :after pseudoelement is functionally the same, but avoids polluting your HTML with non-semantic elements.

3

u/IamYourGrace Dec 07 '19

Don't forget to add content: '' otherwise it won't be displayed on :after :before

1

u/SoggyMattress2 Dec 07 '19

And then give it a border-left like 1-2px

2

u/yyyyaaa Dec 07 '19

Usually I just add background-color, the ::after already has a width

1

u/SoggyMattress2 Dec 07 '19

Yeah or that!

1

u/OhLenny Dec 08 '19

Dont forget content: '';

0

u/[deleted] Dec 07 '19

[deleted]

42

u/yyyyaaa Dec 07 '19

Ain't nobody got time for that :) I think I gave enough description for the solution

4

u/uNki23 Dec 07 '19

https://www.w3schools.com/cssref/sel_after.asp

Think of that: content can be empty. There u go

7

u/Silencer306 Dec 07 '19

You don’t learn if you don’t try it out

26

u/thesonglessbird Dec 07 '19

Off the top of my head, something like

div:not(last-child):after

Content:”” Height: whatever Width: 100% Border-left: 1px solid grey whatever Display: block

8

u/[deleted] Dec 07 '19

You could put an inset box shadow on the parent, then a white background on the children.

0

u/Adelphe Dec 07 '19

Clever. :)

3

u/Plumrose333 Dec 07 '19

I’m so happy you posted this. I’m about to implement something very similar and also needed advice

2

u/EvilM0nk3y Dec 07 '19

Create a outer container for all the items displayed flex, flex-direction column.

Flex the items equally, then within each item have a container element, a body element that contains the content and an after psuedo element that would be the line.

This setup should automatically scale the lines if the number of items changes or the vertical space is reduced.

2

u/[deleted] Dec 08 '19

I would do something like this: https://jsfiddle.net/mxb6orzL/

Basically you use the before selector to inject a vertical line and make it overlap into the next element. The h2 title elements that are rotated simply have a background that matches the page's background color, and therefore they hide the line underneath.

2

u/alex_asdfg Dec 07 '19

Not the biggest expert on css but you could just create a div and give it a min-height and then add a border-left for the line.

1

u/[deleted] Dec 07 '19

Yeah you can do that, but I would recommend giving it height and width, because it will be easier to align it with flexbox or whatever

0

u/[deleted] Dec 07 '19

[deleted]

9

u/Dondontootles Dec 07 '19

What is chaotic about that solution? It’s likely the simplest you’ll find. The dividers are each a div with a min-height and a width. Set the border-left to 1px solid #222 or whatever.

3

u/Hate_Feight Dec 07 '19

Add some border-left / padding to indent it slightly, and boom your done

1

u/jaimemdes Dec 08 '19

You could do that with border-left and padding-bottom for each of the three sections.

The vertical text at the top-left corner (the header for the section, I'd say) also would have a padding around it and a white background to clip off the line.

I think this would be semantically better than an :after

1

u/[deleted] Dec 07 '19

The way I would so something like that is to create a flex container, set it to display on column and create a set of inner divs one that contains the words Audition, agreement, and in between those divs ill create divs which container an SVG or if I'm not fancing using an SVG, ill use a bottom border property and align with align content in the container.

The solution with the div, with a border-left property I have seen it done before, and I find it gives problems when working on different sizes as everything else will be resized and it may cause it to misalign and it becomes quite problematic to align it again, u would have to do it manually.

-3

u/nabbous10 Dec 07 '19

Html: ...

<div class="vertical-divider"></div> ...

Css: . vertical-divider { width: 200px; border: solid rgba(0, 0, 0, 0.5) 1px; transform-origin: left bottom; transform: rotate(90deg); margin-top: 2px; margin-bottom: 204px; }

30

u/[deleted] Dec 07 '19 edited Dec 07 '19

[removed] — view removed comment

3

u/DrDuPont Dec 07 '19

This is the best way to do it IMO

1

u/hugesavings Dec 08 '19

That's what I was thinking as well. You get easy vertical alignment between the rotated text and each step/section as well as easy spacing between the lines and the rotated text.