Center fit images of any size to parent div - html

I've got an image slider, the selected image is set as the main image above. All the images are not same size. How do I make sure that each image is centered in the parent div and the entire parent div?
I'm now doing it with:
width: 100%;height: 100%;object-fit:fill;
but then my image loses the bottom of its content. I would like to center the image vertically.
My parent div has following class style:
.wrapper_image{
max-width:100%;
max-height:100%;
min-height:100%;
min-width: 100%;
margin: 0 auto;
}
code:
<div class="o-grid__col u-12/12" style="width: 400px;
height: 400px;
position: relative;
display: inline-block;
overflow: hidden;
text-align: center;
margin: 0;">
<div class="mySlides wrapper_image_horses">
<img src='url' style="width: 100%;height: 100%;object-fit:fill;">
</div>
</div>

I suggest you use display: flex; to achieve this. With flex it is much easier for you to align you images to center vertically and horizontally. Below is a test code I have done. Hope it helps with your problem
HTML
<div class="d-flex h-100">
<div class="img-slide">
<img src="https://via.placeholder.com/150">
</div>
</div>
CSS
.d-flex {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.h-100 {
height: -webkit-fill-available;
}
JS Fiddle Link : https://jsfiddle.net/SJ_KIllshot/y5fzvox8/

Here is a modern solution, hope it will help you
DEMO
<div class="o-grid__col u-12/12" style="width: 400px;
height: 400px;
position: relative;
display: inline-block;
overflow: hidden;
text-align: center;
margin: 0;
border: 1px solid #000;">
<div class="mySlides wrapper_image_horses" style="position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);">
<img src='https://picsum.photos/id/159/200/200' style="width: auto;height: auto;">
</div>

Related

3 Horizontal Box - CSS

What i would like to achieve is that i want to change the way my boxes appear only on the mobile version of the website. I experimented with display: and flex: rules but had no luck. I want them stick to each other but Couldn't find the right CSS rule. Help.
Thanks.
Example picture of how i want them:
The way they appear on desktop version of the website:
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/></div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/></div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /><img /></div>
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
#media only screen and (max-width: 1000px) {
.main {
display: flex;
gap: 10px;
}
.m-image {
border: solid black 3px;
}
}
<div class = "main">
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1">
</div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1">
</div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" />
</div>
</div>
Couple of things to think about when it comes to mobile view. When wanting certain items to display a certain way on mobile view you want to use #media only screen and (max-width: /* width of the device */. Place all the CSS rules inside of here. These rules will change the rules set above or run new rules that you have define below.
Also, when it comes to display: flex; you want to make sure you wrap it into another div. This "wrapper" or "container" will provide the structure to the way you want the images to display.
add a main container to compress the class m-image then add display flex
Ex:
<div id="main-container">
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
</div>
.main-container {
display: flex;
}
then add padding left and right to your m-image class
what you should do in this case is put your images in a single container and apply flex property in the css.
basically manipulate your html and css like this.
HTML:
<div class="image-container">
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/>
</div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/>
</div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /><img />
</div>
</div>
CSS:
.image-container{
display:flex;
gap: 8px;
flex-wrap: nowrap;
}
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
This should give you your desired result for the desktop and for the mobile version you will have to apply media query into your CSS code.
hope this answers your question!
I couldn't really understand your code, So I wrote one for you suiting your needs
I hope it will fix your problem!
#content{
display: flex;
justify-content: space-around;
width: 100%;
}
.imagecontainer{
overflow: hidden;
width: 30%;
}
img{
width: 100%;
}
<div id="content">
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg"></div>
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg"></div>
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg"></div>
</div>

Images in a flexbox doesn't scale properly

I put 2 images inside a flexbox with column direction and I want images to resize when I resize a browser window. They don't change as I expect.
I want images to change in their original ratio, but no matter what css style I use it fails.
It works fine when I resize a window horizontally buy not when I resize it vertically.
Additionally images go outside of the flexbox div, how can I prevent it?
#box{
width: 100%;
height: 100%;
}
#container {
display: flex;
flex-direction: column;
width: 50%;
height: 30%;
background-color: red;
}
#photo {
width: 100%;
height: auto;
}
<div id="box">
<div id="container">
<img id="photo" src="1.JPG">
<img id="photo" src="2.JPG">
</div>
</div>
One think i normally use in this scenarios is set the image as background image using css an use background-size:contain;
An example:
#box{
width: 100vw;
height: 100vh;
}
#container {
display: flex;
flex-direction: column;
width: 50%;
height: 30%;
background-color: red;
}
.photo {
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-size:contain;
}
<div id="box">
<div id="container">
<div class="photo" style="background-image: url('https://picsum.photos/200/300/?random')"></div>
<div class="photo" style="background-image: url('https://picsum.photos/300/200/?random')"></div>
</div>
</div>
https://jsfiddle.net/vbanj8st/

CSS - center and rounded Img Inside Div

I have an Img inside a Div on my webpage, the image appears inside the div, but the alignment is off depending on the browser width.
this is my code:
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="" src="../images/Joanne.jpg" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>
I was wondering if there was a way I could vertically and horizontally align the Image inside that Div, and maybe also have the image with rounded corners.
I have searched around but nothing I try works, I thought by setting the margin left and right to 'auto' would solve my problem, but the image is still off when I resize the browser.
Any help or advice is appreciated.
Demo:https://jsfiddle.net/jjxbm7j7/
For a rounded image - use the border-radius property
For a vertically centered image - Use display:flex and align-items:center on the parent element of the image that has a specified height.
For a horizontally centered image - display:flex and justify-content:center on the parent element of the image that has a specified width.
However, for your example, I used margin:auto, because it is actually simpler.
For more information for flex stuff, click here: http://www.w3schools.com/css/css3_flexbox.asp
It should look like this:
.image {
border-radius: 50px;
}
.w3-col.w3-container.w3-green {
display: flex;
align-items: center;
margin: auto;
}
.w3-row {
border: 1px solid #aaa;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; height: 100%;">
<div>
<img class="image" src="http://s33.postimg.org/vnc0xbztb/Joanne.jpg
" alt="Chania" style="height: 80px; display: block; margin-left: auto; margin-right: auto;">
</div>
</div>
<!--<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>-->
</div>
I added the 1px border property for clarity.
Perfectly Center your Image inside a DIV using transform: scale()
I have created a box inside that i have placed your image to show how to center the image inside the div(class="box") .
body{
width: 100%;
height: 100vh;
margin: 0;
}
.w3-row {
display: flex; //flexBox
align-items: center;
justify-content: center;
flex-direction: column; //Works as a Stack i.e Image at the Top and Description at the bottom
}
For Scaling Image using CSS Transformations
.image{
width: 100%;
height: auto;
transform: scale(.5); //change the scale value to change size of the Image 0<scale<1
}
For Rounded Image
.img-rounded{
border-radius: 50%
}
Working Fiddle: https://jsfiddle.net/rittamdebnath/jjxbm7j7/3/
before anything it's not good approach to write inline CSS but anyway you can use vertical align way on an sudo element (after) like so
.image {
border-radius: 50px; //adjust yourself
display: inline-block;
vertical-align: middle;
}
.w3-col.w3-container.w3-green{
text-align:center;
height:100px;
border:1px solid;
width:100%;
}
.w3-col.w3-container.w3-green:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
<div class="w3-row" style="100%; background-color: #fff; margin-top: 5px; height: 100px; padding: 5px;">
<div class="w3-col w3-container w3-green" style="width: 15%; margin:auto;">
<img class="image" src=" http://s33.postimg.org/vnc0xbztb/Joanne.jpg" alt="Chania" style="height: 80px; ">
</div>
<div class="w3-col w3-container w3-red" style="width: 85%; height: 100%;">
</div>
</div>

img in flexbox overflows at top and bottom

I have a problem with the flexbox. The image inside should fit into the outer box without overflow. The first and last works perfectly, but the second overflows at the top and the bottom.
How could I fix this?
.outer {
border: 1px solid red;
margin: 25px;
width: 160px;
height: 160px;
display: flex;
justify-content: center;
align-items: center;
}
.outer img {
max-width: 100%;
}
<div class="outer">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/16x9_by_Pengo.svg/220px-16x9_by_Pengo.svg.png" />
</div>
<div class="outer">
<img src="http://www.thepixiecollective.com/files/imagecache/product_full/product_images/blue.png" />
</div>
<div class="outer">
<img src="http://41.media.tumblr.com/fa335632f1b474dd55493757839d2a3a/tumblr_n0ukv5MLqG1qz5uc4o1_500.jpg" />
</div>
This is because the height of the second image is greater than the width. The width of the first and third images is either greater than or equal to the height so are constrained by max-width: 100%; which ensures they will never exceed the width of the parent container.
To fix make the following changes to css:
Add max-height: 100%; to .outer img - This will ensure the height of the image can never exceed the height of the container
.outer {
border: 1px solid red;
margin: 25px;
width: 160px;
height: 160px;
display: flex;
justify-content: center;
align-items: center;
}
.outer img {
max-height: 100%;
max-width: 100%;
}
<div class="outer">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/16x9_by_Pengo.svg/220px-16x9_by_Pengo.svg.png" />
</div>
<div class="outer">
<img src="http://www.thepixiecollective.com/files/imagecache/product_full/product_images/blue.png" />
</div>
<div class="outer">
<img src="http://41.media.tumblr.com/fa335632f1b474dd55493757839d2a3a/tumblr_n0ukv5MLqG1qz5uc4o1_500.jpg" />
</div>
You should insert your image in CSS, it's easier to have this behaviour.
.outer {
border: 1px solid red;
margin: 25px;
width: 160px;
height: 160px;
}
#pengo {
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/16x9_by_Pengo.svg/220px-16x9_by_Pengo.svg.png');
}
#blue {
background-image: url('http://www.thepixiecollective.com/files/imagecache/product_full/product_images/blue.png');
}
#tumblr {
background-image: url('http://41.media.tumblr.com/fa335632f1b474dd55493757839d2a3a/tumblr_n0ukv5MLqG1qz5uc4o1_500.jpg');
}
HTML
<div id="pengo" class="outer"></div>
<div id="blue" class="outer"></div>
<div id="tumblr" class="outer"></div>
Then play with background-position and background-size (see MDN) to do place the images, for example:
background-position: center;
background-size: cover; /* or contain*/

Center divs vertically within divs

So, I have a page (height 100%) split into two rows (top half 30%, bottom half 70%).
https://jsfiddle.net/qL0s07nr/
I have an h2 tag enclosed within one column in the top half which I want to center vertically, and I have 3 other columns in the bottom half which need centering vertically too.
<div class="container gb">
<div class="row" style="height:30%; background: #f6f6f6;">
<div class="twelve columns">
<h2 style="text-align: center;">30%</h2>
</div>
</div>
<div class="row" style="height:70%; background: #d4fff0">
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:red;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:blue;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:green;"></div>
</div>
</div>
</div>
My css for the columns is as follows:
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
Full CSS:
.container.gb {
width: 100%;
max-width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
box-sizing: border-box;
}
.column, .columns {
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.one-third.column {
width: 30.6666666667%;
}
html, body {
height: 100%;
}
But for the life of me, I can't understand why it won't work. I am targeting the correct div, but it won't center for some reason. Any help is most appreciated.
Check this version: http://jsfiddle.net/leojavier/ko8wke5z/
<div class="container gb">
<div class="row" style="height:30%; background: #f6f6f6;">
<div class="twelve columns">
<h2 style="text-align: center;">30%</h2>
</div>
</div>
<div class="row rowb" style="height:70%; background: #d4fff0">
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:red;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:blue;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:green;"></div>
</div>
</div>
</div>
full CSS
.container.gb {
width: 100%;
max-width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
box-sizing: border-box;
}
.column, .columns {
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.one-third.column {
width: 30.6666666667%;
display: table-cell;
text-align: center;
vertical-align: middle;
float: none;
}
.rowb {
display: table;
width: 100%;
}
html, body {
height: 100%;
}
you can try it here: http://jsfiddle.net/leojavier/ko8wke5z/
The div that contains your <h2> does not use the full height of the row. Add his to your css:
.twelve.columns{height:100%}
If that class is going to get used again somewhere that style won't work give that div another class or an id (or an inline style).
Flexbox requires a flex container and flex items - it's a parent:children relationship. If you're using flexbox and presumably don't need older browser support, you can get rid of all those floats and specified widths. Your responsive layout can be done entirely with flexbox.
See this fiddle for responsive centered columns with matching gutters: https://jsfiddle.net/qL0s07nr/1/
Also, this flexbox guide is an excellent resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/