简体   繁体   English

如何在按钮元素的顶部(和背景图像上方)定位文本?

[英]How to position text at the top of button element (and above background image)?

This is the look I'm trying to achieve: 这是我想要实现的目标:

在此输入图像描述

I'm trying to position the text above the background image OR position the background image below the text, AND have the image and the text be clickable as it is a button . 我正在尝试将文本放在背景图像上方或将背景图像放在文本下方,并让图像和文本可以点击,因为它是一个button

But I can't get the text at the top of the button . 但我无法将文本放在button的顶部。 vertical-align doesn't have any effect it seems. vertical-align似乎没有任何影响。

Is it possible to do it with a single button element? 是否可以使用单个按钮元素进行操作?

  button { width: 96px; height: 150px; border: none; border-radius: 0px; line-height: 100px; background-position: 50% 58px; background-size: contain; background-repeat: no-repeat; background-color: transparent; margin: 20px; } button.document { border: 1px solid red; } 
 <button class="document" style="background-image: url(http://icons.iconarchive.com/icons/iynque/flat-ios7-style-documents/96/doc-icon.png)"> Plot Plan.doc </button> 

Remove your line-height and add padding-bottom to offset the text: 删除line-height并添加padding-bottom以偏移文本:

 button { width: 96px; height: 150px; border: none; border-radius: 0px; background-position: 50% 58px; background-size: contain; background-repeat: no-repeat; background-color: transparent; margin: 20px; padding-bottom: 125px; } button.document { border: 1px solid red; } 
 <button class="document" style="background-image: url(http://icons.iconarchive.com/icons/iynque/flat-ios7-style-documents/96/doc-icon.png)"> Plot Plan.doc </button> 

Mine is a modification of James Montagne's. 我的是James Montagne的改编版。 I removed height (which should be a factor of content + (padding/image) and positioned the button to appear at the bottom of the element. 我删除了高度(应该是内容因子+(填充/图像)并将按钮定位在元素的底部。

The way this works in CSS is first, it outputs all text, giving about 40px (Y) of content. 这在CSS中的工作方式首先是输出所有文本,提供大约40px(Y)的内容。 Then, it adds 96px of padding below - enough extra space to contain the background image. 然后,它在下面添加了96px的填充 - 足够的额外空间来包含背景图像。 Finally, it uses background-position to decide where to put the background image; 最后,它使用background-position来决定放置背景图像的位置; at the bottom. 在底部。 This reduces the coupling of each "position variable", which might make it easier to change. 这减少了每个“位置变量”的耦合,这可能使其更容易更改。

 button { width: 96px; border: none; border-radius: 0px; background-position: 50% bottom; background-size: contain; background-repeat: no-repeat; background-color: transparent; margin: 20px; padding-bottom: 96px; } button.document { border: 1px solid red; } 
 <button class="document" style="background-image: url(http://icons.iconarchive.com/icons/iynque/flat-ios7-style-documents/96/doc-icon.png)"> Plot Plan.doc </button> 

The best I could get: 我能得到的最好的:

 button { width: 96px; height: 150px; border: none; border-radius: 0px; background-position: 10% 140%; background-size: contain; background-repeat: no-repeat; background-color: transparent; margin: 20px; } button.document { border: 1px solid red; } 
 <button class="document" style="background-image: url(http://icons.iconarchive.com/icons/iynque/flat-ios7-style-documents/96/doc-icon.png)"> Plot Plan.doc </button> 

Added some background positioning background-position: 10% 140%; 增加了一些背景定位background-position: 10% 140%; - 10% for horizontal and 140% for vertical positioning. - 水平10%,垂直定位140%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM