CSS如何通过按钮背景正确替换链接

标签 css positioning styling

我正在尝试用按钮背景替换我的应用程序上的链接。但是,链接文本既不会水平居中,也不会垂直居中。我正在使用属性“background-position: center center;”我认为这会解决我的问题。

我的 HTML:

<a class="violetButtonLarge" href="#">My Link</a>

CSS:

.violetButtonLarge {
    display: block; 
    width: 304px; 
    height: 41px;
    background: url(../images/violetButton_large.png) no-repeat center center;
    border:none;
    color: #FFFFFF;
    font-weight: bold;
    font-family: Arial;
    background-color: transparent;
    text-decoration: none;
}

我的形象:

enter image description here

我在这里做错了什么?这是我得到的:

enter image description here

提前致谢!

最佳答案

使用 text-align:centerline-height:41px 使文本在垂直和水平居中对齐。

.violetButtonLarge {
    display: block; 
    width: 304px; 
    height: 41px;
    background: url(http://i.stack.imgur.com/S8zvb.png) no-repeat center center;
    border:none;
    color: #FFFFFF;
    font-weight: bold;
    font-family: Arial;
    background-color: transparent;
    text-decoration: none;
    text-align:center;
    line-height:41px;
}

示例:http://tinkerbin.com/q5VZR1At

关于CSS如何通过按钮背景正确替换链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230557/

相关文章:

css - 尽管容器宽度固定,IE 仍强制 float div 到底部?

javascript - 使用 Javascript 保留 CSS 样式

css - 如果验证失败,则设置 HTML5 输入类型的样式

javascript - 在 youtube 页面中放置两个不同的滚动条以改进自定义 UI 体验

html - 在滚动条上保持悬停

html - 隐藏屏幕底部的div?

html - css按左顺序排列div

html - 无法使 div 达到屏幕高度 100%

html - 同时左右放置一个div

HTML/CSS - 更改文本字段的属性