将 CSS/HTML 中的按钮与文本水平对齐在一行中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17629788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:04:52  来源:igfitidea点击:

Aligning buttons in CSS/HTML in one line horizontally w/ text?

htmlcss

提问by Alex

I would like to know how could I align my buttons horizontally? On top of that I want to add text to those buttons. This is what I currently have.

我想知道如何水平对齐按钮?最重要的是,我想向这些按钮添加文本。这就是我目前所拥有的。

HTML body part:

HTML 正文部分:

<body> 
    <div class="tile_div">
        <table class="tile_table">
            <tr>
                <td>
                    <img class="tile_image" height="50px" width="100px" src="./images/button_left.png"/> 
                    <p class="tile_p">Button one</p>
                </td>
                <td>
                    <img class="tile_image" height="50px" width="100px" src="./images/button_left.png"/> 
                    <p class="tile_p">Button two</p>
                </td>
                <td>
                    <img class="tile_image" height="50px" width="100px" src="./images/button_left.png"/> 
                    <p class="tile_p">Button three</p>
                </td>
            </tr>
        </table>
    </div>
</body>

CSS:

CSS:

.tile_image {
    float: left;
    margin: 0px;
}

.tile_image img {
    position:absolute;
}

.tile_p {
    text-align:center;
    position:relative;
}

How it looks like: http://img580.imageshack.us/img580/8867/uo7i.png

它的样子:http: //img580.imageshack.us/img580/8867/uo7i.png

I want the text to be in the middle of the buttons not below them.

我希望文本位于按钮的中间而不是它们下方。

EDIT:

编辑:

Alright so when I use src="./images/button_left.png"the image looks they way it should look:

好吧,所以当我使用 src="./images/button_left.png"图像时,它们看起来应该是这样的:

http://img580.imageshack.us/img580/8867/uo7i.png

But when ever I use background-image This happens: http://img580.imageshack.us/img580/6127/yz4.png

但是当我使用背景图像时会发生这种情况:http: //img580.imageshack.us/img580/6127/yz4.png

回答by bitWorking

tables should not be used for layout. I think the best approach would be to use floated links like this:

表格不应用于布局。我认为最好的方法是使用这样的浮动链接:

<div class="tile_div">
    <a href="#">Button one</a>
    <a href="#">Button two</a>
    <a href="#" class="last">Button three</a>
    <div class="clear"></div>
</div>

CSS:

CSS:

.tile_div a {
    display: block;
    float: left;
    height: 50px;
    width: 100px;
    margin-right: 5px;
    background-image: url(./images/button_left.png);
    text-align: center;
    line-height: 50px;
    text-decoration: none;
}

.title_div a.last {
    margin-right: 0;
}

.clear {
    clear: both;
}

JSFiddle

JSFiddle

回答by Alex

Ok you need to exchange the imgand the pwith this:

好的,您需要将imgp与此交换:

<button style="background: url('./images/button_left.png'); width: 100px; height: 50px; border: 0;">Button one</button>

http://jsfiddle.net/6duRf/

http://jsfiddle.net/6duRf/