How to show middle of image in div using css?
https://jsfiddle.net/yn7hubkd/
CSS
.out{ width: 500px; height: 500px; overflow: hidden; }
HTML:
<div class="out"> <img src="https://redditupvoted.files.wordpress.com/2016/03/waffles-cat.jpg"> </div>
I get this output: http://i.imgur.com/gYzP1xo.png
But I want this to happen (centered in the x-direction, the black square is the image and the red square is the div class): http://i.imgur.com/9QGVYtN.png
Output Result: http://i.imgur.com/EqzM7QO.png
This behavior can be achieved using the image as a background. Just set background-size: cover;and background-position: center;to fill the container with the image and place it in the center:
background-size: cover;
background-position: center;
.out { width: 500px; height: 500px; overflow: hidden; background-image: url(https://redditupvoted.files.wordpress.com/2016/03/waffles-cat.jpg); background-size: cover; background-position: center; }
<div class="out"></div>
<img />, 50%:
<img />
.out { width: 500px; height: 500px; overflow: hidden; } .out img { margin-left: -50%; }
CSS:
.out img { position: relative; left: 50%; transform: translateX(-50%); }
https://jsfiddle.net/456jLdfx/1/
50% 50% , .
: , margin-left: -50%;, , , : 500 , 100 )
margin-left: -50%;
.out { width: 500px; height: 500px; overflow: hidden; background: url('https://redditupvoted.files.wordpress.com/2016/03/waffles-cat.jpg') center center; }
, .
.out img{ margin: 0 auto; max-width: 90%; display: block; }
: margin:0 auto; 0 ( .)
margin:0 auto;
Source: https://habr.com/ru/post/1657258/More articles:How can I reuse the same controller class using different constructor arguments - c #SASS больше не компилирует (смотрит) при использовании функции после обновления Ionic - compilationJoin tables based on column - sqlsqlalchemy existing database query - pythonMySQL - How to select "DISTINCT" overlap periods (dates or number ranges) - sqlNetBeans IDE - неверная спецификация jdkhome - javaJUnit assertThat: check that Object is String - javaRedmine / puma crashed using the undefined `stop 'method for nil: NilClass (NoMethodError) - ruby | fooobar.comWhy is Predicate.isEqual implemented as it is? - javaСпециализация участника для шаблонного класса для групп типов? - c++All Articles