What is the best way to evenly space divs apart so that they perfectly align with the width of the screen?

How can I make three divs fit the screen without expanding, with each one touching a different edge of the screen? The first should touch the left edge, the second in the middle and the third on the right edge.

Check out this code snippet for reference


To play the games, click on the buttons below:
Game 1

</div>
<div class="container">
    <div class="linktopage">
        <br />
        Click on the button below
        <br />
        <button>
           Game 2
        </button>
    </div>


</div>
<div class="container">
    <div class="linktopage">
        <br />
        Click on the button below
        <br />
        <button>
            Game 3
        </button>
    </div>


</div>
        .container {
            text-align: center;

            display:inline-block;
        }
        .linktopage {
            text-align: center;
            background-color: floralwhite;
            color: darkblue;
            height: 25vmin;
            width: 50vmin;
            display: inline-block;
            border-style: outset;
            border-radius: 20px;
            font-family: 'Open Sans', sans-serif;
            font-size: 4vmin;


        }
        button {
            text-align: center;
            display: inline-block;
            background-color: dodgerblue;
            border-radius: 20px;
            color: darkblue;
            height: 10vmin;
            width: 30vmin;
        }

Answer №1

Instead of individually wrapping each item in a .container class, you can simply have one container enclosing them all and utilize flexbox properties like display: flex; and justify-content: space-between;. See the example below:

.container {
  display: flex;
  justify-content: space-between;
}

.linktopage {
  text-align: center;
  background-color: floralwhite;
  color: darkblue;
  height: 25vmin;
  width: 50vmin;
  display: inline-block;
  border-style: outset;
  border-radius: 20px;
  font-family: 'Open Sans', sans-serif;
  font-size: 4vmin;
}

button {
  text-align: center;
  display: inline-block;
  background-color: dodgerblue;
  border-radius: 20px;
  color: darkblue;
  height: 10vmin;
  width: 30vmin;
}
<div class="container">
  <div class="linktopage">
    <br /> Click on the button below
    <br />
    <button>Game 1</button>
  </div>

  <div class="linktopage">
    <br /> Click on the button below
    <br />
    <button>Game 2</button>
  </div>

  <div class="linktopage">
    <br /> Click on the button below
    <br />
    <button>Game 3</button>
  </div>
</div>

Answer №2

If you want to create a layout using flexbox, you can structure your HTML like this:

<div class="container">
        <div class="linktopage">
            <br />
            Click on the button below 
            <br />
            <button>
                Game 1
            </button>
        </div>


        <div class="linktopage">
            <br />
            Click on the button below
            <br />
            <button>
               Game 2
            </button>
        </div>


        <div class="linktopage">
            <br />
            Click on the button below
            <br />
            <button>
                Game 3
            </button>
        </div>

    </div>

To style it with CSS, you can use the following code:

.container {
            display: flex;
            justify-content: space-between;
        }
        .linktopage {
            text-align: center;
            background-color: floralwhite;
            color: darkblue;
            height: 25vmin;
            width: 50vmin;
            display: inline-block;
            border-style: outset;
            border-radius: 20px;
            font-family: 'Open Sans', sans-serif;
            font-size: 4vmin;
        }
        button {
            text-align: center;
            display: inline-block;
            background-color: dodgerblue;
            border-radius: 20px;
            color: darkblue;
            height: 10vmin;
            width: 30vmin;
        }

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

Tips for positioning a canvas and a div element next to each other on a webpage

I have a canvas and a div element that I need to split in a 60% to 40% ratio. However, no matter what changes I make to the display settings, the div is always displayed before the canvas. The Div element contains buttons with color-changing properties fo ...

How to utilize Bootstrap 4's d-flex to make an element span the entirety of the available space?

Creating this specific design is now achievable with the new bootstrap d-flex feature. Can you guide me on how to craft a section similar to the one depicted in the second image? https://i.sstatic.net/LtTux.png https://i.sstatic.net/RlWvJ.png ...

Why does the fillText() method in HTML5 Canvas erase everything after using the clearRect() method?

Whenever I use the writeTextToCanvas method before the clearCanvas method, everything works perfectly. However, if I call the clearCanvas method first and then writeTextToCanvas, the drawing functions work fine after clearing the canvas but the fillText fu ...

Is there a way to determine which radio button has been chosen using jQuery?

I'm trying to retrieve the value of the selected radio button using jQuery. Can anyone help with this? Currently, I am able to target all radio buttons like so: $("form :radio") But how can I determine which one is actually selected? ...

Toggle the jQuery class off when a different element is chosen

I've created a function that toggles a class to change the style of an element when it is selected. However, I'm facing an issue where the new style remains even when another element is selected. Is there a way to make the styling revert back to ...

Tips for Implementing CSS Styles on Ruby on Rails HTML Pages

Just starting out with RoR and trying to customize my form with CSS classes. Here's what I have so far: <%= form_tag :action => 'create' do %> <p><label for = "name">Name</label>: <%= text_field ...

Where to position a button in the middle of a div that varies in size

I'm currently working with this code snippet: <div class="outer"> <ul class="list"> <li class="inner"> <div class="line">information1</div> <div class="line">hyperlink1</div&g ...

Tips for making a div expand to take up the available space on the left side of a taller div

Could you take a look at the scenario below? I'm attempting to position my yellow div beneath the red div and on the left side of the lower part of the green div. When I apply clear: left, it moves down but leaves empty space above it. Is there a way ...

Using jQuery to replace the dynamic keyword in HTML content

<div class="fotorama__stage__frame magnify-wheel-loaded fotorama_vertical_ratio fotorama__loaded fotorama__loaded--img fotorama__active" aria-hidden="false" data-active="true" style="left: 0px;" href="https://static.domain.com/media/catalog/product/cach ...

bootstrap thumbnail displayed without a border

I have decided to incorporate Bootstrap into my latest project: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS ...

How can I make columns with a max-width expand fluidly?

I am currently using a fluid/responsive column layout that looks like this: .row{ float:left; max-width:960px; width:100%; } .column{ float:left; margin:0 15px; } .column:first-child{ margin-left:0; } .column:last-child{ mar ...

Looking for a solution to toggle the visibility of a div based on whether search results are found or not using JavaScript

Running this code <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Searc ...

Converting JSP email text into HTML format with Spring's formatting features

Currently, I am working on implementing an email service using Spring. Users have the ability to input the email subject and body in a form and send the email successfully. However, there is an issue where line breaks and tabs entered by the user are not b ...

The issue arises when the Javascript function is triggered twice, resulting in only 3 out of 4

I'm currently working on a JavaScript calculator project. Everything is running smoothly except for the add() function (subtraction, multiplication, and division are all functioning properly). Additionally, when I hit "=" it displays the answer twice ...

Curved edges achieved without the need for relative positioning

Can anyone help me with a specific code snippet to achieve rounded corners for this page ? Currently, I am using the PIE.htc file which only works when I declare position:relative; throughout the layout, causing disruptions. Is there a code workaround that ...

Creating an insert query in Node.js with frontend HTML and connecting it to a PostgreSQL database is a crucial task in web

I am new to working with Node.js, PostgreSQL. Currently, I am tackling the login form code and facing several issues related to the POST method. I am using HTML, Node.js, and PostgreSQL for this project. Please assist me with solving these problems by revi ...

HTML form for selecting shipping method in PayPal

In my shopping cart setup, I offer two shipping methods: 'express' and 'standard'. I'm wondering if it's feasible to transmit this data to PayPal using HTML variables? ...

Chrome browser causing background image to disappear

Encountering an unusual issue in the Chrome web browser. Whenever I click on an input field, background images with background-attachment:fixed seem to disappear. I've attempted various solutions from similar questions without success. Can someone ass ...

"Bootstrap 4 introduces a new feature where adjusting the spacing between columns causes them to wrap underneath one another

My goal is to create two divs that each take up 6 columns with a space of 1px between them. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoA ...

Creating a Dynamic Navigation Bar with CSS and JavaScript

I have encountered an issue with my responsive menubar where the dropdown button does not have any style when clicked. function myFunction(){ var x = document.getElementById("myMenubar"); if (x.className === "menubar"){ x.className += "responsiv ...