Creating a grid layout with a row of buttons

I'm currently working on aligning buttons within a div to achieve the desired effect shown in the image below. My goal is to have all buttons centered on the page, with the small circles placed in a separate div as they are initially invisible and it's easier to adjust their parameters. Here is my progress so far:
https://i.sstatic.net/jeQmG.png

Here is what I am aiming for:
https://i.sstatic.net/5RA2m.png

.guzik {
    background-color: white;
    display: inline-block;
    padding: 0 2px;
    height: 30px;
    width: 30px;
    border-radius: 50%;
    border: 1px solid black;
    margin: 20px;
}
.guzik1 {
    display: inline-block;
    vertical-align: middle;
    background-color:white;
    padding: 0 0px;
    height: 15px;
    width: 15px;
    border-radius: 50%;
    border: 1px solid black;
    font-size: 9px;
}
<div>
      <input name="1" type="button" class="guzik1" id="k 0+1" />
      <input name="2" type="button" class="guzik1" id="k 0+2" />
      <input name="3" type="button" class="guzik1" id="k 0+3" />
      <input name="4" type="button" class="guzik1" id="k 0+4" />
      <input name="Color1" type="button" class="guzik" id="0+1" onclick="changeColor1(id)" />
      <input name="Color2" type="button" class="guzik" id="0+2" onclick="changeColor1(id)" />
      <input name="Color3" type="button" class="guzik" id="0+3" onclick="changeColor1(id)" />
      <input name="Color4" type="button" class="guzik" id="0+4" onclick="changeColor1(id)" />
</div>

Answer №1

You might want to consider utilizing display: flex combined with a <br> tag for better layout:

.container {
  display: flex;
}

.header {
  margin-left: 80px;
  width: 200px;
  font-family: sans-serif;
  color: blue;
  text-align: center;
}

h3 { color: grey }

.small-pegs {
  padding-top: 15px
}

.big-pegs {}

.button {
  background-color: white;
  display: inline-block;
  padding: 0px 2px;
  height: 30px;
  width: 30px;
  border-radius: 50%;
  border: 1px solid black;
  margin: 20px;
}

.button-small {
  display: inline-block;
  vertical-align: middle;
  background-color: white;
  padding: 0 0px;
  height: 15px;
  width: 15px;
  border-radius: 50%;
  border: 1px solid black;
  font-size: 9px;
}
<div id="board">
  <div class="header">
    <h1>Mastermind</h1>
    <h3>Organize Code</h3>
  </div>
  <div class="container">
    <div class="small-pegs">
      <input name="1" type="button" class="button-small" id="k 0+1" />
      <input name="2" type="button" class="button-small" id="k 0+2" /><br/>
      <input name="3" type="button" class="button-small" id="k 0+3" />
      <input name="4" type="button" class="button-small" id="k 0+4" />
    </div>
    <div class="big-pegs">
      <input name="Color1" type="button" class="button" id="0+1" onclick="changeColor1(id)" />
      <input name="Color2" type="button" class="button" id="0+2" onclick="changeColor1(id)" />
      <input name="Color3" type="button" class="button" id="0+3" onclick="changeColor1(id)" />
      <input name="Color4" type="button" class="button" id="0+4" onclick="changeColor1(id)" />
    </div>
  </div>
  <div class="container">
    <div class="small-pegs">
      <input name="1" type="button" class="button-small" id="k 0+1" />
      <input name="2" type="button" class="button-small" id="k 0+2" /><br/>
      <input name="3" type="button" class="button-small" id="k 0+3" />
      <input name="4" type="button" class="button-small" id="k 0+4" />
    </div>
    <div class="big-pegs">
      <input name="Color1" type="button" class="button" id="0+1" onclick="changeColor1(id)" />
      <input name="Color2" type="button" class="button" id="0+2" onclick="changeColor1(id)" />
      <input name="Color3" type="button" class="button" id="0+3" onclick="changeColor1(id)" />
      <input name="Color4" type="button" class="button" id="0+4" onclick="changeColor1(id)" />
    </div>
  </div>
</div>

Answer №2

If you're looking to achieve a specific layout using css grid, here's a straightforward example.

Utilizing the fraction unit in css grid allows you to divide your grid into sections. For a more in-depth explanation of grid functionality, check out this resource.

Enhanced with a title and control section:

.wrapper {
  height: 400px;
  width: 500px;
  display: grid;
  grid-template-rows: .25fr .25fr .25fr .25fr;
}
.title {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
...
<div class="wrapper">
  <div class="title">
    <h1>MasterMind</h1>
    <p>Guess the Code</p>
  </div>
  ...
</div>

I hope this sample code is useful for you.

Answer №3

Utilizing a grid layout can help you achieve 4 small boxes within one large box, and then another set of 4 in a second row. As a beginner myself, I believe the solution provided below effectively addresses the task at hand.

.gridbox {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  width: 400px;
}

.smoolgrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.smoolgrid button {
  border-radius: 50%;
  height: fit-content;
}

.gridbox button {
  border-radius: 50%;
}
<div class="gridbox">
  <div class="smoolgrid">
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
  </div>
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
</div>

<div class="gridbox">
  <div class="smoolgrid">
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
  </div>
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
</div>

This approach may place 4 small boxes within one of the 5 big grid slots. To resemble your example more closely, adjustments in size and max width can be made.

#gridbox {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
}

#smoolgrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

button {
  border-radius: 50%;
  height: 250px;
}
<div id="gridbox">
  <div id="smoolgrid">
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
  </div>
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
</div>

Answer №4

If you want to create stylish buttons using CSS and Bootstrap, this code snippet can help you achieve that:

.button {
  background-color: #428bca;
  border: 1px solid #357ebd;
  color: #ffffff;
  padding: 10px 20px;
  text-align: center;
  display: inline-block;
  font-size: 16px;
  cursor: pointer;
}

.button:hover {
  background-color: #3071a9;
}
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<div class="text-center">
  <button class="btn btn-primary button">Click Me</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

Having difficulty populating a table with JSON data in Angular without encountering any error messages

After executing a query on my database, I realized that although the JSON data is correct (as confirmed through the console), the table does not populate as expected. Here is the code snippet for my component: @Component({ selector: 'app-envios&apo ...

When using html-pdf-chrome to print to an A4 PDF, there appears to be a significant space below the A

In my Laravel project, I am currently utilizing the westy92 / html-pdf-chrome library to convert HTML to PDF. The front-end of my project is based on React. I have defined a series of divs to act as A4 pages: <div id="pdf" className="pri ...

Creating a moving background for a loading bar with HTML5 and Shadow DOM is currently underway

Can anyone help with animating the progress bar using the HTML5 <progess> tag? I've been able to customize the Shadow DOM elements successfully, but I'm having trouble animating the background (repeating linear gradient). It seems to work i ...

Experiencing a problem with JQuery Offset when a fixed position is applied to a

My website has a page layout and style similar to the example provided in this JsFiddle. When I use JQuery to click on a button, the content is displayed correctly as shown below: However, if I scroll down the page first and then click the button, the co ...

Troubleshooting error in WordPress: Changing innerHTML of dynamically created divs using JavaScript. Issue: 'Unable to set property innerHTMl of null'

I am struggling to modify the innerHTML of a "View cart" button using a dynamically generated div class on my Wordpress/Woocommerce site. In a previous inquiry, I was informed (thanks to Mike :) ) that since JavaScript is an onload event, the class changes ...

Utilizing personalized CSS for specific ioslides

If I decide to stick with the default data and presentation of a new ioslides, changing only the second slide (## R Markdown) without affecting the entire item, how can I do this by setting up the css document accordingly? My goal is to simply adjust the f ...

What is the correct method to define the width as a percentage while also maintaining a minimum width in pixels?

My header/navigation bar div features a repeat-x background image. Within this, I have a horizontal navigation that needs to be centered over the background with a specified min-width and normal width. I want the navigation to be 80% of the full width, wi ...

Gradual disappearance of preloader as the page loads

I'm facing an issue with setting a gif as a website preloader. Despite trying different JavaScript solutions, the preloader remains visible even after the page has finished loading. $(window).on("load", function() { $(".loading").fadeOut("slow"); ...

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

Switching my Selenium code to HtmlUnit: A Step-by-Step Guide

Currently, my Selenium code is working perfectly fine. However, I am looking to convert this code into HtmlUnit. I know I can use the HtmlUnitDriver like WebDriver driver = new HtmlUnitDriver(); I want to make it purely HtmlUnit. Below is the code that I ...

Continuously updating C# HTML parsing

I am facing a challenge with my webpage that is using AJAX queries to display data, as I need to parse this data in a C# program. The issue arises when I try to view the source code of my webpage, which does not show the data due to it being automatically ...

Embracing JQuery for Frontend Development with Node.js

I'm currently enhancing my node.js skills by utilizing express, and I've encountered a situation that is causing me some confusion. Essentially, when a user requests a webpage and the backend sends them the HTML page, how can I incorporate a Java ...

Adjust the position of a child element to be just outside the boundaries of its parent element using CSS

I am facing an issue with 2 nested divs in my HTML code. The design requires the inner div to appear "on top of" the outer div, like this: (source: examplewebsite.com) Although I have applied CSS to both elements, including a negative top margin on t ...

What are the steps to make a counter-clockwise dial with jQuery Knob?

Is there a way to use multiple instances of jQuery.countdown? Can I create countdown circles using jQuery Knob? Find examples here and here. Using Multiple instances of jQuery.countdown <div data-countdown="2016/01/01"></div> <div data-co ...

Prevent Bootstrap 4 from causing content to shift downwards when collapsing

Currently, I am utilizing Bootstrap 4 Collapse and it is functioning flawlessly. However, I am interested in making a slight adjustment. Is there a method to prevent the collapse from pushing down the content below it and instead have it overlap? If this i ...

Clicking on the mat-icon-button with the matSuffix in the password field will always trigger a

Seeking assistance with implementing mat-icon-button matSuffix in Angular for a login page. This is my first time working with Angular and I am facing an issue with the password field. I have used mat-icon-button matSuffix, but whenever I click on the ico ...

Using SVG files as properties in React from a data.js file

I have a website where I store my content in a data.js file and pass it to my components using props. Everything is working correctly, except for my .svg files. When I try to display them, they do not appear. However, if I change the extension of the image ...

Hover over the image to trigger animation effects

Looking for a way to enhance my current jQuery script that creates an orange transparent hover effect over images. How can I add animations, specifically a fade in and out effect? $(document).ready(function() { $('#gallery a').bind('mo ...

Second word in the textbox receiving attention

When I type in the text box, it always focuses on the second word instead of the first word. If I set Maxlength=1, it doesn't allow me to type. I have to press backspace before typing. Can anyone help me with this issue? Below is my code: <input ...

The primary section is not extending completely to the bottom, resulting in the footer not being aligned correctly at the bottom of the

Despite the numerous similar questions on Stack Overflow, I am struggling to get my footer positioned correctly on my page. The <footer> stubbornly remains 800px above the bottom behind the <main>. Even with various height options such as 100%, ...