Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Images, keeping ratio, html?

Tags:

html

css

image

I am trying to resize a background picture to fit in a div. The problem I have is that I want the image to fit the height of the div and keep ratio. For example, I have a div which I don't want it to grow further the width of the screen (to prevent scrolling bars from appearing) and the image that I want to use is 1024px X 400px. If I try to fit the height of the image, It will force the width to fit also and it will lose ratio. I dont care if parts of the image are not shown. In fact, that is what I want to do.
What tags do I need to use? HTML5 or CSS

like image 531
Tino Avatar asked Jun 14 '17 18:06

Tino


People also ask

How do you keep an image ratio in HTML?

In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.

How do you lock an image in a ratio?

Simply select the picture and right click on it. Then select the “Format Picture” option. On the dialogue box that opens, click on “Size & Properties”, and tick the “Lock Aspect Ratio” option. We'll get to the bottom of this in just a moment.


2 Answers

Use CSS background-size: cover; to fill the <div>, or background-size: auto 100%; if you just want to match the height and don't care if the sides over- or under-flow:

html,
body {
  position: relative;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

div {
  background-image: url(https://placebear.com/1024/400.jpg);
  display: inline-block;
  background-repeat: no-repeat;
  border: 1px solid black;
}

.cover {
  background-size: cover;
}

.center {
  background-position: center;
}

.height {
  background-size: auto 100%;
}
<h1>Unstyled</h1>
<div style="width: 50px; height: 200px"></div>
<div style="width: 200px; height: 50px"></div>
<div style="width: 125px; height: 125px"></div>

<h1>Un-centered</h1>
<h2>Cover</h2>
<div class="cover" style="width: 50px; height: 200px"></div>
<div class="cover" style="width: 200px; height: 50px"></div>
<div class="cover" style="width: 125px; height: 125px"></div>

<h2>100% Height</h2>
<div class="height" style="width: 50px; height: 200px"></div>
<div class="height" style="width: 200px; height: 50px"></div>
<div class="height" style="width: 125px; height: 125px"></div>

<h1>Centered</h1>
<h2>Cover</h2>
<div class="cover center" style="width: 50px; height: 200px"></div>
<div class="cover center" style="width: 200px; height: 50px"></div>
<div class="cover center" style="width: 125px; height: 125px"></div>

<h2>100% Height</h2>
<div class="height center" style="width: 50px; height: 200px"></div>
<div class="height center" style="width: 200px; height: 50px"></div>
<div class="height center" style="width: 125px; height: 125px"></div>

In addition, use background-position: center; if you want to crop the image so that the center is always the focus instead of the top-left.

like image 120
Patrick Roberts Avatar answered Nov 15 '22 04:11

Patrick Roberts


If you can lower your ratio threshold a bit, you can also use:

img {
  width: 100vw;
  height: 100vh;
  max-width: 100%;
  max-height: 100%;
}
like image 37
VXp Avatar answered Nov 15 '22 05:11

VXp