引导程序:无法将整个行的内容居中

时间:2019-07-01 10:00:31

标签: html css bootstrap-4

我正在学习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;
}

4 个答案:

答案 0 :(得分: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>

justify-content-center上使用row 作为参考,您可以浏览Bootstrap 4 flex utilities 希望对您有帮助

答案 1 :(得分:1)

您可以使用引导程序4 flex utilities通过使用d-flex justify-content-center行来实现。确保引导程序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>

答案 2 :(得分:1)

请将这2行替换为您的代码

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

答案 3 :(得分:0)

使用此代码

  

只需将<div class="col-xs-12 text-center">更改为<div class="col-md-12 text-center">

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