1

I am trying to create a Card with just html and CSS and for now the card seems okay but in the text section of the card, I see some extra white space just above the text. Can someone please see as to why i might be getting that?

Here is a link to a working codepen.

h1 {
  text-align: center;
}
.container {
  margin: auto;
  border: solid;
  width: 300px;
  text-align: center;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}

.card-text {
  background-color: lightgreen;
  padding: 0;
  margin: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Let's Create a Card</title>
</head>

<body>
  <h1>Card</h1>
  <div class="container">
    <img src="https://i.pinimg.com/originals/ca/76/0b/ca760b70976b52578da88e06973af542.jpg" alt="My-image" height="300px" width="300px">
    <p class="card-text">Some Text that goes for the card Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed ipsa cum illo accusamus aperiam nulla quam. quae?</p>
  </div>
</body>

</html>

Thank you for the assistance.

TreeHuggerRick
  • 402
  • 2
  • 5
  • 18

1 Answers1

2

Your options are to make the <img>:

  • display: block or
  • vertical-align: bottom

<img>: The Image Embed element - Styling with CSS

<img> is a replaced element; it has a display value of inline by default...
<img> has no baseline, so when images are used in an inline formatting context with vertical-align: baseline, the bottom of the image will be placed on the text baseline.

So depending on the font, font-size and line-height styles inherited from the parent element, some space will be reserved for ascenders and descenders of possibly adjacent text.

You can read more about this here.

display: block;

.container {
  border: solid;
  width: 300px;
}

.card-text {
  background-color: lightgreen;
  margin: 0;
}

img {
  display: block;
}
<div class="container">
  <img src="https://i.pinimg.com/originals/ca/76/0b/ca760b70976b52578da88e06973af542.jpg" alt="My-image" height="300px" width="300px">
  <p class="card-text">Some Text that goes for the card Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed ipsa cum illo accusamus aperiam nulla quam. quae?</p>
</div>

vertical-align: bottom;

.container {
  border: solid;
  width: 300px;
}

.card-text {
  background-color: lightgreen;
  margin: 0;
}

img {
  vertical-align: bottom;
}
<div class="container">
  <img src="https://i.pinimg.com/originals/ca/76/0b/ca760b70976b52578da88e06973af542.jpg" alt="My-image" height="300px" width="300px">
  <p class="card-text">Some Text that goes for the card Lorem ipsum dolor sit amet consectetur adipisicing elit. Sed ipsa cum illo accusamus aperiam nulla quam. quae?</p>
</div>
ksav
  • 20,015
  • 6
  • 46
  • 66