13

I have:

<div style="height:15px">
    <img src="image" />
</div>

The image is bigger than 15px, so it's outside the div when you see it. How do I "crop" the image (show only the 15px port of it), only using css?

7 Answers7

21

You need overflow:hidden see an example here:

http://www.jsfiddle.net/S8qAq/

Read about overflow: here W3Schools

Good luck!

ghitesh
  • 160
  • 2
  • 14
Trufa
  • 39,971
  • 43
  • 126
  • 190
6

Try giving an overflow:hidden to the div.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
3

add overflow:hidden; to your div style.

wajiw
  • 12,239
  • 17
  • 54
  • 73
2

Use the overflow css property:

overflow: hidden;
RabidFire
  • 6,280
  • 1
  • 28
  • 24
2

I am surprised no one has suggested object-fit: cover;

RBT
  • 24,161
  • 21
  • 159
  • 240
Javaish
  • 179
  • 14
1

use

overflow:hidden;
Breezer
  • 10,410
  • 6
  • 29
  • 50
0
  1. overlow: hidden, object-fit: cover; and width: fit-content they come with their own hurdles. If you have more than one image, then these methods may not provide you with the best solution.

  2. Instead, you can define parent element size and force children to fit inside accordingly with max-height and max-width

<div style="height:15px; width: 15px;">
    <img src="image" style="max-height:100%; max-width: 100%;">
</div>
Abdulhakim
  • 620
  • 8
  • 11