Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image height same as width

Tags:

html

css

sass

I have an image inside of a container and I want it to be a perfect square. I'm setting my width to be 100% of the container and the goal is to get the height of the image to be the same as the width, as I don't know what size the container will be due to responsive design.

Any ideas? I'm using sass, if that helps..

.container {
width: 50%;
}

.container img {
  width: 100%;
  height: 400px; //this should be the same as the width value..
}
<div class="container"><img src="https://images.pexels.com/photos/1249588/pexels-photo-1249588.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"/></div>

Preferably no javascript solution, but maybe it's not possible otherwise?

like image 288
Simon Avatar asked Jul 20 '18 17:07

Simon


People also ask

How do you make a picture the same width and height?

container img { width: 100%; height: 400px; //this should be the same as the width value.. }

Is height same as width?

Height is something that goes up – even though 8 cm side kind of goes up, it's inclined, so real height is only 6. As for the width, it's more informal, since objects have height, width, and sometimes depth. In your 2D toy example, there's already a height, so other one is width.

Is size of image and height of image same?

Unlike aspect ratios, image size determines an image's actual width and height in pixels. Image size equals the dimensions of an image.

Are images width by height or height by width?

What comes first? The Graphics' industry standard is width by height (width x height). Meaning that when you write your measurements, you write them from your point of view, beginning with the width.


2 Answers

Many of us had given you some hints in the comments, so by now you should be able to create a responsive square.


Fit the image inside the square

Just in case you are missing the last part, here is how you can fit the image (with any aspect ratio) into that square. It also means that your image will be cropped if it's not squared.

Snippet:

.container {
  position: relative;
  width: 37%; /* The size you want */
}
.container:after {
  content: "";
  display: block;
  padding-bottom: 100%; /* The padding depends on the width, not on the height, so with a padding-bottom of 100% you will get a square */
}

.container img {
  position: absolute; /* Take your picture out of the flow */
  top: 0;
  bottom: 0;
  left: 0;
  right: 0; /* Make the picture taking the size of it's parent */
  width: 100%; /* This if for the object-fit */
  height: 100%; /* This if for the object-fit */
  object-fit: cover; /* Equivalent of the background-size: cover; of a background-image */
  object-position: center;
}
<div class="container">
  <img src="https://images.pexels.com/photos/1249588/pexels-photo-1249588.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"/>
</div>
like image 86
Amaury Hanser Avatar answered Oct 28 '22 05:10

Amaury Hanser


If you're willing to use a background image then this solution will work https://codepen.io/anon/pen/jpyrwB .

This will keep a square ratio and make the image cover the whole div. It will also keep the image centered and crop the sides if the image is wider than the height.

HTML

<div class='square-box'>
    <div class='square-content'></div>
</div>

CSS

.square-box{
    position: relative;
    width: 50%;
    overflow: hidden;
}
.square-box:before{
    content: "";
    display: block;
    padding-top: 100%;
}
.square-content{
    position:  absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-image: url(https://images.pexels.com/photos/1249588/pexels-photo-1249588.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
    background-position: center;
    background-size: 100% 100%;
    background-size: cover;
}
like image 26
Axion Avatar answered Oct 28 '22 05:10

Axion