简体   繁体   English

如何在盒子的中心插入不同尺寸的图像?

[英]How can i insert different size image in the center of the box?

How can i insert different size image in box centered? 如何在框中心插入不同尺寸的图像? Image has a different size . 图像具有不同的大小。 portrait (height is bigger than width), landscape (width is bigger than height), square (width is same as a height) and The red box has a convertible size.(not only square) 肖像(高度大于宽度),横向(宽度大于高度),正方形(宽度与高度相同)和红色框具有可转换尺寸。(不仅仅是正方形)

红色框是可见部分。图像具有不同的大小,如肖像,风景,正方形

I use a code like this 我使用这样的代码

HTML HTML

<div class="image">
  <img class="image_insert" src="..">
</div>

CSS CSS

.image{
    position:relative;
    width:100px;
    height:100px;
    overflow:hidden;
}

.image_insert{
    max-width:100%;
    position:absolute;
    margin:auto;
    left:50%;
    -webkit-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform:translateX(-50%);
    top:0;
    bottom:0;
  }

JavaScript JavaScript的

function imageControl(){
 var image = document.getElementsByClassName('image_insert');
 var array = new Array();

 for(var i=0; i<image.length; i++){
  if(image[i].width>image[i].height){
    $(image[i]).css("max-width","none");
    $(image[i]).css("height","100%");
    }
  }
}

It worked. 有效。 But this way depends on document(image,javascript) loading time, so there are any best way to insert different size image in the center of the box? 但这种方式取决于文档(图像,javascript)加载时间,所以有什么最好的方法在框的中心插入不同大小的图像? *There are no empty space exists inner Red box! *内部红盒子里面没有空的空间!

Try this. 试试这个。

 .image { position: relative; display: inline-block; } .img-rectangle { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; border: 5px solid #BC2424;; width: 100px; height: 150px; } 
 <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <div class="image"><img src="http://lorempixel.com/300/400/" /><div class="img-rectangle"></div></div> <div class="image"><img src="http://lorempixel.com/400/300/" /><div class="img-rectangle"></div></div> <div class="image"><img src="http://lorempixel.com/400/400/" /><div class="img-rectangle"></div></div> 

What you want is to use the <picture> HTML element tag , with sources appropriate to the media queries for the various situations (eg landscape vs portait, screen resolution/DPIs, etc). 你想要的是使用<picture> HTML元素标签 ,其中包含适用于各种情况的媒体查询的资源(例如,横向与肖像,屏幕分辨率/ DPI等)。 The browser will then load the appropriate image automatically. 然后,浏览器将自动加载适当的图像。

This blog post should give some additional context on how they are used. 这篇博文应该提供一些关于它们如何使用的附加背景。

use inline-flex box to align the images, I have created this fiddle to show the usage https://jsfiddle.net/swvdfzax/ 使用inline-flex框来对齐图像,我创建了这个小提琴来显示用法https://jsfiddle.net/swvdfzax/

the CSS will be CSS将是

div.image{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  border:2px solid red;
  height:60px;
  width:60px;
  vertical-align:top
}
div.image img{
  max-height:100%;
  max-width:100%;
}

and the HTML will be 而HTML将是

<div class="image"><img src="...path/to/image"></div>

Try this code 试试这个代码

 $(function(){ $(".image_insert").each(function(){ var container = $(this).parent(); var img = $(this); img.css({ left: -1 * (img.width()/2 - container.width()/2), top: -1 * (img.height()/2 - container.height()/2) }); }); }); 
 .image{ position:relative; width:200px; height:200px; overflow:hidden; border: 1px solid red; } .image_insert{ position:absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="image"> <img class="image_insert" src="http://lorempixel.com/300/400/"> </div> <div class="image"> <img class="image_insert" src="http://lorempixel.com/400/300/"> </div> <div class="image"> <img class="image_insert" src="http://lorempixel.com/400/400/"> </div> 

JSFiddel demo here https://jsfiddle.net/g868ae1h/1 这里的JSFiddel演示https://jsfiddle.net/g868ae1h/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM