Mastering the art of CSS horizontal centering: achieving perfect alignment with floating elements

Struggling to center a button among floating elements? Look no further!

button {
  float:left;
}


header {
  overflow: hidden;
  background: #222;
}

header a, header label {
  display: block;
  padding: 20px;
  color: #fff;
  text-decoration: none;
  line-height: 20px;
}

header a:hover, header label:hover { color: #aaa; }

header label {
  float: right;
  padding: 18px 20px;
  cursor: pointer;
}

header label:after {
  content: "\2261";
  font-size: 1.8em;
}

.logo {
  float: left;
  font-weight: bold;
  font-size: 1.5em;
}
  
nav {
  float: right;
  max-height: 0;
  width: 100%;
  -webkit-transition: max-height 0.3s; 
     -moz-transition: max-height 0.3s;
       -o-transition: max-height 0.3s;
          transition: max-height 0.3s;
}

nav ul {
  margin: 0;
  padding: 0;
   padding-bottom: 10px;
}
  
nav li {
  display: block;
  text-align: center;
}
  
nav a {
  padding: 10px;
  width: 100%;
}

#nav { display: none; }

#nav:checked ~ nav {
  max-height: 200px; /* This can be anything bigger than your nav height. The transition duration works with this */
}

@media only screen and (min-width: 700px) {
  
  header label { display: none; }
  
  nav {
    width: auto;
    max-height: none;
  }
  
  nav ul {
    padding: 0;
    padding-right: 10px;
  }
  
  nav li {
    display: inline-block;
    text-align: left;
  }
  
  header nav a {
    display: inline-block;
    padding: 20px 10px;
    width: auto;
  }
  
}
<header>

  <a class="logo" href="http://minimaldev.com">Minimal Menu</a>
  
  <input id="nav" type="checkbox">
  <label for="nav"></label>
  
  <button>centered button ?</button>
  
  <nav>
    <ul>
      <li><a href="#">One</a></li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li><a href="#">Four</a></li>
    </ul>
  </nav> 

</header>

Check out the example on codepen

Attempts using display-table or the technique from this answer didn't work for responsive design. Any suggestions? Maybe calc() could solve it, but the right formula remains elusive.

Answer №1

I've made some adjustments to the layout:

  • Applied text-align: center to the parent element of the button
  • Shifted the button position by manually adjusting pixels to create space at the top (note that this solution is not responsive due to fixed font and padding sizes).

Check out the updated design on CodePen

HTML:

<header>

  <a class="logo" href="http://minimaldev.com">Minimal Menu</a>

  <input id="nav" type="checkbox">
  <label for="nav"></label>

  <button>centered button</button>

  <nav>
    <ul>
      <li><a href="#">One</a></li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li><a href="#">Four</a></li>
    </ul>
  </nav> 

</header>

CSS:

button{
  margin-top: 18px;
}

header {
  text-align: center;
  overflow: hidden;
  background: #222;
}

header a, header label {
  display: block;
  padding: 20px;
  color: #fff;
  text-decoration: none;
  line-height: 20px;
}

header a:hover, header label:hover { color: #aaa; }

header label {
  float: right;
  padding: 18px 20px;
  cursor: pointer;
}

header label:after {
  content: "\2261";
  font-size: 1.8em;
}

.logo {
  float: left;
  font-weight: bold;
  font-size: 1.5em;
}

nav {
  float: right;
  max-height: 0;
  width: 100%;
  -webkit-transition: max-height 0.3s; 
     -moz-transition: max-height 0.3s;
       -o-transition: max-height 0.3s;
          transition: max-height 0.3s;
}

nav ul {
  margin: 0;
  padding: 0;
   padding-bottom: 10px;
}

nav li {
  display: block;
  text-align: center;
}

nav a {
  padding: 10px;
  width: 100%;
}

#nav { display: none; }

#nav:checked ~ nav {
  max-height: 200px; /* This can be anything bigger than your nav height. The transition duration works with this */
}

@media only screen and (min-width: 700px) {

  header label { display: none; }

  nav {
    width: auto;
    max-height: none;
  }

  nav ul {
    padding: 0;
    padding-right: 10px;
  }

  nav li {
    display: inline-block;
    text-align: left;
  }

  header nav a {
    display: inline-block;
    padding: 20px 10px;
    width: auto;
  }

}

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

Align these buttons in a vertical position

I'm trying to place a Twitter button and a LinkedIn profile button next to each other, using the recommended embed code from each service. However, they are not aligning vertically as I would like. I want both buttons to be centered vertically within ...

Establishing visual properties for inactive Mat-Expansion Panels

Is there a way to customize the appearance of a disabled Mat-Expansion-Panel? I have buttons in the header that toggle the panel, so I'm considering disabling the panel itself instead. However, when I disable the panel, all the items inside are greyed ...

Move Picture with Click (like the action on Google Maps)

Currently on the lookout for some code that will enable me to manipulate a floor plan in a manner reminiscent of the one showcased at whitehouse.gov I am in the process of coding a website that would benefit from integrating a similar function with their ...

Is it possible that the HTTP module's deflate compression is only effective on specific CSS files and not all

I've been working on setting up mod deflate and gzip on my server, and while it's mostly functioning smoothly, I've noticed that not all files are being caught. It's not a matter of missing ALL javascript or CSS, but rather that some ar ...

Is it possible to adjust the maximum time limit for uploaded video files in Vue?

I'm facing an issue while trying to dynamically change the maximum value of a time input field in Vue JavaScript after uploading a video file. Despite setting the attribute `max` on the input element using JavaScript, I am unable to update the max val ...

The Google Maps App fails to launch now

Recently, my map started experiencing issues. The problem seems to be related to the HTML using a javascript and css file that is loaded from Google Drive for display. However, when I directly load these files from my computer, it works perfectly fine. Bel ...

Text area featuring unique border design, with border color change upon focus?

Currently, I have a text area with borders that are designed in a unique way. However, I am looking to change the border color when the user focuses on the text area. Do you have any suggestions on how I can achieve this effect without affecting the curre ...

Unlock the power of jQuery to apply CSS transition effects and animations with precision

A web application utilized Bootstrap, featuring a toggle navigation for the sidebar drawer/navigation that was supposed to be animated by default. However, there appeared to be no animation happening. To address this issue, the following CSS code was imple ...

When a selection element has a CSS box-shadow animation applied to it, it reverts to its default settings when expanded or clicked

A user notified me of an issue with a select element that has an animated box-shadow on it. I confirmed the problem on my work Windows computer, but unfortunately, I don't have access to a Windows setup at home for debugging. The select element in qu ...

Interact with the :before pseudo element when hovering over an element

Is there a way to manipulate the :before pseudo element when hovering over the main element? My goal is for the pseudo element to change its height when I hover over the main element. This is how my code looks: HTML <div class="object">& ...

Having trouble retrieving the URL from JSON data - every time I attempt to access it, it just shows as undefined. Any suggestions

Having trouble extracting the URL from JSON, as it shows undefined. Any suggestions? <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta> <script language="JavaScript" type="text/javascript" ...

Center the image within the div by setting its position to absolute

<div class='img-box'> <img /> //position absolute <img /> //position absolute <img /> //position absolute <img /> //position absolute I am struggling to center the images within this div because of their absolute p ...

Designing Tables for User Interfaces

In a table with 4 rows, the first row must always be filled by the user. The remaining rows are optional. What is the best way to visually indicate this requirement? This should be achieved using only HTML, CSS, and jQuery. ...

Guide on Creating a Popup Window When the User's Cursor Moves Beyond the Webpage Boundary

Imagine you're browsing a webpage and as you move your mouse towards the "back" button, a window pops up within the webpage area, displaying important information. This is a clever way to grab the user's attention before they leave the website. B ...

How can one make the image pop and stand out from its background?

I am trying to achieve a similar effect as shown in this design where the image stands out from its background. I have managed to do that, but I am struggling with aligning the image to the center and bottom with overflow. Can anyone help me with this? Th ...

Troubleshooting issues with adding rows to an HTML table

I want to include two buttons at the top of my page: Save and Cancel. These buttons should only appear after clicking the Submit button on the Customer Information panel. However, when trying this, the Customer Information panel is pushed down while I al ...

Flexbox: Arrange header and footer in a sidebar layout using flexbox styling

In my content layout, I have structured the sections as header, content, and footer. While I want to maintain this order for small devices, I am looking to shift the header and footer to the sidebar on desktop. I have already implemented this using floats ...

Limiting the Display of Pages with PHP Pagination

Requesting assistance with updating the code to display full records. In my current setup, I am fetching records from a database and showcasing them in groups of 5 per page using the following code snippet: $num_rec_per_page=5; if (isset($_GET["page"])) ...

Unable to modify the border radius of the carousel indicators in Bootstrap 5

Currently, I am working with the carousel component in Bootstrap 5.1. I have encountered an issue where I am unable to add border-radius to the carousel indicators, despite trying various methods. Even though the inspector shows that the indicators (HTML ...

Every time an input field is clicked, an email is sent

I'm struggling with the contact form on my website. Instead of sending an email after clicking on each input field, I want it to send only one email after hitting the submit button. Currently, it sends a total of 5 emails - 1 with user inputs and 4 wi ...