【问题标题】:Trying to put text under a picture messes up the alignment试图将文字放在图片下会弄乱对齐方式
【发布时间】:2017-07-11 04:41:50
【问题描述】:

我正在尝试让文本在连续 3 个图像下对齐。我在这里找到的每个解决方案最终都会将图像放在与水平相反的垂直线上。

HTML:

<div id="SecondFooter" class="responsiveColumn">
            <a href="link here" target="_blank"><img src="img.jpg"></a>
            <a href="link here" target="_blank"><img src="img.jpg"></a>
            <a href="link here" target="_blank"><img src="img.jpg"></a>
</div> 

CSS:

#SecondFooter {
    width: 600px;
    background-color: #ffffff; 
    color: #000000; 
    font-family: arial,helvetica,sans-serif; 
    font-size: 11px;
    text-align: center;
}

#SecondFooter img{
    display: inline-block;
    width: 155px;
    height: 155px;
    padding: 5px;
    margin: 0;
}

我试过做一个图和图题。尝试将每个图像分离到它自己的 div 中,但它看起来就像我现在改变的任何东西,把所有东西都放在垂直对齐中,我试图让它保持水平。

【问题讨论】:

  • 这是错字吗? &lt;img src="img.jpg"&lt;/a&gt;你错过了图片标签上的右括号
  • 对不起 - 是的,这是一个错字。当我把它放在这里时,我只是替换了实际的 img 链接。
  • 查看我的答案以获得解决方案,您的宽度和高度需要一个单位。您应该编辑问题以修复拼写错误
  • 已编辑并修复 - 谢谢。尽管我不确定您的回答对文本部分有何帮助。它应该是对齐的。只是一旦我添加了文字,事情就开始在我身上分裂。
  • 我不知道你在说什么文字,因为它不是你发布的代码的一部分

标签: html css


【解决方案1】:

如何创建一个主 div 并将所有其他图像 div 放入其中?类似的东西

<div> 
   <div>
      <img>
      <p>
   </div>
   <div>
      <img>
      <p>
   </div>
   <div>
      <img>
      <p>
   </div>
</div>

也许它会起作用。

【讨论】:

  • 似乎每当图像脱离主 div 时,它会将图像放置在与水平相反的垂直线上
  • 试试css属性display: flex来改变这个。
  • 好的,所以将 flex 放在 SecondFooter ID 上就可以了。虽然现在它将图像推向左侧而不是居中。
【解决方案2】:

我想这就是你要找的东西?其他 CSS 仅用于视觉效果。

  align-items: center;
  display: flex;
  height: 100vh;
  justify-content: center;

http://flexboxfroggy.com/ 是展示使用 flex 可以实现什么的绝佳实践。

https://codepen.io/garynorris88/pen/owQpvN?editors=1100

【讨论】:

    【解决方案3】:

    你需要修复的几件事

    1 - 关闭你的图片标签

    2 - 图像的宽度和高度需要一个单位(例如 px)。 155需要155px

    https://jsfiddle.net/eg5a4pjg/1/

    HTML

    <a href="link here" target="_blank"><img src="img.jpg" /></a>
    

    CSS

    #SecondFooter img{
        display: inline-block;
        width: 155px;
        height: 155px;
        padding: 5px;
        margin: 0;
    }
    

    【讨论】:

      【解决方案4】:

      您所要做的就是像这样将您的 #SecondFooter div 中的锚标记浮动到左侧

      #SecondFooter a {
        float: left;
      }
      

      这就是您的 HTML 的外观。我在img标签之后添加了一个p标签,并带有一些文本来演示

      <div id="SecondFooter" class="responsiveColumn">
        <a href="link here" target="_blank">
          <img src="img.jpg">
          <p>hi there</p>
        </a>
        <a href="link here" target="_blank">
          <img src="img.jpg">
          <p>hi there</p>
        </a>
        <a href="link here" target="_blank">
          <img src="img.jpg">
          <p>hi there</p>
        </a> </div>
      

      为了保持相对居中,我添加了一些包装器

      HTML:

      <div id="SecondFooter" class="responsiveColumn">
        <div class="wrapper">
          <div class="link-wrapper">
            <a href="link here" target="_blank">
              <img src="img.jpg">
              <p>hi there</p>
            </a>
            <a href="link here" target="_blank">
              <img src="img.jpg">
              <p>hi there</p>
            </a>
            <a href="link here" target="_blank">
              <img src="img.jpg">
              <p>hi there</p>
            </a>
          </div>
        </div>
      </div>
      

      CSS: 我把 SecondFooter 的宽度改成了 width: 100%;

      #SecondFooter {
        width: 100%;
        background-color: #ffffff; 
        color: #000000; 
        font-family: arial,helvetica,sans-serif; 
        font-size: 11px;
        text-align: center;
      
      }
      
      .wrapper {
        width: 90%;
        margin: 0 auto;
      }
      
      .link-wrapper {
        width: 60%;
        margin: 0 auto;
      }
      

      【讨论】:

      • 好吧,现在这个愚蠢的问题 - 我怎样才能保持这个居中?所以文字现在停留在图像下方,但都被推到了左边。
      • 我更新了我的解决方案,使其在屏幕上相对居中。去看看!
      猜你喜欢
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2017-05-09
      • 1970-01-01
      • 2012-08-23
      • 2017-03-01
      相关资源
      最近更新 更多