简体   繁体   English

如何使用纯HTML和CSS2模仿Android / iOS方形按钮?

[英]How to Mimic Android / iOS square buttons with plain HTML and CSS2?

This seems almost impossible. 这似乎几乎是不可能的。

I am trying to create HTML/CSS-only square link buttons with text centered inside and without using CSS3. 我正在尝试创建仅HTML / CSS的方形链接按钮,文本内部居中,并且不使用CSS3。

I have tried variations of every answer I could find about vertically-centering text in an inline-block , but nothing that I try fulfills the following constraints: 我已经尝试过找到所有关于inline-block垂直居中文本的答案的变inline-block ,但是我尝试满足的所有条件都没有:

  1. The entire button element must be a link, not just the text. 整个按钮元素必须是一个链接,而不仅仅是文本。
  2. The text inside the button element must be vertically and horizontally-center and wrap nicely if the width of the button element is not wide enough. 如果button元素的宽度不够宽,则button元素内的文本必须在垂直和水平方向上居中,并且要很好地环绕。
  3. The height and width of all buttons should be the same. 所有按钮的高度和宽度应相同。
  4. The buttons must be placeable side-by-side horizontally and wrap to the next line below if the width of the browser is reduced or not enough to fit all buttons. 这些按钮必须水平并排放置,并且如果浏览器的宽度减小或不足以容纳所有按钮,则必须环绕到下一行。
  5. To ensure compatibility with older browsers, this solution must not require CSS3 (eg display:flex ). 为了保证与旧的浏览器的兼容性,这种解决方案不能要求CSS3(如display:flex )。

Think something like the side-by-side square buttons on the iOS and Android home screens but using simple and pure HTML/CSS. 想想像iOS和Android主屏幕上的并排方形按钮之类的东西,但要使用简单纯净的HTML / CSS。 The shadowing, gradients, reflections aren't necessary, just the button structures. 不需要阴影,渐变,反射,只需按钮结构即可。

I've tried nested divs using :before as a trick to vertically-align the text within an inline-block , I've tried using display:table with rows and cells, and I've even tried display:flex but that uses CSS3. 我已经尝试使用嵌套的div :before作为特技垂直对齐的中的文本inline-block ,我已经尝试使用display:table的行和细胞,并且甚至我已经试过display:flex ,但其使用CSS3 。 No solution I tried fits all the criteria above. 我尝试过的解决方案都不符合上述所有条件。

So I'm wondering, is this even possible without CSS3? 所以我想知道,如果没有CSS3,这是否有可能? If so, what's the trick as I would have thought something like this would have been very straightforward to accomplish? 如果是这样,那么我会以为这样的事情将非常容易实现,那是什么窍门?

Yes, I believe this is possible, but it uses a lot of nested spans! 是的,我相信这是可能的,但是它使用了大量的嵌套跨度!

The craziest CSS used here is display: table and display: inline-block , so I think you should be pretty safe. 这里使用的最疯狂的CSS是display: tabledisplay: inline-block ,所以我认为您应该很安全。

 body { font-family: sans-serif; } .special-button { display: inline-block; height: 100px; width: 100px; overflow: hidden; position: relative; background: #eee; color: #333; text-decoration: none; } .special-button:hover { background: #333; color: #fff; } .special-button-table { display: table; width: 100%; height: 100%; } .special-button-cell { display: table-cell; vertical-align: middle; text-align: center; padding: 10px; } 
 <a href="#" class="special-button"> <span class="special-button-table"> <span class="special-button-cell">Text Here</span> </span> </a> <a href="#" class="special-button"> <span class="special-button-table"> <span class="special-button-cell">Longer Text Item Here</span> </span> </a> <a href="#" class="special-button"> <span class="special-button-table"> <span class="special-button-cell">Text Here</span> </span> </a> <a href="#" class="special-button"> <span class="special-button-table"> <span class="special-button-cell">Really Really Long Text Item That Overflows Here</span> </span> </a> 

Ive possibly missed some details due to a very quick skim read - let me know how far off this is. Ive可能由于快速阅读了一下而错过了一些细节-让我知道这有多远。

<style>
    .ios li {
        display: inline-block;
        background: #eee;
        padding: 5% 10px 20px 10px;
        width: 100px;
        max-width: 100px;
        height: 50px;
        max-height: 100px;
        vertical-align: top;
        text-align: center;
    }
</style>


<ul class="ios">
    <a href="test.com"><li>short text</li></a>
    <a href="test2.com"><li>much longer lot of text</li></a>
</ul>

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

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