css 超出后才会显示滚动条 没超出时不要显示滚动条

css yekong 174℃

项目开发中,有时候内容过多的话,需要显示滚动条,所以我们设置了overflow-x: scroll;

.progressList {
  width: 400px;
  position: relative;
  height: 74px;
  overflow-x: scroll;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
} 

但是我们又希望当内容不多的时候,隐藏滚动条。

在CSS中,你可以使用overflow-x: auto;来实现当内容超出时显示滚动条,内容没有超出时不显示滚动条。以下是修改后的代码:

.progressList {
  width: 400px;
  position: relative;
  height: 74px;
  overflow-x: auto; /* 修改此处 */
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
}

在这段代码中,overflow-x: auto;表示只有当内容的宽度超出容器的宽度时,才会显示水平滚动条。

喜欢 (0)