鱼C论坛

 找回密码
 立即注册
查看: 2815|回复: 3

[已解决]line-height 已经和盒子高度相同了 但是文字并没有垂直居中

[复制链接]
发表于 2018-8-7 21:38:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
line-height 已经和盒子高度相同了 但是文字并没有垂直居中,代码如下 红字部分是有疑问的地方

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
                * {
                        margin: 0px;
                        padding: 0px;
                }
                .search {
                        margin: 15px auto;
                        height: 336px;
                        width: 268px;
                        border: 1px solid #D9E0EE;
                        border-top: 3px solid #FF8400;
                }
                .search h3 {
                        height: 40px;
                        border-bottom: 1px solid #D9E0EE;
                        line-height: 40px;
                        font: 400 16px "微软雅黑";

                }
        </style>
</head>
<body>
        <div class="search">
                <h3>搜索趣图</h3>
        </div>
</body>
</html>
最佳答案
2018-8-8 09:26:21
本帖最后由 不二如是 于 2018-8-8 10:45 编辑
liumoc 发表于 2018-8-7 21:49
我刚刚试了下 我把font分开写就没有问题 但是用合写的格式就不会居中 为什么啊



1、font元素,在H5中已经不推荐使用,如果需要设置字体大小样式,建议:
  1. font-weight: 400;
  2.             font-size: 16px;
  3.             font-family:"微软雅黑";
复制代码


并且h3元素已有默认大小,不建议当文本用,所以请用p元素。

2、关于垂直居中,请参看:私家藏货之@垂直居中

line-height方法适用于和父元素一样高(336px,而不是40px),例如:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <style>
  7.         #fishc{
  8.             width: 500px;
  9.             height: 500px;
  10.             background: gray;
  11.         }

  12.         #fishc p{
  13.             line-height: 500px;
  14.         text-align: center;
  15.         }
  16.     </style>
  17. </head>
  18. <body>

  19. <div id="fishc">
  20.     <p>搜索趣图</p>
  21. </div>

  22. </body>
  23. </html>
复制代码

Snip20180808_295.png


这才是line-height和盒子高度相同。

3、所以需要在添加一层div,用来放需要的40px:
  1. <div class="content">
  2.     <div id="fishc">
  3.         <p>搜索趣图</p>
  4.     </div>
  5. </div>
复制代码


设置行高后,还需要margin需要反向移动负的一半行高
  1. .content{
  2.             margin: 15px auto;
  3.             height: 336px;
  4.             width: 268px;
  5.             border: 1px solid #D9E0EE;
  6.             border-top: 3px solid #FF8400;
  7.         }
  8.         #fishc{
  9.             margin: -20px 0;
  10.             width: 100%;
  11.             height: 40px;
  12.             border-bottom: 1px solid #D9E0EE;
  13.         }

  14.         #fishc p{
  15.             line-height: 40px;
  16.         text-align: center;
  17.         }
复制代码

Snip20180808_297.png


源码(VIP免费): index.zip (452 Bytes, 下载次数: 1, 售价: 3 鱼币)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-7 21:41:39 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-7 21:49:04 | 显示全部楼层
我刚刚试了下 我把font分开写就没有问题 但是用合写的格式就不会居中 为什么啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-8 09:26:21 | 显示全部楼层    本楼为最佳答案   
本帖最后由 不二如是 于 2018-8-8 10:45 编辑
liumoc 发表于 2018-8-7 21:49
我刚刚试了下 我把font分开写就没有问题 但是用合写的格式就不会居中 为什么啊



1、font元素,在H5中已经不推荐使用,如果需要设置字体大小样式,建议:
  1. font-weight: 400;
  2.             font-size: 16px;
  3.             font-family:"微软雅黑";
复制代码


并且h3元素已有默认大小,不建议当文本用,所以请用p元素。

2、关于垂直居中,请参看:私家藏货之@垂直居中

line-height方法适用于和父元素一样高(336px,而不是40px),例如:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <style>
  7.         #fishc{
  8.             width: 500px;
  9.             height: 500px;
  10.             background: gray;
  11.         }

  12.         #fishc p{
  13.             line-height: 500px;
  14.         text-align: center;
  15.         }
  16.     </style>
  17. </head>
  18. <body>

  19. <div id="fishc">
  20.     <p>搜索趣图</p>
  21. </div>

  22. </body>
  23. </html>
复制代码

Snip20180808_295.png


这才是line-height和盒子高度相同。

3、所以需要在添加一层div,用来放需要的40px:
  1. <div class="content">
  2.     <div id="fishc">
  3.         <p>搜索趣图</p>
  4.     </div>
  5. </div>
复制代码


设置行高后,还需要margin需要反向移动负的一半行高
  1. .content{
  2.             margin: 15px auto;
  3.             height: 336px;
  4.             width: 268px;
  5.             border: 1px solid #D9E0EE;
  6.             border-top: 3px solid #FF8400;
  7.         }
  8.         #fishc{
  9.             margin: -20px 0;
  10.             width: 100%;
  11.             height: 40px;
  12.             border-bottom: 1px solid #D9E0EE;
  13.         }

  14.         #fishc p{
  15.             line-height: 40px;
  16.         text-align: center;
  17.         }
复制代码

Snip20180808_297.png


源码(VIP免费): index.zip (452 Bytes, 下载次数: 1, 售价: 3 鱼币)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表