简体   繁体   English

根据图片大小调整div大小

[英]Resize div according to image size

I am new to CSS and HTML and got the following problem. 我是CSS和HTML的新手,遇到以下问题。 I created a banner that holds an image and resizes accordingly to the screen size. 我创建了一个横幅,其中包含图像,并根据屏幕尺寸进行相应调整。 Now, I want my content underneath to resize according to the image. 现在,我要根据图像调整下面的内容大小。 The content in this case is a table. 在这种情况下,内容为表格。 When I decrease the screen size, the table moves up(padding decreases) and when I increase the screen size, the content moves down. 当我减小屏幕尺寸时,表格向上移动(填充减小),而当我增大屏幕尺寸时,内容向下移动。 This is probably because the image size changes. 这可能是因为图像尺寸改变了。 Do you know how I can resize the table accordingly? 您知道如何调整表格的大小吗?

Here is the HTML code 这是HTML代码

<div class= "resize">
    <%=link_to image_tag("header.png"), "#" %>
  </div>
<div class="table-container">
<h3>Latest Postings</h3>
<table class = "table table-striped table-bordered table-centered">
  <tr>
    <th class="description">Description</th>
    <th class="location">Location</th>
  </tr>
  <td>[description]</td>
  <td>[Location]</td>
</table>
</div>

And the CSS Code /* table */ 和CSS代码/ *表* /

.table-container {
  padding-top: 45%;
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 500px;
}

/*resize container */ / *调整容器大小* /

  .resize {
   position: absolute;
   left: 0;
   right: 0;
    margin-top: 80px;
     padding-bottom: 80px;
    background-size: 100%;

}

/* resize images */ / *调整图片大小* /

.resize img {
    width: 100%;
    height: 100%;
}

Thank you 谢谢

The table is moving up and down because the padding is a percentage, and the image shrinks as it resizes. 表格正在上下移动,因为填充是百分比,并且图像在调整大小时会缩小。 This is natural to see the content move down as the browser resizes. 很自然地看到内容随着浏览器调整大小而下降。

Your image does not need to be absolute positioned, and will create space for itself. 您的图像不需要绝对定位,并且会为其自身创造空间。 Does this help? 这有帮助吗? See my code here: 在这里查看我的代码:

http://jsfiddle.net/38Gba/ http://jsfiddle.net/38Gba/

.table-container {
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 500px;
}
.resize {
    margin-top: 80px;
    padding-bottom: 80px;

}
.resize img {
    width: 100%;
    height: 100%;
}

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

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