×

Save Your Code

If you click the save button, your code will be saved, and you get an URL you can share with others.

By clicking the "Save" button you agree to our terms and conditions.

Report Error

×

Save to Google Drive

If you have a Google account, you can save this code to your Google Drive.

Google will ask you to confirm Google Drive access.

×

Open from Google Drive

If you have saved a file to Google Drive, you can open it here:

Result Size: 625 x 571
x
 
<!DOCTYPE html>
<html>
<head>
<style>
#content {
  height: 100px;
  width: 100px;
  background-color: coral;
  padding: 15px;
  border: 10px solid black;
  margin: 10px;
  overflow: scroll;
}
</style>
</head>
<body>
<p>Click the first button to return the scrollHeight and scrollWidth of div. Click the second button to set the width and height of div to the values returned from scrollHeight and scrollWidth.</p>
<button onclick="getFunction()">Get scrollHeight and scrollWidth</button>
<button onclick="setFunction()">Set height and width</button>
<div id="content">Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..Some content..</div>
<p id="demo"></p>
<script>
var elmnt = document.getElementById("content");
function getFunction() {
  document.getElementById("demo").innerHTML = "Height: " + elmnt.scrollHeight + "px" 
  + "<br>Width: " + elmnt.scrollWidth + "px" ;
}
function setFunction() {
  elmnt.style.height = elmnt.scrollHeight + "px";
  elmnt.style.width = elmnt.scrollWidth + "px";
}
×

Report a Problem: