css - 我不知道为什么我的图片没有 float 到中心

标签 css html

我需要我的图像在中心,但出于某种原因不是这样。
我的 HTML 代码:

<div class="face-img">
    <img id="profile-img" src="http://host2post.com/server13/photos/jI7X5kXAIEk_4M~/1680x1050-lake-tahoe-california-desktop-wallpaper-background.jpg" />
</div>

还有我的 CSS 代码:

.face-img{
  display: inline-block;
}
#profile-img{
  height: 200px;
  width: 200px;
  border-radius: 50%;
}
.face-img #profile-img{
    float: center;

http://jsfiddle.net/BBaughn/e941djLg/

最佳答案

作为MDN states , text-align 属性需要在父元素上:

The text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.

由于 img 元素默认是 inlinetext-align: center 将有效地将子 img 居中> 父元素中的元素。

Updated Example

在你的情况下,你只需要删除 display: inline-block 因为这会导致父级有一个 "shrink to fit" width (这意味着父元素与子元素的宽度相同,因此看不到居中)。

.face-img {
  text-align: center;
}

此外,center 不是 float property 的有效值.

关于css - 我不知道为什么我的图片没有 float 到中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29415684/

相关文章:

html - 仅在由 flex-wrap :wrap 创建的 'second row' 中对齐 flex 元素

html - 格式化和显示 <hr> 时遇到问题

css - 如何在 chrome 自动完成上删除蓝色背景

html - 如何将这个 4 列表转换为单列表,但仍保持表的可访问性

jquery - SVG animateTransform 不适用于 FF 和 IE

javascript - 向网站添加了一些代码,它工作正常但似乎无​​限滚动到页脚之外

javascript - 表单加载时如何显示加载器?

html - 背景大小 :contain on my hero image 遇到一些问题

html - 在 CSS 中操作 div SVG 背景 View 框以实现响应式设计

JavaScript 按索引搜索表格