Perfectly aligning when the width is undetermined

When centering elements with dynamic widths, I typically use the code below:

.horz-vert-center {
   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);
 }

However, a common issue I face is that the element resizes prematurely. For example, a menu may transition to two lines when it could still fit on one:

https://i.sstatic.net/O6WHv.png

I find this confusing - if I am using percentage values, shouldn't the margins continue to decrease?

For a demonstration, check out this link: https://codepen.io/garethj/pen/LWdaRr

Answer №1

To solve the issue, you can add white-space:nowrap; to the ul and

display: inline-block; float: none;
to the li.

.horz-vert-center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  white-space:nowrap;
}
.horz-vert-center li{
  display: inline-block;
  float: none;
}

body {background:#ccc}
*{margin:0;padding:0;font-family:arial;font-size:20px}
ul {list-style-type:none}
li {float:left;margin-right:10px}
li:last-child {margin-right:0;}
<ul class="horz-vert-center">
  <li>About</li>
  <li>Work</li>
  <li>Blog</li>
  <li>Journal</li>
</ul>

Answer №2

Here is a different method utilizing the snippet from "Theodore K."

Check out the demo here

.horz-vert-center {
  width:50%;
  margin: auto;
  border: 1px #000 solid;
  text-align: center;
}
.horz-vert-center li{
  display: inline-block;
  float: none;
}

*{margin:0;padding:0;}
<ul class="horz-vert-center">
  <li>About</li>
  <li>Work</li>
  <li>Blog</li>
  <li>Journal</li>
</ul>

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

Looking to showcase your logo prominently at the center of the page, flanked by two elegant

Is it possible to create a grid-like structure with dimensions 5.5-1-5.5? I would like to center a logo on the page with two lines on the left and right. (Please excuse my limited English skills) https://i.sstatic.net/cgjfS.png ...

Why is there excessive whitespace appearing in Internet Explorer 9?

Visit mimictrading.com/index.php When viewed in Firefox, the website displays as intended. However, in IE9, there seems to be a strange space at the top and above the recent topics list. I suspect that it might be related to the 'header' divisio ...

Incorporate a dropdown menu based on the selection made in another dropdown menu

I have a scenario on my website where I want to dynamically add select boxes based on the value selected in the previous one. Here is the code snippet: <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"> </script> & ...

What is the best way to align multiple container elements alongside the sidebar?

My problem is that small squares on my page are appearing way below where I want them to be, leaving a lot of white space above. I've tried setting them to position: fixed;, but then everything slides off when scrolling down the page. Any suggestions ...

The initial item in the Materializecss slider is failing to display

Currently, I am attempting to implement materialize slider with skrollr. The setup is functional; however, only the first item of the slider is set to opacity: 0 initially. https://i.stack.imgur.com/urSww.jpg After a brief delay, the second item becomes ...

Using mousedown, mousemove, and mouseup to handle touch events in vanilla JavaScript instead of jQuery

Can someone guide me on how to set up a touch event handler in JavaScript using the mousedown, mousemove, and mouseup events? I would really appreciate any suggestions or tips! ...

Issues arise with form submissions when attempting to implement them through jQuery

When submitting a form, I am using jQuery to set the action dynamically. Below is the HTML code: <form name="frmSearchTops" id="frmSearchTops" method="post" onsubmit="searchitem();"> <select name="cboCityNameRes" id="cboCityNameRes"> ...

I encountered an issue while iterating through Object HTMLDivElement and applying ChileNode style z-index, resulting in an undefined output in both Firefox and Chrome browsers

function adjustPosition(currentDiv) { try { for (var i = 0; i < document.getElementById("parentContainer").childNodes.length; i++) { document.getElementById("parentContainer").childNodes[i].style.zIndex = 0; ...

Unable to alter the background color of the text box

I am currently struggling with my code where I am trying to overlay a text box on an image. Even after setting the background color to white, it still shows up as transparent. I have successfully done this in the past, but for some reason, it is not work ...

Insert more text following the existing content

Is it feasible to include text using pseudo-elements ::after or ::before to a specific word? For example, I am interested in placing a PDF icon consistently beside the word "Download". [PDF] Download Alternatively, are there other methods available? ...

Tips on maximizing the file size limit of the fileupload control in asp.net

I am trying to determine the file size limit for my file upload control. Below is the HTML code snippet: <input id="uplTheFile" style="WIDTH: 318px; HEIGHT: 22px" type="file" size="80" name="uplTheFile" runat="server"> I have checked the code but ...

troubleshoot clientWidth not updating when CSS changes are made

Summary: When I adjust the size of a DOM element using CSS, its directive fails to acknowledge this change even with a watch on the size. Aim I have multiple custom directives (simple-graph) each containing an svg. My goal is to enlarge the one under the ...

Experience mind-bending timing functions in CSS animations with IE11

Despite working perfectly on Firefox, Chrome, and Edge, the animations on my website seem to have erratic timing on IE11. To see the animation as intended: However, on IE11, the animation behaves differently: I've attempted adjusting the animation- ...

What could be causing the issue preventing me from applying a border in the code below?

There seems to be an issue in the code below where I am struggling to apply a border Can anyone help identify what the problem might be? <!doctype html> <html> <head> <title> My first Navigation Page</title& ...

Deactivate the ability to hold a button, but still permit clicking in HTML for mobile

I am currently developing a progressive web app for mobile devices. One feature I have implemented is the ability for users to long-press a button with their finger, triggering a popup menu that includes options like "copy link address," "copy text," and " ...

Insert a gap between the two columns to create some breathing room

Does anyone know how to create spacing between columns in an HTML table without affecting the rows? In my table, each column has a border around it: <table> <tr> <td style="padding:0 15px 0 15px;">hello</td> <td style=" ...

Limit the use of Javascript specifically within a section of HTML that includes a rich-text editor

Currently, I am working on a web application that has a historic background. This application uses the summernote editor, a rich-text editor that allows users to save formatted notes to the server. Along with this, there are numerous instances of inline Ja ...

Can the max-height CSS media-query be utilized safely?

It seems that using the max-height media query is proving to be quite a challenge due to an issue with Chrome 67 on iOS. The problem arises when users scroll on Chrome as it constantly adds and removes the address bar. This action causes the max-height va ...

Customizing Material-UI elements in React using CSS styling

My goal is to style all the <Chip> elements within a <Grid> that has the class .table-header, but they are not changing their style (I have a <p> that should be colored in red and it works). Why does the styling work for the <p> ele ...

Help with guiding and redirecting links

Here's the code snippet: <script> if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { document.location.href = 'https://thedomain.com/pages/bestsellers'; } </script& ...