Robotica
Robotica

Reputation: 33

Padding not working on my image with text overlay

What I am trying to achieve:

Images should be centered with equal padding left and right.

Text within images should be centered with equal padding left and right, top and down.

Top padding/margin is currently centering the text such that it becomes hidden if the screen is too small.

$(document).ready(function() {

  $('.gallery-item').hover(function() {
    $(this).find('.img-title').fadeIn(300);
  }, function() {
    $(this).find('.img-title').fadeOut(100);
  });

});
    .gallery
{
	width: 25em;
}
.gallery-item
{
	float: left;
	height: auto;
	max-width: -moz-available;
	position: relative;
	width: 100%;
}
.gallery-item img
{
	width: 100%;
}
.img-title
{
	background: #000;
	background: rgba(0,0,0,.9);
	height: 100%;
	margin: 0;
	max-width: -moz-available;
	position: absolute;
	text-align: center;
	top: 0;
	width: 100%;
}
.img-title p
{
	color: #fff;
	font-size: 22px;
	padding-left: 30px;
	padding-right: 30px;
	position: absolute;
	text-align: center;
	top: 33%;
}
<div class="wrapper clearfix">
  <figure class="gallery-item">
    <img src='https://ichef.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg' alt='image'>
    <figcaption class="img-title">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </figcaption>
  </figure>
    <figure class="gallery-item">
    <img src='https://ichef.bbci.co.uk/news/660/cpsprodpb/37B5/production/_89716241_thinkstockphotos-523060154.jpg' alt='image'>
    <figcaption class="img-title">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </figcaption>
  </figure>
</div>

<script
			  src="https://code.jquery.com/jquery-3.5.1.min.js"
			  integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
			  crossorigin="anonymous"></script>

I need responsive scaling across all devices. Any optimizations welcome.


EDIT: https://codepen.io/AnonymousCaptain/pen/WNQyPZG I have managed to create proper padding on the image and overlay by adding "max-width: -moz-available;" to the css of .img-title and .gallery-item.

I then added padding-left: 30px; padding-right: 30px;

to .img-title.

Only thing missing is vertical alignment of text.

Upvotes: 0

Views: 539

Answers (1)

lakshitha madushan
lakshitha madushan

Reputation: 669

You need to remove the default element padding and margins. add below lines top of the CSS style sheet.

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

And update .ima-title p

.img-title p {
    color: #fff;
    font-size: 22px;
    padding-left: 30px;
    padding-right: 30px;
    position: absolute;
    text-align: center;
    top: 50%;  /* *********** */
    transform: translateY(-50%); /* *********** */
}

with top: 50%; transform: translateY(-50%); we can alingn text vertically.

Upvotes: 2

Related Questions