indefinite
indefinite

Reputation: 431

Image in flex container not centering after defining width and height

I'm doing some personal portfolio using css flex box. what I'm trying to do is to center the image horizontally, It did center but the problem is after I defined width and height to img the image is at the start or left now not in the center.

This is what it looks like when centered and not defining the width and height

.center-img{
  border-radius: 50%;
  display:flex;
  flex-direction:column;
  justify-content:center;
  text-align:center;
}
<div class="center-img">
  <img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png" alt="user">
  <h1>User</h1>
</div>

And this is what it look liked after defining width and height

.center-img{
  width:100px;
  height:100px;
  border-radius: 50%;
  display:flex;
  flex-direction:column;
  justify-content:center;
  text-align:center;
}
<div class="center-img">
  <img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png" alt="user">
  <h1>User</h1>
</div>

I expect that after defining the width and height of the image it would not affect the position of the image. so it should be in the center. thank you!

Upvotes: 6

Views: 9772

Answers (3)

Michael Benjamin
Michael Benjamin

Reputation: 370993

Few things to consider:

  1. You were never centering the image.

    In your first code sample, the image was occupying the entire viewport width, so it just appeared centered. When you reduced the size of the image with width and height, it aligned left, per the default value. Use align-items to horizontally center flex items in a column-direction container.

  2. Don't use images as flex items. There are lots of bugs.

    Images as flex items are known to have rendering problems across different browsers. Instead, wrap the image in a div, and make the div the flex item.

  3. Align the image inside a nested container with flex properties.

    Use a nested flex container to center the image.

.center-img {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: lightgray;
}

.center-img > div {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  border: 1px dashed red;
  
}

.center-img > div > img {
  width: 100%;
}

h1 {
  margin: 0;
}
<div class="center-img">
  <div>
    <img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
      alt="user">
  </div>
  <h1>User</h1>
</div>

Upvotes: 9

BugsArePeopleToo
BugsArePeopleToo

Reputation: 2996

You need to wrangle the dimensions of the items inside.

The image can be controlled with max-width and max-height relative to the container. I added the red border so you can see the container easier.

Also, the h1 element comes with big font-size and margin by default so need to set values for those too.

.center-img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center; /* change text-align to align-items */
  border: 1px red solid; /* this is just so you can easily see the container */
}

.center-img img {
  max-width: 100%; /* dimensions relative to the container size */
  max-height: 40%; /* dimensions relative to the container size */
  height: auto; /* keep aspect ratio */
  width: auto; /* keep aspect ratio */
}

.center-img h1 {
  margin: 5px 0 0; /* h1 has default margin-top and margin-bottom */
  font-size: 18px; /* Set this to a reasonable size */
}
<div class="center-img">
  <img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
    alt="user">
  <h1>User</h1>
</div>

Upvotes: 1

AdityaSrivast
AdityaSrivast

Reputation: 1084

Apply the height and width to the img only and add align-items: center like this:

.center-img {
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

img {
 width: 100px;
 height: 100px;
}
<div class="center-img">
  <img src="https://png2.kisspng.com/sh/06560124247439e98f5534b17467b691/L0KzQYm3VcA5N5DBiZH0aYP2gLBuTgV0baMye9H2cIX3dcO0ifNwdqQyiAt8dHXwPbTvif5me5Yyj9t3ZD33ecXzhb1kd516hdC2NXHpQYW5VBZlO5JnTKo3M0e7RIa8VccyPWM6T6g5NkO8SIeATwBvbz==/kisspng-user-computer-icons-system-chinese-wind-title-column-5af1427fd3ab48.378455571525760639867.png"
            alt="user">
 <h1>User</h1>
</div>

Upvotes: 1

Related Questions