Search code examples
htmlcssflexboxalignment

Borders to take up just element not pseudo element


I'm making a website where I would like to have some borders besides some text. And above it, some icons. What I want is for the border to only occupy the text area of the LinkedIn text, not the entire class. I've tried with position: absolute but gotten nowhere.

.contactSocial {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.facebook:before {
  display: flex;
  justify-content: center;
  content: "\f39e";
  font-family: "Font Awesome 5 Brands";
  font-weight: 900;
  color: blue;
}
 
.linkedIn {
  border-right: 2px solid green;
  border-left: 2px solid green;
}

.linkedIn:before {
 display: flex;
 justify-content: center;
 content: "\f0e1";
 font-family: "Font Awesome 5 Brands";
 font-weight: 900;
 color: blue;
}
 
.instagram:before {
  display: flex;
  justify-content: center;
  content: "\f16d";
  font-family: "Font Awesome 5 Brands";
  font-weight: 900;
  color: blue;
}
<head><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
</head>

<div class="contactSocial">
  <div class="facebook">Facebook</div>
  <div class="linkedIn">LinkedIn</div>
  <div class="instagram">Instagram</div>
</div>


Solution

  • If you'd change you markup, the simplest thing would be to wrap the labels into a span and apply the border to these span element - see demo below:

    .contactSocial {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .facebook:before {
      display: flex;
      justify-content: center;
      content: "\f39e";
      font-family: "Font Awesome 5 Brands";
      font-weight: 900;
      color: blue;
    }
     
    .linkedIn span { /* CHANGED */
      border-right: 2px solid green;
      border-left: 2px solid green;
    }
    
    .linkedIn:before {
     display: flex;
     justify-content: center;
     content: "\f0e1";
     font-family: "Font Awesome 5 Brands";
     font-weight: 900;
     color: blue;
    }
     
    .instagram:before {
      display: flex;
      justify-content: center;
      content: "\f16d";
      font-family: "Font Awesome 5 Brands";
      font-weight: 900;
      color: blue;
    }
    <head><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
    </head>
    
    <div class="contactSocial">
      <div class="facebook"><span>Facebook</span></div>
      <div class="linkedIn"><span>LinkedIn</span></div>
      <div class="instagram"><span>Instagram</span></div>
    </div>