0

I have the following HTML:

<div class="wrapper">
  <div class="container">
    <div class="child">
      1
    </div>
    <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
    <div class="child">
      4
    </div>
  </div>
</div>

How do I make the .container expand it's width over the 100% of the parent width in order to accomodate all his children? The end goal is that I want to use Javascript to check if the child is wider than the parent, if yes I'll add arrows left and right to move the content left and right (it's a tabber).

Here's a fiddle, as you can see javascript sees the same width for both elements, but the child should be larger than the parent:

$(function() {
    $("[data-w-width]").text($(".wrapper").width());
  $("[data-c-width]").text($(".container").width());
})
* {
  box-sizing: border-box;
}

html, body {
  overflow: hidden;
}


.wrapper {
  width: 100%;
}

.container {
  width: auto;
  display: flex;
}

.child {
  width: 200px;
  background: #dadada;
  border: 1px solid black;
  flex-shrink: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="container">
    <div class="child">
      1
    </div>
      <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
    <div class="child">
      4
    </div>
  </div>
</div>
<div>
  wrapper width: <span data-w-width>0</span>
</div>
<div>
  container width: <span data-c-width>0</span>
</div>
Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40

2 Answers2

3

Change display: flex; to display: inline-flex;

$(function() {
    $("[data-w-width]").text($(".wrapper").width());
  $("[data-c-width]").text($(".container").width());
})
* {
  box-sizing: border-box;
}

html, body {
  overflow: hidden;
}


.wrapper {
  width: 100%;
}

.container {
  display: inline-flex;
}

.child {
  width: 200px;
  background: #dadada;
  border: 1px solid black;
  flex-shrink: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="container">
    <div class="child">
      1
    </div>
      <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
    <div class="child">
      4
    </div>
  </div>
</div>
<div>
  wrapper width: <span data-w-width>0</span>
</div>
<div>
  container width: <span data-c-width>0</span>
</div>
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Kaede
  • 198
  • 1
  • 11
1

You are searching for the wrong width.

Here is the trick:

  $("[data-c-width]").text($(".container")[0].scrollWidth);

Subject related: Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively

DEMO:

$(function() {
  $("[data-w-width]").text($(".wrapper").width());
  $("[data-c-width]").text($(".container")[0].scrollWidth);
})
* {
  box-sizing: border-box;
}

html, body {
  overflow: hidden;
}


.wrapper {
  width: 100%;
}

.container {
  width: auto;
  display: flex;
}

.child {
  width: 200px;
  background: #dadada;
  border: 1px solid black;
  flex-shrink: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="container">
    <div class="child">
      1
    </div>
      <div class="child">
      2
    </div>
    <div class="child">
      3
    </div>
    <div class="child">
      4
    </div>
  </div>
</div>
<div>
  wrapper width: <span data-w-width>0</span>
</div>
<div>
  container width: <span data-c-width>0</span>
</div>
MaxiGui
  • 6,190
  • 4
  • 16
  • 33