Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

77
Views
clientWidth of element differs when element is accessed directly instead of logging the HTMLCollection

I'm trying to understand the magic behind sliders and currently following this tutorial on YT. On 8:50 he uses the clientWidth property of a slide to translate the slider-wrapper to the left by the width of one image (they are all scaled equally).

His code (and mine) looks like that:

<div class="carousel">
    <div class="carousel__item">
        <img class="test" src="../images/3.jpeg" id="last-clone"/>
        <img class="test" src="../images/1.jpeg" />
        <img class="test" src="../images/2.jpeg" />
        <img class="test" src="../images/3.jpeg" />
        <img class="test" src="../images/1.jpeg" id="first-clone"/>
    </div>
</div>

<script>
    const slidesWrapper = document.querySelector('.carousel__item');
    const slides = document.querySelectorAll('.carousel__item img');
    const size = slides[0].clientWidth;
    let counter = 1; //refers to the current active slide
    
    slidesWrapper.style.transform = `translateX(${(-size * counter)}px)`;

    console.log(slides); //when inspecting first items clientWidth, the value is correct
    console.log(`slides[0].clientWidth is ${slides[0].clientWidth}`); //logs an incorrect value
</script>

When I inspect the first console.log, the clientWidth of the first item is the correct value - it's the same value I can see, when hovering over the element in the devtools.

first items clientWidth

But when I log the value directly, it's completely different:

directly logged value

This is weird, can someone explain to me, what's happening here?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!