The button now has a stylish 5px border, making it slightly larger in size. However, it seems

How can I add an active class with a 5px border-bottom on a button without increasing the button size by 5px? The box-sizing property is not working for me. Is there a simple solution to achieve this?

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
}

.active {
  border-bottom: 5px solid yellow;
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">button</button>
</div>

Answer №1

When it comes to the border, using box-sizing: border-box will be effective if a fixed height is assigned to the button element.

Approach1 with fixed height

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
  height: 53px;
}

.button.active {
  border-bottom: 5px solid yellow;
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">button</button>
</div>

Approach2 without fixed height(Using :before css pseudo selector)

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
  position: relative;
}

.button.active:before {
  content: "";
  border-bottom: 5px solid yellow;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">button</button>
</div>

Answer №2

So, let me explain why simply adjusting the box-sizing property won't fully solve the issue at hand. When you instruct CSS to apply 20px of padding to both buttons, it complies without hesitation. Then, when you decide to add a 5px border to the active button, CSS once again follows your command. Now, if you want both buttons to appear equal in size, the only way to achieve this is by reducing the padding of the first button. Here's how you can accomplish this:

.active {
  border-bottom: 5px solid yellow;
  padding: calc(20px - 5px);
}

I've utilized the calc() function here to accommodate those utilizing rems or ems as units.

Answer №3

Method 1: box-shadow

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
}

.active {
  box-shadow: 0 -5px 0 yellow inset
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">button</button>
</div>

Method 2: pseudo-element ::after

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  /* Add Position Relative for Button */
  position: relative;
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
}

.active::after {
  content: '';
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  height: 5px;
  width: 100%;
  background: yellow
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">button</button>
</div>

Answer №4

How about enhancing the default button style by incorporating a sleek black 5px border and making the active button standout with a vibrant yellow color?

.button {
  background: black;
  border-bottom: 5px solid black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
}

.active {
  border-bottom: 5px solid yellow;
}

Answer №5

From what I gather, it seems like you're looking to add a shadow effect to the button. Applying a shadow to the button is a good idea.

Here's an example:

box-shadow: 10px 10px 5px #aaaaaa;

You can then customize it according to your preference.

Answer №6

To ensure that your button has the correct height, include the height property within the .active class along with box-sizing: border-box. Once you make these adjustments, your button should be good to go! :)

*,
*:after,
*::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 2px solid blue;
}

.button {
  background: black;
  width: 150px;
  border: 0;
  padding: 20px;
  margin: 0;
  color: white;
}

.active {
border-bottom: 5px solid yellow;
box-sizing: border-box;
height: 55px;
}
<div class="container">
  <button class="button active">button active</button>
  <button class="button">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

What are some strategies for increasing my website's mobile responsiveness?

I've recently completed my online portfolio, and it appears to be functioning properly on desktop computers. However, I'm encountering responsiveness issues when viewing it on other devices or smartphones. Even though I have included the necessar ...

Code that changes every occurrence of a particular filtered selection of HREF values to a different value

When faced with the limitation in Firefox where links cannot be opened in a new tab if they have a HREF tag diverting to a function, it might be necessary to utilize a script to convert them to an actual HREF. Understanding the functionality of foo: func ...

The dimensions of the HTML table do not adjust properly when new items are being appended using JavaScript

I utilized this HTML Code to generate a table: <div class="noten_tabelle"> <table id="grades_table" style="width:100%"> <tr> <th>Subject</th> <th>Oral</th&g ...

What is the process for displaying a PHP array in HTML5 audio and video players?

I am currently working with two PHP arrays. The first array, array "a," contains strings that represent paths for MP3 files on the server. The second array, array "b," contains strings representing paths for MP4 files. For example: $test = 'a.mp3&ap ...

jscrollpane does not work properly in Firefox, but it functions correctly in Chrome

I am currently utilizing jScrollpane to display a combination of images and a div in a horizontal format. While I have successfully implemented it on Chrome, it appears that Firefox is not applying the CSS correctly to prevent images from wrapping, resulti ...

Detecting collisions using CSS animation

I'm currently working on a unique "game" project. Check out the code snippet here: jsfiddle function update() { coyote.applyForce(gravity); coyote.edges(); coyote.update(); cactus.update(); if (coyote.intersects(cactus)){ alert("colisio ...

Unable to post form data into database due to Javascript undefined data situation

I've been working on an HTML form that can interact with a PHP API to retrieve client data and then update user stats in a MySQL database. Currently, I am converting the data into a JSON object and using JSON.stringify to create a string for sending ...

Understanding the relationship between CSS height and line-height can help developers create more

Is there a way to use CSS to set the height of a box based on its own line-height or its parent's line-height? This feature would make it much simpler to create text areas that are a specific multiple of lines, for example, displaying exactly three l ...

Is there a white outline on the Cordova Text Font?

Currently working on a Cordova app where I have encountered an issue with the text display. The problem lies in the white outline surrounding the text, which is not visually appealing. In my code, I have a div with the style attribute background-color: ye ...

Animate the service item with jQuery using slide toggle effect

Currently, I am working on a jQuery slide toggle functionality that involves clicking an item in one ul to toggle down the corresponding item in another ul. However, I am encountering difficulties in linking the click event to the correct id and toggling t ...

Regular expression that removes attributes from all HTML tags except <a>

$text = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>', $text); Greetings. I came across a preg_replace function that identifies all HTML tags and removes their attributes. However, I need to make an exception for t ...

When it comes to Shopify Schema, the section settings are functional within the <style> tag, whereas the block settings do not seem to have the same

While working on updating a section with multiple blocks, I encountered an unusual issue. My goal was to incorporate a new block into an existing section. The schema has been set up correctly, and everything is functioning as anticipated. However, the prob ...

Switching up the image using a dropdown selection menu

I am struggling with updating an image based on the selection made in a dropdown menu. I am quite new to javascript, so I believe there might be a simple issue that I am overlooking. Here is my attempt at doing this: JS: <script type="text/javascript" ...

Is there a way to display .form-check-inline buttons on a different line than their corresponding label?

I'm trying to create a form with radio buttons and using Bootstrap 4 to align them horizontally with the .form-check-inline class. However, this causes the input labels to be on the same line as the buttons: <html> <head> <link ...

Adding an event listener to detect left or right mouse clicks - using onclick doesn't capture right clicks

I'm currently in the process of applying for an Internship through this Internship Link One thing that caught my attention right away is the issue with uploading or pasting a cover letter. When attempting to upload or paste a cover letter, it redirec ...

show textboxes positioned in the middle of a slanted rectangle

Is there a way to place centered textboxes inside each of the colorful boxes in the image below? Update: I've posted my feeble attempt below. Admittedly, design is not my forte and I'm struggling with this task. p.s. To those who downvoted, tha ...

The NVD3 chart embedded in a React application generates HTML elements that persist on the page indefinitely without fading away

In my React application, I integrated an NVD3 scatter chart. However, I have encountered an issue with the tooltip not disappearing when hovering over the chart: https://i.stack.imgur.com/6lLok.png After moving the cursor away from the chart, the tooltip ...

The battle of line-height versus padding for achieving vertical centering with one-lined text

One-lined text can be vertically centered using either the line-height property or by adding padding-top and padding-bottom. What are the advantages and disadvantages of each method? body { font-size: 12px; font-family: ...

Place two divs side by side with the second div filling up the remaining space on the line

Hello there, can anyone lend a hand with this problem? This is the code I have been working with: <html> <body> <div style="width=100%"> <div style="float:left; background-color:Red; height:100px">Red</div> <div st ...

PHP time counting script

Can a countdown script be created using PHP? Websites like 4shared.com or megaupload.com utilize a similar script that starts counting from 20 seconds to 0. I am able to see how many seconds are left on the web page before I can click on the download but ...