Creating a dynamic CSS button with calc() and var() - a step-by-step guide

I recently came across a fascinating CSS glossy button code on GitHub. The CSS contains specific fixed widths for elements such as .container with width: 140px, .glossy with width: 120px, and .glossy:before with width: 110px to create a glossy effect button. The current design has a fixed width, but I'm wondering if it's possible to dynamically calculate the width based on custom text input.

You can find the code snippet 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;
  /* Additional CSS properties... */
}

.glossy:before {
  content: "";
  width: 110px;
  height: 16px;
  display: block;
  position: absolute;
  left: 5px;
  /* Additional CSS properties... */
}
<div class="container">
  <div class="glossy">
    <p>Hello, world!</p>
  </div>
</div>

Answer №1

Avoid using unnecessary calc or var in this case; instead, try to rely less on fixed pixel sizes for all elements.

This snippet utilizes inline-block for the button width within .glossy, allowing it to adjust based on content length. The highlight on ::before matches the container width without specifying a specific width value (since it was already positioned absolutely, just adding a left rule alongside the existing right one.)

The redundant .container rule has been removed as it did not add any value to the solution, along with other unnecessary or outdated 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 remains fixed at a specific pixel size; changing that would require more extensive modifications, and the design may not translate well to different heights.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Softening the edges of a table

Could you assist me in rounding the corners of my table? I attempted using the basic border-radius, however, it only separated the border at the corners. Do you think this issue is due to my browser (Firefox) or could it be a bug? You can view my Jsfiddl ...

Utilizing Bootstrap to display side by side images at full height

Can someone help me achieve the layout shown in the image below? I want a container that spans full width and height with 2 images and other elements inside. The images should be full-height within the container, but also automatically adjust their width t ...

Guide to positioning a button on top of another button using Vuetify

I'm facing an issue where I want to add a button on a clickable card, but clicking the button also triggers the card click event. Here's my current code snippet: <v-card @click="show = !show"> <v-img src="https://cdn.vuetifyjs.com/im ...

Typewriter Effect: The caret is advancing too quickly

I am trying to achieve a typewriter effect on my text, but the blinking cursor is extending further than the actual text. See Image for Problem Here is the code I am using: HTML: <div class="title"> <h1>Hi, I'm ConnerDE< ...

Overlap of images on a responsive CSS page

I'm venturing into the world of responsive design for the first time, but I seem to be facing some challenges. In the initial screenshot, you can see that the tower image is overlapping a div and I can't figure out where I went wrong. Check out t ...

Website issues being displayed by Internet Explorer

Greetings! Let me begin by saying how wonderful this page is - I have already found many answers here before. Please forgive any errors in my English :) In my efforts to optimize my website for SEO and increase its speed, I made several changes. The site ...

"Adding an Image to Another Image in HTML or JavaScript: A Step-by-Step

Hello there! I'm currently working on creating a status bar where I can add an image after another image. Let me explain my idea clearly. So, I have two images in GIF format: one is white and 10x10px, and the other one is black and also 10x10px. On ...

Padding in Bootstrap rows is not uniform

My grid template has 3 columns, but the first row in one of the columns is slightly pushed. Which CSS property should I adjust to fix this issue? Using Bootstrap framework.https://i.sstatic.net/CLSYK.png <div class="col"> <div class="col-sm"> ...

What is the best way to ensure an image fills the entire space within a Bootstrap modal container?

I'm having some issues with my bootstrap modals that contain images. When the images are long vertically, a scrollbar appears which is not ideal. I tried to solve this by setting a max-height for the modal-content and making the modal-body (where the ...

A critical bug has been discovered in Safari and Chrome where HTML5 canvas fails to work in a specific scenario, while Firefox performs perfectly

I am currently using html5 canvas to develop a function-graph software. There seems to be a critical bug that I cannot figure out. For example, when I use the following code: yOFxGraph(plotFUNCinputYofX,-4,5,-4,4,"rgb(66,44,255)",1); everything operates ...

"Create a web resume that is easy to print and

I'm currently working on implementing a print-friendly feature for a Bootstrap 4 template resume/CV. My goal is to minimize the spacing between sections when the document is printed, allowing users to easily convert it into a PDF or Word format. You ...

Showing CKEditor Content in Stimulsoft Report and WebForm Grid View: A Step-by-Step Guide

Is there a way to show CKEditor data in Stimulsoft and WebForm Grid View without HTML tags? I have been using CKEditor to save my data in SQL, where it gets saved as HTML tags in the database. However, when I try to display the data in StimulSoft, it sho ...

Tips for optimizing CSS when two classes share common styles

I have limited experience with CSS, but I am currently using it for a project. I encountered a situation where two CSS classes share some common properties: .KPIDashboardContainerInertiaMatrixCell_data { display: table-cell; font-size: 18px; padding-top: ...

What steps can I take to ensure that my DIV completely surrounds the floating DIVs within it?

Having an issue with my CSS. The problem arises in a specific area of my screen where there are variable numbers of buttons enclosed within a DIV named sbr_btn. The layout is as follows: Outer DIV Outer DIV Outer DIV 01 02 03 04 05 06 07 08 ... ...

Issue with overlapping divs

My challenge involves two divs on opposite sides of the page that should move closer as the screen size decreases, with no space between them. However, I can't seem to prevent them from getting too close together, causing the links and information sec ...

Hovering over a white background will darken the CSS styling

A stunning image on a dark background Displayed on Hover (right) An elegant picture with a white background Activated on hover (incorrectly) I would like it to have a darkened effect on the white background as well. CSS Styling .photo background: n ...

Position the label to the right and left of the input field for alignment

I've been struggling to create an input form with labeled using a dl, dt, dd list. I want the first label on the left, the second on the right, and the input in the center. However, no matter what I try, I can't get the second label to align corr ...

Why doesn't the Kellum method for CSS image replacement work with button elements?

Recently, I discovered that the kellum method doesn't work as effectively with HTML button elements. Specifically, it does work but requires additional text indent. To sum it up, here is the technique: text-indent: 100%; white-space: nowrap; overflow ...

Can a div be easily centered within its parent container?

In my coding project, I've encountered an issue with centering a #text div inside another #box div. My current attempt at centering it involves CSS code like this: #box { width: 50%; #text {position: relative; margin-left: 48%;} } While this cod ...

Aligning inline form controls with Bootstrap and Syncfusion widgets

I'm facing a challenge aligning the Syncfusion JavaScript widget - a dropdownlist - to be displayed inline with the label. I am using Bootstrap 3. I haven't been successful in achieving a nice alignment, meaning getting the middle of the label p ...