简体   繁体   English

继承图片的宽度(css)

[英]inherit width of image (css )

I'm trying to make a screen that 80% of it is filled with an image and the bottom 20% is a div. 我正在尝试制作一个屏幕,其中80%的屏幕充满图像,而底部20%的屏幕为div。 The image style is set so it will scale it's width proportionally to it's height. 设置了图像样式,以便将其宽度与高度成比例地缩放。 The height should fill the 80% div. 高度应填充80%格。 My problem is that I want the bottom 20% div to inherit the automatically scaled width of the image. 我的问题是我希望底部20%的div继承图像的自动缩放宽度。 Here is my css: 这是我的CSS:

<style type="text/css">
    #background {
width: 100%; 
height: 100%; 
position: relative; 
overflow:hidden;
left: 0px; 
top: 0px; 
z-index: -1; /* Ensure div tag stays behind content; -999 might work, too. */
}

.stretch {
max-width:100%;
height:80%;   
}
</style>

And here is the HTML: 这是HTML:

<div id="background" >
  <img id="status_bg" src="online_pend_off.jpg" class="stretch">
</div>
<div id="twenty_per" style="background-color:blue">
</div>

Any idea how can I make the second div (twenty_per) have the same width as status_bg image? 知道如何使第二个div(twenty_per)具有与status_bg图片相同的宽度吗? Thanks 谢谢

Wrap the HTML inside another div with no default width (to follow #background s width), and #twenty_per should have 100% width. 将HTML包裹在没有默认宽度的另一个div中(遵循#background的宽度),并且#twenty_per应该具有100%的宽度。 This way, #twenty_per will automatically have the same width as #background . 这样, #twenty_per将自动具有与#background相同的宽度。

CSS 的CSS

.wrapper {
    display: inline-block;
}

#twenty_per {
    width: 100%;
}

HTML 的HTML

<div class="wrapper">
    <div id="background" >
        <img id="status_bg" src="online_pend_off.jpg" class="stretch">
    </div>
    <div id="twenty_per" style="background-color:blue"></div>
</div>

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

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