What is the best approach to decrease the size of a button in HTML and CSS when the text size is also reduced?

Check out this code snippet! I'm new to coding, so let me know if you see any errors or ways it could be improved.

button {
  font-size: 10px;
  font-family: roboto, Arial;
  color: white;
  background-color: rgb(59, 102, 241);
  border-radius: 100px;
  border-width: 0px;
  width: 190px;
  height: 25px;
  caret-color: transparent;
  transition: all 0.3s;
}

button:hover {
  background-color: rgb(87, 125, 250);
  cursor: pointer;
}

button:active {
  font-size: 9px;
  padding: 1px;
}
Feel free to add HTML here!

I'm trying to make the button 'shrink' when clicked, but adjusting the width and height isn't quite working as expected - it shrinks to a corner instead of the middle. Any tips on how to fix this?

Answer №1

Within your Button CSS, the width and height are set without any padding adjustments. However, in the active state, there is a slight alteration in the padding but not in the width and height.

There are a couple of approaches to achieve the desired outcome.

You can opt to disregard the width and height attributes and utilize padding instead, or modify the width and height in the active state.

I have provided two examples of buttons demonstrating each method of modifying either the width/height or padding.

button {
  font-size: 10px;
  font-family: roboto, Arial;
  color: white;
  background-color: rgb(59, 102, 241);
  border-radius: 100px;
  border-width: 0px;
  caret-color: transparent;
  transition: all 0.3s;
}

button:hover {
  background-color: rgb(87, 125, 250);
  cursor: pointer;
}

button:active {
  font-size: 9px;
  padding: 5px 40px;
}

.padded{
  padding: 10px 80px;
}

.padded:active {
  font-size: 9px;
  padding: 5px 40px;
}

.dimensions{
    width: 190px;
    height: 35px;
}

.dimensions:active{
    width: 130px;
    height: 25px;
}
<button class="padded">Pad Change</button>
<button class="dimensions">Width/Height Change</button>

Answer №2

To achieve the desired effect, you must center your button both horizontally and vertically.

I was able to accomplish this using an old trick for centering elements. Flex appears to be the optimal choice for centering items in both directions.

button {
  font-size: 10px;
  font-family: roboto, Arial;
  color: white;
  background-color: rgb(59, 102, 241);
  border-radius: 100px;
  border-width: 0px;
  caret-color: transparent;
  transition: all 0.3s;
}

.container {
position: relative;
width: 100%;
}

.container button {
  margin: 25px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%)
}

button:hover {
  background-color: rgb(87, 125, 250);
  cursor: pointer;
}

<button:active {
  font-size: 8px;
  padding: 5px 20px;
}

.dimensions{
    width: 190px;
    height: 35px;
}

.dimensions:active{
    width: 130px;
    height: 25px;
}
<div class="container">
<button class="dimensions">Change button</button>
</div>

Answer №3

To perfectly center the button, you can utilize the display:grid; property along with place-items:center; on a wrapper element. Play around with this by adjusting the font size in the wrapper container - since EM was used for measurement, you can easily modify all sizes in one place.

body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 16px;
}

.button-container {
  /* uncomment this to see your original size smaller button */
  /* font-size: 0.625em; */
  display: grid;
  place-items: center;
  border: solid #00FF00 1px;
  padding: 1em;
}

.my-button {
  font-size: 1em;
  font-family: roboto, Arial, sans-serif;
  color: #FFFFFF;
  background-color: #3b66f1;
  border-radius: 6.25em;
  border-width: 0px;
  width: 12em;
  height: 1.5em;
  caret-color: transparent;
  transition: all 0.3s;
}

.my-button:hover {
  background-color: #577dfa;
  cursor: pointer;
}

.my-button:active {
  font-size: 0.75em;
  padding: 0.25em;
  background-color: #577dfa44;
}
<button type="button" class="my-button-test">Click me then click TAB to focus:My Button and it spacebar to make it "active"</button>
<div class="button-container">
  <button type="button" class="my-button">My Button</button>
</div>

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

Navigate to the previous or next page using JavaScript

I've been struggling to figure out how to navigate one page up and down using simple links, but I haven't found a solution that works for me. The URL I am working with is: . When the "next page" button is clicked, it should go to ?page=2, and whe ...

Moving data from one table to another and making changes or removing it

After successfully adding data from one table to another using .click, I encountered an issue. Whenever I utilize the search field in the top table, it clears the appended rows in the bottom table. I am looking for a solution to have these tables generate ...

The background image on my CSS is not showing up

I just added the file to my git repository and shared the link here. Is there a specific way to do this? html{ background-image:url("https://github.com/vindhya97/images/blob/master/pinkbackground.jpg"); } ...

Is there a way for me to access the information on a web page?

Currently, I am attempting to extract web page data as a string in order to parse it. Unfortunately, I have not been able to find any suitable methods in qwebview, qurl, and other resources. Can anyone offer assistance? My environment is Linux, C++, and Qt ...

Determining html column values using a related column and user input

Is there a way to populate an HTML table column (using Javascript or jQuery) based on the values in another column and an input field? For instance, if I input the number 115 into the field, then the advance column should display a value of 1 for each ath ...

Is it possible for a kraken.js backend to manipulate the content of a webpage?

Currently using kraken.js, which is an express.js framework, to construct a localized website. Within the header section, there are 3 language options provided as links. FR | EN | DE My goal is to have the link corresponding to the currently set locale ( ...

Show product name when hovering over the image

Trying to achieve a hover effect where product titles appear in a contained box when hovering over the product image on the bottom overlay, instead of below the image itself. Tried CSS code recommended by Shopify, but it had no effect: <noscript> ...

Encountering a MySQL error upon inputting an apostrophe into the form

I'm facing an issue where I can't save a form with an apostrophe to the Database. Here's the code I'm using: <?php $abouttitle=$_POST[abouttitle]; $aboutcontent=$_POST[aboutcontent]; $aboutside=$_POST[aboutside]; $abouts ...

Is it necessary for the input element to be placed within a form?

One of the components I have in my React project has a specific purpose: allowing the user to input a word, press enter or click a link, and go to that word's definition page. 'use client'; import { FontAwesomeIcon } from '@fortawesome ...

Maintain the div's aspect ratio by adjusting its width according to its height

I am seeking a way to maintain the width of the .center div in relation to its height, following a 4:3 aspect ratio (4 units wide for every 3 units high). The height of the div should be set to 100%, and I also need to align the div horizontally at the cen ...

What steps can be taken to update the ID's of <tr> and <td> elements within a table after deleting a row?

I am working on a dynamic table with 5 rows, each row containing 3 columns - partNO, quantity, and a delete button. Each row is assigned a unique ID for identification purposes. If a user deletes the second row, I want the IDs of the remaining rows to be ...

Navigating through child elements within a div using JavaScript

I recently coded a div using this snippet... let sidebarBox = document.createElement("div"); sidebarBox.id = "sidebarBox"; ...and then I created a second div like so... let sidebarAd = document.createElement("div"); sidebarAd.className = "sidebarAd"; B ...

Aligning content in a div with proportional elements, maintaining left alignment of the elements

Trying my hand at creating an HTML template to display TV shows as cover artwork. The layout is flexible, showing up to five shows per row on larger screens and a minimum of two on smaller screens like smartphones. It's been a while since I've wo ...

Is it possible to execute a PHP script within a .css file and ensure it functions correctly?

Recently, I've been making some changes to the .css file in order to get my PHP codes to work. However, when I insert them as usual, they appear as regular text. I'm curious to find out if it's possible to execute a PHP file within a .css fi ...

Make the background image draggable while maintaining its size with background-size:cover

I'm working on a fixed div with an image as the background, set up like this: <div style=' width:230px; height:230px; background-repeat: no-repeat; background-image:url(img/myimage.jpeg) background-size: cover;'&g ...

What is the best way to clear and fill a div without causing it to resize?

I am faced with a challenge involving four thumbnail divs labeled .jobs within a #job-wrap container. When a .job is clicked, I want the #job-wrap to fade out, clear its contents, load information from the selected .job, and then fade back in. However, whe ...

Tips on aligning images of various sizes with text within a row using CSS

Looking for help aligning four different height (ranging from 160-180px) svg images with text positioned directly below each image. I want the images to be neatly aligned in a row, but I'm struggling to align the short text underneath them in a single ...

Improving the efficiency of CSS animations

Is it possible to improve the performance of CSS animation by preventing it from running when the animated object is not visible on the viewport? Thank you! ...

What is the best way to arrange my crimson blocks to sit next to each other?

Is there a way to align the red boxes side by side with some space in between them for separation, and how can I ensure that the signin/signup button appears against the blue background of the header? I attempted using float but encountered issues. The d ...