0

I'm using d3.js for a diagram. I want it to fill the whole window, no margin, no scrollbar, full height and full width. I don't necessary want it to be responsive.

Here is my failed attempt :

CSS:

body {
   margin: 0;
}

JS :

var width = window.innerWidth,
    height = window.innerHeight;

var svg = d3.select("#d3").append("svg")
    .attr("width", width)
    .attr("height", height);

HTML :

<body id="d3">    
    <script src="scripts/d3.js"></script>
    <script src="scripts/app.js"></script>
</body>

On chrome both toolbars are displayed (the generated SVG is actually slightly bigger than the body).

Edit : second attempt, works in chrome, height still incorrect in IE :

var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
    height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

and :

body {
    margin: 0;
    padding: 0;
    overflow: hidden;
}

(Credit to : Get the browser viewport dimensions with JavaScript)

PS : this is pure madness

2nd edit : : window.innerWidth and window.innerHeight works (at least in chrome and ie11). If you are also banging your head against the wall note that the element inspector report a wrong value for the height ... I checked the height and width of the viewport with an image editor and they match with window.innerWidth and window.innerHeight.

Community
  • 1
  • 1
Christophe Le Besnerais
  • 3,895
  • 3
  • 24
  • 44

1 Answers1

1

I think you'll find that your scrollbars move about the width/height of the scrollbars themselves. Easy fix is:

body {
  margin: 0;
  overflow: hidden;
}

Example here.

Mark
  • 106,305
  • 20
  • 172
  • 230