PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 30, 2022

[FIXED] How to make an image always 100% width but adjust the height and keep the aspect ratio?

 September 30, 2022     bootstrap-5, css, twitter-bootstrap     No comments   

Issue

I know this must have been asked before but I just cannot find any css to help me. How do I make an image always 100% height and 100% width to match the screen size but always keeping the image's aspect ratio?

.myimage {  
object-fit: cover;
width: 100%;
height: 250px;
}

Above almost works but I don't want to hard code height and would like to almost be 100% of the browser's view but keeping the aspect ratio. So the image should always fit 100% height and width wise without any scrolling and maintain that even if the browser is resized. I am using boostrap-5 framework.


Solution

.myimage {
  object-fit: cover;
  width: 100vw;
  height: 100vh;
  display: block;
}

html, body {
  margin: 0;
  padding: 0;
}
<img class="myimage" src="//placekitten.com/400" />



Answered By - ray hatfield
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing