在水平滚动元素的子元素的全宽上显示背景颜色

时间:2017-02-08 08:18:49

标签: html css

在水平滚动div中,当我移动水平滚动条时,background-color的{​​{1}}未完全显示(对于整个宽度)。

(如果我将.filesColumn:focus添加到overflow-x:auto,则会显示全宽。)

当我移动包含.fileColumn的水平滚动条时,如何完全显示子元素上的background-color

请参阅代码:jsfiddle



div.contentModel

html,
body {
  height: 100%;
}

body {
  padding: 10px;
  box-sizing: border-box;
}

.container {
  width: 100%;
  height: 100%;
  padding: 20px 0;
  box-sizing: border-box;
  border-radius: 7px;
  background-color: rgba(16, 40, 136, 1);
}

.contentModal {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;
}

.filesColumn {
  display: block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  box-sizing: border-box;
  font-size: 14px;
  text-align: left;
  text-decoration: none;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 1);
  background-color: rgba(16, 40, 136, 1);
}

.filesColumn:hover {
  background-color: rgba(48, 88, 184, 1);
}

.filesColumn:active,
.filesColumn:focus {
  background-color: rgba(79, 134, 235, 1);
}




3 个答案:

答案 0 :(得分:1)

设置.filesColumn display: inline-block并移除width: 100%



html,
body {
  height: 100%;
}

body {
  padding: 10px;
  box-sizing: border-box;
}

.container {
  width: 100%;
  height: 100%;
  padding: 20px 0;
  box-sizing: border-box;
  border-radius: 7px;
  background-color: rgba(16, 40, 136, 1);
}

.contentModal {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;
}

.filesColumn {
  display: inline-block;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  box-sizing: border-box;
  font-size: 14px;
  text-align: left;
  text-decoration: none;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 1);
  background-color: rgba(16, 40, 136, 1);
}

.filesColumn:hover {
  background-color: rgba(48, 88, 184, 1);
}

.filesColumn:active,
.filesColumn:focus {
  background-color: rgba(79, 134, 235, 1);
}

<div class="container">
    <div class="contentModal">
        <a href="#" class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
        <a href="#" class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
        <a href="#" class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

确实如此,这似乎是JSfiddle的一个错误。在codepen上查看一个工作示例。 http://codepen.io/anon/pen/EZebBB

    html,
body {
  height: 100%;
}

body {
  padding: 10px;
  box-sizing: border-box;
}

.container {
  width: 100%;
  height: 100%;
  padding: 20px 0;
  box-sizing: border-box;
  border-radius: 7px;
  background-color: rgba(16, 40, 136, 1);
}

.contentModal {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;
}

.filesColumn {
  display: block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  padding: 0 20px;
  box-sizing: border-box;
  font-size: 14px;
  text-align: left;
  text-decoration: none;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 1);
  background-color: rgba(16, 40, 136, 1);
}

.filesColumn:hover {
  background-color: rgba(48, 88, 184, 1);
}

.filesColumn:active,
.filesColumn:focus {
  background-color: rgba(79, 134, 235, 1);
}

答案 2 :(得分:0)

我希望这是你的解决方案:-)

html,
body {
  height: 100%;
}

body {
  padding: 10px;
  box-sizing: border-box;
}

.container {
  width: 100%;
  height: 100%;
  padding: 20px 0;
  box-sizing: border-box;
  border-radius: 7px;
  background-color: rgba(16, 40, 136, 1);
}

.contentModal {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;
}

.filesColumn {
  display: inline;
  width: 100%;
  height: 30px;
  line-height: 30px;
  padding: 10px 20px;
  box-sizing: border-box;
  font-size: 14px;
  text-align: left;
  text-decoration: none;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 1);
  background-color: rgba(16, 40, 136, 1);
}

.filesColumn:hover {
  background-color: rgba(48, 88, 184, 1);
}

.filesColumn:active,
.filesColumn:focus {
  background-color: rgba(79, 134, 235, 1);
}
<div class="container">
    <div class="contentModal">
        <a class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
        <a class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
        <a class="filesColumn">ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ</a>
    </div>
</div>