Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position buttons in container as a grid using CSS

Tags:

html

css

grid

I have a container with multiple anchor tags styled as buttons (it could have more or less buttons):

<div class="menu-container">
    <div class="button-container">
       <a href="#/Action1" class="button">Action1</a>
       <a href="#/Action2" class="button">Action2</a>
       <a href="#/Action3" class="button">Action3</a>
       <a href="#/Action4" class="button">Action4</a>
       <a href="#/Action5" class="button">Action5</a>
    </div>
</div>

How ca I style this using CSS to be able to have those buttons listed as a grid with 4 columns max? I tried with display:inline-block and float:left but without success.

I know that I need to fix a width for the container but I'm not able to but all anchor tags inside the container. And this must be dinamic as is could have more or less buttons...

What I'm trying to achieve is something like:

enter image description here

like image 803
amp Avatar asked Dec 30 '13 23:12

amp


People also ask

How do you put a button on a grid?

To add a row of buttons, right-click anywhere in the grid, and then click Add row. Retail adds a new row at the bottom of the grid. To add a column of buttons, right-click anywhere in the grid, and then click Add column.

How do I center a container in CSS Grid?

To center the <div> element horizontally using grid. Use the property of display set to grid i.e. display: grid; Then use property place-items: center; Example: The following example demonstrates to set the <div> to the center using grid property.


1 Answers

You need to give the links a width and height in addition to float or inline. Also, you need to define a width for the parent element, so that you can define where they can 'float' to.

.button-container {
    width: 520px;
    background: gray;
    overflow-y: auto;
}

.button-container > a {
    width: 100px;
    height: 100px;
    float: left;
    background: lightgray;   
    margin: 15px;
}

Here is an implementation with jsfiddle

Next time try to include an example with fiddle so we can understand what you have already tried and where the code could have gone wrong.

like image 112
C. S. Avatar answered Oct 15 '22 23:10

C. S.