Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] cumulative layout shift issue #279

Closed
teoh4770 opened this issue Apr 9, 2024 · 5 comments · Fixed by #283
Closed

[Bug] cumulative layout shift issue #279

teoh4770 opened this issue Apr 9, 2024 · 5 comments · Fixed by #283
Assignees
Labels
bug Something isn't working

Comments

@teoh4770
Copy link
Contributor

teoh4770 commented Apr 9, 2024

No description provided.

@teoh4770 teoh4770 added the bug Something isn't working label Apr 9, 2024
@GabeDiniz
Copy link
Contributor

Hey @teoh4770, what do you mean by this?

@teoh4770 teoh4770 changed the title [Bug] About section causes cumulative shift [Bug] About section causes cumulative layout shift Apr 10, 2024
@teoh4770
Copy link
Contributor Author

article about cumulative layout shift: https://web.dev/articles/cls

@teoh4770 teoh4770 changed the title [Bug] About section causes cumulative layout shift [Bug] cumulative layout shift issue Apr 10, 2024
@teoh4770
Copy link
Contributor Author

The most common causes of a poor CLS are the following:

  1. Images without dimensions.

@teoh4770
Copy link
Contributor Author

Link: A more elegant and easier to use solution

solution:

<style>
  img {
    width: 100%;
    height: auto;
    aspect-ratio: attr(width) / attr(height);
  } 
</style>
<img src="hero_image.jpg" alt="" width="500" height="500">

Basically, this solution means if the following four conditions are true,

  1. height is set on the element in HTML
  2. width is set on the element in HTML
  3. height (or width) is set in the CSS — including using percentage values like max-width: 100%;
  4. width (or height) is set to auto in the CSS.

then the correct image dimensions could be calculated without needing to wait for the images to download, and so without the need of a content layout shift:

@teoh4770 teoh4770 self-assigned this Apr 10, 2024
@teoh4770 teoh4770 linked a pull request Apr 10, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants