Vertically centering content of :before/:after pseudo-elements

前端 未结 6 1122
闹比i
闹比i 2020-11-29 17:51

I\'m trying to achieve something similar to this picture:

\"enter

I have an im

相关标签:
6条回答
  • 2020-11-29 18:35

    Assuming your element is not an <img> (because pseudo elements are not allowed on self-closing elements), let's suppose it's a <div>, so a way could be:

    div {
        height: 100px ;
        line-height: 100px;
    }
    div:before, div:after {
        content: "";
        ...
        display: inline-block;
        vertical-align: middle;
        height: ...;
        line-height: normal;
    }
    

    If you cannot change the line-height of the div, another way is:

    div {
        position: relative;
    }
    div:before, div:after {
        position: absolute;
        display: block;
        top: 50%;
        -webkit-transform: translateY(-50%);
        -moz-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        transform: translateY(-50%);
        content: "";
        width: ...
    }
    

    Otherwise, just place the indicators as a background in center position.

    0 讨论(0)
  • 2020-11-29 18:35

    Using flex box, you should set a fixed width and height in the parent first then

    div::after {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    0 讨论(0)
  • 2020-11-29 18:36

    Here is a way of creating next and previous controls, using :before and :after pseudo-elements. Along with border trick to create triangles for previous/next buttons. It does not give you an area 100% of height to click, but if you make the triangle (arrows) a big enough size it should make up for that.

    div {
      position: relative;
      width: 800px;
      max-width: 80%;
      border: 1px solid red;
      text-align: center;
      margin: 0 auto;
    }
    
    div:before, div:after {
      opacity: 0.5;
      display:block;
      content: "";
      position:absolute;
      width: 0; 
      height: 0;
      }
    
    div:before {
      top: 40%; left: 0;
      border-top: 25px solid transparent; 
      border-right: 50px solid blue; 
      border-bottom: 25px solid transparent;
    }
    
    div:after {
      top: 40%; right: 0;
      border-top: 25px solid transparent; 
      border-left: 50px solid blue; 
      border-bottom: 25px solid transparent;
    }
    

    Here is the code working: http://jsfiddle.net/fiddleriddler/rPPMf/

    0 讨论(0)
  • 2020-11-29 18:51

    You can do this without resorting to images. (Sometimes you can't, e.g. using font icons inside :before or :after).

    div {
       position: relative;
       overflow:hidden;
    }
    
    div:before, div:after {
       position: absolute;
       display: block;
       top: 50%;
       -webkit-transform: translateY(-50%);
       -moz-transform: translateY(-50%);
       -ms-transform: translateY(-50%);
       transform: translateY(-50%);
    
       height:20000px;
       line-height:20000px;
    
       content: ">>";
    }
    

    Admittedly, it's a bit cheeky to use 20000px If your div will ever be larger than that, just increase the px.

    In your case, you have an image inside the div, so hit that image with display:block (images don't default to display:block)

    Here's the updated fiddle for your particular case. http://jsfiddle.net/rdy4u/56/

    0 讨论(0)
  • 2020-11-29 18:51

    I know I'm late for the party but this simple flex solution worked like a charm for me in case it helps any of you.

    .main-div {
        display: flex;
        align-items: center;
    }
    
    .my-div-name:before, .my-div-name:after {
        /* This content will be vertically centered with the attributes of the main-div */
    }
    
    0 讨论(0)
  • 2020-11-29 18:55

    Using flex in the pseudo element's css it is rather easy:

    .parent::after {
      content: "Pseudo child text";
      position: absolute;
      top: 0;
      right: 0;
      width: 30%;
      height: 100%;
      border: 1px solid red;
      display:flex;
      flex-direction:row;
      align-items: center;
      justify-content: center;
    }
    

    see https://jsfiddle.net/w408o7cq/

    0 讨论(0)
提交回复
热议问题