Position a button centrally beneath three div elements

I have 3 different colored squares and a button. How can I align the button directly beneath the squares?

Currently, the button is displaying on the same line as my colored squares in #squares, and it's aligned to the left. Any help or suggestions would be greatly appreciated.

#div1 {
  background-color: red;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}
#div2 {
  background-color: green;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}
#div3 {
  background-color: blue;
  width: 100px;
  height: 100px;
}
#squares {
  display: flex;
  position: absolute;
  margin-left: 35%;
}
#button {
  width: 100px;
  height: 50px;
  clear: both;
}
<div id="squares">
  <div id="div1"></div>
  <div id="div2"></div>
  <div id="div3"></div>
</div>
<div id="button">
  <button>Click me!</button>
</div>

Answer №1

If you're already utilizing flexbox, there are several straightforward solutions available to you.

Here's a suggestion:

  • Enclose both containers within a parent container.
  • Apply display: flex and flex-direction: column to this new container.
  • This will enable you to effortlessly align the squares and the button both vertically and horizontally.

#container {
  display: flex;
  flex-direction: column;
}
#div1 {
  background-color: red;
  width: 100px;
  height: 100px;
}
#div2 {
  background-color: green;
  width: 100px;
  height: 100px;
  margin: 0 10px;
}
#div3 {
  background-color: blue;
  width: 100px;
  height: 100px;
}
#squares {
  display: flex;
  align-self: center;
  margin-bottom: 10px;
}
#button {
  align-self: center;
  text-align: center;
  width: 100px;
  height: 50px;
}
<div id="container">
  <div id="squares">
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
  </div>
  <div id="button">
    <button>Click me!</button>
  </div>
</div>

Answer №2

Have you considered changing the positioning of the square-container from position: absolute to relative?

If not, adjusting the position to become relative and centering the content along with the button could simplify the layout.

Noteworthy code modifications:

#squares {
    position: relative;
    justify-content: center;
}

#button {
    margin: 0 auto;
}

Interactive example: https://jsfiddle.net/x43gh92u/

Answer №3

Another method to address this issue is by defining the left and top padding for the #button div as shown below:

#button {
    width: 100px;
    height: 50px;
    clear: both;
    padding: 120px 35%;
}

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

Arranging divs using inline-block display. How to adjust the heights consecutively?

After much searching and attempting, I am still unable to achieve a simple task. My goal is to apply display inline-block to some divs while aligning them in a row, similar to the image below: https://i.sstatic.net/iLhLS.png The issue arises when number ...

What is the reason this :class="{}" is not functioning properly in Vue.js?

I have created a simple file that contains a reactive Array of objects where each object has a property called "checked" which is a boolean that toggles based on a checkbox. I am using a v-for directive to iterate through the array of employees and render ...

Encountered an issue during my initial AJAX call

Hello everyone, I am currently attempting to use AJAX to send a request with a button trigger and display the response HTML file in a span area. However, I am encountering some issues and need help troubleshooting. Here is my code snippet: //ajax.php < ...

Activate a large sub menu upon hover starting from the first `li` using CSS

I have an issue with the menu on my website. The main menu is working fine, but the sub-menu does not start from the first list item, which is needed for it to function as a Mega menu. I attempted to fix this by adding the following code: #ayurmenu ul li ...

Responsive design for Galaxy S7 devices is optimized through the use of media

I am encountering an issue with media queries on two different websites hosted on the same server, both being accessed using a Galaxy S7 device. @media only screen and (min-width:501px) and (max-width: 999px) { } @media only screen and (max-width: 500p ...

"Adjusting the width of columns in a Datatable

I have arranged a data table with multiple rows and columns, and I am seeking guidance on how to increase the width of the "Tel. 1, Tel. 2, and Fecha" columns to ensure that the text appears on a single line. I've attempted adjusting the s width and t ...

When the jquery loop fails to function

I need to dynamically add a class to a specific tag using jQuery depending on an if condition. Here's the code snippet: if ($(".asp:contains('Home')")) { $("ul.nav-pills a:contains('Home')"). parent().addClass('active ...

The presentation changes when styling is added through the <link> element

I've encountered a strange issue. Here's the HTML I'm currently using: <!DOCTYPE HTML> <html> <head> <style type='text/css'> <title>Site name</title> *{ ...

Utilize a single CSS class or theme to style multiple boxes within a stack - Material-UI

I'm currently diving into the world of React js and MUI components. I'm in the process of creating a stack with multiple boxes, each with its unique style that's consistent across all boxes in the stack. Is there a simpler way to define one ...

Encountering a 504-loadbalancer error (Gateway Time-out) on PythonAnywhere after waiting for 5 minutes for a response, despite setting SQLALCHEMY_POOL_RECYCLE to 600

Currently, I am utilizing the SPOTIPY API to insert a user's song, album, artist, and user_info into a database. Approximately 4,000 records are being inserted in total. The database updates successfully, but after 5 minutes (response_time = 300 secon ...

Updating all the direct components within the corresponding category with jQuery

Here is the HTML content I am working with: <li class="info"> info<li> <li class="other"> info<li> <li class="other"> info<li> <li class="Error"> error<li> <li class="other"> error<li> < ...

What methods do HTML document Rich Text Editors use to incorporate rich text formatting features?

I am curious about the way text in the textarea is styled in rich-text editors. I have attempted to style text in the form but it doesn't seem to change. How does JS recognize the characters typed by the user? Can you explain this process? Edit: Aft ...

I am having trouble with the hover feature on the image tag. Does anyone know how to troubleshoot this issue?

h1{ color:red; font-size: 100px; } img :hover { background-color: gold; } .bacon{ background-color: green; } .broccoli{ background-color: red; } /* .circular{ border-radius: 100%; } */ #heading{ background-color: aquamarine; ...

combine multiple select options values in a single function using jQuery

My HTML code includes two select options for users to choose the origin and destination cities. I need to calculate the cost of travel between these cities. How can I compare the selected options using jQuery? </head> <body> <div> ...

What benefits do Adobe Creative Cloud apps offer for developing interactive content in AEM?

My client recently transitioned to AEM for their website, and we are tasked with creating an interactive HTML5 'component' to be embedded on a page. While we have previously used custom HTML5 for components, integrating them into pages on the old ...

Is there a way to restrict the orientation of a UIWebView using JavaScript or HTML?

Currently, I am working on HTML content for an iPad application. The challenge at hand is locking the UIWebView's content to portrait mode. Despite having already submitted the app to Apple, I have only come across a solution in this question, which s ...

Using Angular 2 to position a md-fab button with 'position: fixed' inside an inner component

Utilizing the md-fab-button from the initial angular2 material-framework has presented a challenge for me. I am attempting to set the md-fab-button(for adding a new entity) with position: fixed, but my goal is to position the button within an inner compone ...

Is there a way to create a header that fades out or disappears when scrolling down and reappears when scrolling up?

After spending some time researching and following tutorials, I have not made much progress with my goal. The task at hand is to hide the top header of my website when the user scrolls down and then make it reappear when they scroll back up to the top of t ...

Having trouble adjusting the outer margin of a PDF generated with html/css in a Flutter iOS app

I am currently facing an issue with the PDF generated from Html/css in my flutter iOS app. While the layout appears fine on Android, there seems to be a problem with left margins when it comes to iOS, as illustrated in the attachment provided. For referen ...

Can a font be imported directly in the code without relying on the use of <style> or <link> tags?

I am well aware that importing fonts in this manner is not the recommended way to do it. I understand that it may seem a bit hacky, but the blog platform I use only provides 5 font options, none of which are Roboto. Additionally, the blog automatically s ...