box-sizing解决input溢出父元素问题

本文探讨了在CSS布局中遇到的一个常见问题:当子元素(如input框)的尺寸设置不当导致溢出其父元素时,如何使用box-sizing属性进行有效调整。通过对比content-box和border-box两种模式,阐述了border-box模式下,即使增加内边距和边框,元素的实际大小也不会超出预设值,从而避免了溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做项目时,遇到了一下问题:
当我子input框设置了
.right input { height: 30px; width: 100%; margin-top: 8px; margin-left: 3px; padding: 3px; }
在这里插入图片描述
可以看到input框已经放不下了,溢出了父元素的框。可能有人说设置overflow:hidden就不会啦。
但是,超过父边框的就直接隐藏起来了,我们的input框就好像硬生生的被切断了,不美观。

所以,以下就要用box-sizing这个属性来解决:

box-sizing常用有两个属性:content-box和border-box

content-box:是默认值,width和height直接决定了内容content的高度(上面我们宽度设为100%,还要加上padding和border的宽度,不撑开才怪)

border-box:width和height就已经包含了padding+border+content的宽度了,你边框、内边距设置得越多,内容也会随之缩减,最终宽度都是你设定好的那个宽度。

所以我在input的样式里面加上了 box-sizing:border-box

	.right input {
				box-sizing: border-box;
				height: 30px;
				width: 100%;
				margin-top: 8px;
				margin-right: 3px;
				padding: 3px;
			}
			

duang!!最终效果ok!
在这里插入图片描述

<template> <view class=“box”> <view class=“box-header”> <view class=“box-header-left”> <!-- 二期的对齐 --> </view> <view class=“box-header-right”> <text class=“box-header_label”>分值:</text> <text class=“box-header_value”>{{ item.indicatorScore }}</text> </view> </view> <view class=“box-title”> <view class=“box-title_left”> <text class=“sort”>O{{ item.numSort }}</text> <view class=“title”> <u–textarea v-model=“item.performanceIndicators” placeholder=“请输入关键目标” autoHeight height=“35” maxlength=“500” ></u–textarea> <view class=“type”> <text class=“typeName”>{{ item.typeName }}</text> <u-icon name=“arrow-down”></u-icon> </view> </view> </view> <view class=“box-title_right” :class=“{ arrowDrown: !item.show }” @click=“changeShow(item)”> <image src=“@/static/image/OKR/arrow-up.png” mode=“widthFix” /> </view> </view> <transition name=“slide-fade”> <view class=“box-list” v-show=“item.show”> <view class=“krBox” v-for=“(element, index) in item.krList” :key=“index” > <view class=“krBox-header”> <view class=“krBox-header-left”> <text class=“sort” >KR{{ item.numSort }}.{{ element.numSort }}</text > <u–textarea v-model=“element.quantitativeObjectives” placeholder=“请输入关键结果,格式:(通过xxx策略/措施),实现XX量化结果)” autoHeight height=“35” maxlength=“500” border=“surround” ></u–textarea> </view> <view class=“krBox-header_close”> <image src=“@/static/image/OKR/close.png” mode=“widthFix” /> </view> </view> <view class=“krBox-item”> <view class=“krBox-item_label”> <text>填报部门</text> </view> <view class=“krBox-item_value”> <u–input placeholder=“请选择填报部门” border=“surround” v-model=“element.fillDeptName” suffixIcon=“arrow-down” suffixIconStyle=“color: #C0C4CC;font-size: 18px;” ></u–input> </view> </view> </view> </view> </transition> </view> </template> <script> export default { props: { item: { type: Object, default: () => { return { id: “”, typeId: “”, typeName: “”, performanceIndicators: “”, numSort: 1, dataType: 0, indicatorScore: 0, krList: [], show: true, }; }, }, }, data() { return {}; }, methods: { changeShow(item) { item.show = !item.show; }, }, }; </script> <style lang=“scss” scoped> .box { background: #ffffff; border-radius: 16rpx; overflow: hidden; // padding: 20rpx; box-sizing: border-box; /* font-size: 30rpx; color: #303133; */ &-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8rpx; padding: 20rpx 20rpx 0; box-sizing: border-box; &_label { font-size: 26rpx; color: #909399; } &_value { font-size: 26rpx; color: #303133; } } &-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30rpx; padding-left: 20rpx; box-sizing: border-box; &_left { flex: 1; display: flex; align-items: center; justify-content: flex-start; .sort { background: #409eff; border-radius: 8rpx; padding: 0 12rpx; font-weight: 500; font-size: 30rpx; color: #ffffff; margin-right: 12rpx; } .title { position: relative; flex: 1; border: 1rpx solid #efefef; border-radius: 8rpx; .u-textarea { text-indent: 6em; } .type { position: absolute; top: 0; left: 10rpx; display: flex; align-items: center; height: 70rpx; .typeName { margin-right: 10rpx; } .u-icon { margin-top: 8rpx; } } } } &_right { width: 60rpx; display: flex; justify-content: center; align-items: center; transition: all 0.3s ease; image { width: 44rpx; } } .arrowDrown{ transform: rotate(180deg); } } &-list { } } } .krBox { padding: 24rpx 60rpx 32rpx 20rpx; box-sizing: border-box; border-top: 1rpx solid #dcdfe6; } </style> 完善样式,实现slide-fade,类似于折叠面板形式的展开收起效果
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值