html - 在固定宽度的 div 中居中动态宽度的 div

标签 html css position positioning

我对 css 有相当多的经验,甚至这个问题也难倒了我。

我正在为 Wordpress 的 NextGen gallery 插件设置主题,这意味着我无法真正控制 HTML,而且我在尝试排列图像时遇到了问题。

在 NextGen 中,图像的最大尺寸为 200 像素 x 200 像素。这些图像是较大图像的缩略图,每个图像都有自己的大小和尺寸限制,但都在 200 像素 x 200 像素以下

主要目标:
类图像的宽度是固定的,但其宽度会根据其中的图像而变化。类 imageBox 围绕着图像并将其宽度固定为图像的最大宽度(200px 宽,200px 高)。对于宽度不是 200 像素的图像,我希望它们在 imageBox 的中心排列。

NextGen Gallery 生成的基本 HTML:

<div class="imageBox">
   <div class="image">
      <img />
   </div>
</div>

到目前为止我的 CSS:

.imageBox{
    width: 218px;
    height: 218px;
    float: left;
    position: relative;
    margin-top: 20px;
    margin-bottom: 20px;
    text-align: center;
}

.image{
    margin-right: 5px;
    text-align: center;
    position: absolute;
    bottom: 0px;
    display: inline-block;
}

我使用绝对定位来确保图像都沿着它们的底部边缘排列,因此 bottom:0px。

任何帮助都会很棒。

编辑 搞乱了 CSS,以错误的方式设置了类。

最佳答案

试试这个:

.image{
    width: 218px;
    height: 218px;
    margin: 0 auto;
    position: relative;
    margin-top: 20px;
    margin-bottom: 20px;
    text-align: center;
 }

关于html - 在固定宽度的 div 中居中动态宽度的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6808477/

相关文章:

html - 如何解决 IE 中的渲染性能问题

javascript - 在不修改属性的情况下设置隐藏的输入值

javascript - Internet Explorer 中的工具提示问题

css - 是否可以在 html5 页面中使底部边框自动调整大小(根据最后一行的文本宽度)?

css - 我怎样才能做我的背景图片响应

css - 位置:Chrome 与 Safari 中的粘性

java - 如何将选定的值从 <select> 传递到 java 代码

javascript - 为什么我无法访问 AngularJS Controller 的子元素?

java - 可变大小块的高效定位

python - pygame 检测绘制的圆形 Sprite 是否被单击失败