Fitting elements into a grid in a responsive way - html

Consider a <div> element that contains <button> elements:
<div id="box">
<button class="button">1</button>
<button class="button">2</button>
<button class="button">3</button>
...
</div>
I want to fit these elements into a grid pattern, so that
buttons are of equal dimensions and have a fixed minimum width,
a maximal amount of buttons is stacked on each row,
button width is adjusted so that each row is filled completely,
and this behavior persists when #box is resized.
Is there a way to do so using native HTML/CSS features (without using JavaScript)? I tried using the new CSS Flexbox layout:
#box {
display: flex;
width: 500px;
height: 500px;
}
.button {
flex: 1 1 auto;
min-width: 40px;
height: 40px;
}
But this doesn't work..
Thanks for your help!

You can easily use CSS grid layout.
I prefer it over flexbox.
<div id='grid'>
<button class='button'>1</button>
<button class='button'>2</button>
<button class='button'>3</button>
<button class='button'>4</button>
<button class='button'>5</button>
<button class='button'>6</button>
<button class='button'>7</button>
<button class='button'>8</button>
<button class='button'>9</button>
<button class='button'>10</button>
<button class='button'>11</button>
<button class='button'>12</button>
<button class='button'>13</button>
<button class='button'>14</button>
<button class='button'>15</button>
<button class='button'>16</button>
</div>
<style>
#grid {
display: grid;
grid: 50px / auto-flow;
}
</style>
Here is the codepen.
https://codepen.io/kenanbalija/pen/bYYarj

You can use CSS flex for this, as you suspected.
flex-wrap is what you were missing, as well as using percentages for responsiveness.
#box {
display: flex;
flex-wrap: wrap;
}
#box > * {
flex: 1 0 100px; /* each button should be at least 100px wide */
}
<div id='box'>
<button class='button'>1</button>
<button class='button'>2</button>
<button class='button'>3</button>
<button class='button'>4</button>
<button class='button'>5</button>
<button class='button'>6</button>
<button class='button'>7</button>
<button class='button'>8</button>
<button class='button'>9</button>
<button class='button'>10</button>
<button class='button'>11</button>
<button class='button'>12</button>
<button class='button'>13</button>
<button class='button'>14</button>
<button class='button'>15</button>
<button class='button'>16</button>
</div>

Related

How to make two buttons next to each other be the same size always

Notice(in mobile laytout): Both buttons have the same height. The height is determined by the button with a long name. And the text in the first button is centered vertically and horizontally.
Notice(in desktop laytout): Both buttons have the same width. The width is determined by the width button with a long name. Meaning that the buttons do not have to occupy 50% of the screen each. And the text in the first button is centered. Also the two buttons are centered with some space between them.
I have two buttons next to each other, as shown in the code below:
.my-buttons {
display: flex;
justify-content: center;
button {
cursor: pointer;
}
}
<div class="my-buttons">
<button type="button" class="btn btn-outline-primary">
1st Button
</button>
<button type="button" class="btn btn-outline-primary">
Button with a long name.
</button>
</div>
How can I make it so that at all times the buttons are of the same width and height regardless of the content within them. And when the screen size is large the size of the buttons is to be determined by the size of the 'Button with a long name'.
When in small screen, like mobile. The height of the buttons should again be determined by the height of the 'Button with a long name'.
CSS grid can do this
.my-buttons {
display: inline-grid;
grid-template-columns: 1fr 1fr;
grid-gap: 5px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="text-center">
<div class="my-buttons">
<button type="button" class="btn btn-outline-primary">
1st Button
</button>
<button type="button" class="btn btn-outline-primary">
Button with a long name.
</button>
</div>
</div>
Adding a flex-basis of 50% will make both children 1/2 the width of .my-buttons
.my-buttons {
display: flex;
justify-content: center;
}
button {
cursor: pointer;
flex: 1 0 50%;
}
<div class="my-buttons">
<button type="button" class="btn btn-outline-primary">
1st Button
</button>
<button type="button" class="btn btn-outline-primary">
Button with a long name.
</button>
</div>
.btn {
flex: 1 1 0px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<div class="my-buttons d-flex">
<button type="button" class="btn btn-outline-primary">
1st Button
</button>
<button type="button" class="btn btn-outline-primary">
Button with a long name.
</button>
</div>
Set them so that their flex-basis is 0 and allow them to grow:

why aren't my html buttons aligning properly

I cant get the 4 buttons to align in two rows. I've tried using flexbox yet I can only get them to align in two rows of three buttons at the top and one at the bottom. Ideally the buttons would be in the centre of the page, equally distant from each other.
HTML code
CSS code
Result
You can do it this way using flexbox note that you have to make two containers as a parent to hold your buttons.
.button_tense{
width:300px;
height: 300px;
}
.container{
display: flex;
flex-direction: row;
justify-content: space-around;
}
<div class="container">
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
</div>
<div class="container">
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>
And using grid with this
.button_tense{
width:300px;
height: 300px;
}
.container{
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
}
<div class="container">
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>
I would recommend for you to see this
Guide To Grid
and this
Guide To Flexbox
And welcome to stackoverflow :)
You have to add divs around each of the 2 buttons you want in the same row. Code is below.
button{
margin:5px;
}
<div>
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
</div>
<div>
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>

How to place a div below another div and fill the size?

I am converting my desktop project into web, and I have this layout so far. My concern is how can I place my div next below another div and take/fill the parent width. My aim is to show (depending on the selected view: list view, kanban view, and form view) lt-list, lt-kanban, and lt-form just below the buttons. This is what I've done so far:
.lt-content { grid-area: content; height: 85vh;}
#lt-button {
vertical-align:top;
display: inline-block;
padding : 0;
margin : 0;
float : left;
}
#lt-view {
vertical-align:top;
display: inline-block;
padding : 0;
margin : 0;
float : right;
}
#lt-list {
display: inline-block;
padding : 0;
margin : 0;
float : left;
background: #9ec4ff;
width: inherit;
}
#lt-kanban {
vertical-align:top;
display: inline-block;
padding : 0;
margin : 0;
float : left;
background: #a3ffa3;
}
#lt-form {
vertical-align:top;
display: inline-block;
padding : 0;
margin : 0;
float : left;
background: #fff5b7;
}
and here is my html:
<div class="lt-content">
<div id="lt-button" style="position: relative">
<div class="btn-group btn-group-sm" role="group" style="background: " aria-label="Basic example">
<button type="button" class="btn btn-default btn-sm" style="border-radius: 0;">CREATE</button>
<button type="button" class="btn btn-default btn-sm" style="border-radius: 0;">EDIT</button>
<button type="button" class="btn btn-default btn-sm" style="border-radius: 0;">DELETE</button>
<button type="button" class="btn btn-default btn-sm" style="border-radius: 0;">SAVE</button>
<button type="button" class="btn btn-default btn-sm" style="border-radius: 0;">CANCEL</button>
</div>
</div>
<div id="lt-view">
<div class="btn-group btn-group-sm" role="group" aria-label="Basic example">
<button type="button" class="btn btn-floating cyan btn-sm" style="border-radius: 0;"><i class="fas fa-list" aria-hidden="true"></i></button>
<button type="button" class="btn btn-floating cyan btn-sm" style="border-radius: 0;"><i class="fas fa-th-large" aria-hidden="true"></i></button>
<button type="button" class="btn btn-floating cyan btn-sm" style="border-radius: 0;"><i class="fab fa-wpforms"></i></button>
</div>
</div>
<div id="lt-list">
This is a list view....
<br>
This is a list view....
<br>
</div>
<div id="lt-kanban" style="position: relative">
This is a Kanban view....
<br>
This is a Kanban view....
<br>
</div>
<div id="lt-form" style="position: relative">
This is a Form view....
<br>
This is a Form view....
<br>
</div>
</div>
This is the image of my layout:
First of all, when you use the float: right/left property afterwards, the display: inline-block gets overridden.
You can get the desired results in a number of ways, grid layout, flex, etc. Using floats and the such to make it work won't be the best. Here's an example using flex. I got rid of all the floats, vertical-align and inline-blocks, and wrapped your buttons in a div and added a class.
Here you go
Change the css for #lt-button to be the following:
#lt-button {
display: block;
padding : 0;
margin : 0;
}
This will get the list view, kanban view, and form view under your buttons. After that you can use js/jQuery (or other scripting language) to show and hide divs based off clicks.
Add two divs, one that wraps lt-button and lt-view, and another (with id board) that wraps the lt-list, lt-kanban and lt-form
Then add this css:
.lt-content {
display: flex;
flex-flow: column;
}
#board {
display: grid;
grid-auto-flow: column;
height: 100%;
}
That is, if your objective was to make three columns with those colors, but it will give an idea.

HTML Formatting Aligning Buttons in a Box

I've been brushing up on html, css and javascript by working on a little project that I thought would be useful. It is a recipe finder based on what recipes I have in home. Anyways, my question is about lining up a few of the elements in the html page.
The part of the html page in question is:
<form>
<select size=10 id="first"></select>
<button type="button" id="addAll" onclick="rightAll()">
Add All
</button>
<button type="button" id="removeAll" onclick="leftAll()">
Remove All
</button>
<button type="button" id="add" onclick="right()">
>>
</button>
<button type="button" id="remove" onclick="left()">
<<
</button>
<select size=10 id="second"></select>
</form>
My css document:
html, body {
height: 100%;
min-height: 100%;
}
select{
width: 300px;
}
Because all the elements are in a form tag they all appear inline. I tried putting the buttons in a table, but the tables formatting put the first select on one line, the table on a new line, and the second select on a new line.
Here is what my formatting looks like now:
and here is what I want it to look like (thanks to paint)
I'm sure I could figure out how to center the buttons vertically in the form, but if you could help me out with that too then I would really appreciate it!
HTML
<form>
<div id="sel1">
<select size=10 id="first"></select>
</div>
<div id="buttons">
<div id="buttons1">
<button type="button" id="addAll" onclick="rightAll()">
Add All
</button>
<button type="button" id="removeAll" onclick="leftAll()">
Remove All
</button>
</div>
<div id="buttons2">
<button type="button" id="add" onclick="right()">
>>
</button>
<button type="button" id="remove" onclick="left()">
<<
</button>
</div>
</div>
<div id="sel2">
<select size=10 id="second"></select>
</div>
</form>
CSS
html, body {
height: 100%;
min-height: 100%;
}
select{
width: 300px;
}
div {
float: left;
}
#buttons1, #buttons2 {
float: none;
text-align: center;
}
It is up to you to tweak paddings and margins to leave it at your desire.
you should use div if you want to extent your design further, table is not responsive directly and type of fixed layout with messy code , you may try use div and other css property .
anyway just for that middle section , you may try div inside your table td like this :
<div style="width:100%;">
<div style="width:50%;float:left;">
<button type="button" id="addAll" onclick="rightAll()">
Add All
</button>
<div>
<div style="width:50%;float:right;">
<button type="button" id="removeAll" onclick="leftAll()">
Remove All
</button>
<div>
<div style="clear:both;"></div>
</div>
<div style="width:100%;">
<div style="width:50%;float:left;">
<button type="button" id="add" onclick="right()">
>>
</button>
<div>
<div style="width:50%;float:right;">
<button type="button" id="remove" onclick="left()">
<<
</button>
<div>
<div style="clear:both;"></div>
</div>
Cheers !

Fill parent width equally [duplicate]

This question already has answers here:
CSS side by side div's auto equal widths
(5 answers)
Closed 8 years ago.
I have a container width some buttons in it:
<div class="button-container">
<button type="button" class="edit-quantity">Edit Quantity</button>
<button type="button" class="edit-price">Edit Price</button>
<button type="button" class="remove-item">Remove</button>
<button type="button" class="remove-all">Remove All</button>
</div>
http://jsfiddle.net/gRzAB/
How can I style the buttons and the container in a way that the buttons fill the container width equally, even if there is just 1 or 2 or 3 buttons?
Use display: flex from CSS3: Check my fiddle.
.button-container{
width:100%;
display: flex;
}
.button-container button{
width:100%;
}
You can use display:table; and display:table-cell; if you add a wrapping div around your buttons :
FIDDLE
HTML :
<div class="button-container">
<div>
<button type="button" class="edit-quantity">...</button>
</div>
<div>
<button type="button" class="edit-price">...</button>
</div>
<div>
<button type="button" class="remove-item">...</button>
</div>
<div>
<button type="button" class="remove-all">...</button>
</div>
</div>
CSS :
.button-container {
width:100%;
height:100px;
display:table;
}
.button-container >div {
display:table-cell;
}
.button-container button {
height:100%;
width:100%;
}