Chart.js bar chart is overflowing its containing element

11,364

Solution 1

As with most CSS, the answer is to use another wrapper!

I had the same problem (and I'm using box sizing)

Just put one div around your canvas! (the divs will inherit the box sizing)

<div>
  <canvas></canvas>
</div>

Good luck

:)

Solution 2

This will solve the issue

Javascript:

new Chart(ctx).Line(data, {
    responsive:true,
    maintainAspectRatio: false
});

ES6

Chart.defaults.global.responsive = true;
Chart.defaults.global.maintainAspectRatio = false;
Share:
11,364
corysimmons
Author by

corysimmons

I'm a fullstack web-developer currently specializing in React, React Native, Node, and Typescript. I have over 20 years of experience making websites (really, I can pull up my FF7 website from 1997) and over a decade of professional experience. My open source libraries have thousands of Github Stars and have been downloaded millions of times. I've written for just about every big publisher in the industry (Envato, CSS Tricks, a book for Packt Publishing, .net Magazine, etc.) and spoken at a few frontend conferences. Check out my website at corysimmons.com and feel free to reach out to me on there.

Updated on June 21, 2022

Comments

  • corysimmons
    corysimmons almost 2 years

    Chart.js

    I can't get this stupid chart to stay in its container when padding has been applied to the container. It doesn't matter if the responsive option is set to true.

    It also doesn't matter what I set box-sizing to. What's the purpose of the responsive option if not to fit it's container?