You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用HTML CSS实现分类水平列表超出父容器并支持横向滚动

解决方案

要实现分类列表超出外层容器并支持横向滚动,你需要调整以下几个CSS属性,同时修正语义化标签问题:

关键修改点

  • 调整外层容器溢出规则,允许内容横向超出边界
  • 修正列表布局逻辑,让列表项紧密排列且不被父容器宽度限制
  • 语义化标签修正:将列表项的<ul>改为<li>,外层保留<ul>作为列表容器

修改后的完整代码

CSS部分

html,
body,
* {
  -webkit-app-region: drag;
  -webkit-user-select: none;
  cursor: default;
  padding: 0;
  margin: 0;
}

.header {
  padding: 0;
  text-align: center;
  font-size: 40px;
  margin-top: 0px;
  margin-bottom: 4px;
}

.searcharea {
  margin: 10px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-content: flex-start;
}

.location {
  width: 40px;
  height: 40px;
  border-radius: 5px;
  box-shadow: 1px 1px 4px rgb(170, 170, 170);
  text-align: center;
  line-height: 58px;
}

.searchbar {
  width: 65%;
  height: 40px;
  border-radius: 5px;
  box-shadow: 1px 1px 4px rgb(170, 170, 170);
  border: none;
  padding: 0px 57px 0px 13px;
  background-image: url('assets/search-icon.png');
  background-position: 95% 5px;
  background-repeat: no-repeat;
}

.searchbar:focus {
  outline: none;
}

#location_icon {
  font-size: 29px;
}

.categories {
  border: 3px solid black;
  width: 90%;
  margin: auto;
  padding: 0 0 10px 0; /* 移除左侧padding,给标题单独加margin */
  overflow-x: visible; /* 允许内容横向超出容器 */
}

.categoriesheader {
  margin: 0 0 10px 10px; /* 给标题添加左侧margin,对齐原位置 */
}

.items {
  display: flex;
  flex-direction: row;
  gap: 10px; /* 控制列表项之间的间距,可按需调整 */
  overflow-x: auto; /* 开启横向滚动 */
  padding: 0 10px; /* 左右padding保证首尾项和标题的间距 */
  margin: 0 -20px; /* 负margin让列表超出外层容器左右边界 */
  height: 80px; /* 固定高度,匹配列表项尺寸 */
}

.itemlist {
  flex-shrink: 0; /* 禁止列表项被压缩 */
  background-repeat: no-repeat;
  background-size: cover;
  height: 80px;
  width: 80px;
  border: 2px solid black;
  list-style: none; /* 移除默认列表样式 */
}

#item1 {
  background-image: url('https://th.bing.com/th/id/OIP.MfhIfzrC6x6T1-szQkjtCgHaEo?rs=1&pid=ImgDetMain');
}

#item2 {
  background-image: url('');
}

#item3 {
  background-image: url('');
}

#item4 {
  background-image: url('');
}

#item5 {
  background-image: url('');
}

#item6 {
  background-image: url('');
}

.material-symbols-outlined {
  font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 24
}

HTML部分

<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" />
<h1 class="header">FOODIFY</h1>
<div class="searcharea">
  <div class="location">
    <span class="material-symbols-outlined" id="location_icon">location_on</span>
  </div>
  <input class="searchbar" placeholder="Search for meals or area">
</div>
<div class="categories">
  <h2 class="categoriesheader">Top Categories</h2>
  <ul class="items"> <!-- 外层用ul作为列表容器 -->
    <li class="itemlist" id="item1"></li> <!-- 列表项改用li标签 -->
    <li class="itemlist" id="item2"></li>
    <li class="itemlist" id="item3"></li>
    <li class="itemlist" id="item4"></li>
    <li class="itemlist" id="item5"></li>
    <li class="itemlist" id="item6"></li>
  </ul>
</div>

核心逻辑说明

  1. 负margin实现超出容器:给.items设置margin: 0 -20px,让列表左右两端超出.categories的边界,匹配示例图效果
  2. flex布局控制列表排列:使用flex-shrink: 0确保列表项保持80×80的正方形尺寸不被压缩;gap属性灵活控制项之间的间距
  3. 溢出与滚动设置:.items开启overflow-x: auto实现横向滚动,.categories设置overflow-x: visible允许内容超出自身边界
  4. 语义化修正:将原有的<ul>列表项改为<li>,外层用<ul>包裹,符合HTML语义规范

内容的提问来源于stack exchange,提问作者JollyJoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:27:35