CSS - center and rounded Img Inside Div - html

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>

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>

Setting "overflow: auto;" somehow magically fixes layout issues [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
I have a bunch of divs that I laid out next to each other (depending upon window size there are 2-4 per "row"). Inside these divs, I added some wrappers and such, but at the center of each div there is an image that only has its width (not height) property set via CSS. The odd thing that was happening was that images with a greater width than height in the original png caused the outermost div containing it to shift down. I searched for a solution for a while and found that setting overflow: auto; in the divs with class "item-card-wrapper" (see below) somehow magically fixed the layout. I thought I understood how overflow works, but I am so confused as to how it seemed to magically fix the issue (and this isn't the only instance in which this has happened for me).
The only "correct" div here is the one that sticks out from the rest. When I added overflow: auto; it was fixed:
Here is the HTML (I clipped it early because it just starts repeating itself for each "square"):
<div id="shop">
<div id="shop-center-wrapper">
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="static/products/Three Kings Glow.png">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="static/products/Amor Azul.png">
</div>
</div>
</div>
...
And here are the relevant CSS classes:
#shop {
margin-top: 40px;
margin-left: 50px;
margin-right: 50px;
overflow: auto;
}
#shop-center-wrapper {
text-align: center;
margin: 0px auto;
}
.item-card-wrapper {
margin-top: 20px;
margin-bottom: 20px;
display: inline-block;
overflow: auto;
}
.item-card {
width: 92%;
margin-left: 4%;
margin-right: 4%;;
background-color: white;
}
.image-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.image {
width: 90%;
margin-left: 5%;
margin-right: 5%;
}
I really just want to understand how overflow: auto; was able to solve all of my problems.
The key property here should be inline-block in .item-card-wrapper. Using the inline-block display, the component will be aligned to the baseline, which same like vertical-align: baseline;.
Through my small experiment, you can see 3 images are attached on the baseline. You can remove the overflow: auto; in my snippet to create the same effect.
By adding overflow:auto, the item-card-wrapper's content should be clipped (if it is larger than its parent) to maintain the same height with the parent, therefore all item-card-wrapper component should be same height at last.
You might have question about, your picture have the same size, which shouldn't have this effect. I am guessing background-color: white; in .item-card is covered your actual picture size. Try change the color and see if my assumption is correct or not.
Ps. If you want to search for alternative from inline-block, flexbox with flex-direction might be an another good option.
#shop {
margin-top: 40px;
margin-left: 50px;
margin-right: 50px;
overflow: auto;
background: green; /* Edited */
padding: 10px; /* Edited */
}
#shop-center-wrapper {
text-align: center;
margin: 0px auto;
}
.item-card {
width: 92%;
margin-left: 4%;
margin-right: 4%;;
background-color: white;
}
.item-card-wrapper {
margin-top: 20px;
margin-bottom: 20px;
display: inline-block;
overflow: auto; /* Edited */
}
.image-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: yellow; /* Edited */
}
.image {
width: 90%;
margin-left: 5%;
margin-right: 5%;
}
<div id="shop">
<div id="shop-center-wrapper">
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://www.designhill.com/design-blog/wp-content/uploads/2015/02/McDonald-Logo-1.jpg">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://d1.awsstatic.com/case-studies/600x400_mcdonalds_logo.58256463615a3353933179883a8c58f593a00880.png">
</div>
</div>
</div>
<div class="item-card-wrapper" style="width: 25%; min-width: 200px;">
<div class="item-card" style="height: 245px;">
<div class="image-wrapper">
<img class="image" src="https://media-exp1.licdn.com/dms/image/C4E0BAQHWxquJ9PJxvw/company-logo_200_200/0?e=2159024400&v=beta&t=95WVdd_Q6vNKUybW3mX2odTGxRJ30bwKjF9SkeSH96w">
</div>
</div>
</div>
</div>
</div>

Center fit images of any size to parent div

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>

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/