VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • HTML

  • CSS

    • CSS Tutorials and Tips Collection
    • Flex Layout Syntax
    • Flex Layout Example - Basics
    • Flex Layout Example - Dice
    • Flex Layout Example - Holy Grail Layout
    • Flex Layout Example - Grid Layout
    • Flex Layout Example - Input Layout
    • CSS3 Transition
    • CSS3 Animation
    • Layout Tip - Auto-Expand Element Height Before Images Load
    • Text Overflow Ellipsis for Single and Multiple Lines
    • Understanding the Box Model Through the box-sizing Property
    • Horizontal and Vertical Centering Methods - Examples
    • How to Automatically Respond to CSS Dark Mode Based on System Theme
    • CSS Trick - Custom Hover Tooltips with :hover and attr()
    • CSS Functions Summary
    • Adding a Scrollbar to a Table's tbody with CSS
    • Animations in Grid Layout
  • 页面
  • CSS
xugaoyi
2020-02-22

Layout Tip - Auto-Expand Element Height Before Images Load

# "Layout Tip" Auto-Expand Element Height Before Images Load

In mobile development, some elements are auto-expanded based on image height, and the height cannot be hardcoded (e.g., the outer element of a carousel). On slow networks, images take time to load, leaving the element's height unexpanded and causing undesirable layout shifts. In this case, you can set the following styles to define the element's height:

.wrapper
  overflow hidden
  width 100%
  height 0
  padding-bottom 26.66% // This value is the image's height-to-width ratio, i.e., height/width
  background #eee
1
2
3
4
5
6

In the code above, the padding-bottom value is the image's height-to-width ratio (i.e., height/width). It is calculated as 26.66% of the width value, so the final element height matches the height after the image loads.

You can also use the vw approach, though some browsers may have compatibility issues:

.wrapper
  width 100%
  height 26.66vw
  background #eee
1
2
3
4

vw,viewpoint width,视窗宽度,1vw=视窗宽度的1%

vh,viewpoint height,视窗高度,1vh=视窗高度的1%

更多 CSS单位 (opens new window)。

Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
CSS3 Animation
Text Overflow Ellipsis for Single and Multiple Lines

← CSS3 Animation Text Overflow Ellipsis for Single and Multiple Lines→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode