Why doesn't the IMG scale maintaining aspect-ratio? CSS - css

So I am trying to put some PNG files/Logos side-by-side in one line. I used flexbox for this purpose. However, the aspect ratio of the files is not correct. When I increase the Width, the size increases, however, it takes up the full Height and the aspect ratio breaks down.
The HTML:
`
<section class="companyLogos">
<img src="./images/logo-google.png" alt="Logo of Google">
<img src="./images/logo-ibm.png" alt="Logo of IBM">
<img src="./images/logo-microsoft.png" alt="Logo of Microsoft">
<img src="./images/logo-hp.png" alt="Logo of HP">
<img src="./images/logo-vector-graphics.png" alt="Logo of Vector Graphics">
</section>
`
The CSS:
`
.companyLogos {
width: 96%;
margin: 0 auto;
display: flex;
justify-content: space-evenly;
}
.companyLogos img {
width: 8%;
}
`
I am trying to do this:

You have to set object-fit: contain like this:
.companyLogos {
width: 96%;
margin: 0 auto;
display: flex;
justify-content: space-evenly;
}
.companyLogos img {
width: 8%;
object-fit: contain;
}
<section class="companyLogos">
<img src="https://via.placeholder.com/400x300" alt="">
<img src="https://via.placeholder.com/400x280" alt="">
<img src="https://via.placeholder.com/400x260" alt="">
<img src="https://via.placeholder.com/400x240" alt="">
</section>
I hope this will help you.

Following two approaches would be useful-
1. Using object-fit: contain-
If we use object-fit: contain; the image keeps its aspect ratio, but is resized to fit within the given dimension.
.companyLogos {
width: 96%;
margin: 0 auto;
display: flex;
justify-content: space-evenly;
}
.companyLogos img {
width: 8%;
object-fit: contain;
}
<section class="companyLogos">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Google">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of IBM">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Microsoft">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of HP">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Vector Graphics">
</section>
2. Using object-fit: cover-
If we use object-fit: cover; the image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit.
.companyLogos {
width: 96%;
margin: 0 auto;
display: flex;
justify-content: space-evenly;
}
.companyLogos img {
width: 8%;
object-fit: cover;
}
<section class="companyLogos">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Google">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of IBM">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Microsoft">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of HP">
<img src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png" alt="Logo of Vector Graphics">
</section>

Related

image gallery with flexbox

I want to make a small flexbox gallery of images with inconsistent height.
Desired result using grid:
Current result with flexbox:
.
This content belongs to a course, but I want to do that part with flexbox, as a learning method
container {
width: 90%;
overflow: hidden;
margin: 0 auto;
max-width: 1200px;
padding: 80px 0;
}
.projects {
background-color: #f2f2f2;
overflow: hidden;
margin-bottom: 500px;
}
.projects__grid {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
align-content: space-between;
height: 550px;
background-color: aqua;
}
.projects__item {
width: 32.8%;
height: 270px;
}
.projects__item--first {
height: 550px;
}
.projects__img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
<section class="projects">
<div class="container">
<h2 class="subtitle">mis proyectos</h2>
<div class="projects__grid">
<article class="projects__item projects__item--first">
<img src="assets/img1.jpg" alt="" class="projects__img">
</article>
<article class="projects__item">
<img src="assets/img2.jpg" alt="" class="projects__img">
</article>
<article class="projects__item">
<img src="assets/img3.jpg" alt="" class="projects__img">
</article>
<article class="projects__item">
<img src="assets/img4.jpg" alt="" class="projects__img">
</article>
<article class="projects__item">
<img src="assets/img5.jpg" alt="" class="projects__img">
</article>
</div>
</div>
</section>

How to align text under a flex image?

I am trying to display some text below the two pictures.
Below the closing divs to be exact. The text is incorrectly displayed on the side of the image.
What am I doing wrong?
It's my first time posting here in a long time so I apologize for any posting mistakes.
/* Helper classes */
.container {
max-width: 1400px;
margin: 0 auto;
width: 90%;
}
.my-sec-1-mt {
margin-top: 4rem;
}
.my-sec-2-mt {
margin-top: 1.8rem;
}
/* Services section */
.rmi-services_services-wrapper display: flex;
flex-direction: column;
gap: 1.8rem;
.rmi-services__service-gallery {
width: 100%;
margin-top: 1rem;
display: flex;
flex-direction: column;
gap: 0.8rem;
}
.rmi-services__service-gallery>.rmi-services__service-visual {
display: flex;
aspect-ratio: 1;
border-radius: 0.5rem;
overflow: hidden;
}
.rmi-services__service-gallery>.rmi-services__service-visual picture {
height: 100%;
}
.rmi-services__service-gallery>.rmi-services__service-visual img {
height: 100%;
object-fit: cover;
object-position: center;
}
<section class="rmi-services my-sec-1-mt">
<div class="container">
<div class="rmi-services__services-wrapper my-sec-2-mt">
<div class="rmi-services__service">
<div class="rmi-services__service-gallery">
<div class="rmi-services__service-visual curve-border">
<picture>
<source srcset="{% static 'image_location" type="image/webp">
<img loading="lazy" class="img-left" src="{% static 'image_location' %}" alt="">
</picture>
</div>
<div class="rmi-services__service-visual curve-border">
<picture>
<source srcset="{% static 'image_location" type="image/webp">
<img loading="lazy" src="{% static 'image_location' %}" alt="">
</picture>
</div>
</div>
</div>
</div>
</div>
</section>

What am i doing wrong with flexbox trying to align these images?

So I am trying to align images one next to another with display flex but they're not working, tried a lot of things but none of them helped
.container {
display: flex;
width: 100%;
flex-wrap: wrap;
}
.image {
width: 100%;
margin-right: 10px;
}
<div class="container">
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
</div>
You need to remove
width: 100%;
from your .image styles.
Working Example:
.container {
display: flex;
width: 100%;
flex-wrap: wrap;
}
.image {
margin-right: 10px;
}
<div class="container">
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
<div class="image">
<img src="photo.jpg">
</div>
</div>
Update:
Do you want a single row of 4 images? If so, you'll need to give each image a width or a flex-basis of equal to or less than 25% of the .container.
Otherwise an image will be assumed to have a width corresponding to its original size.
You can achieve this by inserting .image {flex: 0 1 25%;} into your CSS:
.container {
display: flex;
width: 100%;
flex-wrap: wrap;
}
.image {
flex: 0 1 25%;
margin-right: 10px;
}

Fill vertical space with Flexbox in a list of items

I have a list of items, each one in display: flex to position its children elements. The height of the item is given by the image, and the button at the right position should fill the vertical space of the item. But I can't do that.
.Main-item {
display: flex;
border: 1px solid gray;
margin-bottom: 2px;
}
.Main-img {
height: 50px;
width: 50px;
overflow: hidden;
object-fit: cover;
object-position: center;
border-radius: 50%;
}
.Main-name {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: space-evenly;
align-items: flex-start;
flex: 1 1;
padding: 0 10px;
}
.Main-buttonWrapper {
height: 100%;
width: 146px;
}
.Main-button {
height: 100%;
width: 100%;
}
<div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
</div>
Any idea will be welcome!
When you create a flex container various default flex rules come into play.
Two of these default rules are flex-direction: row and align-items: stretch. This means that flex items will automatically align in a single row, and each item will fill the height of the container.
If you change the value of align-items to for example flex-start it will change the default behavior 'stretch' as well if you set a specific height to the child of a flex container.
So just remove the height: 100% from .Main-buttonWrapper and it will have the default behavior.
For a better understanding look this answer: https://stackoverflow.com/a/33220564/4966662
I've given a height to the Main-item + align-items:stretch;. I hope this is what you need.
.Main-item {
align-items:stretch;
height: 50px;
}
.Main-element {
padding-top: 16px;
}
.Main-item {
display: flex;
border: 1px solid gray;
margin-bottom: 2px;
align-items:stretch;
height: 50px;
}
.Main-img {
width: 50px;
overflow: hidden;
object-fit: cover;
object-position: center;
border-radius: 50%;
}
.Main-name {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: space-evenly;
align-items: flex-start;
flex: 1 1;
padding: 0 10px;
}
.Main-buttonWrapper {
width: 146px;
}
.Main-button {
width: 100%;
height: 100%;
}
<div class="Main-element">
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
<div class="Main-item">
<img class="Main-img" src="https://picsum.photos/200/300" />
<div class="Main-name">Lorem ipsum</div>
<div class="Main-buttonWrapper"><button class="Main-button">button</button></div>
</div>
</div>
A flex item stretches along the container cross axis by default, so the only thing you need to make the button stretch visually as well, is to make the button container a flex container by adding display: flex to .Main-buttonWrapper.

I am trying to make two divs to be next to each other in a responsive way

I am trying to make part of my responsive page look like: http://s17.postimg.org/k4bh7qxnj/Screen_Shot_2014_07_26_at_8_08_02_PM.png
I have tried a few things and I got the result found at this jsfiddle: http://jsfiddle.net/TachionFiddle/4HkTS/3/
This is my code:
<div id="my-wrapper" style="
max-width: 941px;
display: inline-block;
width: auto;
height: auto;">
<div class="left-stuff" style="
display:inline-block;
width: 18%;">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png" style="width: auto;
height: auto; max-width:50%;">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png" style="width: auto;
height: auto; max-width: 50%;">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png" style="width: auto;
height: auto; max-width: 50%;">
</div>
<div class="right-stuff" style="display:inline-block;max-width: 75%">
<img src="http://s17.postimg.org/gns7jkwkf/webbanner2.png" style="width: auto; height: auto;
max-width:100%;">
</div>
</div>
Here's the general idea:
<div class="wrapper">
<div class="col-10">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png">
<img alt="" src="http://s16.postimg.org/db3hs4ndt/sac_history.png">
</div>
<div class="col-90">
<img src="http://s17.postimg.org/gns7jkwkf/webbanner2.png">
</div>
</div>
.wrapper {
width: 100%;
}
.col-10 {
width: 10%;
float: left;
vertical-align: top;
}
.col-10 img {
display: block;
max-width: 100%;
width: 100%;
}
.col-90 {
width: 90%;
float: left;
}
.col-90 img {
max-width: 100%;
width: 100%;
}
DEMO
I would suggest putting the three blocks into one image and making the heights line up in photoshop, will make your life a lot easier.

Resources