Search code examples
cssanchorcentervertical-alignment

Center Vertically Anchor Tag in inline-block


Hello I need to center anchor tag vertically and I'm using vertical-align: middle; but it doesn't work for me. Can someone tell me what can I do. I'm using inline-block for display and MDN Reference shows that vertical align works for inline-block elements but it doesn't for me. I'm giving out the code see if someone can help

@import url('https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,[email protected],100;8..144,300&family=Roboto+Mono&family=Roboto:wght@100;400&display=swap');

*{
    background-color: rgb(26, 26, 26);
    margin: 0;
    padding: 0;
}
a{
    display: inline-block;
    border: 1px solid rgb(255, 90, 90);
    color: rgb(255, 124, 124);
    background-color: rgb(255, 85, 85, 0.5);
    text-decoration: none;
    border-radius: 5px;
    padding: 1.5px 12px;
    text-transform: capitalize;
    font-family: 'Roboto Flex', Sans-Serif;
    font-size: xx-large;
    vertical-align: middle;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>button</title>
    <link rel="stylesheet" href="button.css">
</head>
<body>
   
    <center><a href="#" target="_blank"">Export</a>
</body>
</html>


Solution

  • You need to use flex property to center align anchor tag.

    @import url('https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,[email protected],100;8..144,300&family=Roboto+Mono&family=Roboto:wght@100;400&display=swap');
    
    *{
        background-color: rgb(26, 26, 26);
        margin: 0;
        padding: 0;
    }
    body{
          display: flex;
          justify-content: center;
          align-items: center;
          height:100vh;
     }
    a{
         display: flex;
         justify-content: center;
         align-items: center;
         width:200px;
        border: 1px solid rgb(255, 90, 90);
        color: rgb(255, 124, 124);
        background-color: rgb(255, 85, 85, 0.5);
        text-decoration: none;
        border-radius: 5px;
        padding: 1.5px 12px;
        text-transform: capitalize;
        font-family: 'Roboto Flex', Sans-Serif;
        font-size: xx-large;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>button</title>
        <link rel="stylesheet" href="button.css">
    </head>
    <body>
       
        <center><a href="#" target="_blank"">Export</a>
    </body>
    </html>