display:flex布局下white-space:nowrap失效问题解决

当使用Flex布局且元素宽度自适应时,white-space:nowrap可能导致内容区撑开,影响父元素的自适应宽度。为修复此问题,只需在父元素样式中添加min-width:0。这是一个关于前端开发中的CSS Flex布局和文本溢出处理的技巧。
部署运行你感兴趣的模型镜像

文字长度过长显示省略号的样式设置方法众所周知:

h3 {
	white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

然而有一次,我在项目中用到white-space: nowrap的时候,发现它失效了,我的项目是flex布局,宽度自适应的部分,white-space: nowrap撑开了内容区的长度,省略号是显示了,但是父元素的自适应宽度完全不对了。

页面部分布局如下:

<template>
	<div class="parent">
		<el-col :span="6" v-for="item in list" :key="item.id">
			<h3>{{ item.title }}</h3>
			......
		</el-col>
	</div>
</template>

样式如下:

	.parent {
		flex: auto;
	    min-height: 100vh;
	    display: flex;
	    flex-direction: column;
	}
	.parent h3 {
		white-space: nowrap;
	    overflow: hidden;
	    text-overflow: ellipsis;
	}
}

查了一下才知道,原来是因为display:flex影响的,white-space:nowrap会影响flex布局下未设定宽度的元素,所以给设置了display:flex的父元素设置一下min-width: 0;即可解决问题。

.parent {
	flex: auto;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
	min-width: 0;
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

数据集列表的渲染如下: <div className={css.datasetFilesPanel}> {/* 静态写入 10 条数据 */} {datasetFiles.map((file, index) => ( <div className={css.datasetFileItem} key={index}> <div className={css.leftGroup}> <div className={css.fileIconContainer}> {file.type === 1 ? <img src={fileFolderIcon} /> : <img src={fileIcon} />} </div> <div className={css.fileInfo}> {/* 文件名固定长度超出省略显示 */} <div className={css.fileName} {...{ onClick: () => { if (file.type === 1) { currentRef.current = 1; getDatasetFiles(datasetId, file.fileName, 1); } else { if (canPreview && checkDatasetPreview(parseFileName(file.type, file.fileName))) { setPreViewFile(file); setPreVisible(true); } } }, }} > {parseFileName(file.type, file.fileName)} </div> <div className={css.fileSummary}> <div> {file.lastModified ? moment(Number(file.lastModified)).format('YYYY-MM-DD') + t('updated') : ''} </div> <div>{formatFileSize(Number(file.contentLength)).split('--')}</div> {/* 能否预览还要看文件是否可读 */} {file.type === 2 && canPreview && checkDatasetPreview(parseFileName(file.type, file.fileName)) ? ( <div onClick={(e) => { e.preventDefault(); setPreViewFile(file); setPreVisible(true); }} > {t('previewable')} </div> ) : ( '' )} </div> </div> </div> <div className={css.rightAction}> <div className={css.operationButton}> {file.type === 2 && canDownload ? ( <img src={downloadIcon} alt="download" onClick={(e) => { e.preventDefault(); handleDownload(); }} /> ) : null} </div> </div> </div> ))} </div> 样式:.tableContainer { padding: 5px 10px; } .datasetFilesPanel { margin: 0 12px; padding: 0 10px; } .totalDownload { display: flex; justify-content: space-between; align-items: center; padding: 0 10px; margin-top: 15px; } .total { color: rgba(51, 51, 51, 1); font-family: 'HarmonyHeiTi'; font-weight: 400; font-size: 14px; line-height: 20px; } .downloadButton { min-width: 100px; 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: center; /* 左侧顶部对齐 */ align-items: flex-start; /* 左侧顶部对齐 */ padding: 8px 0; box-sizing: border-box; } /* 左侧组合:图标 + 文字 */ .leftGroup { display: flex; align-items: center; flex-shrink: 0; /* 固定宽度,不压缩 */ width: 80%; } .fileIconContainer img { flex-shrink: 0; } /* 中间文件信息 */ .fileInfo { margin-left: 8px; min-width: 0; display: flex; flex-direction: column; } .fileName { color: rgba(29, 29, 29, 1); font-family: 'HarmonyHeiTi'; 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: 'HarmonyHeiTi'; font-weight: 400; font-size: 12px; line-height: 16px; margin-top: 4px; display: flex; align-items: center; gap: 10px; } .fileSummary > div:first-child { min-width: 120px; /* 如果需要确保长度生效,设置盒模型属性 */ box-sizing: border-box; } .fileSummary > div:nth-child(2) { min-width: 50px; /* 如果需要确保长度生效,设置盒模型属性 */ box-sizing: border-box; } .fileSummary > div:nth-child(3) { min-width: 45px; /* 如果需要确保长度生效,设置盒模型属性 */ box-sizing: border-box; color: rgba(75, 154, 246, 1); font-weight: 500; text-align: right; } /* 右侧操作区 —— 关键修改:使用 flex: 1 并设置最小间距 */ .rightAction { display: flex; justify-content: center; /* 水平居中按钮 */ align-items: center; /* 垂直居中 */ flex: 1; /* 占据剩余空间 */ min-width: 60px; /* 防止太窄 */ margin-left: 16px; /* 与左侧保持一定距离 */ } /* 操作按钮本身尺寸固定 */ .operationButton { display: flex; align-items: center; justify-content: center; width: 32px; height: 32px; flex-shrink: 0; border-radius: 8px; align-self: center; /* 独立垂直居中(兼容父级 align-start) */ } .dialog { z-index: 1222; } .dialog :global .adm-center-popup-wrap { transform: translate(-50%, -50%) !important; top: 50% !important; bottom: 0 !important; display: flex; justify-content: center; align-items: center; } .dialog :global .adm-dialog-body { border-radius: 32px; } border-radius .dialog :global .adm-center-popup-wrap .adm-dialog-title { font-size: 18px; line-height: 24px; color: #1d1d1d; } .dialog :global .adm-dialog-body:not(.adm-dialog-with-image) { padding-top: 24px; } .dialog :global .adm-center-popup-wrap .adm-dialog-content { font-size: 16px; line-height: 24px; color: #000000; padding: 0 12px 17px; } .dialog :global .adm-center-popup-wrap .adm-dialog-footer { padding-bottom: 24px; } 运行后页面的文件名如果过长没有按照规定的长度省略显示,而是超出,请改关键代码修复这个问题
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值