3

.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>

.wrapper is set with overflow: scroll

Inside I have a timeline with flex and items flex-basis set to 60px; When the size of the screen changes the user must be able to scroll the timeline.

A green div inside .wrapper will contain tasks positioned so that left border will mark the start time and the right border of each task - the end time.

Currently, when I change the viewport width the green div occupied 100% of the visible wrapper. So when I scroll to the right the green div cuts at the place where the end of the viewport was.

Question: how do I make the child green div to always stretch to the whole width of the .wrapper div?

Edit: After some online research I conclude that there is no pure css solution. With this markup the only way to stretch child divs to parent div's scroll width is by using javascript. This question is not a duplicate: other solutions require the parent div to have a predefined width. In my case it is calculated by the browser at runtime.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
magom001
  • 608
  • 6
  • 16
  • 1
    Can you please update the code sample with the complete code you're using so we can replicate the issue and help resolve? – Brett DeWoody Feb 01 '18 at 10:48
  • Sorry. I posted a link to codepen, but stackoverfow removed it https://codepen.io/magom001/pen/rJOejw – magom001 Feb 01 '18 at 10:50
  • 1
    I removed it, because the code sample posted on Codepen can be posted within the question itself. – Brett DeWoody Feb 01 '18 at 10:53
  • Might be helpful? https://stackoverflow.com/questions/33746041/child-element-100-width-of-its-parent-with-overflow-scroll – sol Feb 01 '18 at 10:58
  • @sol, for some reason that solution does not work in my case. The green div still stretches till the border of the viewport then cuts off. – magom001 Feb 01 '18 at 11:18
  • You say: _"With this markup the only way..."_, so I wonder, why is a minor markup change to solve your issue not a viable option? – Asons Feb 01 '18 at 18:51

4 Answers4

1

You could use viewport width?

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

This is because you have applied the width:100% to the .wrapper class with position:absolute which means it will take the 100% width from the document(relative element) viewport.

Remove width:100% from the .wrapper

Stack Snippet

.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;
}
<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>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
  • the width of .wrapper is set to calc(100% - 180px). If I remove it the .wrapper would just sit in the middle. And for some reason the scroll would not show and work at all. – magom001 Feb 01 '18 at 11:35
  • @magom001 Is there more css apart from that....or any markup....? As you can see it works fine in above snippet.. – Bhuwan Feb 01 '18 at 11:46
  • check it on codepen: codepen.io/magom001/pen/rJOejw the .wrapper must expand to viewport's right and bottom borders – magom001 Feb 01 '18 at 12:26
  • @magom001 See this https://codepen.io/bhuwanb9/pen/aqvpKg..... remove `overflow:hidden` from `html` css – Bhuwan Feb 01 '18 at 13:25
0

The only workable css solution I found was to add min-width to .item and .timeline.

Check on 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;
    }
}
magom001
  • 608
  • 6
  • 16
-1

Please update following CSS if you looking for that:

.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
}
Hanif
  • 3,739
  • 1
  • 12
  • 18
  • See here also: https://codepen.io/markupsolution/pen/yvYOWz – Hanif Feb 01 '18 at 10:59
  • "how do I make the child green div to always stretch to the whole width of the .wrapper div?" Not sure this is what OP is looking for – sol Feb 01 '18 at 11:07
  • @Hanif, resize you browser: if you scroll right the green div will not stretch till the right border of the .wrapper div. – magom001 Feb 01 '18 at 11:10