如何使用CSS将文本相对于另一个文本居中?

时间:2019-04-07 11:50:31

标签: html css twitter-bootstrap-3 css-position

我正在建立一个记分卡,其中将显示一些有关您健康状况的不同参数。这是我的下面的代码

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <title>Scorecard</title>
    <style>
        .sub-score {
            margin-left: 20%;
            color: #00FF00;

        }

        .category {
            color: DodgerBlue;
        }
    </style>

  </head>

  <body>
    <div class="container-fluid">
        <br>

        <div>
            <h1 style="display: flex; align-items: center; justify-content: center; color: #FA0066;">SCORE</h1>
        </div>
        <br>
        <br>
        <div class="row" style="margin-left:10%">
            <div class="col-lg-2 col-sm-3">
                <h3 class="category">Sugar Level</h3>
                <h2 class="sub-score">46<h2>
            </div>
            <div class="col-lg-2 col-sm-3"> 
                <h3 class="category">Hemoglobin Level</h3>
                <h2 class="sub-score">82<h2>
            </div>

            <div class="col-lg-2 col-sm-3"> 
                <h3 class="category">Water</h3>
                <h2 class="sub-score">46<h2>
            </div>

            <div class="col-lg-2 col-sm-3"> 
                <h3 class="category">Blood Pressure</h3>
                <h2 class="sub-score">54<h2>
            </div>

            <div class="col-lg-2 col-sm-3"> 
                <h3 class="category">Security</h3>
                <h2 class="sub-score">46<h2>
            </div>


        </div>

    </div>

    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
   </body>

</html>

这是浏览器中的外观

enter image description here

如您所见,有不同的健康参数,例如Sugar levelHemoglobin level等。但是,如果仔细观察,分数并不是位于其各自参数的中心,尤其是对于{{1} }和Water。现在,我不确定如何相对于分数上方的参数居中。

我了解了诸如Securityrelative之类的CSS位置,但是我不确定如何针对其他元素使用它。我还尝试了absolutedisplay: flex; align-items: center;的div,但无济于事。

如何相对于参数定位数字?

5 个答案:

答案 0 :(得分:2)

您在对主h1进行样式化时实际上找到了解决方案。它只需要多一点方法。 content-justifycenter

方法1

  

我正在编写此方法,因为,您正在将bootstrap 3与flex一起使用。   您可以切换到4。no-floating-promises

.sub-score {
  color: #00FF00;
}

.category {
  color: DodgerBlue;
}

.center {
  display: flex!important;
  align-items: center!important;
  justify-content: center!important;
}

.column {
  flex-direction: column!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container-fluid">
  <br>

  <div>
    <h1 class="center">SCORE</h1>
  </div>
  <br>
  <br>
  <div class="row">
    <div class="col-lg-2 col-sm-3 column center">
      <h3 class="category">Sugar Level</h3>
      <h2 class="sub-score">46
        <h2>
    </div>
    <div class="col-lg-2 col-sm-3 column center">
      <h3 class="category">Hemoglobin Level</h3>
      <h2 class="sub-score">82
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 column center">
      <h3 class="category">Water</h3>
      <h2 class="sub-score">46
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 column center">
      <h3 class="category">Blood Pressure</h3>
      <h2 class="sub-score">54
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 column center">
      <h3 class="category">Security</h3>
      <h2 class="sub-score">46
        <h2>
    </div>


  </div>

</div>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

方法2

.sub-score {
  color: #00FF00;
}

.category {
  color: DodgerBlue;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container-fluid">
  <br>

  <div>
    <h1 class="text-center">SCORE</h1>
  </div>
  <br>
  <br>
  <div class="row">
    <div class="col-lg-2 col-sm-3 text-center">
      <h3 class="category">Sugar Level</h3>
      <h2 class="sub-score">46
        <h2>
    </div>
    <div class="col-lg-2 col-sm-3 text-center">
      <h3 class="category">Hemoglobin Level</h3>
      <h2 class="sub-score">82
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 text-center">
      <h3 class="category">Water</h3>
      <h2 class="sub-score ">46
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 text-center">
      <h3 class="category">Blood Pressure</h3>
      <h2 class="sub-score">54
        <h2>
    </div>

    <div class="col-lg-2 col-sm-3 text-center">
      <h3 class="category">Security</h3>
      <h2 class="sub-score">46
        <h2>
    </div>


  </div>

</div>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

text-center引导程序3 See also.

答案 1 :(得分:1)

将以下代码复制到空白html文件中,然后在浏览器中将其打开。您将获得所需的输出。

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

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <title>Scorecard</title>
  <style>
    .sub-score {
      text-align: center;
      color: #00FF00;
    }

    .category {
      text-align: center;
      color: DodgerBlue;
    }
  </style>

</head>

<body>
  <div class="container-fluid">
    <br>

    <div>
      <h1 style="display: flex; align-items: center; justify-content: center; color: #FA0066;">SCORE</h1>
    </div>
    <br>
    <br>
    <div class="row" style="margin-left:10%">
      <div class="col-lg-2 col-sm-3">
        <h3 class="category">Sugar Level</h3>
        <h2 class="sub-score">46
        </h2>
      </div>
      <div class="col-lg-3 col-sm-3">
        <h3 class="category">Hemoglobin Level</h3>
        <h2 class="sub-score">82
        </h2>
      </div>

      <div class="col-lg-2 col-sm-3">
        <h3 class="category">Water</h3>
        <h2 class="sub-score">46
        </h2>
      </div>

      <div class="col-lg-2 col-sm-3">
        <h3 class="category">Blood Pressure</h3>
        <h2 class="sub-score">54
        </h2>
      </div>

      <div class="col-lg-2 col-sm-3">
        <h3 class="category">Security</h3>
        <h2 class="sub-score">46
        </h2>
      </div>


    </div>

  </div>

  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>

</html>

答案 2 :(得分:0)

您可以在text-align: center;<h2>上使用<h3>来使文本居中。

答案 3 :(得分:0)

您可以使用text-center引导程序类在中心对齐元素

示例jsfiddle

答案 4 :(得分:0)

HTML表非常适合此目的:

<table>

  <tr>
    <th><h3 class="category">Sugar Level</h3></th>
    <th><h3 class="category">Hemoglobin Level</h3></th>
    <th><h3 class="category">Water</h3></th>
    <th><h3 class="category">Blood Pressure</h3></th>
    <th><h3 class="category">Security</h3></th>
  </tr>

  <tr>
    <td><h2 class="sub-score">46</h2></td>
    <td><h2 class="sub-score">82</h2></td>
    <td><h2 class="sub-score">46</h2></td>
    <td><h2 class="sub-score">54</h2></td>
    <td><h2 class="sub-score">46</h2></td>
  </tr>

</table>

然后,您可以使用CSS来设置表格的填充样式,并以自己喜欢的方式设置边距。这将与所有单元格保持一致。

请注意,以这种方式使用H2H3并非“ SEO友好”,您可能希望将该文本放在跨度之内。但是如果您只是进行测试就可以。