gpt4 book ai didi

html - 如何将 calc() 和 var() 用于具有动态值的 CSS 按钮?

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:59 25 4
gpt4 key购买 nike

我从 GitHub 获得了 CSS 光泽按钮代码.在CSS中,使用了三个固定的width (.container - width: 140px, .glossy - width: 120px, and .glossy:before - width: 110px) 来设计光面效果按钮。按钮宽度是固定的。是否可以自动计算自定义文本的宽度

代码片段如下,

.container {
width: 140px;
margin: 50px auto;
}

.glossy p {
margin: 6px 0 0 0;
text-align: center;
position: relative;
z-index: 1;
}

.glossy {
width: 120px;
height: 25px;
margin: 10px auto;
position: relative;
background: #94c4fe;
background: -webkit-gradient(linear, left top, left bottom, color-stop(31%, #94c4fe), color-stop(100%, #d3f6fe));
background: -webkit-linear-gradient(top, #94c4fe 31%, #d3f6fe 100%);
background: -moz-linear-gradient(top, #94c4fe 31%, #d3f6fe 100%);
background: -o-linear-gradient(top, #94c4fe 31%, #d3f6fe 100%);
background: -ms-linear-gradient(top, #94c4fe 31%, #d3f6fe 100%);
background: linear-gradient(to bottom, #94c4fe 31%, #d3f6fe 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#94c4fe', endColorstr='#d3f6fe', GradientType=0);
-webkit-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
border: 1px solid #4864a9;
color: #000;
font-size: 0.750em;
text-shadow: 1px 1px 0px rgba(255, 255, 255, .5);
-webkit-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, .2);
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, .2);
position: relative;
}

.glossy:before {
content: "";
width: 110px;
height: 16px;
display: block;
position: absolute;
left: 5px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
background: -moz-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(8%, rgba(255, 255, 255, 0.7)), color-stop(100%, rgba(255, 255, 255, 0)));
background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
background: -o-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
background: -ms-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#00ffffff', GradientType=0);
}
<div class="container">
<div class="glossy">
<p>Hi, are you there?</p>
</div>
</div>

最佳答案

这里根本不需要calcvar;只是停止依赖固定像素大小的一切。

这使用 inline-block 而不是 .glossy 上的固定宽度,根据内容设置按钮宽度;它还在 ::before 上设置高亮以匹配容器宽度,而不是完全使用 width (它已经是绝对定位的,所以这只是一个问题添加 left 规则和 right 规则。)

我还删除了 .container 规则,因为它没有做任何与问题相关的事情,并删除了一堆不必要或冗余的规则,包括早已过时的供应商前缀规则。

.glossy p {
margin: 6px 0 0 0;
text-align: center;
}

.glossy {
display: inline-block;
padding: 0 25px;
height: 25px;
position: relative;
background: linear-gradient(to bottom, #94c4fe 31%, #d3f6fe 100%);
border-radius: 25px;
border: 1px solid #4864a9;
color: #000;
font-size: 0.750em;
text-shadow: 1px 1px 0px rgba(255, 255, 255, .5);
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, .2);
}

.glossy:before {
content: "";
height: 16px;
position: absolute;
right: 5px; left: 5px;
border-radius: 8px;
background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0.7) 8%, rgba(255, 255, 255, 0) 100%);
}
<div class="glossy">
<p>short</p>
</div>

<div class="glossy">
<p>medium medium</p>
</div>

<div class="glossy">
<p>long text long text long text long text</p>
</div>

高度仍然固定为特定的像素大小;需要更大量的重写才能纠正这个问题,而且图形设计在其他高度也不会真正起作用。

关于html - 如何将 calc() 和 var() 用于具有动态值的 CSS 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53393902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com