Search code examples
htmlcssimage

how to center background image added by image sprites


.social-media-icon{
            display: inline-block;
    
            }
    
            .social-media-icon a{
                text-decoration: none;
                display: block;
                text-indent: -9999px;
                border: 2px solid #c3d4e2;  
                position: relative;
                    
                height: 36px;
                width: 36px;
                background: url(/img/social-icons.png) ;
                background-repeat: no-repeat;
                background-position: 50%;
    
            }
            .social-media-icon a:hover{
                background-color: #c3d4e2;
            }
    
            .fb-icon a{
                background-position:  0 0 ; 
    
            }
            .fb-icon a:hover{
                background-position: 0 -36px;
            }
    
            .x-icon a{
                background-position:-36px 0 ; 
            }
    
            .x-icon a:hover{
                background-position: -36px -36px ; 
            }
    
            .youtube-icon a{
                background-position: -72px 0 ; 
            }
            .youtube-icon a:hover{
                background-position: -72px -36px ; 
            }
    
            .insta-icon a{
                background-position: -108px 0 ; 
            }
            .insta-icon a:hover{
                background-position: -108px -36px ; 
            }
<div class="social-media">
<div class="fb-icon social-media-icon icon">
    <a href="">

        <span class="social-text">fb</span>
    </a>
</div>
<div class="x-icon social-media-icon icon">
    <a href="">
        <span class="social-text">x</span> 
    </a>
</div>
<div class="youtube-icon social-media-icon icon">
    <a href="">
        <span class="social-text">youtube</span>
    </a>
</div>
<div class="insta-icon social-media-icon icon">   
    <a href="">
        <span class="social-text">intergram</span>
     </a>
</div>

This code snippet is for creating background images using image sprites. when hovering over the image that background image should change.

I want to center those social media icons in that div element. I tried background-position: center center but it didn't work. Here I attached an output of the above code snippet. That isn't centered properly. What can I do to center all images in each div elements enter image description here

This is the main imageenter image description here


Solution

  • You need to adjust the background-position for all the icons.

    .social-media-icon{
        display: inline-flex;
        justify-content: center;
        align-items: center;
    
    }
    
    .social-media-icon a{
        text-decoration: none;
        display: block;
        text-indent: -9999px;
        border: 2px solid #c3d4e2;  
    
        height: 36px;
        width: 36px;
        background: url(https://i.sstatic.net/eSmLc9vI.png);
    
    }
    .social-media-icon a:hover{
        background-color: #c3d4e2;
    }
    
    .fb-icon a{
        background-position: 2px 2px; 
    
    }
    .fb-icon a:hover{
        background-position: 2px 2px;
    }
    
    .x-icon a{
        background-position:-36px 0 ; 
    }
    
    .x-icon a:hover{
        background-position: -36px -36px ; 
    }
    
    .youtube-icon a{
        background-position: -71px 2px ; 
    }
    .youtube-icon a:hover{
        background-position: -71px -34px ; 
    }
    
    .insta-icon a{
        background-position: -106px 2px ; 
    }
    .insta-icon a:hover{
        background-position: -106px -34px ; 
    }
    <div class="social-media">
    <div class="fb-icon social-media-icon icon">
        <a href="">
    
            <span class="social-text">fb</span>
        </a>
    </div>
    <div class="x-icon social-media-icon icon">
        <a href="">
            <span class="social-text">x</span> 
        </a>
    </div>
    <div class="youtube-icon social-media-icon icon">
        <a href="">
            <span class="social-text">youtube</span>
        </a>
    </div>
    <div class="insta-icon social-media-icon icon">   
        <a href="">
            <span class="social-text">intergram</span>
         </a>
    </div>