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

Is there a regular expression that can be used to extract sentences from HTML

As I delve into the world of HTML parsing, I'm starting to understand that using regular expressions for this task is not always the best approach. However, in my Perl class, I am currently experimenting with utilizing regular expressions to extract a ...

Transferring a parameter from link_to to a popup window in a Rails application

I am facing an issue with passing an object to my modal in Rails. The object has a table of results with an attribute "email", but I seem unable to pass it so that I can use it within my modal. index.html.erb <div class="modal fade bs-example-modal-lg ...

(HTML)(PHP)Restricting Access to Login Page

My user page is the only one being accessed for both users and admins. Within my database and table, each are divided by 'type', where 0 represents a user and 1 represents an admin. Here is my code: <?php session_start(); include 'dbcon. ...

Ways to repair an element

Can anyone help me troubleshoot the issue with my count text on a div block? It seems to be affected by changes in screen size. Normal https://i.stack.imgur.com/RZkgr.png On screen resize https://i.stack.imgur.com/hTdCG.png The Code: span.right-cor ...

A method for expanding the menu upwards to make room for newly added items

Looking at the images below, I have a menu that needs new items added to it while maintaining the position of the lower-left corner. Essentially, each time an entry is added, the menu should expand upwards from "the upper-left corner" while keepi ...

Preventing JavaScript from refreshing the page when using location.replace for the second time with the identical URL

I've encountered an issue while using location.replace to reload a page that relies on GET variables for displaying a success message. The problem arises when the URL specified in the location.replace call is identical to the current one, causing the ...

Accessing an element by its ID with the help of a looping

Looking to retrieve a string from my database and insert it into my paragraphs: <p id="q_1"></p> <p id="q_2"></p> The following code works correctly: $.get("bewertung/get/1", function (data) { document.getElementById("q_1") ...

The website is failing to adapt properly to smaller screen sizes

I would share some code here, but it might be easier for you to just check out the URL instead. The issue is that the website was responsive across different screen sizes at first, but after making some changes in the CSS and HTML, it's not working pr ...

What is the best way to target specific text within the DOM without including text within attributes?

We are currently displaying search results from various posts on our website, and we would like to highlight the search terms in the displayed content. Currently, we are implementing this functionality on the backend using PHP. We iterate through the post ...

Using PHP to nest a table within HTML and populate it with numbers ranging from 1 to 100

I am struggling with a practice question that involves creating a table using PHP nested in HTML. I need some help fixing it and understanding where I went wrong. The task is simple: generate numbers from 1 to 100 in a table, with each row containing 10 n ...

how to prevent autoscrolling in an angular application when overflow-x is set to

In my socket event, I am using $scope.items.unshift(item) to place the new item at the top of the list. The html code includes <ol ng-repeat="item in items"><li>{{item.name}}</li></ol> An issue arises when a new item is added whil ...

What events precede and follow a keydown action in a Textarea field?

I need to prevent users from pressing function keys (F1, F2, etc.), the tab key, and any other characters from being added. The code below is supposed to achieve this on my website but it's not working. document.getElementById("code").addEventList ...

Is there a way to integrate a Python script or code directly into an HTML document?

I am currently facing a challenge while working on an assignment. The task at hand requires me to establish a connection with an Oracle database using Python in order to retrieve information about a specific table. Subsequently, I need to display this data ...

The Stripe card element seems to be missing from the form

I'm a newcomer to angularjs and currently working on integrating version 3 of the stripe api into my angular application. I followed the steps provided on the stripe website, but faced an issue when trying to incorporate their code which is primarily ...

Stopping individuals from engaging in the act of spamming a picture upload form

I am currently developing an innovative Image hosting platform, and I am faced with a crucial challenge – how can I effectively prevent users from continuously refreshing the form, causing the image to be repeatedly uploaded until my disk space allocat ...

Creating a design with multiple `<thead>` and `<tbody>` elements

Seeking advice on usability and design elements. I am currently utilizing JQuery to hide/show entire sections within a single large table structure. The layout consists of multiple thead sections, with the main header at the top, followed by sub-headers fo ...

React does not allow for images to be used as background elements

I am currently working on a web page and I have attempted to use both jpg and png images as backgrounds, but they do not seem to display on the page. import './Entrada.css' const Entrada = () => { return( <div style={{ b ...

What are effective methods for testing HTML5 websites?

I'm currently working on a project to create a dynamic website using HTML5. The first page will prompt the user for specific inputs, and the following page will adjust accordingly based on these inputs. My main concern now is how to effectively test ...

What is the best way to make changes to the DOM when the state undergoes a

I've programmed the box container to adjust dynamically based on input changes. For instance, if I entered 1, it will generate one box. However, if I modify the input to 2, it mistakenly creates 3 boxes instead of just 2. import React from 'rea ...

Populate HTML Table with Array Elements

I need assistance with inserting array values into an HTML table. The table should have 5 columns and enough rows to accommodate all the information. I envision it looking like this: 1 | 2 | 3 | 4 | 5 11 | 22 | 33 | 44 | 55 111 | 222 | 333 | 444 | 555 ...