html - 将一个 div 中的图像与其他 div 对齐

标签 html css

我有两个 div 的 A 和 B。div A 的宽度是自动的,div B 的宽度设置为一个特定的值,该值小于浏览器窗口的宽度。 div B 也位于浏览器窗口的中间。

在 div A 中,我有一张图片。我想要的是图像始终与 div B 对齐,即使在我调整浏览器窗口大小时也是如此。

enter image description here

#header {
  background-color: #761D1F;
  height: 150px;
  width: auto;
  margin: -8px;
  margin-top: 20px;
}
#div-body {
  width: 1100px;
  height: 1000px;
  margin: 0 auto;
  margin-top: 50px;
}
#img {
  margin-top: 25px;
  height: 100px;
  width: 100px;
  margin-left: 48px;
}
<div id="header">
  <img id="img" src="http://lorempixel.com/100/100/">
</div>

<div id="div-body">
  ...
</div>

最佳答案

您可以将 img 包装在另一个 div 中,并将相同的 CSS 应用于 div B 和包装器。

#header {
  background-color: #761D1F;
  height: 150px;
  width: auto;
  margin: -8px;
  margin-top: 20px;
}
#div-body, #image-wrapper {
  width: 1100px;
  height: 1000px;
  margin: 0 auto;
  margin-top: 50px;
}
#img {
  margin-top: 25px;
  height: 100px;
  width: 100px;
}
<div id="header">
  <div id="image-wrapper">
    <img id="img" src="http://lorempixel.com/100/100/">
  </div>
</div>

<div id="div-body">
  ...
</div>

关于html - 将一个 div 中的图像与其他 div 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863251/

相关文章:

javascript - 滚动列表不工作

html - 如何在 Jade 中仅将背景图像添加到一个页面

css - 尽管文件完全相同,但 Laravel 开发的网站在 localhost 上看起来与在 webhost 上看起来不同

javascript - 在materializecss中,如何从右侧而不是默认左侧打开滑出菜单?

jquery - 使用 JQuery 将 XML 图像路径转换为图像

javascript - 如何创建 "floating"框?

javascript - 使用 div 单元格高度问题的表(带 Angular )

javascript - 为什么 jquery 不是 :not() selector working as I expect it to?

css - 如何更改uploadify的进度条样式

javascript - 试图在 javascript 中更改光标