I have four buttons (a
-elements) that should take up the entire available horizontal space of a div
with some margin space in between. The four buttons should all have equal width (their contents may flow). I currently do this as follows:
css:
div {
text-align: justify;
}
div::after {
content: "";
display: inline-block;
width: 100%;
}
a {
display: inline-block;
text-align: left;
width: 20%; /* not precisely 20% and 4%, that's just for */
margin-right: 4%; /* the sake of the question */
min-width: 10em;
}
html:
<div>
<a href="/">btn 1</a>
<a href="/">btn 2</a>
<a href="/">btn 3</a>
<a href="/">btn 4</a>
</div>
When the page gets narrower, the buttons shrink until they are 10em wide. Then they break the row, leaving three buttons on row 1 and one button on row 2.
However, the buttons are now still at 10em. I would like them to grow to a little less (to allow for margins) than 33%. If the page gets even narrower, and the buttons shrink again down to 10em, the row should break again, leaving two buttons on row 1 and two buttons on row 2. These buttons should take up a little less than 50%.
And finally, if the outer div shrinks to less than 20em, four rows are expected with buttons of 100% wide (the outer div may have a minimum width of 10em here, that's fine).
To get an ASCII-art impression (I'm no artist):
initial "full" page width
+-----------------------------------+
|[ab ] [cdef ] [ghi ] [jk lm ]|
|[ ] [ ] [ ] [ ]|
page shrinks
+-------------------------------+---+
|[ab ] [cdef ] [ghi ] [jk lm]|
|[ ] [ ] [ ] [ ]|
page shrinks further, last btn flows its content
+---------------------------+-------+
|[ab ] [cdef] [ghi ] [jk ]|
|[ ] [ ] [ ] [lm ]|
shrinking even further, too narrow for four btn's row 1 has 3 btn's, full width, row 2 has 1 btn, equal width
+-----------------------+-----------+
|[ab ] [cdef ] [ghi ]|
|[ ] [ ] [ ]|
|[jk lm] |
|[ ] |
shrinking even further, all four btn's shrink
+--------------------+--------------+
|[ab ] [cdef] [ghi ]|
|[ ] [ ] [ ]|
|[jk ] |
|[lm ] |
shrinking even further, too narrow for three btn's
+-----------------+-----------------+
|[ab ] [cdef ]|
|[ ] [ ]|
|[ghi ] [jk lm ]|
|[ ] [ ]|
shrinking further
+-------------+---------------------+
|[ab ] [cdef]|
|[ ] [ ]|
|[ghi ] [jk ]|
|[ ] [lm ]|
shrinking further, too narrow for two btn's
+---------+-------------------------+
|[ab ]|
|[ ]|
|[cdef ]|
|[ ]|
|[ghi ]|
|[ ]|
|[jk lm ]|
|[ ]|
Thus, actual result:
+-----------------+-----------------+
|[ab ] [cdef] |
|[ ] [ ] |
|[ghi ] [jk ] |
|[ ] [lm ] |
Intended result:
(optimal width of buttons)
+-----------------+-----------------+
|[ab ] [cdef ]|
|[ ] [ ]|
|[ghi ] [jk lm ]|
|[ ] [ ]|
Tried
I have tried to use flexbox. However, I still do not feel confortable using it because of older browsers (and with such a big break, much usability is at stake) and moreover, I could not get further than having the last button take up the entire space (and thus becoming 3x bigger than the other buttons). Such as:
+-----------------------+-----------+
|[ab ] [cdef ] [ghi ]|
|[ ] [ ] [ ]|
|[jk lm ]| <-- this btn is as wide as _three_ other buttons
|[ ]|
I have also tried display: table
. However, this does not account for the "equal width" requirement and the cells do not break if the page is too narrow, instead the 100% width table grows beyond its container.
How is this solvable?
I am looking for a solution WITHOUT javascript.