@GregLyons
Posted
Good work on using semantic HTML. Here's what I think you can do for the profile picture.
First, I'll point out then when I try to adjust the height on my computer, the alignment gets messed up, and the profile pic is no longer properly centered. I believe this is because of a height/margin mismatch between your .card-top
, height: 20vh;
and your .profile_pic
, top: 23%;
properties. The issue is that the 23%
refers to 23%
of the .card
height, whereas the 20vh
refers to the screen itself. The former depends on the content of the card, whereas the latter depends solely on the viewport height. I'd recommend changing the .card-top
height
to some sort of percentage instead, so that it depends on the height of your actual content itself. This'll prevent this weird behavior where things get out of alignment.
As for positioning the profile pic, I'd still use absolute positioning, but in a different way:
1) Re-work HTML Put your .profile-pic
<img>
inside your .card-top
<div>
, and put position: relative;
on .card-top
instead of on .card
. This is so that you can position .profile-pic
relative to the .card-top
instead of .card
.
2) Center horizontally Check out thgaskell's answer, as it has some good info on left
/right
vs. margin-left
/margin-right
. You may need to explicitly set the width of your element in this case. If you don't want to specify the width (it's not always desirable to do so), check out Adel's answer instead.
3) Position vertically This is where setting position: absolute;
on .card-top
instead of .card
pays off. You want .profile-pic
to align with the bottom of .card-top
, which you can easily do with bottom: 0;
(on .profile-pic
). Then you need to push .profile-pic
down a bit more so that its centerline aligns with the bottom edge of .card-top
. For that, you can add transform: translateY(50%);
to do the trick. Here, note that the "50%" in the translateY
refers to 50% the height of .profile-pic
, NOT that of .card-top
(this is different behavior than, e.g. height: 50%
).
This solves your responsivity issue (where the profile pic gets misaligned on different screen heights), because you're now binding the positioning of .profile-pic
to the bottom edge of .card-top
, instead of binding it to the top of .card
and then doing some percentage math to get the result on a particular screen size (a calculation which is very tricky and probably not stable across different screen sizes).
Hope this helps, and best of luck in your future projects!
Marked as helpful
@zambobence
Posted
Dear @GregLyons, Thank you very much for taking the time and reviewing my code. I have moved the profile picture to the div you mentioned and used the transform attribute to align the picture vertically and the changing of the positioning of the card resolved also the margin problem in the card, now it seems to be correct.
I really appreciate your help, you gave me great insights and I have learned a lot from you, thank you.
Thank you very much.
@GregLyons
Posted
@benceturbulence Glad I could help! 😀