存在滚动条时,项目宽度不是100%

时间:2019-01-08 21:16:47

标签: html css

当我的内容比容器宽时,我会将滚动条设置为auto,但是悬浮颜色效果不适用于整个子节点。悬停颜色仅显示父容器初始宽度的宽度,而没有滚动条。

HTML

<div class="container">
  <div class="tab">
    <label>Tab 1</label>
    <div class="content">
      <div>asdasd 1111</div>
      <div>asdasd 2222</div>
      <div>asdasd 3333</div>
      <div>asdasd 4444</div>
      <div>asdasd 5555</div>
      <div>asdasd 6666666666666666666666166666666666</div>
    </div>
  </div>
  <div class="tab">
    <label>Tab 2</label>
    <div class="content">
      <div>asdasd 7777</div>
      <div>asdasd 8888</div>
      <div>asdasd 9999</div>
    </div>
  </div>
</div>

CSS

.container {
  width: 240px;
  height: 98vh;
  background: tan;
  height: calc(100vh - 40px);
}

.tab label {
  position: relative;
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.5);
  background: #434343;
  cursor: pointer;
}

.content {
  overflow: auto;
}

.content div {
  white-space: nowrap;
  margin-left: 10px;
}

.content div:hover {
  cursor: pointer;
  background: grey;
}

JSFiddle

在上面的提琴中,我更清楚地谈论了什么。选项卡1的一些子内容超出了容器的范围,但悬停颜色仅适用于初始容器宽度。如果您在标签1中一直向右滚动,则悬停颜色不是整个宽度的100%。

1 个答案:

答案 0 :(得分:2)

制作元素inline-block并添加min-width:100%,以确保元素的行为像块一样,并在内容较大时扩展:

.container {
  width: 240px;
  height: 98vh;
  background: tan;
  height: calc(100vh - 40px);
}

.tab label {
  position: relative;
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.5);
  background: #434343;
  cursor: pointer;
}

.content {
  overflow: auto;
}

.content div {
  white-space: nowrap;
  margin-left: 10px;
  display:inline-block;
  min-width:calc(100% - 10px); /*remove margin*/
}

.content div:hover {
  cursor: pointer;
  background: grey;
}
<div class="container">
  <div class="tab">
    <label>Tab 1</label>
    <div class="content">
      <div>asdasd 1111</div>
      <div>asdasd 2222</div>
      <div>asdasd 3333</div>
      <div>asdasd 4444</div>
      <div>asdasd 5555</div>
      <div>asdasd 6666666666666666666666166666666666</div>
    </div>
  </div>
  <div class="tab">
    <label>Tab 2</label>
    <div class="content">
      <div>asdasd 7777</div>
      <div>asdasd 8888</div>
      <div>asdasd 9999</div>
    </div>
  </div>
</div>

您还可以考虑对全部内容进行内联阻止:

.container {
  width: 240px;
  height: 98vh;
  background: tan;
  height: calc(100vh - 40px);
}

.tab label {
  position: relative;
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: bold;
  color: rgba(255, 255, 255, 0.5);
  background: #434343;
  cursor: pointer;
}

.wrapper {
  overflow: auto;
}
.content {
  display: inline-block;
  min-width: 100%;

}
.content div {
  white-space: nowrap;
  margin-left: 10px;
}

.content div:hover {
  cursor: pointer;
  background: grey;
}
<div class="container">
  <div class="tab">
    <label>Tab 1</label>
    <div class="wrapper">
      <div class="content">
        <div>asdasd 1111</div>
        <div>asdasd 2222</div>
        <div>asdasd 3333</div>
        <div>asdasd 4444</div>
        <div>asdasd 5555</div>
        <div>asdasd 6666666666666666666666166666666666</div>
      </div>
    </div>
  </div>
  <div class="tab">
    <label>Tab 2</label>
    <div class="wrapper">
      <div class="content">
        <div>asdasd 7777</div>
        <div>asdasd 8888</div>
        <div>asdasd 9999</div>
      </div>
    </div>
  </div>
</div>