简体   繁体   English

调整浏览器窗口大小时如何防止表格和图像元素移动[REPOST]

[英]How to prevent the table and image elements from moving when resizing browser window [REPOST]

I previously posted a question and edited it but the edited version does not show the correct code so I'm reposting it. 我以前发布了一个问题并对其进行了编辑,但是编辑后的版本未显示正确的代码,因此我将其重新发布。 When I resize my browser window, the images seem to go out of the "box" or it's container. 当我调整浏览器窗口的大小时,图像似乎超出了“框”或它的容器。 I'm a beginner at coding and I know it's much better if I use div instead of table, but this is for practice purposes (and also, I need it for a school project and I figured that's how I want to use the table element). 我是编码的初学者,我知道如果使用div而不是table会更好,但这是出于实践目的(而且,我在学校项目中需要它,而且我认为这就是我想使用table元素的方式) )。 Anyways, please help me figure out what to do/ how to fix the code. 无论如何,请帮助我弄清楚该怎么做/如何修复该代码。

EDIT: Looks like this when I resize. 编辑:调整大小时看起来像这样

 .post { padding: 3em 3em 1em 3em; background: #ffffff; border: solid 1px rgba(160, 160, 160, 0.3); margin: 0 0 3em 0; position: relative; } .post>header { display: flex; border-bottom: solid 1px rgba(160, 160, 160, 0.3); left: -3em; margin: -3em 0 3em 0; position: relative; width: calc(100% + 6em); } .post>header .title { flex-grow: 1; flex: 1; padding: 3.75em 3em 3.3em 3em; } .post>header .title h2 { font-weight: 900; font-size: 1.5em; text-align: center; } .post>header .title> :last-child { margin-bottom: 0; } .image { width: 200px; height: 260px; } table { margin-left: auto; margin-right: auto; } td { padding: 10px; } .group { margin-left: auto; margin-right: auto; } 
 <article class="post"> <header> <div class="title"> <h2><a href="#">Favorite Books</a></h2> </div> </header> <table> <tr> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> </tr> </table> </article> 

Just update the image css 只需更新图像css

.image {
  display: block;
height: auto;
max-width: 100%;
}

You just need to add 您只需要添加

width: 700px;

To your post class: 到您的帖子课:

 .post { padding: 3em 3em 1em 3em; background: #ffffff; border: solid 1px rgba(160, 160, 160, 0.3); margin: 0 0 3em 0; position: relative; width: 700px; } .post>header { display: flex; border-bottom: solid 1px rgba(160, 160, 160, 0.3); left: -3em; margin: -3em 0 3em 0; position: relative; width: calc(100% + 6em); } .post>header .title { flex-grow: 1; flex: 1; padding: 3.75em 3em 3.3em 3em; } .post>header .title h2 { font-weight: 900; font-size: 1.5em; text-align: center; } .post>header .title> :last-child { margin-bottom: 0; } .image { width: 200px; height: 260px; } table { margin-left: auto; margin-right: auto; } td { padding: 10px; } .group { margin-left: auto; margin-right: auto; } 
 <article class="post"> <header> <div class="title"> <h2><a href="#">Favorite Books</a></h2> </div> </header> <table> <tr> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> <td> <a href="#" target="_blank"><img src="#" alt="#" class="image" /> </a> </td> </tr> </table> </article> 

As you already admit that the html structure is not good (better say should be avoided), still I am trying to introduce a different approach. 您已经承认html结构不好(最好避免使用),但我仍在尝试引入另一种方法。

css CSS

td{
 /*append this style along yours*/
 height:200px;
 width:200px;
 display:block;
}

html HTML

<td style="background-image: url('YOUR IMAGE URL');background-repeat: no-repeat;background-attachment: fixed;background-position: center;" onclick="javascript:window.location = 'YOUR HREF OF OLD A TAG'">
    &nbsp;
  </td>

YOUR IMAGE URL is your images' absolute urls YOUR HREF OF OLD A TAG the url's where the it should be redirected on click 您的图像URL是图像的绝对URL 您的旧标签的引用是单击后应将其重定向到的URL

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

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