flex:1,min-width: 0 保证内容不超出父盒子

本文介绍了一个在Flex布局中防止内容溢出的技巧,通过设置flex:1和min-width:0,可以确保子元素的内容不会超出其父容器的边界。

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

在一个flex布局中,设置flex:1,min-width: 0 ,保证内容不超出外层容器

##如果没有设置min-width,当内容大于剩余盒子宽度时会超出父盒子,设置min-width保证内容局限在父盒子内。

<template> <view class="container"> <view class="banner-section"> <image class="banner-image" src="/static/IntegrityCulture.png" /> </view> <view class="title">廉洁文化资讯</view> <view class="news-list"> <scroll-view scroll-y="true" :enhanced="true" :show-scrollbar="false" > <view v-for="item in newsList" :key="item.id" class="news-item" @click="navigateToDetail(item)" > <view class="news-content"> <text class="news-title">{{ item.title }}</text> <text class="news-summary">{{ item.summary }}</text> <text class="news-date">{{ item.publishDate }}</text> </view> <view class="status-box" :class="item.read ? 'read' : 'unread'" > {{ item.read ? '已阅' : '未阅' }} </view> </view> </scroll-view> </view> </view> </template> <script setup> import { ref, onMounted } from 'vue'; const newsList = ref([]); const fetchData = () => { setTimeout(() => { newsList.value = [ { id: 1, title: '廉洁从业,从我做起', summary: '廉洁从业是每一位员工应尽的责任和义务,本文介绍了公司廉洁文化建设的相关要求和实践。', publishDate: '2024-09-10', read: true }, { id: 2, title: '加强反腐倡廉,构建阳光企业', summary: '公司持续加强反腐倡廉工作,推动形成风清气正的企业文化氛围。', publishDate: '2024-09-08', read: false }, { id: 3, title: '廉洁文化进班组,清风正气润人心', summary: '公司组织廉洁文化进班组活动,通过宣传、培训等方式提升员工廉洁意识。', publishDate: '2024-09-05', read: false }, ]; }, 500); }; onMounted(fetchData); const navigateToDetail = (item) => { uni.navigateTo({ url: `/pages/IntegrityCulture/IntegrityCultureDetail/IntegrityCultureDetail?id=${item.id}` }); }; </script> <style scoped> /* 整体容器 */ .container { padding: 30rpx; background-color: #f9f9f9; height: 100%; overflow: hidden; box-sizing: border-box; /* display: flex; */ flex-direction: column; } /* 顶部区域 */ .banner-section { margin-bottom: 40rpx; border-radius: 16rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1); flex-shrink: 0; } .banner-image { width: 100%; height: 320rpx; display: block; } .title { font-size: 36rpx; font-weight: bold; text-align: center; display: block; margin-bottom: 40rpx; flex-shrink: 0; } /* 滚动区域 */ .news-list { background-color: #fff; border-radius: 16rpx; overflow: hidden; flex: 1; min-height: 0; display: flex; flex-direction: column; box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05); } /* 滚动容器 */ .scroll-container { flex: 1; min-height: 0; overflow: hidden; /* box-sizing: border-box; */ } /* 隐藏滚动条 */ .scroll-view ::-webkit-scrollbar { display: none; width: 0; height: 0; color: transparent; } /* 新闻项样式 */ .news-item { padding: 30rpx; display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 1rpx solid #eee; transition: background-color 0.2s; } .news-item:active { background-color: #f5f7fa; } .news-content { flex: 1; padding-right: 20rpx; } .news-title { font-size: 32rpx; font-weight: bold; color: #333; display: block; margin-bottom: 10rpx; } .news-summary { font-size: 28rpx; color: #666; display: block; margin-bottom: 10rpx; line-height: 1.5; } .news-date { font-size: 24rpx; color: #999; display: block; } .status-box { padding: 8rpx 16rpx; font-size: 24rpx; border-radius: 8rpx; min-width: 80rpx; text-align: center; flex-shrink: 0; } .status-box.read { background-color: rgba(76, 175, 80, 0.1); color: #4caf50; } .status-box.unread { background-color: rgba(244, 67, 54, 0.1); color: #f44336; } /* 响应式调整 */ @media (max-height: 600px) { .banner-image { height: 280rpx; } .news-list { border-radius: 12rpx; } } </style> 帮我修改这段代码,我想要限定列表板块的高度超出部分隐藏,然后该板块滚动影响其他板块
最新发布
08-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值