You can use position: fixed
and calc
to achieve that
Check the updated css below
.content {
width: 50%;
height: 2000px;
margin: 0 auto;
position: relative;
background: white;
}
.content:before {
content: "";
display: block;
width: 50px;
height: 200px;
position: fixed; //fixed position
left: calc(50% - 25% - 50px); // used calc to determine the correct position
background: red;
}
Explanation
left: calc(50% - 25% - 50px);
50%
is to position the item at the center of the page.
-25%
is half the value of the parent container which is 50%
-50px
is the width of :before
to move it to the left
Quick Note: Most browsers will set the default left
and top
positions of a fixed
position element to be the same left
and top
of its parent container, so if you used position:fixed
without specifying left
and top
the fixed element will default to its parent element position
Edit: as mentioned in comments above position:sticky
will also do the trick but it might give some different behaviour on old browsers and mobile devices.