0

我有一个固定宽度为 20px 的输入按钮,使用box-sizing: content-box.

<input data-role="none" type="button" class="icon_text" value="click me" />

按钮的宽度仅使用填充设置。

例子:

.icon_text {
    border: 1px solid #aaa;
    border-radius: .7em;
    -moz-border-radius: .7em;
    -webkit-border-radius: .7em;
    color: white !important;
    font-size: 16px;
    font-weight: 800;
    cursor: pointer;
    background-color: #7EB238;
    background-image: url("p.png");
    background-image: url("p.png"), -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(rgba(52,109,28,1)), color-stop(rgba(52,109,28,0) )), -webkit-gradient(linear, left top, left bottom, from( #9ad945 ), to( #7eb238 )); 
    background-image: url("p.png"), -webkit-radial-gradient(center, ellipse cover,  rgba(52,109,28,1), rgba(52,109,28,0) ), -webkit-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -moz-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -moz-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -ms-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -ms-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -o-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -o-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), radial-gradient(ellipse at center, rgba(52,109,28,1) 67%, rgba(52,109,28,0) 69%), linear-gradient( #9ad945, #7eb238 );
    background-attachment: scroll, scroll, scroll;
    background-repeat: no-repeat, no-repeat, no-repeat;
    background-position: -100px 50%, 7px 50%, center center;
    background-size: 864px 18px, 20px 20px, auto auto;
    -webkit-background-size: 864px 18px, 20px 20px, auto auto;
    background-clip: content-box, content-box, padding-box;
    -webkit-box-sizing: content-box; 
    -moz-box-sizing: content-box;   
    box-sizing: content-box;
    text-indent: 2.1em;
    padding-top: 6px;
    padding-bottom:7px;
    padding-left: 7px;
    padding-right:92px;
    width: 20px;
    height: 20px;

}

虽然这很好用,但我需要能够生成带有居中文本的按钮的全屏。我可以或多或少地将按钮设置为padding-right96%但我不知道如何使文本居中,因为text-indent不采用百分比值。

问题:
只有在按钮宽度固定、按钮长度使用填充设置的情况下,是否可以通过 CSS 拉伸按钮并将其文本居中?只是好奇是否有人有想法,如何做到这一点。

谢谢!

4

1 回答 1

2

使用固定宽度时无法实现这一点。如果您愿意调整宽度,则以下样式将起作用。重要的部分是display:block, width:100%, text-align:center

.icon_text {
    border: 1px solid #aaa;
    border-radius: .7em;
    -moz-border-radius: .7em;
    -webkit-border-radius: .7em;
    color: white !important;
    font-size: 16px;
    font-weight: 800;
    cursor: pointer;
    background-color: #7EB238;
    background-image: url("p.png");
    background-image: url("p.png"), -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(rgba(52,109,28,1)), color-stop(rgba(52,109,28,0) )), -webkit-gradient(linear, left top, left bottom, from( #9ad945 ), to( #7eb238 )); 
    background-image: url("p.png"), -webkit-radial-gradient(center, ellipse cover,  rgba(52,109,28,1), rgba(52,109,28,0) ), -webkit-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -moz-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -moz-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -ms-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -ms-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), -o-radial-gradient(center, ellipse cover, rgba(52,109,28,1), rgba(52,109,28,0) ), -o-linear-gradient( #9ad945, #7eb238 ); 
    background-image: url("p.png"), radial-gradient(ellipse at center, rgba(52,109,28,1) 67%, rgba(52,109,28,0) 69%), linear-gradient( #9ad945, #7eb238 );
    background-attachment: scroll, scroll, scroll;
    background-repeat: no-repeat, no-repeat, no-repeat;
    background-position: -100px 50%, 7px 50%, center center;
    background-size: 864px 18px, 20px 20px, auto auto;
    -webkit-background-size: 864px 18px, 20px 20px, auto auto;
    background-clip: content-box, content-box, padding-box;
    -webkit-box-sizing: content-box; 
    -moz-box-sizing: content-box;   
    box-sizing: content-box;
    text-indent: 2.1em;
    padding-top: 6px;
    padding-bottom:7px;
    padding-left: 7px;
    padding-right:92px;
    height: 20px;
    display: block;
    width: 100%;
    text-align: center;
}

工作示例:http: //jsfiddle.net/3bCB2/

于 2013-04-09T08:41:08.770 回答