Can't get canvas element height to equal container height

Colin Young

I'm trying to set a series of canvas elements to the same height as their containers:

$(document).ready(function() {
  var c = $('.canvas canvas');
  var container = $(c).parent();

  $(window).resize(respondCanvas);

  function respondCanvas() {
    c.each(function(index) {
      $(this).attr('width', $(container[index]).width());
      $(this).attr('height', $(container[index]).height());
    });
  }

  respondCanvas();
});
.flex {
  display: flex;
  overflow: auto;
}
#viewport {
  flex: 1 0 100%;
  flex-flow: row wrap;
  justify-content: space-between;
}
.canvas {
  display: flex; /* new addition from answer */
  background: #ccc;
  margin: 0.25rem;
}
#half-breadth,
#profile {
  flex: 1 0 100%;
}
#body-plan,
#current-section,
#rendering {
  flex: 1 0 32%;
}
canvas {
  flex: auto; /* new addition from answer */
  background: #ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<figure id="viewport" class="flex">
  <div id="half-breadth" class="canvas">
    <canvas id="half-breadth-canvas"></canvas>
  </div>
  <div id="profile" class="canvas">
    <canvas id="profile-canvas"></canvas>
  </div>
  <div id="body-plan" class="canvas">
    <canvas id="body-plan-canvas"></canvas>
  </div>
  <div id="current-section" class="canvas">
    <canvas id="current-section-canvas"></canvas>
  </div>
  <div id="rendering" class="canvas">
    <canvas id="rendering-canvas"></canvas>
  </div>
</figure>

In Google Chrome on OS X at least, the canvas is less than the height of the containing div elements, and in the last 3, the amount it is less is different for each. Without the flex layout, all are a consistent amount smaller than the container. Is there some way to make them all actually exactly the same size? Ideally also a fix also when using flex?

At this point, I don't care about browser compatibility/portability (I'm using flex, so I've already decided to live on the bleeding edge), but I do need to use canvas attributes as far as I'm aware since I do not want to distort the contents (i.e. I can't use CSS 100% width/height).

UPDATE: added CSS to code snippet to reflect accepted solution provided in answers (see comments in CSS).

markE

The canvas elements are not direct children of viewport, so you have to add display:flex to the .canvas class and instruct canvas to be flex:auto.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get DIV's equal in height

From Dev

Can't get the height of a div element

From Dev

Flexboxes of equal height, content filling height of container

From Dev

Flexboxes of equal height, content filling height of container

From Java

Equal height rows in a flex container

From Dev

Why can't I make button's height equal their width?

From Dev

How to get the content of the bottom element of a flexbox to be 100% height its container

From Java

ReactJS - Get Height of an element

From Dev

Can't get img width and height jQuery

From Dev

Can't get height of div to adjust

From Dev

Can't get height and width of image in javascript

From Dev

Can't get img width and height jQuery

From Dev

Can't get WKWebView content height correctly

From Dev

jquery set line-height equal to parent container height

From Dev

html2canvas doesn't get image full height

From Dev

Flexbox container in Chrome doesn't get 100% height

From Dev

jQuery get height of the contents in a container

From Dev

Get height of container with responsive image

From Dev

How to equal height of <select> and <button> element with padding

From Dev

Selenium: Wait for element's height to be not equal to X

From Dev

Set the height of an <aside> element equal to height of an <article> element but not less than the height of browser

From Dev

Why can't I set the height of an element with ng-style?

From Java

Get width/height of SVG element

From Dev

Get height of an element's content

From Dev

AngularJS - Cannot get height of an element

From Dev

Get height of element and use it in css

From Dev

AngularJS: How to get element height

From Dev

Get height of an element in Angular 2

From Dev

How to get equal height for all sections?

Related Related

  1. 1

    Can't get DIV's equal in height

  2. 2

    Can't get the height of a div element

  3. 3

    Flexboxes of equal height, content filling height of container

  4. 4

    Flexboxes of equal height, content filling height of container

  5. 5

    Equal height rows in a flex container

  6. 6

    Why can't I make button's height equal their width?

  7. 7

    How to get the content of the bottom element of a flexbox to be 100% height its container

  8. 8

    ReactJS - Get Height of an element

  9. 9

    Can't get img width and height jQuery

  10. 10

    Can't get height of div to adjust

  11. 11

    Can't get height and width of image in javascript

  12. 12

    Can't get img width and height jQuery

  13. 13

    Can't get WKWebView content height correctly

  14. 14

    jquery set line-height equal to parent container height

  15. 15

    html2canvas doesn't get image full height

  16. 16

    Flexbox container in Chrome doesn't get 100% height

  17. 17

    jQuery get height of the contents in a container

  18. 18

    Get height of container with responsive image

  19. 19

    How to equal height of <select> and <button> element with padding

  20. 20

    Selenium: Wait for element's height to be not equal to X

  21. 21

    Set the height of an <aside> element equal to height of an <article> element but not less than the height of browser

  22. 22

    Why can't I set the height of an element with ng-style?

  23. 23

    Get width/height of SVG element

  24. 24

    Get height of an element's content

  25. 25

    AngularJS - Cannot get height of an element

  26. 26

    Get height of element and use it in css

  27. 27

    AngularJS: How to get element height

  28. 28

    Get height of an element in Angular 2

  29. 29

    How to get equal height for all sections?

HotTag

Archive