ak-SE
ak-SE

Reputation: 1010

How to use calc() and var() for a CSS button with dynamic value?

I obtained as CSS glossy button code from GitHub. In the CSS, there are three fixed width used (.container - width: 140px, .glossy - width: 120px, and .glossy:before - width: 110px) to design a glossy effect button. The button width is fixed. Is it possible to automatically calculate width for a custom text?

The snippet with code is below,

.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>

Upvotes: 0

Views: 64

Answers (1)

Daniel Beck
Daniel Beck

Reputation: 21514

There's no need for calc or var here at all; just stop depending on fixed pixel sizes for everything.

This uses inline-block instead of a fixed width on .glossy, to set the button width based on the contents; it also sets the the highlight on ::before to match the container width instead of using width at all (it was already absolute-positioned, so this was just a matter of adding a left rule along with the right one.)

I also removed the .container rule as it wasn't doing anything relevant to the question, and removed a bunch of unnecessary or redundant rules, including the long-obsolete vendor-prefixed rules.

.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>

The height is still fixed to a specific pixel size; it would have taken a more substantial rewrite to correct that, and the graphic design wouldn't really work at other heights anyway.

Upvotes: 1

Related Questions