Я думаю, что использование querySelector будет лучшим вариантом, который решит проблемы, которые у вас возникают
var car = document.querySelector('#car');
var container = document.querySelector('.container');
var carLeft = 0;
function anim(e) {
if (e.keyCode==87) {
//W
}
if (e.keyCode==65) {
//A
carLeft+=2;
car.style.left = carLeft + 'px';
}
if (e.keyCode==83) {
//S
}
if (e.keyCode==68) {
//D
carLeft-=2;
car.style.left = carLeft + 'px';
}
}
document.onkeydown = anim;
#car {
background-color: blue;
height: 10px;
width: 20px;
z-index: 10;
position: absolute;
left: 0;
}
<div class="container">
<p id="car">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
Было бы очень полезно, если бы вы могли также предоставить свою версию html, а также сообщить мне, помог ответ или нет чтобы я мог улучшить его для вас.