【问题标题】:Bootstrap : Unable to center contents of entire row [duplicate]Bootstrap:无法将整行的内容居中[重复]
【发布时间】:2019-11-11 23:47:33
【问题描述】:

我正在学习 HTML5、CSS3 和 Bootstrap4,并创建了一个虚拟项目。

我正在尝试创建一个将我的图标和文本(公司名称)并排放置的行,并使用display:inline 来实现相同的目的。这之后是一个标语行。

但是,我无法在中心获取整行的内容。我试过text-centertext-xs-center 但无济于事。我什至创建了一个自定义 css 类

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

但这也没用。

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;
}

【问题讨论】:

  • 尝试使用bootstrap的类...like
  • 试试这个row d-flex justify-content-center
  • 您的代码中有一些更正。在 Bootstrap 4 中,col-xs 已被删除。所以使用col-12 而不是col-xs-12。我在下面做了一个工作的sn-p,请看一下

标签: html css bootstrap-4


【解决方案1】:

您可以使用引导程序 4 flex utilities 通过使用 d-flex justify-content-center 来执行此操作。确保 bootstrap 4 没有类 col-xs-12,它被 col-12 替换

#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 d-flex 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>

【讨论】:

  • 感谢用 col-12 替换 col-xs-12。
【解决方案2】:

请将此 2line 替换为您的代码

<div class="row text-center" id="topSection">
<div class="col-12 text-center w-100">

#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 text-center" id="topSection">
      <div class="col-12 text-center w-100">
        <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>

【讨论】:

  • 注意点。 Bootstrap 4 没有类col-xs-**。替换为col-**
  • 你是对的,但只是在他的代码上工作,到目前为止它运行良好:)
【解决方案3】:

#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>

row 上使用justify-content-center 供参考,您可以通过Bootstrap 4 flex utilities 希望对你有帮助

【讨论】:

    【解决方案4】:

    使用此代码

    只需将&lt;div class="col-xs-12 text-center"&gt; 更改为&lt;div class="col-md-12 text-center"&gt;

    <!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-md-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>
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签