Почему появляется прокрутка

для блока .intro я прописал CSS:

min-height: calc(var(--myvh)*100 - var(--heightHeader));

100% высоты экрана минус высота header

В JavaScript я создаю CSS - переменные --heightHeader и --myvh

Что не так? Почему появляется прокрутка (по вертикали)

UPD: как я заметил, прокрутка по вертикали появляется тогда, когда появляется прокрутка по горизонтали

const root = document.querySelector(':root');
const header = document.querySelector('header');

function calcHeightHeader() {
  const heightHeader = header.scrollHeight;
  const heightWindow = window.innerHeight;
  root.style.setProperty('--heightHeader', heightHeader + 'px');
  root.style.setProperty('--myvh', heightWindow / 100 + 'px');
  console.log('heightHeader =', heightHeader);
  console.log('100vh = ', heightWindow);
}

calcHeightHeader();

window.addEventListener('resize', calcHeightHeader);
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800;900&display=swap');
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --grey: #bab8b8;
  --blue: #3984f3;
}

html,
body {
  height: 100%;
  font-family: 'Nunito', sans-serif;
}

.container {
  max-width: 1200px;
  padding: 0 15px;
  margin: 0 auto;
}

img {
  display: block;
  max-width: 100%;
}

a {
  text-decoration: none;
  color: black;
}

.btn {
  white-space: nowrap;
  display: inline-block;
  color: var(--blue);
  border: 2px solid var(--blue);
  padding: 10px 30px;
  transition-duration: 0.2s;
  transition-timing-function: linear;
  transition-property: color, background-color;
}

.btn:hover {
  color: white;
  background-color: var(--blue);
}

.link {
  color: var(--grey);
  transition: color 0.2s linear;
}

.link:hover {
  color: var(--blue);
}

header {
  padding: 10px 0;
  z-index: 2;
  position: relative;
  color: var(--grey);
  font-size: 18px;
}

.header__container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header__img {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.header__img span {
  color: var(--blue);
  font-size: 26px;
  font-weight: bold;
  display: block;
}

.header__img img {
  min-width: 50px;
}

.header__img>*:not(:last-child) {
  margin-right: 10px;
}

.header__nav {
  display: flex;
  align-items: center;
}

.header__nav>*:not(:last-child) {
  margin-right: 10px;
}

.intro {
  min-height: calc(var(--myvh)*100 - var(--heightHeader));
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<header>
  <div class="container">
    <div class="header__container">
      <div class="header__img">
        <div>
          <img src="https://picsum.photos/50/50" alt="">
        </div>
        <span>Rhea</span>
      </div>
      <nav class="header__nav">
        <a class="link" href="">Home</a>
        <a class="link" href="">Video</a>
        <a class="link" href="">Destinations</a>
        <a class="link" href="">Booking</a>
        <a class="btn" href="">Sign In</a>
      </nav>
    </div>
  </div>
</header>

<div class="intro">
  <div class="container">
    <div class="intro__container">
      <h1>Travel around the world</h1>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Et sequi accusantium voluptas asperiores necessitatibus perferendis cumque vel!</p>
    </div>
  </div>
</div>


Ответы (1 шт):

Автор решения: Михаил Камахин

Надо было дождаться загрузки изображения из header, поставить слушатель события load на картинку и выполнить основный код только тогда, когда загрузится картинка

const root = document.querySelector(':root');
const header = document.querySelector('header');
const headerImg = document.querySelector('header img');
headerImg.addEventListener('load', mainCode);

function mainCode() {
  function calcHeightHeader() {
    const heightHeader = header.scrollHeight;
    const heightWindow = window.innerHeight;
    root.style.setProperty('--heightHeader', heightHeader + 'px');
    root.style.setProperty('--myvh', heightWindow / 100 + 'px');
  }

  calcHeightHeader();

  window.addEventListener('resize', calcHeightHeader);
}
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800;900&display=swap');
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --grey: #bab8b8;
  --blue: #3984f3;
}

html,
body {
  height: 100%;
  font-family: 'Nunito', sans-serif;
}

body {
  overflow-x: hidden;
}

.container {
  max-width: 1200px;
  padding: 0 15px;
  margin: 0 auto;
}

img {
  display: block;
  max-width: 100%;
}

a {
  text-decoration: none;
  color: black;
}

.btn {
  white-space: nowrap;
  display: inline-block;
  color: var(--blue);
  border: 2px solid var(--blue);
  padding: 10px 30px;
  transition-duration: 0.2s;
  transition-timing-function: linear;
  transition-property: color, background-color;
}

.btn:hover {
  color: white;
  background-color: var(--blue);
}

.link {
  color: var(--grey);
  transition: color 0.2s linear;
}

.link:hover {
  color: var(--blue);
}

header {
  padding: 10px 0;
  z-index: 2;
  position: relative;
  color: var(--grey);
  font-size: 18px;
}

.header__container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header__img {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.header__img span {
  color: var(--blue);
  font-size: 26px;
  font-weight: bold;
  display: block;
}

.header__img img {
  min-width: 50px;
}

.header__img>*:not(:last-child) {
  margin-right: 10px;
}

.header__nav {
  display: flex;
  align-items: center;
}

.header__nav>*:not(:last-child) {
  margin-right: 10px;
}

.intro {
  min-height: calc(var(--myvh)*100 - var(--heightHeader));
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<header>
  <div class="container">
    <div class="header__container">
      <div class="header__img">
        <div>
          <img src="https://picsum.photos/50/50" alt="">
        </div>
        <span>Rhea</span>
      </div>
      <nav class="header__nav">
        <a class="link" href="">Home</a>
        <a class="link" href="">Video</a>
        <a class="link" href="">Destinations</a>
        <a class="link" href="">Booking</a>
        <a class="btn" href="">Sign In</a>
      </nav>
    </div>
  </div>
</header>

<div class="intro">
  <div class="container">
    <div class="intro__container">
      <h1>Travel around the world</h1>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Et sequi accusantium voluptas asperiores necessitatibus perferendis cumque vel!</p>
    </div>
  </div>
</div>

→ Ссылка