【问题标题】:Set height of an image same as a neighboring element CSS将图像的高度设置为与相邻元素 CSS 相同
【发布时间】:2015-03-07 07:30:57
【问题描述】:

我想知道如何将图像的高度设置为相邻元素的高度。所以,基本上我想要这样:

[div] [img]

除了 javascript,我看不到如何做到这一点。那么,不使用 js 可以只使用 CSS 吗?

谢谢

到目前为止的代码(没什么特别的):

<div style="text-align:right;">
                        <label for="file-upload">Choose file</label>
                        <img><!-- Updates dynamically using js-->
                        <input id="file-upload" type="file" name="photo"/>
                    <input type="submit" value="Upload Image" name="submit" />
                </div>

【问题讨论】:

  • 任何代码? ... 看看 flex box-model :)
  • 为什么不使用javascript?能否提供更多代码?
  • @Paul 因为它看起来。我们使用 css 进行样式设置,使用 js 进行行为。分离是易于维护应用程序和页面的关键。

标签: css image height element


【解决方案1】:

为了在 CSS 中设置相邻元素的样式,您可以使用相邻选择器(加号)。 如下:

label + img{height:300px}

这将在任何标签“之后”在您的代码中定位 img。

JSFiddle

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2021-05-17
    相关资源
    最近更新 更多