简体   繁体   English

CSS div高度百分比不起作用

[英]CSS div height percent not working

So I have an img #anniversary-img inside a div #anniversary-img-container .所以我在 div #anniversary-img-container有一个 img #anniversary-img I simply want to have the img to resize automatically (with the max size being 15em by 15em ) as you resize the browser window (to accommodate smaller displays such as phones).我只是想让 img 在您调整浏览器窗口的大小时自动调整大小(最大大小为15em x 15em )(以适应较小的显示器,例如手机)。 I have tried many things but using a percent height doesn't affect it at all.我尝试了很多东西,但使用百分比高度根本不会影响它。

JSFiddle JSFiddle

HTML HTML

<div class ="col1-sec sec" id="col1-sec1">
  <div id="anniversary-container">
    <h2>Welcome</h2>
    <div class="img-container" id="anniversary-img-container">
      <img src="images/anniversary-img.jpg" width="862" height="840" alt="Celebrating 60 years of Catholic service" id="anniversary-img" />
    </div><!-- end #anniversary-img-container -->
    <div id="contact-container">
      <p>1450 W. La Venture Avenue<br />Las Vegas, NV 12345-0000</p>
      <p><span class="contact-text">Phone</span> 123-456-7890</p>
      <p><span class="contact-text">Fax</span> 123-456-7890</p>
      <p><span class="contact-text">Email</span> <a href="mailto:me@yahoo.com">me@yahoo.com</a></p>
    </div><!-- end #contact-container -->
  </div><!-- end #anniversary-container -->
</div><!-- end #col1-sec1 -->

CSS CSS

.sec, .img-container {
    border: 1px solid #D8D8D8;}
#col1-sec1 {
    height: 100%;
    overflow: auto;}
#anniversary-container {
    overflow: auto;
    width: 100%;}
#anniversary-img-container {
    height: 15em;
    width: 15em;
    margin-left: 2em;
    margin-bottom: 1em;
    float: left;} /* make container proportionate to img */
#anniversary-img {
    max-width: 100%;
    max-height: 100%;}
#contact-container {
    width: 40%;
    float: right;
    margin-right: 5em;
    margin-top: 2em;
    text-align: left;}
.contact-text {
    font-weight: bold;}

EDIT: Solved by removing height & width (of #anniversary-img) in HTML file and setting:编辑:通过删除 HTML 文件中的高度和宽度(#anniversary-img)并设置解决:

#anniversary-img-container {
    max-height: 15em;
    max-width: 15em;}

You could always use a background-image for the image-container, it gives you more control over the behaviour of the image.您始终可以为图像容器使用背景图像,它使您可以更好地控制图像的行为。 Like here http://jsfiddle.net/mpx6vmL4/2/像这里http://jsfiddle.net/mpx6vmL4/2/

#anniversary-img-container {
    height: 15em;
    width: 15em;
    margin-left: 2em;
    margin-bottom: 1em;
    float: left; /* make container proportionate to img */
    background: url(images/anniversary-img.jpg) no-repeat; /*center center maybe?*/
    background-size: cover; /*contain or custom value*/
} 
/* #anniversary-img {
    max-width: 100%;
    max-height: 100%;} */

Try to play with the background attributes.尝试使用背景属性。

As you have said, the image is nested inside a container:正如您所说,图像嵌套在容器中:

<div class="img-container" id="anniversary-img-container">
  <img src="images/anniversary-img.jpg" width="862" height="840" alt="Celebrating 60 years of Catholic service" id="anniversary-img" />
</div><!-- end #anniversary-img-container -->

So first of all, you should make sure, that the container has the width and height you want him to be (and afterwards although the image).因此,首先,您应该确保容器具有您希望他成为的widthheight (然后是图像)。 As I guess you want it to be responsive and stretch to the window screen, its a good practice to set its width to 100%:我猜你希望它能够响应并拉伸到窗口屏幕,将其宽度设置为 100% 是一个好习惯:

#anniversary-img-container {
  width: 100%;
  height: auto;
}

Afterwards, you want to make sure, that the image always fits the container.之后,您要确保图像始终适合容器。 We will use !important to override the width="862" you have set inside your html markup, and although set its max-width: 100% to make sure, it does not upscale higher then 862px:我们将使用!important来覆盖您在 html 标记中设置的width="862" ,尽管设置了它的max-width: 100%以确保它不会比 862px 更高:

#anniversary-img {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
}

Thats a basic build up.那是一个基本的建立。 Check the JS FIDDLE DEMO检查JS FIDDLE 演示

It would be a better practice to remove the width="" height="" inside of your html markup, so that you do not need to use !important inside your css file!删除 html 标记中的width="" height=""是一个更好的做法,这样您就不需要在 css 文件中使用!important

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

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