简体   繁体   English

设置多个div大小的随机图像

[英]set random images in multiple div sizes

I have a gallery grid that has four div containers. 我有一个包含四个div容器的画廊网格。 I want to enclose whatever image inside the divs, keeping in mind that each div box has a different height and width. 我想在div内包含任何图像,请记住每个div框的高度和宽度都不同。 The should be centered in the div leaving reasonable padding on all sides. 的位置应在div的中心,并在所有面上都留有合理的填充。 Please check the link to get a better understanding JsFiddle http://d.alistapart.com/fluid-images/3-4.png 请检查链接以更好地了解JsFiddle http://d.alistapart.com/fluid-images/3-4.png

Any help is highly appreciated. 非常感谢您的帮助。 enter code here 在这里输入代码

 <div id="body">
  <div class="container">
<img class="magic" src="http://photos.smugmug.com/photos/i-2wtTsHn/0/M/JLF_3559-M.jpg">          
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div class="container h2">
    <img class="magic" src="http://photos.smugmug.com/photos/i-2wtTsHn/0/M/JLF_3559-M.jpg">
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div class="container h3">
    <img class="magic" src="http://photos.smugmug.com/photos/i-2wtTsHn/0/M/JLF_3559-M.jpg">
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div class="container w2">
    <img class="magic" src="http://photos.smugmug.com/photos/i-2wtTsHn/0/M/JLF_3559-M.jpg">
</div>

 .container {
  height:100px;
   width: 100px;
  overflow: hidden;
   position: relative;
  margin-bottom: 10px;
  float: left;
  background: #CCC;
   border-radius: 5px;
  cursor: pointer;
   box-shadow: 4px 0 2px -2px rgba(0, 0, 0, 0.4);
     }
  .magic {
   max-width:90%;
   height:70%;
   }
   .h2 {
  height: 210px;
   width: 220px;
    }
  #body {
    margin: 10px;
   }
 .h3 {
  width: 340px;
  height: 210px;
  } 
  .w2 {
    width: 220px;
   }

I'm not sure that i understood your problem... if you use 我不确定我是否理解您的问题...如果您使用

.magic {
    max-width:100%;
    max-height:100%;
}

with big pictures what is the problem ? 看大图有什么问题?

you want them to fill the entire div AND respecting theire size ratio ? 您希望他们填补整个div并尊重他们的规模比例吗?

I recommend adding these images as a background-image property to a block-level div, such as: 我建议将这些图像作为背景图像属性添加到块级div中,例如:

<div class="container">
    <div class="magic" style="background-image: url(SOURCEHERE.JPG)"></div>
</div>

With additional CSS like: 与其他CSS类似:

.container{
    width: 100px; /* or some other width */
    height: 100px; /* or some other height */
    padding:20px; /* this will add a little margin around your image */
    box-sizing:border-box; /* padding will be calculated with the container dimensions */
}

.magic{
    width:100%;
    height:100%;
    background-size:contain;
    background-position:center center;
    background-repeat:no-repeat;
    box-sizing:border-box;
}

jsfiddle: http://jsfiddle.net/PDL2U/ jsfiddle: http : //jsfiddle.net/PDL2U/

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

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