【问题标题】:Why is margin: 0 auto centering text on image without width specified?为什么边距:0 在未指定宽度的图像上自动居中文本?
【发布时间】:2018-09-29 10:17:25
【问题描述】:

我在带有margin: 0 auto 的英雄图像上水平居中文本,但没有指定宽度。我在很多地方读到这个技巧只在指定宽度时才有效;为什么它在这种情况下有效?我的猜测是宽度只是设置为默认图像宽度。

这是我正在使用的代码(查看CodePen 以获取完整代码)。如果我从 .hero-image 的 CSS 规则集中删除除图像 URL 之外的所有内容,它仍然有效。

HTML:

<!-- Inside (Bootstrap 4) container-fluid div -->
    <div class="row">
      <div class="col">
        <div class="hero-image">
          <div class="hero-text text-white">
            <h1>Natalie Cardot</h1>
            <h5>Web developer</h5>
          </div>
        </div>
      </div>
    </div>

CSS:

.hero-image {
  background-image: url(https://image.ibb.co/fzz87S/laptop_reversed.jpg);
  /* Sets height to full height (100%) of viewport */
  height: 100vh;
  /* Ensures background image spans the full width of the viewport */
  background-size: cover;
  /* Aligns image to center of viewport */
  background-position: center;
  background-repeat: no-repeat;
  /* Enables flex layout */
  display: flex;
  /* Vertically aligns (align-items defines how the browser distributes space between and around flex items along cross-axis of container; works like justify-content but in the perpendicular direction) */
  align-items: center;
}

.hero-text {
  margin: 0 auto;
}

【问题讨论】:

  • 因为类.container-fluid设置了宽度100%
  • 我删除了包含 .container-fluid 的 div,它仍然有效。
  • 另外,如果宽度设置为 100%,margin: 0 auto 不应该居中;必须指定宽度,但必须小于 100% w3schools.com/css/css_align.asp

标签: css bootstrap-4


【解决方案1】:

Margin:0 自动工作,因为默认 font-size 我建议您将英雄文本的宽度设置为 100%。在使用 text-align:center; 时,它将始终居中文本。 我的最终代码

.hero-text {
    margin:0px;
    width:100%;
    text-align:center
}

【讨论】:

  • 如果它可以使用默认字体大小,那么为什么我们应该指定一个宽度才能让它工作(总是有一个默认字体大小)?
  • 您并不总是需要指定宽度,但如果我们没有默认大小,页面将几乎无法查看。 Chrome 和其他浏览器具有默认大小,无需 css 即可轻松查看页面。
【解决方案2】:

文本居中是因为您有body { text-align: center }。英雄文本div的宽度不是浏览器窗口全宽的原因是因为.hero-imagedisplay: flex。为了让自动边距做某事,div 的宽度必须小于 100%。是的,所以它可以工作,但你并不真正需要它,因为居中的文本会为你做。

如果您删除 display: flex,您会看到 .hero-image 变为 100% 宽,并且您的自动边距没有任何作用,因为 L/R 边距没有空间(但请注意,由于 text-align,文本仍然居中)。

【讨论】:

  • 无边距:0 auto;对于 .hero-text,文本不是水平居中,而是左对齐。
  • 如果 .hero-text 没有 0 auto,文本仍然居中对齐在 hero-text 内,但是 hero-text 框会移到左侧。为什么?由于 display: flex,英雄文本的宽度仅与它的内容一样宽。如果你删除 display: flex 那么 hero-text div 将再次变为 100% 宽。
  • 你说“如果你删除 display: flex 那么你会看到 .hero-image 变成 100% 宽并且你的自动边距没有任何作用”...... h1 文本块仍然水平居中没有显示:flex;自动边距仍然有效
  • 文本居中是因为text-align: center。尝试删除 display:flex 和 .hero-text 边距。文本仍将居中。
【解决方案3】:

这里不是关于经典的margin:auto,而是关于 flexbox。你的容器设置了display:flex.hero-image 是一个弹性项目,它的宽度不再是 100% 像默认 div 但它等于其内容的大小,这就是为什么 margin:auto 是在不需要指定固定宽度的地方居中元素。

一些相关问题:

What's the difference between margin:auto and justify-content / align-items center?

Flex item width flex-direction: column and margin: 0 auto

【讨论】:

  • 即使没有 display: flex 也能工作,所以不可能。正如我所提到的,如果我从 .hero-image 的 CSS 规则集中删除除图像 URL 之外的所有内容,它仍然有效。
  • 不,它不会工作 :) 移除 flex 并看到您的 div 将变为 100% 宽度并且它将以 text-align:center 居中;) 所以 text-align:center
  • 刚刚了解到 .col div 的最大宽度为 100%,有无显示:flex;这一定是原因。此外,正文的 text-align: center 不会水平居中英雄文本--margin: 0 auto 是需要的。
  • @NatalieCardot text align 将使所有文本居中 .. 现在您有两种情况,英雄默认为 100% 宽度(因此文本居中),当您添加 flex 时,它将不再是 100 % width 在这里你需要 margin:auto 来使块居中而不是文本居中(因为文本已经居中)
猜你喜欢
  • 2019-05-10
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 2013-01-05
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多