So I created a basic HTML file with a div and linked it to a CSS file for styling:
HTML :
<html>
<head>
<title>Simple Movement</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="index.js"></script>
</head>
<body>
<div id="square"></div>
</body>
</html>
CSS:
body {
margin: 0;
}
#square {
width: 100px;
height: 100px;
border: 1px solid #095057;
background-color: #20979e;
position: absolute;
left: 200px;
top: 200px;
}
In my JavaScript file, I have a log statement like this:
console.log(document.getElementById('square').style.top)
But unfortunately, I am encountering an error message:
Uncaught TypeError: Cannot read properties of null (reading 'style') at index.js:1
I am puzzled as to why it is saying that the style property is null. Any ideas?