luke003 2024-07-21 10:09 采纳率: 66.7%
浏览 5

vue,html,css 样式问题处理

我写了一段前端对吗,但是样式很奇怪。我使用 table 表格老显示数据,有个数据项是由两个按钮,和一个数据展示框组成的,但是在我搞好样式之后,我发现,这数据项中,数据文字样式一直是偏上的。使用元素 从 span 换成 div 又换成 p 都不起作用。请教一下经验丰富的朋友。
源码如下:

 <table>
                    <thead class="thead">
                        <tr>
                            <th v-for="(title,index) in theadList">{{title}}</th>
                        </tr>
                    </thead>

                    <tbody class="product">
                        <tr  v-for="(item,index) in fruitList" :key="item.id">
                            <td width="50"> <input type="checkbox"></td>
                            <td> <img :src="item.icon"/></td>
                            <td>{{item.price}}</td>
                            <td class="pro_num">
                                <button class="min_but">-</button>
                                <div class="num">{{item.num}}</div>
                                <button class="plus_but">+</button>
                            </td>
                            <td>{{item.num * item.price}}</td>
                            <td> <button>删除</button></td>
                        </tr>
                    </tbody>

                    <tbody class="empty">
                        <tr >
                            <td colspan="6">购物车空空如也</td>
                        </tr>
                    </tbody>

                    <tfoot class="foot">
                        <tr>
                            <td colspan="6">
                                <input type="checkbox" />全选
                                <span>总价:¥ </span>
                                <span>2400000</span>
                                <button>结算(600)</button>
                            </td>
                        </tr>
                       
                    </tfoot>
                </table>


样式的代码如下:

.table .product tr td .min_but{
    display: inline-block;
    border: 1px solid #333;
    height: 30px;
    width: 30px;
    vertical-align: middle;
}
.table .product .pro_num .num{
    display: inline-block;
    border: none;
    border-top: 1px solid #333;
    border-bottom: 1px solid #333;
    padding: 0;
    width: 80px;
    height: 30px;
    vertical-align: middle;
    text-align: center;
}
.table .product tr td .plus_but{
    display: inline-block;
    border: 1px solid #333;
    height: 30px;
    width: 30px;
    vertical-align: middle;
}

效果如下截图:

img

这个红框内的数字,一直不能居中,一直偏上一线,也不知道怎么处理。请教一下大家,谢谢。

  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-21 10:09
    关注

    以下内容由AIGC及阿里嘎多学长共同生成、有用望采纳:


    从你提供的代码和描述来看,问题可能出在vertical-align: middle;的使用上。vertical-align属性通常用于行内或行内块元素,以调整它们相对于周围元素的垂直对齐方式。但是,如果你的.num元素是div,它默认是块级元素,这时候vertical-align可能不会如预期那样工作。

    以下是一些可能的解决方案:

    1. 使用line-height属性: 确保line-height的值与height的值相等,这样可以在垂直方向上居中文本。例如:

      .table .product .pro_num .num {
          /* ...其他样式... */
          line-height: 30px; /* 与height相同 */
      }
      
    2. 使用Flexbox: 将.pro_num容器设置为Flexbox布局,并使用align-items: center;来垂直居中其子元素。例如:

      .table .product .pro_num {
          display: flex;
          align-items: center;
      }
      
    3. 调整padding: 如果vertical-align不起作用,尝试给.num元素添加适当的padding-toppadding-bottom来调整文本位置。例如:

      .table .product .pro_num .num {
          /* ...其他样式... */
          padding-top: 5px; /* 根据需要调整 */
          padding-bottom: 5px; /* 根据需要调整 */
      }
      
    4. 检查父元素的影响: 有时候,父元素的样式也会影响到子元素的垂直对齐。确保.pro_num的父元素没有设置影响垂直对齐的样式。

    5. 使用transform属性: 如果其他方法都不起作用,你可以尝试使用transform属性来微调元素的位置。例如:

      .table .product .pro_num .num {
          /* ...其他样式... */
          transform: translateY(50%);
      }
      

    请注意,使用transform属性可能需要额外的调整,以确保元素不会超出其容器的边界。

    你可以根据这些建议尝试调整你的CSS代码,看看哪种方法能够解决你的问题。如果问题依旧存在,可能需要更详细地检查HTML结构和CSS样式,以找到问题的根源。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月21日

悬赏问题

  • ¥20 c#使用NPOI向word表格中添加字段导致word有概率警告不可识别
  • ¥15 怎么使用hxe16进制编码器修改视频数据和图片一样!求教程!
  • ¥15 怎么用python模拟光的折射,反射,和全反射
  • ¥15 python打印机打印word和PDF文件出来的是乱码
  • ¥15 求达梦8数据库安装包
  • ¥15 YOLOv8模型网络结构更改后报错
  • ¥15 深度学习中梯度为None
  • ¥15 zynq烧写程序到flash后不运行
  • ¥15 裸板ESD问题的思考与实验
  • ¥15 electron+python