【问题标题】:How to make parent div width shrink to wrap exactly the child imgs?如何使父 div 宽度缩小以完全包裹子 imgs?
【发布时间】:2014-04-12 05:59:26
【问题描述】:

Jsfiddle 在这里:http://jsfiddle.net/zxJbV/

<div class="container" style="border:1px solid black;text-align:left">
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
    <img src="" style="width:60px;height:60px;" />
</div>

可能有很多 和换行符。父 div 总是展开到 100% 的宽度,并且不能完全包裹图像。
我现在得到的结果(右边有间距):
我想要的结果是:

【问题讨论】:

  • 我不明白。你的小提琴完全符合你的要求……对吗?

标签: html css center


【解决方案1】:

单独使用 CSS 并不容易。您必须编写一堆媒体查询以适应容器的每个宽度 - 取决于容器中有多少项目/列。

话虽如此,我曾经写过一些 LESS 代码来生成媒体查询。

FIDDLECODEPEN (Supports LESS)

以下是利用 LESS 设置媒体查询的方法:

像这样设置一个迭代混合:(您可以将此代码粘贴到http://less2css.org

@item-width:100px;
@item-height:100px;
@margin: 5px;
@min-cols:2;
@max-cols:12; //set an upper limit of how may columns you want to write the media queries for


.loopingClass (@index-width) when (@index-width <= @item-width * @max-cols) {
    @media (min-width:@index-width) {
        #content{
            width: @index-width;
        }
    }

    .loopingClass(@index-width + @item-width);
}

.loopingClass (@item-width * @min-cols);

上面的mixin会以表格形式吐出一系列媒体查询:

@media (min-width: 200px) {
  #content {
    width: 200px;
  }
}
@media (min-width: 300px) {
  #content {
    width: 300px;
  }
}
@media (min-width: 400px) {
  #content {
    width: 400px;
  }
}
...
@media (min-width: 1200px) {
  #content {
    width: 1200px;
  }
}

所以用一个简单的标记,比如:

<ul id="content">
    <li class="box"></li>
    <li class="box"></li>
    ...
    <li class="box"></li>
</ul>

剩余的 CSS (LESS):

 #content {
    margin:0 auto;
    overflow: auto;
    min-width: @min-cols * @item-width;
    max-width: @max-cols * @item-width;
    display: block;
    list-style:none;
    background: aqua;
}
.box {
    float: left;
    height: @item-height - 2 *@margin;
    width: @item-width - 2*@margin;
    margin:@margin;
    background-color:blue;
}

... 你会得到想要的结果。

...自定义布局非常容易:

我需要做的就是根据我的需要更改我在 LESS 混合中使用的变量 - 我得到了我想要的确切布局。

假设我有 300px X 100px 的项目,最少 2 列,最多 6 列,边距为 15px - 我只是像这样修改变量:

@item-width:300px;
@item-height:100px;
@margin: 15px;
@min-cols:2;
@max-cols:6;

...瞧,我明白了CODEPEN

【讨论】:

  • 结果看起来和我想要的完全一样。我试试看,谢谢。
【解决方案2】:

我理解你的问题,试试这个代码

<div class="container" style="border:1px solid black;width:70px;padding:5px;">
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
<img src="" style="width:60px;height:60px;" />
</div>

上面的代码每行包含 1 个图像,因为我已将宽度设置为 70px,根据您想要每行的图像数量,请设置宽度,例如每行 3 个图像(3x60px = 180px + 10px 用于填充)。希望它有效。祝你有美好的一天。

【讨论】:

  • 我不想为容器设置固定宽度。
【解决方案3】:

一点 jQuery 会帮助你:

Javascript:

var IMG_WIDTH = 60; // Define your minimum image width here.
$(function() {
    $(window).resize(function() {
        // $('#debug').html($('.container').width());
        var width = $('.container').width();
        if (width % IMG_WIDTH != 0) {
            $('.container img').css({
                width: width / Math.floor(width / 60),
                height: width / Math.floor(width / 60)
            });
        }
    }).resize();
});

CSS:

img {
    float: left;
    margin: 0;
}

.

JSFiddle Demo

非jQuery解决方案

var IMG_WIDTH = 60;
window.onresize = function() {
    var width = document.getElementById('container').offsetWidth;
    if (width % IMG_WIDTH != 0) {
        var imgElements = document.querySelectorAll('#container img');
        Array.prototype.forEach.call(imgElements, function(el, i){
            el.style.width = width / Math.floor(width / 60) + 'px';
            el.style.height = width / Math.floor(width / 60) + 'px';
        });
    }
};
window.onresize();

JSFiddle Demo no jQuery

【讨论】:

    【解决方案4】:

    您可以使用 flexbox 轻松做到这一点,而不会受到太多影响。代码将如下所示:

    index.html

    <div class="container" style="border:1px solid black;text-align:left">
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
        <img src="" style="width:60px;height:60px;" />
    </div>
    

    main.css

    .container {
      display: flex;
      flex-flow: row wrap;
    }
    .container img {
      flex: 1; 
    }
    

    工作笔:https://codepen.io/pen/?editors=0100

    希望有帮助:)

    【讨论】:

      【解决方案5】:

      .main {
        border: 10px solid red;
        padding:10px;
        display: flex;
        flex-flow: wrap;
        background-color:yellow!important;
      }
      
       .main div {
       
        flex-grow: 1;
        border: 1px solid black;
      }
      
      .main div.nogrow{
        flex-grow: 0;
        }
      <div class="main" >
        <div class="nogrow" style="background-color:coral;">dsfddsfdfA</div>
        <div class="nogrow" style="background-color:lightblue;">dfdfdfdB</div>
        <div class="nogrow" style="background-color:khaki;">dfdfdf d fd fdC</div>
        <div class="nogrow" style="background-color:pink;">Ddssd sdf sdf</div>
        <div class="nogrow" style="background-color:lightgrey;"> df df df ddf dfE</div>
        <div class="nogrow" style="background-color:lightgreen;"> dfd fd df dfdF</div>
      </div>

      @TrungDQ 感谢您的支持。我有类似的问题,但宽度是动态的,并且不希望右侧有空格。你能看一下吗? https://jsfiddle.net/phyle5/07pwdy12/请调整大小看看。

      【讨论】:

        【解决方案6】:

        首先,规范化你的 css。

        其次,display: inline-block; 在图像上,display: block; 在容器上。

        http://jsfiddle.net/JakobJingleheimer/zxJbV/7/

        【讨论】:

        • 我没有看到任何差异...我的问题是右侧总是有间距,因为图像会出现换行符...
        • 我的小提琴看起来与您发布的具有所需结果的图像完全一样。但是,要删除图像之间的空格,您有几个选项:manually remove themdo it with javascript
        • 我不认为你的小提琴看起来和我发布的图片完全一样。如果调整结果区域的大小,可以看到右边框和图像之间有空白区域。
        • 对于图像之间的空间,实际上有一个技巧可以做到这一点。只需添加“字体大小:0px;”用于容器。
        猜你喜欢
        • 2017-08-05
        • 1970-01-01
        • 2016-03-02
        • 1970-01-01
        • 2021-02-09
        • 2012-12-13
        • 1970-01-01
        • 2013-07-19
        • 1970-01-01
        相关资源
        最近更新 更多