skip to Main Content
.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

2

Answers


  1. Here is a better way to code is using more generic values but you may have to edit the image because the icons inside the image aren’t perfectly centered. it’s not a CSS issue.

    .social-media-icon {
      display: inline-flex;
    }
    
    .social-media-icon a {
      text-decoration: none;
      text-indent: -9999px;
      height: 36px;
      aspect-ratio: 1;
      padding: 5px;
      border: 2px solid #c3d4e2;
      background: 
        url(https://i.sstatic.net/eSmLc9vI.png) content-box
        var(--x) var(--y,0%) / 400% 200%, var(--c,#0000);
    }
    .social-media-icon a:hover {
      --y:100%;
      --c:#c3d4e2;
    }
    .fb-icon a      {--x:calc(000%/3)} /* 0% */
    .x-icon a       {--x:calc(100%/3)} /* 33.3% */
    .youtube-icon a {--x:calc(200%/3)} /* 66.7% */
    .insta-icon a   {--x:calc(300%/3)} /* 100% */
    <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>
    Login or Signup to reply.
  2. 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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search