Image not having a responsive margin-bottom - html

This website I'm coding has a header with a portfolio. I want the persons "avatar" to be halfway onto the portfolio. Basically I want the avatar image to always be 50% down on to the portfolio div. The page is responsive so it shrinks accordingly.
The avatar image shrinks/resizes accordingly, however; the margin-bottom doesn't keep the same proportion. I always want it to be 50% below, onto the next div.
Here's the GIF. the start of it is how I want it, watch when I resize. Thanks.
https://imgur.com/a/nL6m9ow
here's my code
body {
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar img {
width: 100%;
margin-bottom: -100px;
}
<div class="avatar">
<img src="images/portfolio-avatar.png" class="banner">
</div>
<div class="portfolio">
<img src="images/banner.png" class="banner">
</div>

body{
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar {
position: relative;
padding-top: 100px;
}
.avatar img{
max-width: 100px;
position:absolute;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
.portfolio img {
max-width: 100%;
}
<div class="avatar">
<img src="https://www.ienglishstatus.com/wp-content/uploads/2018/04/Sad-Profile-Pic-for-Whatsapp.png" class="banner">
</div>
<div class="portfolio">
<img src="https://about.canva.com/wp-content/uploads/sites/3/2015/02/Etsy-Banners.png" class="banner">
</div>
check this code, or you can check this here also.
find the link for codepen.
https://codepen.io/atulraj89/pen/qQQBMm
Resize the window and enjoy

If you use relative units (%) for the width of your avatar, as well as the padding-top, it will grow/shrink accordingly.
body {
width: 95%;
max-width: 1200px;
margin: 0 auto;
}
.avatar {
position: relative;
padding-top: 7.5%;
}
.avatar img {
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
width: 15%;
border-radius: 50%;
border: 6px solid white;
}
.portfolio img {
max-width: 100%;
}
<div class="avatar">
<img src="https://picsum.photos/120/120
" class="banner">
</div>
<div class="portfolio">
<img src="https://picsum.photos/1200/400" class="banner">
</div>

Related

Bootstrap 3 Aspect ratio on image

I am make a banner with Bootstrap 3, but having problems keeping the aspect ratio on the banner image.
When I see the image on small viewports, the image is getting squeezed. Can anybody help me with a solution on this?
<link rel="stylesheet" href="https://use.typekit.net/nai6cyj.css">
<style>
/**** Banner ****/
.image-wrap {
position: relative;
width: 100%;
height: 100vh;
overflow-x: hidden;
}
.banner-content {
position: absolute;
z-index: 99999;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
text-align: center;
font-size: 1.5em;
color: #fff;
line-height: 1.5;
}
.img-content img {
width: 100%;
height: 80vh;
display: block;
}
</style>
<!-- Banner -->
<div class="image-wrap">
<div class="img-content">
<img src="https://www.bmw.dk/content/dam/bmw/marketNORDICS/common/All-models/BMW-i/i4/2021/BMW_G26_BEV_i4_Stage-front_2_1680x756.jpg.asset.1621330813802.jpg" alt="">
</div>
<div class="overlay"></div>
</div>
<div class="banner-content">
<h1>MAKE THE BEST OF YOUR HTML EXPERIENCE WITH YOUR DAILY LIFE</h1>
</div>
You could use object-fit: cover to have your image fill your available space while maintaining its aspect ratio. You would need to put the 80vh value on the image container.
.image-wrap {
position: relative;
width: 100%;
height: 100vh;
overflow-x: hidden;
}
.banner-content {
position: absolute;
z-index: 99999;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
text-align: center;
font-size: 1.5em;
color: #fff;
line-height: 1.5;
}
.img-content {
height: 80vh;
}
.img-content img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 50%;
display: block;
}
<!-- Banner -->
<div class="image-wrap">
<div class="img-content">
<img src="https://www.bmw.dk/content/dam/bmw/marketNORDICS/common/All-models/BMW-i/i4/2021/BMW_G26_BEV_i4_Stage-front_2_1680x756.jpg.asset.1621330813802.jpg" alt="">
</div>
<div class="overlay"></div>
</div>
<div class="banner-content">
<h1>MAKE THE BEST OF YOUR HTML EXPERIENCE WITH YOUR DAILY LIFE</h1>
</div>
You may want to adjust the banner-content so it lines up better on small screens.
When you set your width and height (by relative) together, you force the image size to change together with your screen. For your text to be not aligned in the centre, is due to your text should be within the same division as your image so that they are able to refer to the same initial point when positioning.
<link rel="stylesheet" href="https://use.typekit.net/nai6cyj.css">
<style>
/**** Banner ****/
.image-wrap {
position: relative;
width: 100%;
overflow-x: hidden;
}
.banner-content {
position: absolute;
z-index: 99999;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
text-align: center;
font-size: 1.5em;
color: #fff;
line-height: 1.5;
}
.img-content > img {
width: 100%;
display: block;
}
</style>
<!-- Banner -->
<div class="image-wrap">
<div class="img-content">
<img src="https://www.bmw.dk/content/dam/bmw/marketNORDICS/common/All-models/BMW-i/i4/2021/BMW_G26_BEV_i4_Stage-front_2_1680x756.jpg.asset.1621330813802.jpg" alt="">
</div>
<div class="overlay"></div>
<div class="banner-content">
<h1>MAKE THE BEST OF YOUR HTML EXPERIENCE WITH YOUR DAILY LIFE</h1>
</div>
</div>

Center an image hyperlink within responsive background div

I've been trying to code an email. It contains a responsive div with background image and an image button at the center of that background. I want the image button to be exactly at the center. However, the button moves away from the center when tested on different devices with different viewport width.
https://jsfiddle.net/kevinsalgatar/7x3trcu6/
div.container {
background: url(https://gallery.mailchimp.com/38a79fccde10813ac5aad2f4d/images/6f1f0255-24a6-49a6-8a22-fc337c1d3430.png)no-repeat;
max-width: 100%;
height: auto;
position: relative;
}
div.button {
position: max-width: 100%;
height: auto;
}
div.button>img {
display: block;
max-width: 100%;
height: auto;
}
div.button>a {
-webkit-transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
<div class="container">
<div class="button">
<a href="https://www.youtube.com/watch?v=XejJw3Xgsr4">
<img src="https://gallery.mailchimp.com/38a79fccde10813ac5aad2f4d/images/780372ac-7648-4dd4-9a29-9bfc7ca6c54c.png" alt="780372ac-7648-4dd4-9a29-9bfc7ca6c54c.png"></a>
</div>
<img src="https://gallery.mailchimp.com/38a79fccde10813ac5aad2f4d/images/6f1f0255-24a6-49a6-8a22-fc337c1d3430.png" style="visibility: hidden;" />
</div>
div.container {
background: url(https://gallery.mailchimp.com/38a79fccde10813ac5aad2f4d/images/6f1f0255-24a6-49a6-8a22-fc337c1d3430.png)no-repeat;
max-width: 100%;
height: auto;
position: relative;
display: inline-block;
}

mobile compatible map markers

I placed an image of a map and put two little markers on it (I replaced them simply with letters for the time being). The issue is, that on the mobile view they are totally in a different position than on desktop view.
Can some please explain me how can I resolve this issue and why did it happen?
<html>
<body>
<style>
.container{
width: 70%;
margin: 0 auto;
position: relative;
}
#map > img{
max-width: 100%;
max-height: 100%;
}
.marker{
height: 1.5em;
font-size: 10px;
width: 1.5em;
position: absolute;
cursor: pointer;
}
.europe{
top: 25%;
left: 50%;
}
.canada{
top: 25%;
left: 25%;
}
</style>
<div class="container">
<div id="map">
<img src="https://linmark.com/assets/site/images/network/map.png" />
<div class="marker europe">A</div>
<div class="marker canada">B</div>
</div>
</div>
</body>
</html>
My goal is to make the markers appear on the same place on both mobile view and desktop.
I think you just need a transform:translate to keep the markers "centered" in their original positions.
Recall, your elements are a fixed size but your positioning and image is % based.
.container {
width: 70%;
margin: 0 auto;
position: relative;
}
#map>img {
max-width: 100%;
max-height: 100%;
}
.marker {
height: 1.5em;
font-size: 10px;
width: 1.5em;
position: absolute;
transform: translate(-50%, -50%); /* this */
cursor: pointer;
}
.europe {
top: 25%;
left: 50%;
}
.canada {
top: 25%;
left: 25%;
}
<div class="container">
<div id="map">
<img src="http://linmark.com/assets/site/images/network/map.png" />
<div class="marker europe">A</div>
<div class="marker canada">B</div>
</div>
</div>
Codepen Demo

The image exceed to the padding area of the parent div

I'm trying to make thumbnails. Here's the HTML code:
<div class="product-preview">
<div data-img-url="images/image7.jpg" class="p-thumbnail">
<img src="images/image7.jpg" />
</div><div data-img-url="images/image8.jpg" class="p-thumbnail">
<img src="images/image8.jpg" />
</div><div data-img-url="images/image9.jpg" class="p-thumbnail">
<img src="images/image9.jpg" />
</div><div data-img-url="images/image10.jpg" class="p-thumbnail">
<img src="images/image10.jpg" />
</div>
</div>
And css
.product-preview {
margin-left: -5px;
margin-right: -5px;
}
.product-preview .p-thumbnail {
height: 120px;
width: 25%;
display: inline-block;
padding: 10px 5px;
overflow: hidden;
}
.p-thumbnail img {
height: 100%;
display: block;
margin: 0 auto;
}
How to fix so that the the img will not exceed to the padding area of .p-thumbnail? I attach a photo below, hope I make clear enough so that someone can understand what I'm trying to say! Thanks!
It's because overflow property will only hide the child of the element from the border and outwards. If you want to limit your image not to exceed the content box, you have to put some inner container or make the image to contain.
First Option
<div data-img-url="images/image7.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image8.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image9.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div><div data-img-url="images/image10.jpg" class="p-thumbnail">
<div class="imgcover">
<img src="images/image7.jpg" />
</div>
</div>
With additional CSS
div.imgcover{
width: 100%;
overflow: hidden;
}
If you want your image to be centered on the imgcover div you can use
.p-thumbnail{
position: relative;
}
.p-thumbnail img {
max-height: 100%;
max-width: 100%;
display: block;
margin: 0 auto;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
Second Option
Change the style of the image to this to emulate object-fit: contain
.product-preview .p-thumbnail {
height: 120px;
width: 25%;
display: inline-block;
padding: 10px 5px;
overflow: hidden;
text-align: center;
}
.p-thumbnail img {
max-height: 100%;
max-width: 100%;
display: block;
margin: 0 auto;
}
what you want is
.product-preview img{
max-width: 100%;
height: auto; /* instead of height: 100%; */
}
and try to used images with same width and height so you dont have any problem
The very first simple solution that comes to mind is that you can use a overflow: hidden; on the parent element.
But if you want your image to fully be visible on the thumbnail you can use this codes to your image tag
{
width: 100%;
object-fit: cover;
}

Make smaller image cover bigger div and maintain aspect ratio

I have a user uploaded image inside of a div with a fixed width and height. The image inside is aligned to the center vertically. Works fine.
The problem is that sometimes, the height of the image is smaller than the height of the div.
See this jsfiddle for example.
How do I make the image always fit the height of the div and maintain its aspect ratio?
HTML:
<div class="container">
<div class="b_feat_img">
<img src="http://i.imgur.com/iEJWyXN.jpg">
</div>
<div class="b_feat_img">
<img src="http://i.imgur.com/qRkEJni.jpg">
</div>
</div>
CSS:
.container {
width:120px;
}
.b_feat_img {
border: 1px solid green;
background:red;
float: left;
height: 96px;
margin-bottom: 10px;
overflow: hidden;
width: 100%;
}
.b_feat_img img {
height: auto;
position: relative;
top: 50%;
transform: translate(0px, -50%);
width: 100%;
}
In supported browsers*. You can use object-fit with value of cover, it works similarly to background-size:cover, but for inline images.
.b_feat_img img {
object-fit: cover;
height: 100%;
width: 100%;
}
.container {
width: 120px;
}
.b_feat_img {
border: 1px solid green;
background: red;
float: left;
height: 96px;
margin-bottom: 10px;
overflow: hidden;
width: 100%;
}
.b_feat_img img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="container">
<div class="b_feat_img">
<img src="http://i.imgur.com/iEJWyXN.jpg">
</div>
<div class="b_feat_img">
<img src="http://i.imgur.com/qRkEJni.jpg">
</div>
</div>
* Note, at the time of writing, IE doesn't support it, be sure to see the support tables.