【问题标题】:Centering a div with words and a picture用文字和图片将 div 居中
【发布时间】:2016-12-02 17:19:54
【问题描述】:

我正在尝试使包含两个 div 的 div 居中 - 一个是段落,另一个是图像。我试图将自动 0 边距应用于容器 div,但它没有居中任何东西。有没有办法让所有内容都居中,同时保留我在较小 div 中所做的格式? 我的代码

.whole {
  height: 800px;
  max-width: 98%;
  margin-left: 0 auto;
  margin-right: 0 auto;
  text-align: center;
}
.about-text {
  width: 40%;
  color: #66096c;
  font-family: Lucida bright;
  font-size: 20px;
  height: 100%;
  float: left;
  padding: 1.8 em;
  padding-right: 6em;
  margin-left: 5%;
  margin-right: auto;
}
.about_pic {
  width: 40%;
  height: 100%;
  float: left;
  margin-left: auto;
  margin-right: auto;
}
<div class="whole">
  <div class="about-text">
    <p> <strong> Hi, I'm Kelsie. </strong> 
      <p>
        <p>Hello there.
          <br />My name is Kelsie, and I like to create things. So I decided to do it for a job. I am learning how to do this at Michigan State University, where I study professional writing. This program teaches web design, print design, editing, and writing.
          I am also learning about how to make rhetorical decisions that will have a positive impact on an audience by studying visual design principles, website accessibility and usability laws, and persuasion techniques.</p>
        <p>I live in East Lansing where I am a full-time student. So basically I study all the time. On the rare occasions that I do have some free time, I like to read, shop, and visit my <span> home</span> cats.</p>
        <p>Want to create something together? Me too. Send me a message using the contact form, and let's get together.</p>
  </div>

  <div class="about_pic">
    <img src="https://msu.edu/~donald88/kelsie.jpg" alt="kelsie by river" align="center" />
  </div>
</div>

【问题讨论】:

  • margin-left: 0 auto; margin-right: 0 auto; - 这些属性中的每一个都不能获得 2 个值
  • 您希望文字和图片并排还是在下方?
  • img 元素没有align 属性。你也有 unclosed &lt;p&gt; 标签,而其他标签是关闭的。不要混合这两种使用类型。两者在 HTML 5 中都是有效的,但我建议始终关闭所有打开的元素,并且 b) 如果您不希望这样做,则不要混淆它。
  • 我希望它们挨着

标签: html css containers center


【解决方案1】:

如果您希望中的元素相同 高度,一个不错的选择是flexbox。如果宽度(例如文本)变小,请使用 media query 并使 div 的 with 为 100% 和 flex-direction: column

总的来说可能看起来像:

body {
  margin: 0;
}
.whole {
  display: flex;
  text-align: center;
  width: 95%;
  margin: 0 auto;
}
.whole div {
  padding: 2em;
  box-sizing: border-box;
}
.about-text {
  color: #66096c;
  font-family: Lucida bright;
  font-size: 20px;
}
@media screen and (max-width: 1000px) {
  .whole {
    flex-direction: column;
  }
  .whole div {
    width: 100%;
  }
  .whole img { /* Make image responsive */
    max-width: 100%;
    height: auto;
  }
}
<div class="whole">
  <div class="about-text">
    <p> <strong> Hi, I'm Kelsie. </strong> 
      <p>
        <p>Hello there.
          <br />My name is Kelsie, and I like to create things. So I decided to do it for a job. I am learning how to do this at Michigan State University, where I study professional writing. This program teaches web design, print design, editing, and writing.
          I am also learning about how to make rhetorical decisions that will have a positive impact on an audience by studying visual design principles, website accessibility and usability laws, and persuasion techniques.</p>
        <p>I live in East Lansing where I am a full-time student. So basically I study all the time. On the rare occasions that I do have some free time, I like to read, shop, and visit my <span> home</span> cats.</p>
        <p>Want to create something together? Me too. Send me a message using the contact form, and let's get together.</p>
  </div>

  <div class="about_pic">
    <img src="https://msu.edu/~donald88/kelsie.jpg" alt="kelsie by river" align="center" />
  </div>
</div>

【讨论】:

    【解决方案2】:

    不要使用margin-left: 0 automargin-right: 0 auto。 只需在主 div 上使用 margin: auto

    【讨论】:

    • 这并没有改变任何东西,但是感谢您的建议;我不知道为什么它不会以 margin: auto 为中心!
    • 不管怎样保留它。然后将两个内部 div 更改为每个宽度为 50%。如果这是你想要的,大的到 80%。
    【解决方案3】:

    为文本提供margin-left:30%;,因为您的 div 宽度为 50%,并从中删除所有右边距,就好像 div 居中对齐一样,则无需指定右边距

    .whole {
      height: 800px;
      max-width: 98%;
      margin-left: 0 auto;
      margin-right: 0 auto;
      text-align: center;
    }
    .about-text {
      width: 40%;
      color: #66096c;
      font-family: Lucida bright;
      font-size: 20px;
      height: 100%;
      float: left;
      margin-left:30%;
    }
    .about_pic {
      width: 40%;
      height: 100%;
      float: left;
      margin-left: auto;
      margin-right: auto;
    }
    <div class="whole">
      <div class="about-text">
        <p> <strong> Hi, I'm Kelsie. </strong> 
          <p>
            <p>Hello there.
              <br />My name is Kelsie, and I like to create things. So I decided to do it for a job. I am learning how to do this at Michigan State University, where I study professional writing. This program teaches web design, print design, editing, and writing.
              I am also learning about how to make rhetorical decisions that will have a positive impact on an audience by studying visual design principles, website accessibility and usability laws, and persuasion techniques.</p>
            <p>I live in East Lansing where I am a full-time student. So basically I study all the time. On the rare occasions that I do have some free time, I like to read, shop, and visit my <span> home</span> cats.</p>
            <p>Want to create something together? Me too. Send me a message using the contact form, and let's get together.</p>
      </div>
    
      <div class="about_pic">
        <img src="https://msu.edu/~donald88/kelsie.jpg" alt="kelsie by river" align="center" />
      </div>
    </div>

    【讨论】:

    • 那她为什么要让她的容器完全漂浮呢?
    • 我怎么知道?她想要中心对齐。所以它可能会帮助她
    【解决方案4】:

    使用传统的盒子模型,padding-leftpadding-right被添加到元素的 width。所以你的.about-text 超过了你定义的40% 1.8em + 6em。

    尝试将以下内容添加到您的 CSS 以将 box-model 切换为更直观的模型:

    body, html {
      box-sizing: border-box; /* as opposed to the default content-box */
    }
    *, *:before, *:after {
      /* make that the default box-model for all elements, still allowing simple override if needed */
      box-sizing: inherit;
    }
    

    下一个问题是浮动。如果一个元素只包含浮动元素,它将折叠到0 的高度。您需要清除父元素上的浮动以使其包含浮动。你可以使用所谓的clearfix

    .clearfix:after {
      content: "";
      display: table;
      clear: both;
    }
    

    并将该类应用于您想要包含浮动子级的任何容器。

    【讨论】:

    • 谁-1'ed,请评论原因。
    • 那段代码把我的网站搞得一团糟;我可以改变我的 .about-text 的百分比宽度吗?或者以某种方式调整填充? (我没有-1,只是让你知道)
    • 不过,绝对必要了解这两种不同的盒子模型。你还没有回答我在你的问题下面的评论。到目前为止,尚不清楚您想要的结果是什么样的。
    【解决方案5】:

    您总是可以直接在 HTML 中添加 &lt;center&gt;&lt;/center&gt;...还是我误解了您的问题?

    【讨论】:

    • 那一定是 1997 年的 HTML 知识。&lt;center&gt; 已经被弃用了 174 年。话虽如此,这是一个非常糟糕的建议,因此也是一个非常糟糕的答案
    • 添加&lt;center&gt; 是一个不好的建议,因为该标签自 HTML4 出现以来已被贬值,您应该知道改用 CSS。仅仅因为它有效并不能使它正确。
    • 没有必要用所有的 CSS 让事情变得过于复杂
    • 仅出于向后兼容性原因工作。
    • 对它的支持不会很快结束,这解决了问题,因此这是一个完全有效的答案
    猜你喜欢
    • 2019-11-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2016-05-24
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多