如何制作带有内边框的按钮

时间:2014-07-01 21:06:55

标签: html css

如果有意义的话,我正在尝试制作一个具有“内部边框”的响应式按钮,请参阅附图,了解我正在谈论的内容。

button preview

我的一些按钮比其他按钮宽一些,因为按钮文本较长,这是响应部分的来源。我通过将我的按钮放入容器div来创建此效果,这是代码:

HTML

<div class="ttw_button_container">
<a class="ttw_button">START THE JOURNEY</a>
</div>

CSS

.ttw_button_container {
    background: #27d9b4;
    width: 330px;
    height: 50px;
    margin: 0 auto;
}
a.ttw_button{  
    background: #27d9b4;
    color: {{ settings.btn_text_color }};
    border: 1px solid white;
    padding: 6px 52px;
    margin: 4px 0 0 0;
    cursor: pointer;
    font-weight: bold;      
    font-size: 19px;
    text-transform: {{ settings.button_font_style }};
    display: inline-block;
    -webkit-transition: all 200ms ease 0s;
    -moz-transition: all 200ms ease 0s;
    -ms-transition: all 200ms ease 0s;
    -o-transition: all 200ms ease 0s;
    transition: all 200ms ease 0s;
    -webkit-appearance: none;
    -webkit-font-smoothing: antialiased;
    font-smoothing: antialiased;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

有没有办法让容器div调整大小以适应按钮?目前我手动编辑容器的宽度以适合每个按钮,但理想情况下这样做是自动完成的。有没有办法只使用CSS来实现这一点,还是我们需要将javascript引入混合?

谢谢!

4 个答案:

答案 0 :(得分:2)

这是一个非常简单的使用box-shadow的解决方案,需要更少的标记和更少的CSS:http://jsfiddle.net/Jdzpv/3/

HTML:

<a class="ttw_button">START THE JOURNEY</a>

CSS:

.ttw_button {
    display: inline-block;
    padding: 6px 25px;
    font-weight: bold;
    font-size: 19px;
    background: #27d9b4;
    box-shadow: 0 0 0 5px #27d9b4,
                inset 0 0 0 1px #fff;
}

答案 1 :(得分:1)

当然有,这里有一个JS小提琴,它将接受并扩展到那里的任何数量的文本。

http://jsfiddle.net/T9Wk7/

不要在容器上使用宽度,只需使用填充:

.ttw_button_container {
    background: #27d9b4;
    padding: 4px 10px;
    margin: 4px 0;
    display: inline-block;
}
a.ttw_button{  
    background: #27d9b4;
    color: {{ settings.btn_text_color }};
    border: 1px solid white;
    padding: 6px 52px;
    font-weight: bold;      
    font-size: 19px;
    display: inline-block;
}

如果你想要丢失所有的div,你可以做一些评论建议的东西,只使用box-shadow:

a.ttw_button{  
    background: #27d9b4;
    color: {{ settings.btn_text_color }};
    border: 1px solid white;
    padding: 6px 52px;
    font-weight: bold;      
    font-size: 19px;
    display: inline-block;
    -moz-box-shadow: 0px 0px 0px 6px #27d9b4
    -webkit-box-shadow: 0px 0px 0px 6px #27d9b4
    box-shadow: 0px 0px 0px 6px #27d9b4
}

这是一个JSfiddle,显示:http://jsfiddle.net/T9Wk7/1/

答案 2 :(得分:0)

而且,这是一个适用于不支持box-shadow的旧浏览器的解决方案。这个使用outlineborder声明:http://jsfiddle.net/Jdzpv/5/

HTML:

<a class="ttw_button">START THE JOURNEY</a>

CSS:

.ttw_button {
    display: inline-block;
    padding: 6px 25px;
    font-weight: bold;
    font-size: 19px;
    background: #27d9b4;
    outline: 5px solid #27d9b4;
    border: 1px solid #fff;
}

答案 3 :(得分:-1)

试试这个:

.ttw_button_container {
    background: #27d9b4;
    width: 330px;
    height: 50px;
    margin: 0 auto;
    padding:6px;
}
a.ttw_button{  
    background: #27d9b4;
    color: {{ settings.btn_text_color }};
    border: 1px solid white;
    padding: 6px;
    line-height:35px;
    cursor: pointer;
    font-weight: bold;      
    font-size: 19px;
    text-transform: {{ settings.button_font_style }};
    display: inline-block;
    -webkit-transition: all 200ms ease 0s;
    -moz-transition: all 200ms ease 0s;
    -ms-transition: all 200ms ease 0s;
    -o-transition: all 200ms ease 0s;
    transition: all 200ms ease 0s;
    -webkit-appearance: none;
    -webkit-font-smoothing: antialiased;
    font-smoothing: antialiased;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width:100%;
    height:100%;
    text-align:center;    
}
相关问题