"Can I align a div in the center using inline-block with an

Despite my efforts, I still can't figure this out. My apologies for the duplication. I attempted to use text-align: center, margin: 0 auto;, and display: flex;, but none of them worked as expected in the end.

I apologize for the messy formatting.

body {
  background-color: #333;
}

div#BreadDiv {
  display: inline-block;
  text-align: center;
  margin: 0 auto;
  box-shadow: 0 2px 8px black;
  border: 1px solid #4A4A4B;
}

ul.BreadUl {
  display: inline-block;
  position: static;
  text-align: center;
  list-style: none;
  border: 1px solid #4A4A4B;
  font-size: 150%;
  padding: 8px 15px;
  margin: 0 auto;
}

ul.BreadUl li {
  display: inline-block;
  color: #AAA;
  text-align: center;
  margin: 5px auto;
}

ul.BreadUl li+li:before {
  content: "/ ";
}

ul.BreadUl li a {
  color: #CCC;
  text-decoration: none;
}

ul.BreadUl li a:hover {
  text-decoration: underline;
}
<div id="BreadDiv">
  <ul class="BreadUl">
    <li><a href="#">Library</a></li>
    <li><a href="#">Folder</a></li>
    <li>Document</li>
  </ul>
</div>

Answer №1

If you're looking for a more streamlined approach, consider this alternative method:

Update: Let me break it down as best as I can! The techniques you experimented with - text-align: center, margin: 0 auto, and display: flex - don't quite achieve the desired outcome individually (I'm still grasping how margin auto functions myself).

When using display: flex, your elements will default to flex-direction: row and justify-content: flex-start, which aligns them top-left. To center the child elements, include justify-content: center.

I tried applying flex and justify-content center to the #BreadDiv but it caused the div's border to span across the entire screen. If that's what you want, move the display flex and justify-content center to the #BreadDiv section of your CSS. For a centered bordered area without extending it, create a separate enclosing div around the entire #BreadDiv like I did with #Container.

body {
  background-color: #333;
}

div#Container {
  display: flex;
  justify-content: center;
}

div#BreadDiv {
  display: inline-block;
  text-align: center;
  margin: 0 auto;
  box-shadow: 0 2px 8px black;
  border: 1px solid #4A4A4B;
}

ul.BreadUl {
  display: inline-block;
  position: static;
  text-align: center;
  list-style: none;
  border: 1px solid #4A4A4B;
  font-size: 150%;
  padding: 8px 15px;
  margin: 0 auto;
}

ul.BreadUl li {
  display: inline-block;
  color: #AAA;
  text-align: center;
  margin: 5px auto;
}

ul.BreadUl li+li:before {
  content: "/ ";
}

ul.BreadUl li a {
  color: #CCC;
  text-decoration: none;
}

ul.BreadUl li a:hover {
  text-decoration: underline;
}
<div id="Container">
  <div id="BreadDiv">
    <ul class="BreadUl">
      <li><a href="#">Library</a></li>
      <li><a href="#">Folder</a></li>
      <li>Document</li>
    </ul>
  </div>
</div>

Answer №2

text-align: center; doesn't align itself, it aligns its children instead. This means that if you want to center the #BreadDiv Box, you need to apply body { text-align: center; }. By doing this, the box will be aligned to the center:

body {
  background-color: #333;
  text-align: center;
}

div#BreadDiv {
  display: inline-block;
  text-align: center;
  margin: 0 auto;
  box-shadow: 0 2px 8px black;
  border: 1px solid #4A4A4B;
}

ul.BreadUl {
  display: inline-block;
  position: static;
  text-align: center;
  list-style: none;
  border: 1px solid #4A4A4B;
  font-size: 150%;
  padding: 8px 15px;
  margin: 0 auto;
}

ul.BreadUl li {
  display: inline-block;
  color: #AAA;
  text-align: center;
  margin: 5px auto;
}

ul.BreadUl li+li:before {
  content: "/ ";
}

ul.BreadUl li a {
  color: #CCC;
  text-decoration: none;
}

ul.BreadUl li a:hover {
  text-decoration: underline;
}
<div id="BreadDiv">
  <ul class="BreadUl">
    <li><a href="#">Library</a></li>
    <li><a href="#">Folder</a></li>
    <li>Document</li>
  </ul>
</div>

However, using display: block; instead of display: inline-block; is a better choice for alignment. To address width issues and wrapping problems, you can use width: min-content; and white-space: nowrap;. This simplifies the code and ensures proper alignment as demonstrated in the example below:

body {
  background-color: #333;
}

div#BreadDiv {
  display: block;
  margin: 0 auto;
  box-shadow: 0 2px 8px black;
  border: 1px solid #4A4A4B;
  width: min-content;
  white-space: nowrap;
}

ul.BreadUl {
  list-style: none;
  border: 1px solid #4A4A4B;
  font-size: 150%;
  padding: 8px 15px;
  margin: 0;
}

ul.BreadUl li {
  color: #AAA;
  margin: 5px auto;
}

ul.BreadUl li+li:before {
  content: "/ ";
}

ul.BreadUl li a {
  color: #CCC;
  text-decoration: none;
}

ul.BreadUl li a:hover {
  text-decoration: underline;
}
<div id="BreadDiv">
  <ul class="BreadUl">
    <li><a href="#">Library</a></li>
    <li><a href="#">Folder</a></li>
    <li>Document</li>
  </ul>
</div>

Answer №3

take a look at this solution.

body {
  background-color: #333;
}

div#BreadDiv {
  display: block;
  text-align: center;
  margin: 0 auto;
  
}

ul.BreadUl {
  display: inline-block;
  position: static;
  text-align: center;
  list-style: none;
  border: 1px solid #4A4A4B;
  box-shadow: 0 2px 8px black;
  font-size: 150%;
  padding: 8px 15px;
  margin: 0 auto;
}

ul.BreadUl li {
  display: inline-block;
  color: #AAA;
  text-align: center;
  margin: 5px auto;
}

ul.BreadUl li+li:before {
  content: "/ ";
}

ul.BreadUl li a {
  color: #CCC;
  text-decoration: none;
}

ul.BreadUl li a:hover {
  text-decoration: underline;
}
<div id="BreadDiv">
  <ul class="BreadUl">
    <li><a href="#">Library</a></li>
    <li><a href="#">Folder</a></li>
    <li>Document</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

Combining two table headers into one table and organizing it for a clean appearance

Is it possible to use two different <thead>'s in the same table but have them stacked neatly? Currently, the first heading has 1 fewer column than the second one, resulting in a slightly awkward appearance. If there is a way to extend that first ...

Embedding Google+ Sharing in an iframe

I'm currently working on a web application that needs to be compatible with PC, tablets, and mobile phones. I'm interested in integrating Google+ Sharing into the app. However, it appears that when using the url , there are issues with blocking ...

What is the best way to right-align a label in a vertical MUI Slider in ReactJS?

I am currently working with ReactJS/TS and MUI (Material-UI) to develop a vertical slider that requires the label to be positioned on the right side. By default, MUI places the label on the left for vertical sliders. I have reviewed the API documentation e ...

The layout of my website appears distorted on mobile devices

When I view my website, http://www.healthot.com, on an iPhone or other mobile device, the page doesn't display correctly. It scales the window, requiring me to zoom out to see the entire page. To better understand the issue, please refer to this photo ...

Ways to navigate by percentage in react

I'm working on a flexbox that scrolls horizontally with boxes set to 25% width. Currently, I have it scrolling by 420px which is causing responsive issues. Is there a way to implement the scroll using percentages instead of fixed pixel values in React ...

Jquery form extension

I've implemented the Jquery form plugin for submitting login form data and it's working well. However, I am facing an issue with redirecting a successfully logged-in user to the client panel from the login page in my PHP script. Whenever a user s ...

What is the best way to extract a piece of text and incorporate it onto my website?

I'm currently attempting to extract the URL from a page on Steam. My goal is to obtain the picture URL of any user who visits it. Here is the JSON code provided by Steam when requesting a user's information: { "response": { "players": [ ...

Stop flex child from shrinking in size

I have a form row with two inputs. There is a requirement to show ⚠️ (warning emoji) next to the second input in the row, based on the input number. However, because of how flexbox behaves, the input size gets reduced... How can I maintain the input s ...

Tips for avoiding label overlap in JavaScript programming

I am having some trouble adding labels to a sphere. Only one label is attaching correctly, while the rest are overlapping in the corner of the screen. I have attached an image to show the issue. It may be a CSS problem, but I'm not entirely sure. Belo ...

Adjust the color of the background when hovering with the cursor

I am currently working on a project with a menu of buttons that dynamically changes based on the elements in a list. For instance, if my list contains ["A", "B", "C"], then I would have a menu with 3 buttons. Here is how I display the buttons using a java ...

What is the best way to utilize jQuery to expand an HTML form?

For my new web application project, I am working on a feature that allows users to create messages with attached files. Check out this image for multiple HTML file inputs using jQuery: http://img39.imageshack.us/img39/4474/attachments.gif One of the func ...

Steps to easily modify the color of Font Awesome stacked icons when hovering over them

I have incorporated two Font Awesome icons into my design: fa-circle-thin fa-user-plus These icons are stacked to create a circular icon appearance. <span class="fa-stack fa-sm"> <i class="fa fa-circle-thin fa-stack-2x"></i> <i c ...

Implement various actions when the window is resized

The carousel code I have found returns a different number of items to display based on screen size. You can check it out here. $(function() { var mini = 1000; var big = 1280; if (window.screen.availWidth < mini) { $('#carousel1').jsCar ...

What causes the disparity between Chrome's print preview and printed output? [HTML - CSS]

In my Angular demo project, I have included basic text and a table. There is a print button that calls window.print() to print the page with applied styling. printPage() { window.print(); } CSS: @media print { @page { size: landscap ...

Is it feasible to retrieve and view local storage data within an Electron application?

I created an electron application that enables users to drag and drop elements like divs on the screen, saving their positions in localstorage as a class. The purpose is to ensure that any modifications made by the user persist even after reloading or re ...

Uninstalling an Element Using jQuery

There is a requirement to dynamically insert city names into the input field with the ID citiesList. If the value of the input field is empty and the user clicks on the cancel button, then the input should be deleted. However, upon clicking the edit but ...

How can you combine accessibility with absolute positioning?

Have you seen our unique hamburger design? https://i.stack.imgur.com/AmT6P.png A simple click will reveal a neat popup (the class "open" is added to a div). https://i.stack.imgur.com/yPydJ.png This cool effect is achieved using fixed positioning, but i ...

Updates made to CSS are not appearing on the Joomla site

My website is based on Joomla and has been functioning well since it was created. However, recently I have noticed that any CSS changes made in the files are not showing up on the site. I have tried clearing the cache and viewing it from different ISPs, ...

What is the process for implementing an image as the background instead of a color for each column in a Google chart?

I'm currently working with Google Chart and I have set up a column chart. However, I am looking to display a background image instead of a background color in each column. Is this achievable with Google Chart? If so, how can I accomplish this? If anyo ...

A step-by-step guide on implementing an if-else statement to remove an item from Angular

I am currently working on implementing a feature that allows me to delete an item only if the quantity of that item is 0. If the quantity is not equal to 0, I need to prevent deletion and send an error message back to the front end. Below is my implementa ...