首页 > 解决方案 > Bootstrap : Unable to center contents of entire row

问题描述

I'm learning HTML5, CSS3 and Bootstrap4 and have created a dummy project.

I'm attempting to create a row that will house my icon and text (company name) side-by-side and have use display:inline to achieve the same. This is followed by a tag line.

However I'm unable to get the content of the entire row in the center. I have tried text-center or text-xs-center but to no avail. I even created a custom css class

.text-center-custom{
    text-align: center !important;
}

but that also didn't work.

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Project1</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=B612:700&display=swap" rel="stylesheet"> 
    <link href="https://fonts.googleapis.com/css?family=Frank+Ruhl+Libre&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="main.css" type="text/css">
</head>

<body>
    <div class="container">
        <div class="row" id="topSection">
            <div class="col-xs-12 text-center">
                <img src="icons/icon.png" class="inline">
                <h1 class="inline">Test Project</h1>
                <br><br>
                <h3>This is my first test project</h3>
                <h3>BOOTSTRAP</h3>
                <a href="" class="btn btn-danger">Sign-Up</a>
                <br>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
</body>

</html>

main.css

#topSection{
    background: url(images/background.jpg);
    background-position: center;
    background-size: 100%;
    color: bisque;
}

.inline{
    display: inline;
    font-family: 'B612', sans-serif;
}

.text-center-custom{
    text-align: center !important;
}

标签: htmlcssbootstrap-4

解决方案


#topSection{
    background: url(images/background.jpg);
    background-position: center;
    background-size: 100%;
    color: bisque;
}

.inline{
    display: inline;
    font-family: 'B612', sans-serif;
}

.text-center-custom{
    text-align: center !important;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Project1</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Karla&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=B612:700&display=swap" rel="stylesheet"> 
    <link href="https://fonts.googleapis.com/css?family=Frank+Ruhl+Libre&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center" id="topSection">
            <div class="col-12 text-center">
                <img src="icons/icon.png" class="inline">
                <h1 class="inline">Test Project</h1>
                <br><br>
                <h3>This is my first test project</h3>
                <h3>BOOTSTRAP</h3>
                <a href="" class="btn btn-danger">Sign-Up</a>
                <br>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
</body>
</html>

justify-content-center作为row 参考,您可以通过Bootstrap 4 flex 实用程序 希望它会有所帮助


推荐阅读