X
X
Xaip2018-04-30 23:02:03
css
Xaip, 2018-04-30 23:02:03

Positioning an image inside a block?

I use bootstrap and flex blocks to create a grid of goods for an online store. It is necessary to properly process images so that they fit into a block with a certain height. And all the images are very different, from the phone to the refrigerator).
So far I have only such a code (for the image)

<div class="img-block">
     <img href="something.img" />
</div>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Antony Bark, 2018-05-10
@Xaip

I would venture to suggest, perhaps not the best, but a quick and almost hassle-free option using background-image

<div class="img-block"  style="background-image: url(something.img)">
     <img href="something.img" />
</div>

Basically, we have this:
<div class="img-block"  style="background-image: url(something.img)">
</div>

.img-block {
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
.img-block img {
    opacity: 0;
}

Hide the IMG , show background-image instead .
Pros: cross-browser compatibility, easy editing of the code, no preparation of illustrations required
Cons: clogging of the code, although if the SEO is not so important and the block sizes are set, then <img>you can completely delete it.
example https://codepen.io/tolfy/pen/MGVbwJ

P
profesor08, 2018-04-30
@profesor08

img {
  width: 100%;
  height: 100%;
  object-fit: cover; // contain
}

You can't do anything else with your pictures.

M
Monty Python, 2018-05-01
@Austin1

If the Aspect Ratio is different (aspect ratio 1:1, 3:2, 8:5 etc) then I think the robots will not help you in this case, when the product cards must be the same size - only handmade, customized by hand, or by hiring a designer and content manager

J
Jumandjilos, 2018-05-01
@Jumandjilos

if for an online store, then it’s better not to use flex, but use css grid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question