文本居中对齐line-height:等于盒子高度实现居中和 text-align: center+ padding-top: 17px实现居中

本文介绍两种使导航栏及其文字在页面上水平及垂直居中的方法。通过使用CSS的text-align和padding-top属性,或者利用行高(line-height)特性,实现导航栏的美观布局。文章详细解释了每种方法的实现步骤,并对比了它们之间的差异。

效果图

1、导航栏整体要水平居中,且文字要不仅要水平居中,而且需要垂直居中。背景图和为橙色的图,当鼠标经过时,背景图片变为蓝色的背景图
在这里插入图片描述
2、实现方法一:
html代码部分

<div class="father">
        <div class="nav">
            <a href="#">首页</a>
            <a href="#">文档</a>
            <a href="#">案例</a>
            <a href="#">开始</a>
            <a href="#">关于我们</a>
        </div>
    </div>

CSS 代码部分:

    <style>
        .father{
            background-color: palegreen;
            height: 1000px;
        }
        .nav{
            text-align: center;   /*让这个导航栏居中*/
        }
        .nav a {
            width: 120px;  /* 背景图片的高度和宽度分别是120 和 50 */
            height: 33px;
            text-decoration: none;                  /*取消a链接的下划线*/ 
            background-image: url(../img/bg.png);   /*添加背景图片  不平铺*/
            display:inline-block;    /*显示形式为行内块元素*/
            text-align: center;    /*文字水平居中*/
            padding-top: 17px;  /*垂直居中*/
        }
        .nav a:hover {
            background-image: url(../img/bgc.png);  /*鼠标事件*/
        }
    </style>

2、实现方法二:
CSS代码部分

 <style>
        .father{
            background-color: palegreen;
            height: 1000px;
        }
        .nav{
            text-align: center;   /*让这个导航栏居中*/
        }
        .nav a {
            width: 120px;  /* 背景图片的高度和宽度分别是120 和 50 */
            height: 50px;
            text-decoration: none;                  /*取消a链接的下划线*/ 
            background-image: url(../img/bg.png);   /*添加背景图片  不平铺*/
            display:inline-block;    /*显示形式为行内块元素*/
           line-height: 50px;   /*行高等于盒子的高度,可以实现单行文本垂直居中 */
        }
        .nav a:hover {
            background-image: url(../img/bgc.png);
        }
    </style>

2、两种方法的分析比较:
不相同的地方:
- 就是在字体居中的时候
- 方法一:
- 是先让字体水平居中 :text-align: center;
- 然后让字体垂直居中: padding-top: 17px;
- 但是这个样子会撑大盒子的大小所以要设置 li的高度: height: 34px; 34px+34px+16px(字体大小) = 50px
- 方法二
- 利用行内块元素的特点
- 行高等于盒子的高度,可以实现单行文本垂直居中
- 这样就不用调整 li 的高度

.tableContainer { margin-top: 10px; margin-bottom: 20px; padding: 10px; } .totalDownload{ display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } .total{ color:rgba(51, 51, 51, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 400; font-size: 14px; line-height: 20px; } .downloadButton{ width: 96px; height: 28px; padding: 15px; background-color: rgba(247, 247, 247, 1); border: none; border-radius: 14px; display: flex; align-items: center; justify-content: center; color: rgba(102, 102, 102, 1); font-size: 12px; font-weight: 500; white-space: nowrap; } .datasetFileItem{ display: flex; align-items: flex-start; /* 左侧图标和 fileInfo 顶部对齐 */ justify-content: space-between; padding: 8px 0; box-sizing: border-box; } /* 左侧图标容器(可选包裹) */ .fileIconContainer img { flex-shrink: 0; } /* 中间文件信息区域 */ .fileInfo { flex: 1; margin-left: 8px; min-width: 0; /* 关键:允许内部内容截断,防止溢出 */ display: flex; flex-direction: column; } .fileName{ color: rgba(29, 29, 29, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 500; font-size: 12px; line-height: 20px; max-width: 200px; /* 可根据需要调整 */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .fileSummary{ color: rgba(153, 153, 153, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 400; font-size: 12px; line-height: 16px; margin-top: 4px; white-space: nowrap; } /* 操作按钮区域:垂直居中 */ .operationButton { display: flex; align-items: center; /* 垂直居中 */ justify-content: center; flex-shrink: 0; margin-left: 12px; } 目前右侧操作按钮是没有水平垂直居中的,请修改关键代码
11-11
现在的情况是operationButton水平方向上没有单独处理居中,导致它离中间的fileInfo很远.tableContainer { margin-top: 10px; margin-bottom: 20px; padding: 10px; } .totalDownload{ display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } .total{ color:rgba(51, 51, 51, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 400; font-size: 14px; line-height: 20px; } .downloadButton{ width: 96px; height: 28px; padding: 15px; background-color: rgba(247, 247, 247, 1); border: none; border-radius: 14px; display: flex; align-items: center; justify-content: center; color: rgba(102, 102, 102, 1); font-size: 12px; font-weight: 500; white-space: nowrap; } .datasetFileItem{ display: flex; align-items: flex-start; /* 左侧图标和 fileInfo 顶部对齐 */ justify-content: space-between; padding: 8px 0; box-sizing: border-box; } /* 左侧图标容器(可选包裹) */ .fileIconContainer img { flex-shrink: 0; } /* 中间文件信息区域 */ .fileInfo { flex: 1; margin-left: 8px; min-width: 0; /* 关键:允许内部内容截断,防止溢出 */ display: flex; flex-direction: column; } .fileName{ color: rgba(29, 29, 29, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 500; font-size: 12px; line-height: 20px; max-width: 250px; /* 可根据需要调整 */ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .fileSummary{ color: rgba(153, 153, 153, 1); font-family: &#39;HarmonyHeiTi&#39;; font-weight: 400; font-size: 12px; line-height: 16px; margin-top: 4px; white-space: nowrap; } /* 操作按钮区域:垂直居中 */ .operationButton { display: flex; align-items: center; /* 垂直居中 */ justify-content: center; flex-shrink: 0; /* 关键:让这个盒子自己垂直居中,而不受父级 align-items 影响 */ align-self: center; justify-self: flex-start; } 请修改关键代码
11-11
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值