如何将子div扩展为具有overflow-x:scroll的父div的100%

时间:2018-02-01 10:44:17

标签: html css css3 flexbox



.wrapper {
  position: absolute;
  width: 100%;
  border: 1px solid black;
  overflow-x: auto;
  display: flex;
  flex-direction: column;
}

.timeline {
  position: relative;
  left: 0;
  right: 0;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
}

.timeline::after {
  content: '';
  display: table;
  clear: both;
}

.timeline .date {
  position: relative;
  width: 60px;
  height: 50px;
  flex-shrink: 0;
  flex-basis: 60px;
  flex-grow: 1;
}

.timeline .date span {
  display: inline-block;
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 50px;
}

.timeline .date::before {
  content: '';
  position: absolute;
  height: calc(100vh - 125px);
  margin-top: 50px;
  left: 50%;
  background-color: black;
  width: 1px;
}

.timeline .time-tag {
  width: 60px;
  height: 25px;
  background-color: blue;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: 30px;
}

.timeline .time-tag::before {
  content: '';
  position: absolute;
  height: calc(100vh - 87px);
  width: 3px;
  background-color: blue;
  left: calc(50% - 2px);
}

.item {
  position: relative;
  min-width: 100%;
  height: 50px;
  background-color: rgba(0, 200, 10, 0.3);
  z-index: 3;
}

<div class="wrapper">
  <div class="timeline">
    <div class="date">
      <span>8:00</span>
    </div>
    <div class="date">
      <span>9:00</span>
    </div>
    <div class="date">
      <span>10:00</span>
    </div>
    <div class="date">
      <span>11:00</span>
    </div>
    <div class="date">
      <span>12:00</span>
    </div>
    <div class="date">
      <span>13:00</span>
    </div>
    <div class="date">
      <span>14:00</span>
    </div>
    <div class="date">
      <span>15:00</span>
    </div>
    <div class="date">
      <span>16:00</span>
    </div>
    <div class="date">
      <span>17:00</span>
    </div>
    <div class="date">
      <span>18:00</span>
    </div>
    <div class="date">
      <span>19:00</span>
    </div>
    <div class="date">
      <span>20:00</span>
    </div>
    <div class="date">
      <span>21:00</span>
    </div>
    <div class="date">
      <span>22:00</span>
    </div>
    <div class="date">
      <span>23:00</span>
    </div>
    <div class="time-tag"></div>
  </div>
  <div class="item"></div>
</div>
&#13;
&#13;
&#13;

.wrapper设置为overflow: scroll

内部我有一个flex的时间轴,项目flex-basis设置为60px;当屏幕尺寸发生变化时,用户必须能够滚动时间线。

div内的绿色.wrapper将包含定位的任务,以便左边框标记每个任务的开始时间和右边框 - 结束时间。

目前,当我更改视口宽度时,绿色div占用了100%的可见包装。因此,当我向右滚动时,绿色div会切割到视口末端的位置。

问题:如何让孩子绿色div始终伸展到.wrapper div的整个宽度?

编辑:经过一些在线研究后,我得出结论,没有纯粹的CSS解决方案。使用此标记,将子div延伸到父div的滚动宽度的唯一方法是使用javascript。这个问题不重复:其他解决方案要求父div具有预定义的宽度。在我的情况下,它是由浏览器在运行时计算的。

4 个答案:

答案 0 :(得分:0)

如果您正在寻找,请更新以下CSS:

.timeline {
    position: relative;
    left: 0;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    border-bottom: 1px solid black;
    border-top: 1px solid black;
    white-space: pre; // Key line
}

答案 1 :(得分:0)

这是因为您已将width:100%应用于.wrapperposition:absolute,这意味着它将占据文档(相对元素)视口的100%宽度。

width:100%

中删除.wrapper

Stack Snippet

&#13;
&#13;
.wrapper {
  position: absolute;
  border: 1px solid black;
  overflow-x: auto;
  display: flex;
  flex-direction: column;
}

.timeline {
  position: relative;
  left: 0;
  right: 0;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
}

.timeline::after {
  content: '';
  display: table;
  clear: both;
}

.timeline .date {
  position: relative;
  width: 60px;
  height: 50px;
  flex-shrink: 0;
  flex-basis: 60px;
  flex-grow: 1;
}

.timeline .date span {
  display: inline-block;
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 50px;
}

.timeline .date::before {
  content: '';
  position: absolute;
  height: calc(100vh - 125px);
  margin-top: 50px;
  left: 50%;
  background-color: black;
  width: 1px;
}

.timeline .time-tag {
  width: 60px;
  height: 25px;
  background-color: blue;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: 30px;
}

.timeline .time-tag::before {
  content: '';
  position: absolute;
  height: calc(100vh - 87px);
  width: 3px;
  background-color: blue;
  left: calc(50% - 2px);
}

.item {
  position: relative;
  min-width: 100%;
  height: 50px;
  background-color: rgba(0, 200, 10, 0.3);
  z-index: 3;
}
&#13;
<div class="wrapper">
  <div class="timeline">
    <div class="date">
      <span>8:00</span>
    </div>
    <div class="date">
      <span>9:00</span>
    </div>
    <div class="date">
      <span>10:00</span>
    </div>
    <div class="date">
      <span>11:00</span>
    </div>
    <div class="date">
      <span>12:00</span>
    </div>
    <div class="date">
      <span>13:00</span>
    </div>
    <div class="date">
      <span>14:00</span>
    </div>
    <div class="date">
      <span>15:00</span>
    </div>
    <div class="date">
      <span>16:00</span>
    </div>
    <div class="date">
      <span>17:00</span>
    </div>
    <div class="date">
      <span>18:00</span>
    </div>
    <div class="date">
      <span>19:00</span>
    </div>
    <div class="date">
      <span>20:00</span>
    </div>
    <div class="date">
      <span>21:00</span>
    </div>
    <div class="date">
      <span>22:00</span>
    </div>
    <div class="date">
      <span>23:00</span>
    </div>
    <div class="time-tag"></div>
  </div>
  <div class="item"></div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用视口宽度?

.item {
    position: relative;
    width: calc(100vw - 180px);
    height: 50px;
    background-color: rgba(0,200,10,0.3);
    z-index: 3;
}

答案 3 :(得分:0)

我发现唯一可行的css解决方案是将min-width添加到.item和.timeline。

检查codepen:https://codepen.io/magom001/pen/rJOejw

   /*! HTML5 Boilerplate v6.0.1 | MIT License | https://html5boilerplate.com/ */

/*
 * What follows is the result of much research on cross-browser styling.
 * Credit left inline and big thanks to Nicolas Gallagher, Jonathan Neal,
 * Kroc Camen, and the H5BP dev community and team.
 */

/* ==========================================================================
   Base styles: opinionated defaults
   ========================================================================== */

* { margin: 0; padding: 0; box-sizing: border-box; }

html {
    color: #222;
    font-size: 1em;
    line-height: 1.4;
}

/*
 * Remove text-shadow in selection highlight:
 * https://twitter.com/miketaylr/status/12228805301
 *
 * Vendor-prefixed and regular ::selection selectors cannot be combined:
 * https://stackoverflow.com/a/16982510/7133471
 *
 * Customize the background color to match your design.
 */

::-moz-selection {
    background: #b3d4fc;
    text-shadow: none;
}

::selection {
    background: #b3d4fc;
    text-shadow: none;
}

/*
 * A better looking default horizontal rule
 */

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #ccc;
    margin: 1em 0;
    padding: 0;
}

/*
 * Remove the gap between audio, canvas, iframes,
 * images, videos and the bottom of their containers:
 * https://github.com/h5bp/html5-boilerplate/issues/440
 */

audio,
canvas,
iframe,
img,
svg,
video {
    vertical-align: middle;
}

/*
 * Remove default fieldset styles.
 */

fieldset {
    border: 0;
    margin: 0;
    padding: 0;
}

/*
 * Allow only vertical resizing of textareas.
 */

textarea {
    resize: vertical;
}

/* ==========================================================================
   Browser Upgrade Prompt
   ========================================================================== */

.browserupgrade {
    margin: 0.2em 0;
    background: #ccc;
    color: #000;
    padding: 0.2em 0;
}

/* ==========================================================================
   Author's custom styles
   ========================================================================== */
html {
    height: 100%;
    overflow: hidden;
}

body {
    min-height: 100%;
}

nav {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;

}

img {
    height: 50px;
    width: auto;
}

@media (max-width: 767px) {
    button {
        display: none;
    }
}


.wrapper {
    position: absolute;
    top: 50px;
    left: 180px;
    bottom: 0;
    width: calc(100% - 180px);
    overflow-x: auto;
    display: inline-block;
}

.timeline {
    position: relative;
    left: 0;
    right: 0;
    min-width: 960px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    border-bottom: 1px solid black;
    border-top: 1px solid black;
}

.timeline::after {
    content: '';
    display: table;
    clear: both;
}

.timeline .date {
    position: relative;
    width: 60px;
    height: 50px;

    flex-shrink: 0;
    flex-basis: 60px;
    flex-grow: 1;
}

.timeline .date span {
    display: inline-block;
    width: 100%;
    height: 100%;
    text-align: center;
    line-height: 50px;
}

.timeline .date::before {
    content: '';
    position: absolute;
    height: calc(100vh - 125px);
    margin-top: 50px;
    left: 50%;
    background-color: black;
    width: 1px;
}

.timeline .time-tag {
    width: 60px;
    height: 25px;
    background-color: blue;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 30px;
}

.timeline .time-tag::before {
    content: '';
    position: absolute;
    height: calc(100vh - 87px);
    width: 3px;
    background-color: blue;
    left: calc(50% - 2px);
}

.item {
    position: relative;
    min-width: 960px;
    height: 50px;
    background-color: rgba(0,200,10,0.3);
    z-index: 3;
}


/* ==========================================================================
   Helper classes
   ========================================================================== */

/*
 * Hide visually and from screen readers
 */

.hidden {
    display: none !important;
}

/*
 * Hide only visually, but have it available for screen readers:
 * https://snook.ca/archives/html_and_css/hiding-content-for-accessibility
 *
 * 1. For long content, line feeds are not interpreted as spaces and small width
 *    causes content to wrap 1 word per line:
 *    https://medium.com/@jessebeach/beware-smushed-off-screen-accessible-text-5952a4c2cbfe
 */

.visuallyhidden {
    border: 0;
    clip: rect(0 0 0 0);
    -webkit-clip-path: inset(50%);
    clip-path: inset(50%);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    white-space: nowrap; /* 1 */
}

/*
 * Extends the .visuallyhidden class to allow the element
 * to be focusable when navigated to via the keyboard:
 * https://www.drupal.org/node/897638
 */

.visuallyhidden.focusable:active,
.visuallyhidden.focusable:focus {
    clip: auto;
    -webkit-clip-path: none;
    clip-path: none;
    height: auto;
    margin: 0;
    overflow: visible;
    position: static;
    width: auto;
    white-space: inherit;
}

/*
 * Hide visually and from screen readers, but maintain layout
 */

.invisible {
    visibility: hidden;
}

/*
 * Clearfix: contain floats
 *
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    `contenteditable` attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that receive the `clearfix` class.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

/* ==========================================================================
   EXAMPLE Media Queries for Responsive Design.
   These examples override the primary ('mobile first') styles.
   Modify as content requires.
   ========================================================================== */

@media only screen and (min-width: 35em) {
    /* Style adjustments for viewports that meet the condition */
}

@media print,
       (-webkit-min-device-pixel-ratio: 1.25),
       (min-resolution: 1.25dppx),
       (min-resolution: 120dpi) {
    /* Style adjustments for high resolution devices */
}

/* ==========================================================================
   Print styles.
   Inlined to avoid the additional HTTP request:
   http://www.phpied.com/delay-loading-your-print-css/
   ========================================================================== */

@media print {
    *,
    *:before,
    *:after {
        background: transparent !important;
        color: #000 !important; /* Black prints faster:
                                   http://www.sanbeiji.com/archives/953 */
        box-shadow: none !important;
        text-shadow: none !important;
    }

    a,
    a:visited {
        text-decoration: underline;
    }

    a[href]:after {
        content: " (" attr(href) ")";
    }

    abbr[title]:after {
        content: " (" attr(title) ")";
    }

    /*
     * Don't show links that are fragment identifiers,
     * or use the `javascript:` pseudo protocol
     */

    a[href^="#"]:after,
    a[href^="javascript:"]:after {
        content: "";
    }

    pre {
        white-space: pre-wrap !important;
    }
    pre,
    blockquote {
        border: 1px solid #999;
        page-break-inside: avoid;
    }

    /*
     * Printing Tables:
     * http://css-discuss.incutio.com/wiki/Printing_Tables
     */

    thead {
        display: table-header-group;
    }

    tr,
    img {
        page-break-inside: avoid;
    }

    p,
    h2,
    h3 {
        orphans: 3;
        widows: 3;
    }

    h2,
    h3 {
        page-break-after: avoid;
    }
}