如何使用 css 使一个文本相对于另一个文本居中?
How to center a text relative to another text using css?
我正在构建一个记分卡,它将显示您的健康状况的一些不同参数。下面是我的代码
<!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>
这是它在浏览器中的样子
如您所见,有不同的健康参数,例如 Sugar level
、Hemoglobin level
等。但如果您仔细观察,分数并不在各自参数的中心,尤其是 Water
和 Security
。现在我不确定如何根据分数上方的参数将其居中。
我阅读了 css 中的位置,例如 relative
和 absolute
,但我不确定如何将其用于另一个元素。我也试过 display: flex; align-items: center;
到 sub-score
div 但没有用。
如何根据参数定位数字?
您可以在 <h2>
和 <h3>
上使用 text-align: center;
使文本居中。
您可以使用 text-center
bootstrap class 将您的元素居中对齐
样本jsfiddle
Html table非常适合这个目的:
<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 设置 table 的内边距和边距的样式。它对所有单元格都是一致的。
请注意,以这种方式使用 H2
和 H3
不是 'SEO-friendly',您可能希望将该文本放在 span 等中。但如果你只是测试也没关系。
您在设计主 h1 样式时确实找到了解决方案。它只需要多一点方法。 content-justify
: center
.
方法一
I'm writing this method because, you are using flex with bootstrap 3. So
you can switch to 4. See also.
.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>
方法二
.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
bootstrap 3 helper classes.
将以下代码复制到空白 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>
我正在构建一个记分卡,它将显示您的健康状况的一些不同参数。下面是我的代码
<!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>
这是它在浏览器中的样子
如您所见,有不同的健康参数,例如 Sugar level
、Hemoglobin level
等。但如果您仔细观察,分数并不在各自参数的中心,尤其是 Water
和 Security
。现在我不确定如何根据分数上方的参数将其居中。
我阅读了 css 中的位置,例如 relative
和 absolute
,但我不确定如何将其用于另一个元素。我也试过 display: flex; align-items: center;
到 sub-score
div 但没有用。
如何根据参数定位数字?
您可以在 <h2>
和 <h3>
上使用 text-align: center;
使文本居中。
您可以使用 text-center
bootstrap class 将您的元素居中对齐
样本jsfiddle
Html table非常适合这个目的:
<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 设置 table 的内边距和边距的样式。它对所有单元格都是一致的。
请注意,以这种方式使用 H2
和 H3
不是 'SEO-friendly',您可能希望将该文本放在 span 等中。但如果你只是测试也没关系。
您在设计主 h1 样式时确实找到了解决方案。它只需要多一点方法。 content-justify
: center
.
方法一
I'm writing this method because, you are using flex with bootstrap 3. So you can switch to 4. See also.
.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>
方法二
.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
bootstrap 3 helper classes.
将以下代码复制到空白 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>