运行 ❯
获取您
自身
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <head> <style> #container { display: grid; width: 90%; margin: auto; border: solid black 1px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; padding: 10px; } #container > div { aspect-ratio: 3 / 2; } img { width: 100%; height: 100%; object-fit: cover; object-position: center; } </style> </head> <body> <h3>CSS aspect-ratio property.</h3> <p>Resize browser window to see how the layout behaves.</p> <p>Div elements with images inside are organized in responsive grid layout to make it look good on all devices. CSS aspect-ratio property adjusts height automatically to maintain correct ratio between width and height of div element. Correct ratio between width and height of the image is also maintained with the object-fit property set to cover.</p> <div id="container"> <div><img src="img_chania.jpg" alt="alley" width="460" height="345"></div> <div><img src="img_flowers.jpg" alt="flowers by the street" width="460" height="345"></div> <div><img src="img_mountains.jpg" alt="mountains" width="600" height="400"></div> <div><img src="img_5terre.jpg" alt="Cinque Terre" width="600" height="400"></div> </div> </body> </html>