Home > database >  Centering Bootstrap Icons Horizontally
Centering Bootstrap Icons Horizontally

Time:10-26

I'm having trouble centering Bootstrap Icons inside my buttons. As you can see, the icon isn't centered horizontally. I can't get it to work (I've tried text-align, flexbox with justify-content, ...), because apparently the "letter" itself is taking up some extra space on the right - at least that's what it looks like to me. Can anybody help me?

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

button {
    padding: 4px;
    font-size: 24px;
    text-align: center;
}
<!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">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<body>
    <button>
        <i class="bi-play"></i>
    </button>
</body>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

This doesn't have anything to do with text as what you are trying to center is the i element. Therefore it's the button that needs to be displayed as flex and to set the centering.

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

button {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 4px;
    font-size: 24px;
}
<!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">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<body>
    <button>
        <i class="bi-play"></i>
    </button>
</body>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

*{
padding:0;
margin:0;
}
button {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 30px;
    width: 60px;
    font-size: 30px;
    border-radius: 5px;
    background-color: red;
}
.bi-play{
color: white;
}
<!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">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<body>
    <button>
        <i class="bi-play my4"></i>
    </button>
</body>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Check now if it solves your problem. I think it is centered now.

  • Related