复刻YouTube时视频标题超出容器宽度的修复请求
修复YouTube复刻版视频标题溢出容器的问题
在复刻YouTube视频列表时,添加视频标题后出现标题宽度超出容器、覆盖到其他视频上方的异常,以下是具体修复方案:
问题根源
当前视频信息区域(.vidinfo)未设置宽度约束,且长文本没有换行规则,flex布局也未合理分配频道头像与文本区域的空间,导致标题溢出。
修复方案
添加以下CSS规则,或直接替换为完整修改后的CSS代码:
/* 新增的视频信息样式 */ .vidlist .vidinfo { flex: 1; min-width: 0; /* 确保flex子项可收缩至容器内 */ } .vidinfo a { display: block; color: #000; text-decoration: none; font-weight: 600; margin-bottom: 5px; overflow-wrap: break-word; /* 强制长文本换行 */ white-space: normal; /* 允许文本自动换行 */ /* 可选:实现最多2行的标题省略效果 */ display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; } .vidinfo p { color: #606060; font-size: 14px; margin: 2px 0; }
完整修改后的CSS代码
body{ padding: 0; margin: 0; box-sizing: border-box; } .flexbox{ display: flex; align-items: center; } nav{ padding: 10px 2%; justify-content: space-between; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); background: #fff; position: static; top: 0; z-index: 10; } .navright img{ width: 25px; margin-right: 25px; } .navright .profile{ width: 30px; border-radius: 30px; } .navleft .menuicon{ width: 25px; } .navleft .logo{ width: 130px; padding-left: 20px; } .navmiddle .voice{ width: 16px; } .navmiddle .search{ width: 16px; } .navmiddle .searcharea{ border: 1px solid #ccc; margin-right: 15px; padding: 8px 12px; border-radius: 25px; } .navmiddle .searchbox{ width: 400px; border: none; outline: none; } .sidebar{ background: #fff; width: 15%; height: 100vh; position: fixed; padding-left: 2%; padding-top: 20px; z-index: -10; } .shortcuts a img{ width: 20px; margin-right: 20px; } .shortcuts a{ display: flex; align-items: center; flex-wrap: wrap; text-decoration: none; width: fit-content; color: #000; margin-bottom: 20px; } .shortcuts a:first-child{ color: red; } .sidebar hr{ border: 0; width: 85%; height: 1px; background: #ccc; margin: 15px 0; } .small-sidebar{ width: 5%; height: 200px; } .container{ padding-left: 17%; padding-right: 2%; padding-top: 20px; padding-bottom: 20px; } .container .banner{ width: 100%; border-radius: 8px; } .small-sidebar p{ display: none; } .flexbox .channelpic{ border-radius: 40px; width: 50px; margin-right: 12px; } .listcontainer{ display: grid; grid-template-columns: repeat(auto-fit, minmax(250px,1fr)); grid-column-gap: 16px; grid-row-gap: 30px; margin-top: 15px; } .vidlist .thumbnail{ width: 100%; border-radius: 5px; } .vidlist .flexbox{ align-items: flex-start; margin-top: 7px; } /* 新增的视频信息样式 */ .vidlist .vidinfo { flex: 1; min-width: 0; } .vidinfo a { display: block; color: #000; text-decoration: none; font-weight: 600; margin-bottom: 5px; overflow-wrap: break-word; white-space: normal; /* 可选多行省略 */ display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; } .vidinfo p { color: #606060; font-size: 14px; margin: 2px 0; }
关键修复细节
flex: 1:让视频信息区域占据flex容器的剩余空间,确保不会超出视频卡片宽度。min-width: 0:解决flex子项默认无法收缩到内容宽度以下的问题,适配容器边界。overflow-wrap: break-word:强制连续字符(如示例中的全x)在容器边缘换行,避免溢出。- 可选多行省略:通过WebKit属性实现最多2行标题展示,超出部分用省略号代替,还原YouTube的视觉效果。
内容的提问来源于stack exchange,提问作者Najad
相关产品推荐
相关产品推荐

