What is the best way to align the content of a <ul> in the center while keeping the text left-aligned and setting a max-width

UPDATE: Including an image for better visualization:

[![Check out the image link][1]][1]

To understand more clearly, refer to this fiddle: https://jsfiddle.net/c7jazc9z/2/

Currently, I have a list with items aligned to the left. The goal is to center this list on the screen based on the longest item it contains. This can be achieved using display:inline-block.

However, when trying to set a max-width on the list and the items wrap due to limited width, it creates unnecessary empty space on the right side, making the alignment appear off.

This gives the impression that the list is not actually centered below the headline, even though it technically is.

Looking for suggestions on how to fix this issue. (Feel free to suggest the use of any other HTML elements)

body {
  background: gray;
  margin: 100px 0;
  text-align: center;
}

div {
  max-width: 300px;
  margin: 0 auto;
}

ul {
  background: pink;
  display: inline-block;
  list-style-type: none;
  padding: 0;
  text-align: left;
}

li {
  font-size: 24px;
}
<div>
  <h1>Headline</h1>
  <ul>
    <li>This is a the longest line, at least here.</li>
    <li>This one's short.</li>
    <li>And this one so and so.</li>
  </ul>
</div>

Answer №1

Are you satisfied with this?

body {
  background: gray;
  margin: 100px 0;
  text-align: center;
}

div {
  max-width: 300px;
  margin: 0 auto;
}

ul {
  background: pink;
  display: inline-block;
  list-style-type: none;
  padding: 0;
  text-align: left;
}

li {
  font-size: 24px;
}
<div>
  <ul>
    <li>
      <center>This is a the longest line, at least here.</center>
    </li>
    <li>
      <center>This one's short.</center>
    </li>
    <li>
      <center>And this one so and so.</center>
    </li>
  </ul>
</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

Website glitch encountered on Flash when using Firefox and IE9, though surprisingly functions perfectly on IE6!

My website isn't functioning properly on Firefox versions 4 and 7. Nothing is appearing, not even the alternative content. In IE9, the issue is that the content is condensed at the top of the page... although it works fine on IE6! Here is the HTML c ...

Consistent dimensions for columns in a table, automatically adjusting based on content

Can we ensure that all rows and columns maintain equal height, whether or not they contain an image? If you need a visual representation of this concept, refer to this example: [] [] [] ...

Assistance needed in regards to relative positioning

I am working on a feature where the customer's shopping cart is displayed when they hover over "My Cart." I plan to achieve this effect using CSS, but I have encountered an issue with the size of .cart causing .my-cart to stretch... update The previ ...

JavaScript/DOM - What sets apart a "CSS Selector" from an attribute?

When it comes to excluding declarative event handlers: <a href='#' onclick=<handler> ... /> Is there a significant difference between an Attribute and a CSS Selector? For example, if I define my own attribute: <a href='#&a ...

Tips for creating dynamic row styles in a fluid table layout?

Could someone help me figure this out? I'm not familiar with JavaScript and need assistance as I've never created it, only edited existing scripts. I have a layout with two tables side-by-side. On mobile devices, the second table is pushed below ...

What is the best way to show only one div at a time when selecting from navbar buttons?

To only display the appropriate div when clicking a button on the left navbar and hide all others, you can use this code: For example: If "Profile" is clicked in the left navbar, the My Profile Form div will be displayed (and all others will remain hidde ...

Embed a video clip into an HTML webpage

I attempted to embed a video using HTML. I followed the player script instructions and added the necessary paths and scripts, but unfortunately, the video is not visible on the webpage. Instead, when clicking on the area where the video should be, it opens ...

Avoiding the separation of hyphenated words when they overflow on a new line

My code is designed to limit text to two lines and show an ellipsis if there is more content. However, I am facing an issue with hyphenated words like "breath-taking" not being treated as one word. Instead, they are often cut off at the end of the second l ...

Stacking divs one on top of the other

My goal is to design a simple webpage with two full screen background colored divs. On top of these background divs, I want to add two smaller divs where I can input text and place a button underneath to randomize the words (refer to the attached screensho ...

most effective method for recycling dynamic content within Jquery mobile

My jQuery mobile app has a requirement to reuse the same content while keeping track of user selections each time it is displayed. I have successfully created the necessary html content and can append it to the page seamlessly. The process goes something ...

Updates to the Tailwind file are not appearing on the page after reopening the file

Every time I reopen the project, the file tailwind properties do not get rebuilt and if I add new properties like text-color, they are also not reflected. Any suggestions on how to solve this issue? I need the project to update and take in new properties ...

What is the method for assigning a class name to a child element within a parent element?

<custom-img class="decrease-item" img-src="images/minus-green.svg" @click="event => callDecreaseItem(event, itemId)" /> Here we have the code snippet where an image component is being referenced. <template&g ...

Tips on adjusting the width of a border

I'm facing a challenge with my select box setup: <select class="selectpicker" multiple data-live-search="true" name="color[]" title="Nothing selected yet"> <option class="" style="border-bottom:2px solid; border-color:red" value="">Red&l ...

The frosting glass effect blur filter is malfunctioning, resulting in no blurring effect and affecting the content

I need help achieving a background photo with a div in front, featuring a blurry dark background with white text. Currently, the setup shows a dark background without blur and dark text. Can someone please assist me? #section0 { background-color: light ...

Guide to incorporating JSON data into HTML through JavaScript

As I attempt to load data from a JSON file to .js and then to an HTML file, I am facing a challenge. While I can successfully load the JSON values into .js, I am unable to transfer the same values to HTML. Below is the code snippet - could anyone provide a ...

What is the best way to add a gradient background image to a container that also has margins?

I am currently working on a push menu that shifts the main content container by adding margin-left to it. I am aiming to achieve a background image effect similar to what is seen on . Here is the progress I have made so far in my codepen: https://codepen ...

Retrieve a targeted tag from the API snippet

I need to extract the IMG SRC tag from the given code: <pod title="Scientific name" scanner="Data" id="ScientificName:SpeciesData" position="200" error="false" numsubpods="1"> <subpod title=""> <plaintext>Canis lupus familiaris</plain ...

Creating a Triangle Slider Component with React and Material-UI (MUI)

Struggling with creating a price control using react js and MUI, similar to the image below: https://i.stack.imgur.com/ZP8oc.png I've searched online for libraries or solutions, but haven't found anything that works. ...

Ways to Adjust Website Scaling for Varying Screen Sizes

Is there a way to adjust my website's scale based on the device width, such as changing it to 0.7 for certain devices? The only information I've found so far is using the <meta> tag: <meta name="viewport" content="width=device-width, i ...

Adjusting text alignment to span the entire width with consistent automatic spacing

Imagine if I have a collection of N words. To illustrate, consider the following: Dog Peanut Oranges Lemon Disciplinary All these words vary in length. Let's assume there is a container with a variable width X (the container may adjust its size ac ...