Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make buttons the same width without specifying the exact size

Tags:

html

css

I have five buttons. I want them to take up the available width of their parent div, each being equally sized:

<div style="width: 100%; background-color: red;">
    <button type="button" style="width: 20%;">A</button>
    <button type="button" style="width: 20%;">B</button>
    <button type="button" style="width: 20%;">C</button>
    <button type="button" style="width: 20%;">D</button>
    <button type="button" style="width: 20%;">E</button>
</div>

Is there a way I can do this without having the manually figure out that they each require 20% width? I want to remove a button at runtime, and that means I have to go and update each remaining button again to width=25%.

I am just checking if there's a more automated way of doing it.

like image 587
user291701 Avatar asked Nov 20 '12 22:11

user291701


People also ask

Should buttons have fixed width?

Fixing the width for buttons will give a coherent aspect to the interface. Yet, it has the risk of not being able to handle further buttons with perhaps longer labels. This reason makes it incompatible with a fixed guideline. You can always emphasise one particular button by playing on size, colour and padding.

How do you make a button occupy full width?

display: block and width: 100% is the correct way to go full width but you need to remove the left/right margin on the button as it pushes it outside the containing element. for more information on the box-sizing property, read the MDN docs.

How do I make all selected controls the same size?

From the Format menu, choose Make Same Size. From the cascading menu, choose one of the following: Height, to make all selected controls the same height as the dominant control. Width, to make all selected controls the same width as the dominant control. Both, to make all selected controls the same height and width as the dominant control.

Is there a way to force buttons to be the same size?

Is there a way of forcing the buttons to all be the same size, regardless of the text label? yep. Basically, if you want them the same size, you have to give the same width. First, in your html put the items in a list, and put the list inside a division (for this example I’ll call the div “navigation”)

What is the difference between height and width in VBA?

Height, to make all selected controls the same height as the dominant control. Width, to make all selected controls the same width as the dominant control. Both, to make all selected controls the same height and width as the dominant control. Have questions or feedback about Office VBA or this documentation?

How do I make a button with a Div width?

get the width of the div, put into a variable. divide the width by the number of buttons and put that answer into a variable. run a function that creates a button at that width by however many times required.


1 Answers

The simplest way, and the most robust way, is to use a table:

<style>
.buttons { 
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background-color: red; 
}
.buttons button { 
  width: 100%;
}
</style>
<table class=buttons>
 <tr>
    <td><button type="button">A</button>
    <td><button type="button">B</button>
    <td><button type="button">C</button>
    <td><button type="button">D</button>
    <td><button type="button">E</button>
</table>

(This won’t improve your reputation among colleagues these days if they see your code, though it actually solves the problem probably better than any alternative. So you might consider doing the next best thing: use div markup and display: table etc. Fails on old browsers that don’t support such CSS features.)

like image 82
Jukka K. Korpela Avatar answered Oct 26 '22 05:10

Jukka K. Korpela