B
B
Bobull2016-10-01 14:20:50
css
Bobull, 2016-10-01 14:20:50

How to center align a span block with a sprite with different image sizes?

There is a code:

<ul>
  <li><span class="item1"></span></li>
  <li><span class="item2"></span></li>
  <li><span class="item3"></span></li>
</ul>

scss
ul {
  li {
    background: #262626;
    width: 109px;
    height: 109px;
    margin-left: 20px;
    list-style: none;
    border-radius: 50%;
    float: left;
    span {
     &.item1 {
       &:after {
          content: "";
          background: transparent url('https://s11.postimg.org/rrxo3gsz7/sprite_test.png') 0 0 no-repeat;
          width: 70px;
          height: 52px;
          display: inline-block;
        }
      }
       &.item2 {
       &:after {
          content: "";
          background: transparent url('https://s11.postimg.org/rrxo3gsz7/sprite_test.png') -70px -8px no-repeat;
          width: 23px;
          height: 32px;
          display: inline-block;
        }
      }
      &.item3 {
       &:after {
          content: "";
          background: transparent url('https://s11.postimg.org/rrxo3gsz7/sprite_test.png') -93px 0 no-repeat;
          width: 52px;
          height: 52px;
          display: inline-block;
        }
      }
    }
  }
}

https://jsfiddle.net/0s6u1b3b/
We need to center the span block with the image in relation to li:
sprite_norm.png
You can margin or position: absolute (), but if we have a lot of images in the sprite, we will need to set parameters for the skin :after ... and it's stressful :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Bogachev, 2016-10-01
@Bobull

In the case of your sprite, you can use this thing for span and not think about their location:

position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question