Paul
Paul

Reputation: 3368

How to get an appended element to be the height of an image

I am appending the div .soonOverlay to specific .smallCatalogBlock's. What I can't seem to figure out is how I get the height of soonOverlay to be only as tall as the img in smallCatalogBlock. Right now its height covers the entire container.

Does anyone see what I am doing wrong or how I can fix this?

$('.smallCatalogBlock').each(function() {
		if ($(this).data('availability') === 'No') {
			$(this).append('<div class="soonOverlay"><div class="soonOverlayInner"><div class="total-center"><p class="dGw">Coming Soon</p></div></div></div>');
			console.log("It should be working");
		}
	});
.smallCatalogWrap {
	width: 100%;
	height: auto;
	margin: 60px 0;
}
.smallCatalogBlock {
	width: 60%;
	height: auto;
	display: inline-block;
	vertical-align: top;
	margin: 20px auto;
	text-decoration: none;
}
.smallCatalogBlock img {
	width: 80%;
	height: auto;
	box-shadow: 10px 5px 5px rgba(0,0,0,.3);
	display: block;
	margin: 0px auto 15px auto;
}
.smallCatalogTitle {
	font-family: 'Nunito', sans-serif;
	color: #4d4d4d;
	font-size: 1.3rem;
	text-align: center;
	display: block;
	font-weight: 400;
}
.comingSoonSmall {
	position: relative;
}
.comingSoonSmall .soonOverlay {
	width: 80%;
	height: 100%;
	background: #b82222;
	opacity: .8;
	position: absolute;
	top: 0;
	margin: 0 10%;
}
.soonOverlayInner {
	position: relative;
	min-height: 350px;
}
.soonOverlayInner .dGw {
	font-weight: 600;
	text-transform: uppercase;
	font-size: 2.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="smallCatalogBlock comingSoonSmall" data-availability="No">
<img src="https://boygeniusreport.files.wordpress.com/2016/11/puppy-dog.jpg?quality=98&strip=all&w=782" alt="Fastening Technology Catalog">
  <span class="smallCatalogTitle">Technology</span>
  <div class="smallCatalogButtonWrap">
    <div class="catalogSmallCircle"></div>
  </div>
</div>

Upvotes: 0

Views: 56

Answers (1)

Yashwant Munda
Yashwant Munda

Reputation: 41

I have updated the script below.

$('.smallCatalogBlock').each(function() {
		if ($(this).data('availability') === 'No') {
			$(this).append('<div class="soonOverlay"><div class="soonOverlayInner"><div class="total-center"><p class="dGw">Coming Soon</p></div></div></div>');
			console.log("It should be working");
      var img_w = $('.smallCatalogBlock > img').width();
      var img_h = $('.smallCatalogBlock > img').height();
      $('.soonOverlay').width(img_w).height(img_h);
		}
    
	});
.smallCatalogWrap {
	width: 100%;
	height: auto;
	margin: 60px 0;
}
.smallCatalogBlock {
	width: 60%;
	height: auto;
	display: inline-block;
	vertical-align: top;
	margin: 20px auto;
	text-decoration: none;
}
.smallCatalogBlock img {
	width: 80%;
	height: auto;
	box-shadow: 10px 5px 5px rgba(0,0,0,.3);
	display: block;
	margin: 0px auto 15px auto;
}
.smallCatalogTitle {
	font-family: 'Nunito', sans-serif;
	color: #4d4d4d;
	font-size: 1.3rem;
	text-align: center;
	display: block;
	font-weight: 400;
}
.comingSoonSmall {
	position: relative;
}
.comingSoonSmall .soonOverlay {
	width: 80%;
	height: 100%;
	background: #b82222;
	opacity: .8;
	position: absolute;
	top: 0;
	margin: 0 10%;
}
.soonOverlayInner {
	position: relative;
	min-height: 350px;
}
.soonOverlayInner .dGw {
	font-weight: 600;
	text-transform: uppercase;
	font-size: 2.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<div class="smallCatalogBlock comingSoonSmall" data-availability="No"> 
<img src="https://boygeniusreport.files.wordpress.com/2016/11/puppy-dog.jpg?quality=98&strip=all&w=782" alt="Fastening Technology Catalog"> 
<span class="smallCatalogTitle">Technology</span> 
<div class="smallCatalogButtonWrap"> 
<div class="catalogSmallCircle"></div> 
</div> 
</div>

Upvotes: 1

Related Questions