You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Bootstrap实现个人资料页字段居左、值居中布局

实现Bootstrap个人资料页字段居左、值在容器内居中的布局

直接上修改后的完整代码,你可以直接替换使用:

<!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/bootstrap@5.0.2/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/core@2.9.2/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);
        }
        .info-item {
            padding-left: 0.5rem; /* 避免字段贴容器边缘 */
        }
        .info-item .value {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }
        /* 小屏幕自动调整布局,避免内容重叠 */
        @media (max-width: 576px) {
            .info-item .value {
                position: static;
                display: block;
                text-align: center;
                margin-top: 0.25rem;
            }
        }
    </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="info-item position-relative mb-3">
                        <strong>Email</strong>
                        <span class="value">example@example.com</span>
                    </div>
                    <div class="info-item position-relative mb-3">
                        <strong>姓名</strong>
                        <span class="value">张三</span>
                    </div>
                    <div class="info-item position-relative mb-3">
                        <strong>电话</strong>
                        <span class="value">138xxxx1234</span>
                    </div>
                </div>
            </div>
        </div>
    </div>    
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>

关键实现思路:

  1. 定位核心逻辑:给每个信息项容器.info-item加上Bootstrap的position-relative类,作为.value绝对定位的参考容器;通过left:50%配合transform: translateX(-50%)让值精准居中在父容器内,不受内容宽度影响。
  2. 响应式适配:在小屏幕(宽度≤576px,对应Bootstrap的sm断点)时,把.value改为静态定位,块级显示并居中,避免字段和值重叠,保证移动端可读性。
  3. 细节优化:给.info-item添加左边距避免字段贴边,用mb-3类给信息项增加间距,让布局更清爽。

内容的提问来源于stack exchange,提问作者Mark Kalinkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:48:26