css - DIV 讓數字水平垂直置中?

 拍友2502876287 发布于 2022-11-01 16:41
.unsolve
{
    position: absolute;
    right: 10px;
    top:50%;
    transform: translate(0,-50%);
    z-index: 1;
    font-size: 13px;
    background-color: red;
    border-radius: 50%;
    color:#fff;
    width: 15px;
    height: 15px;
    padding: 5px;
    text-align: center;
}

12

這樣設定的時候,數字都會中間偏下,沒辦法完全中間。
請問該怎麼讓他完全水平垂直置中?

7 个回答
  • p中单行文字垂直居中,设置line-height和height两个属性,且两者值相同即可实现。

    2022-11-02 13:58 回答
  • 单行的垂直居中 使用line-height 即可。

    2022-11-02 13:59 回答
  • **设置一下行高就好了 transform: translate(0,-50%)
    这句 可以 删除**

    2022-11-02 14:00 回答
  • 既然你都设置 height 了,那就直接设置 line-height

    2022-11-02 14:06 回答
  • text-align:center; + line-height:容器元素高度

    2022-11-02 14:13 回答
  • 不用这么麻烦
    单行文字
    将行高 line-height 和 height 设为相等即可
    多行文字
    需要将文字抱在p标签中
    外层p display:table 内层p display:table-cell

    2022-11-02 14:19 回答
  • .unsolve
        {
            position: absolute;
            right: 10px;
            top:0;
            bottom: 0;
            margin:auto;
            z-index: 1;
            font-size: 13px;
            background-color: red;
            border-radius: 50%;
            color:#fff;
            width: 15px;
            height: 15px;
            line-height: 15px;
            padding:5px;
            text-align: center;
            
        }
    2022-11-02 14:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有