【问题标题】:HTML Align Text to Bottom of Button with ImagesHTML将文本与带有图像的按钮底部对齐
【发布时间】:2017-09-17 08:03:46
【问题描述】:

我有 198px-198px 的方形按钮。每个按钮下方都有一个图像和文本。由于并非所有图像都是方形的,因此我将所有图像的 max-width 和 max-height 设置为 128px,以便它们按比例缩放。不幸的是,我不能再将文本与底部中心对齐了。

我不能使用padding-top: 65px,它解决了其他一些 SO 问题,因为图像的高度可变。

我在文本的范围内尝试了position: absolute; top: 180px,但在那之后我无法让它居中(不能只指定left: 10px,因为文本的长度可变)

感谢您的帮助。

这里是一个 JSFiddle https://jsfiddle.net/vbgg3keu/3/(注意:你可以扩大输出面板的宽度,让所有的按钮排成一行)

HTML:

<button class="square-button">
<img class="small-img" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/><br/>

<span class="button-text">This text is too high</span>
</button>

<button class="square-button">
<img class="small-img" src="http://etc.usf.edu/clipart/36400/36459/ruler1_36459_lg.gif"/><br/>

<span class="button-text">This text is too high</span>
</button>

<button class="square-button">
<img class="small-img" src="https://tall.life/wp-content/uploads/2013/04/Giraffe.jpg"/><br/>

<span class="button-text">This text is the correct height</span>
</button>

<button class="square-button">
<img class="small-img" src="https://lh3.googleusercontent.com/dB3Dvgf3VIglusoGJAfpNUAANhTXW8K9mvIsiIPkhJUAbAKGKJcEMPTf0mkSexzLM5o=w300"/><br/>

<span class="button-text">This text is the correct height</span>
</button>

CSS:

.square-button {
  width: 198px;
  height: 198px;
  float: left;
}
.small-img {
    max-width: 128px;
  max-height: 128px;
}

.button-text {
  // this doesn't work because now I can't center the text
  //position: absolute;
  //top: 180px;
}

【问题讨论】:

    标签: html css text-alignment text-align


    【解决方案1】:

    试试下面的代码:

    .square-button {
        width: 198px;
        height: 198px;
        float: left;
        position: relative;
    }
    .small-img {
        max-width: 128px;
        max-height: 128px;
        position: absolute;
        top: 50%;left: 50%;
        transform: translate(-50%,-50%);
    }
    
    .button-text {
        position: absolute;
        transform: translateX(-50%);
        bottom: 10px;
        width: 100%;
        left: 50%;
    }
    

    https://jsfiddle.net/vbgg3keu/10/

    【讨论】:

      【解决方案2】:

      您可以做的是将您的方形按钮类设置为相对位置并使用百分比来放置文本,同时将宽度跨越到 100%:

      https://jsfiddle.net/vbgg3keu/4/

      .button-text {
        position: absolute;
        bottom: 5%;
        width: 100%;
        left: 0%;
      }
      
      .square-button {
        width: 198px;
        height: 198px;
        float: left;
        position: relative;
      }
      

      【讨论】:

      • 非常适合我发布的示例案例!谢谢。文本多一点的情况呢?例如,我更新了小提琴 jsfiddle.net/vbgg3keu/6,其中一个按钮的文本跨越 3 行。有什么办法可以将图像移高吗?我尝试在图像 css 中添加相同的内容:` position: absolute;最高:0%; left: 0%;` 但它失去了居中
      • 现在你有一个固定的正方形和图像高度,所以你必须做出这些百分比
      • nvm 我用margin-top: -50px; 修复了它的图像。图像上方有很多额外的空间。感谢上述解决方案!
      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多