【问题标题】:Can I dynamically resize an element's contents based on its own size?我可以根据自己的大小动态调整元素内容的大小吗?
【发布时间】:2013-03-14 08:01:44
【问题描述】:

我已经非常熟练地使用媒体查询来动态更改 DOM 元素。但是,据我所知,这些只能检测窗口/设备宽度/高度,而不是单个 DOM 元素。我希望能够制作一个 CSS 来根据元素的大小重新排列和调整元素内容的大小。例如:

╒══════════════════════════════╕
│Browser Window                │
├──────────────────────────────┤
│ ┌──────────────────────────┐ │
│ │.resizingParent           │ │
│ │┌──────┐ Some caption text│ │
│ ││ IMG  │ directly relating│ │
│ ││      │ to the image.    │ │
│ │└──────┘                  │ │
│ └──────────────────────────┘ │
│                              │
└──────────────────────────────┘
╒══════════════════════════════╕
│Browser Window                │
├──────────────────────────────┤
│ ┌───────────────┐            │
│ │.resizingParent│            │
│ │┌─────────────┐│            │
│ ││             ││            │
│ ││  I  M  G    ││            │
│ ││             ││            │
│ ││             ││            │
│ │└─────────────┘│            │
│ │Some caption   │            │
│ │directly       │            │
│ │relating to the│            │
│ │image.         │            │
│ └───────────────┘            │
│                              │
└──────────────────────────────┘

我希望解决方案像使用媒体查询一样简单,例如下面的(无效)代码:

<STYLE>
.resizingParent>IMG {
    display: inline-block;
    width: 25%;
}

.resizingParent(max-width:10em)>IMG {
    display: block;
    width: 100%;
}
</STYLE>

<DIV CLASS=".resizingParent">
    .resizingParent
    <IMG SRC="IMG.png" ALT="IMG"/>
    Some caption text directly relating to the image
</DIV>

【问题讨论】:

    标签: html css media-queries


    【解决方案1】:

    很遗憾,媒体查询并非如此。 CSS 中有许多自然响应的选项(即,它们不需要在视口改变大小时重新格式化媒体查询),具体取决于您需要完成的工作类型。对于您的特定问题,Flexbox 是最合适的。

    http://codepen.io/cimmanon/pen/Azone

    figure {
      display: -webkit-flexbox;
      display: -ms-flexbox;
      display: -webkit-flex;
      -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
      flex-wrap: wrap;
      border: 1px solid;
    }
    @supports (flex-wrap: wrap) {
      figure {
        display: flex;
      }
    }
    
    figure div {
      -webkit-flex: 1 1;
      -ms-flex: 1 1;
      flex: 1 1;
      margin: 1em;
      text-align: center;
    }
    
    figure figcaption {
      margin: 1em;
      -webkit-flex: 10 1 20em;
      -ms-flex: 10 1 20em;
      flex: 10 1 20em;
    }
    

    HTML:

    <figure>
      <div><!-- this div is mostly here to prevent our image from getting distorted -->
        <img src="http://placehold.it/200x200" />
      </div>
    
      <figcaption>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porta elit vel ante hendrerit facilisis. Curabitur aliquam sollicitudin diam, id posuere elit consectetur nec. Vestibulum quam dolor, feugiat in posuere a, posuere imperdiet tellus. Mauris sed ligula vitae urna consequat aliquet. Curabitur pellentesque consectetur ornare. Pellentesque vel euismod odio. Nulla tempus pharetra nulla. In justo dolor, sollicitudin nec commodo sit amet, adipiscing eget lectus. Phasellus velit enim, tempus et lobortis eget, rhoncus nec dolor.</figcaption>
    </figure>
    

    因为我们需要我们的 flex 项目来换行,所以目前浏览器支持仅限于 Chrome、Opera 和 IE10。 http://caniuse.com/#feat=flexbox

    【讨论】:

    • 我在这里收集了各种自然响应的演示:codepen.io/collection/LfoGq
    • 最大的问题是图像不能按照我需要的方式改变大小......无论如何,谢谢。
    • 我已更新演示以删除 div 以便图像调整大小。
    • 更接近了,但现在它会扭曲,并且在调整较小时会溢出。
    • 看起来它在实现上有所不同。我使用 Opera 进行测试,图像在调整大小时保持正确的纵横比。
    猜你喜欢
    • 2011-04-04
    • 2017-02-11
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多