I'm currently learning bootstrap to design a website. And I'm now creating a profile page of a user. There will be fields (e. g. name, email etc.) and values against them (on one line, if the screen size allows). So I want the field to be at the left, and the value at the center, but I can't find out how to implement it using bootstrap classes and css, if needed. So here is the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"><script src="https://kit.fontawesome.com/2e9370a952.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>Profile</title>
<style>
body {
background: linear-gradient(to right, #7A96E9, #BFAEE0);
text-align: center;
}
.profile-info {
background: white;
border-radius: 4px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="container d-flex flex-column justify-content-center" style="height: 100vh">
<div class="row">
<div class="col-lg-7 col-md-8 col-sm-10 mx-auto">
<div class="profile-info p-4">
<h1 class="mb-4">Profile</h1>
<div class="text-start">
<p><strong>Email</strong><span class="value">[email protected]</span></p>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>
So here I want "Email" (in the tag "strong") to be at the left of the parent "profile-info" and the span (with class "value") to be centered in that parent.
Text-Align
the entire parent to center
, and then position
absolute
ly with the combination of left:0
and right:0
the value
class, and float
the strong
to the left
, Like so:
.text-start p {
text-align: center
}
.text-start strong {
float: left;
}
.value {
position: absolute;
left: 0;
right: 0;
margin: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/2e9370a952.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>Profile</title>
<style>
body {
background: linear-gradient(to right, #7A96E9, #BFAEE0);
text-align: center;
}
.profile-info {
background: white;
border-radius: 4px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="container d-flex flex-column justify-content-center" style="height: 100vh">
<div class="row">
<div class="col-lg-7 col-md-8 col-sm-10 mx-auto">
<div class="profile-info p-4">
<h1 class="mb-4">Profile</h1>
<div class="text-start">
<p><strong>Email</strong><span class="value">[email protected]</span></p>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>