The alignment of 5 boxes is off, failing to position them in the center of the page

Just dipping my toes into the world of programming. I managed to line up 5 boxes horizontally, but now I want to center them. The challenge is also making sure the boxes remain responsive. I've been scouring for ways to center a div, and all I come up with is margin: 0 auto; width: 800px;. However, when I resize my browser to 100%, the fifth box ends up below the others.

.square { 
float:left;
width: 12.5vw;
    height: 12.5vw;
    margin-left: 10px;
    margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center; 
}

.square span {
font-size: 3vw;
}
<div class="outside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>

Answer №1

If you want a simple solution, try removing the floating property from the inner divs and instead display them as inline blocks. The outer container can have auto margins and center text alignment for a clean layout:

.outer {
  border: 1px solid blue;
  text-align: center;
  margin: 0 auto;
}

.box { 
  display: inline-block;
  width: 12.5vw;
  height: 12.5vw;
  margin-left: 10px;
  margin-right: 10px;
  position: relative;
  border: 0.5px solid red;
  text-align: center;
}

.box span {
  font-size: 3vw;
}
<div class="outer">
  <div class="box">
    <span>2.1</span>
  </div>
  <div class="box">
    <span>2.3</span>
  </div>
  <div class="box">
    <span>2.5</span>
  </div>
  <div class="box">
    <span>2.6</span>
  </div>
  <div class="box">
    <span>2.7</span>
  </div>
</div>

Answer №2

To center the parent container, add either a max-width or width and use margin: 0 auto. Adjust the width of the parent element as needed – in this example, it is set to 520px.

According to W3Schools:

To horizontally center a block element (like ), use margin: auto;

Setting the width of the element will prevent it from stretching out to the edges of its container.

The element will then take up the specified width, with remaining space split equally between the two margins.

Refer to the example below.

.outside {
  max-width: 520px;
  margin: 0 auto;
}

.square { 
float:left;
width: 12.5vw;
    height: 12.5vw;
    margin-left: 10px;
    margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center; 
}

.square span {
font-size: 3vw;
}
<div class="outside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>

Answer №3

apply text-align center to outside

.outside{
  text-align:center;
}

Delete float & insert display inline-block square

.square{
    display:inline-block;
    width: 12.5vw;
    height: 12.5vw;
    margin-left: 10px;
    margin-right: 10px;
    position: relative;
    border: 0.5px solid red;
    text-align: center; 

}

Answer №4

I didn't completely grasp your requirements, but you can try the following solution. This code snippet will not only center your boxes but also prevent them from wrapping to the next line when the resolution is reduced.

Update your CSS with:

.square { 
    float: left;
    width: 19%;
    height: 12.5vw;
    margin-right: 1%;
    position: relative;
    border: 0.5px solid red;
    text-align: center;
    box-sizing: border-box; 
}

.square span {
    font-size: 3vw;
}

.outside {
    width: 100%;
}

.inside {
    width: 69%;
    margin: 0 auto;
    overflow: auto;
}

Now, adjust your HTML as follows:

<div class="outside">
  <div class="inside">
        <div class="square">
            <span>2.1</span>
        </div>
        <div class="square">
            <span>2.3</span>
        </div>
        <div class="square">
            <span>2.5</span>
        </div>
        <div class="square">
            <span>2.6</span>
        </div>
        <div class="square">
            <span>2.7</span>
        </div>
   </div>
</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

The color of the SVG is not visible in the PNG rendition

I have applied a style to an svg image and successfully changed the fill color using a random colors function in JavaScript. However, when I convert the svg to a png format after making these color changes, the PNG file retains the original colors instead ...

How can I store HTML5 input type date/time data accurately as Date Time in Rails?

Is there a way to handle two inputs in HTML5 so that both can be sent to the controller and saved as Date Time? Alternatively, is there any helper or gem that mimics a date and time picker from HTML5? HTML5 inputs: %input{:type => "date", :value => ...

How to locate href links with Selenium and Java

I'm working with some HTML code that contains href links. The format of these links can vary: <a href="#"> and some are like <a href = "http://.....'>. It's a challenge because I can't use //a[contains(@href,'#&apo ...

Vertical Alignment of Bootstrap Columns

Within the post section of my website, I have 4 columns with varying heights based on their contents using Bootstrap 4 grid system. As shown in the image below https://i.sstatic.net/ZHHqR.png When I resize the 4th column it rearranges. https://i.sstatic. ...

Images in Angular Firebase causing scroll problems in Chrome

In regard to Angular 6, a common issue arises with slow image loading specifically in Chrome, not other browsers. The project utilizes Firebase and the snapshotchange method to fetch images for the Angular project. Image URLs used are like this: . The iss ...

A guide to adding images to dropdown options in Angular 6

I'm having trouble getting an icon or image to appear next to text in a dropdown menu. I'm currently working with Angular 6 and despite trying multiple solutions, I haven't been able to make it work. All I want is to display a flag next to ...

How come I'm unable to showcase the inventory of items to the customer even though they are saved in the database and visible in the Network tab on the console?

Basically, my goal is to showcase products on the client side. I am using Angular 12 with .NET 5 for the backend. Although I am fetching products from the product's controller, they are not visible to me. However, I can see the "welcome" title and I k ...

The Z-index property does not seem to be functioning properly for a drop-down component that is located within a disclosure panel

Looking for a solution with my custom GWT drop down component, which includes a text box and vertical panel of check boxes. I need to integrate this component into a GWT disclosure panel. When the panel is expanded and the user interacts with the componen ...

Developing a custom JavaScript function to iterate through a group of html elements

In my mission to create a function that loops through a set of HTML elements and gathers their values, I aim to then utilize those values to produce an HTML textarea. Thus far, I've established a series of HTML input boxes and a JavaScript function f ...

Stylish Wave Animation Effects in CSS for iPad Native Application

The ripple effect on the iPad Native app is malfunctioning. The effect is mistakenly applied to all buttons in the navigation instead of just the li elements. Please identify the error and provide a solution. (function (window, $) { $(function() ...

Loading data with Ajax from a remote file using a specific identifier

Can someone lend me a hand with an issue I'm facing? I've set up a small like system here. Within my HTML file, I have various data with unique IDs. Take a look at the code in my remote.html file below: <span id="14">314</span> < ...

Achieving a dynamic scrollable table that adjusts its height based on its parent container (HTML + Bootstrap)

I am trying to create a table that can be scrolled vertically using the standard overflow: auto property without specifying a set height for the table's container. While Bootstrap provides a class called table-responsive for handling horizontal scrol ...

Continue duplicating image to stretch header across entire screen

Looking to create a seamless connection between a Header image (2300x328px) and another image (456x328px) so that the header fills the entire width available without overlapping due to a pattern. I need the second image to extend the header to th ...

How can you use the transform property in CSS to rotate the dropdown arrow by 180 degrees and move it vertically by 2 pixels?

gradeexpanded:false, gradeOnClick: function (event) { this.gradeexpanded = !this.gradeexpanded; }, .dropdown-check-list { margin-left: 35px; displa ...

Issue identified in the recent Bootstrap 4 update: now accepting offset-md-* instead of col-offset-md-* as the

I'm relatively new to Bootstrap 4. Recently, I embarked on learning Bootstrap 4 but unfortunately, I've run into some issues already. I attempted to modify code directly from the Bootstrap 4 manual itself and encountered a problem with the offse ...

Struggling with the alignment of pictures inside a container

I utilized the Instafeed.js library to fetch the three most recent images from an Instagram account. These images are loaded into a specific div and I successfully customized their styling according to my requirements. However, the current setup is quite s ...

Adding the header.php file in WordPress caused the style.css to stop working

Whenever I include a basic "header.php" file in my theme directory, my style.css mysteriously stops working. But when I remove the "header.php" file, everything goes back to normal. How can I troubleshoot this issue? Below is the code snippet from my fil ...

CSS media query does not ensure that images will always fill the container to 100% width

I am currently facing an issue where my images are not displaying at 100% width within a container when I have a wide screen. Instead, there are spaces between the images. How can I fix this problem? See below for the code: <div class="container-respo ...

Tips for preventing the collapse of a table row using a button?

I have implemented a Bootstrap collapse feature on my table row. Currently, the collapse can be triggered by clicking anywhere on the row, which is working as intended. However, I would like to make one exception - if the user clicks on the desktop icon i ...

Unable to eliminate added jQuery Element

Note: This question may seem similar to a previous one I posted, but it actually addresses a different issue. I am currently utilizing Jquery.dotdotdot to handle truncation with a "Read More" feature. What is the problem? After clicking on "Read More" t ...