How to center a vertical bar in CSS? The strip itself is made:

.vline { border-right: 1px solid white; height: 70px; } 
  • margin: auto tried? - kvvk
  • Can you add details and running code? - Vadim Ovchinnikov

2 answers 2

If the strip is a block element, and you need to align it in the center of the parent container, in which nothing else lies, then you can:

 margin:0 auto; 

If there is something in the parent container and you don’t want to deal with flex, you can do this:

 .container{ position:relative; } .container .vline{ position: absolute; left: 50%; } 

If you need this bar on top of everything in the center of the screen, you can do this:

 .vline{ position:fixed; left:50vw; z-index: 100; } 

Or specify the question and add more code.

    1. You can do this through "position: fixed". Example
    2. You can do this in a container using an offset inside the container. Example
    3. You can do this with the Flexbox. An example .

    Depending on how the rest of the elements will be located, the answer will change. Add a usage example.

    • one
      Can you transfer the code from a third-party resource here? - Vadim Ovchinnikov