The `margin:auto;` property does not function properly with inline-block elements

I recently adjusted my "container" div by changing it to an inline-block. Previously, I had applied margin:auto; to center it, which worked well with a specified width.

Original Code (functional)

#container {
    border: 1px solid black;
    height: 200px;
    width: 200px;
}
.MtopBig {
    margin-top: 75px;
}
.center {
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}
<div class="center MtopBig" id="container"></div>

Updated Code (issue)

#container {
    border: 1px solid black;
    display: inline-block;
    padding: 50px;
}
.MtopBig {
    margin: 75px auto;
    position: relative;
}
.center {
    text-align: center;
}
<div class="center MtopBig" id="container"></div>

See DEMO on fiddle.

Answer №1

It appears off-center now because it flows on the page like inline elements, similar to images. To center the inline-block div, you need to use text-align: center on the containing element.

#container {
    border: 1px solid black;
    display: inline-block;
    padding: 50px;
}
.MtopBig {
    margin: 75px auto;
    position: relative;
}
.center {
    text-align: center;
}
<div class="center">
    <div class="MtopBig" id="container"></div>
</div>

Answer №2

Understanding the Use of 'auto' in CSS:

When using the value auto for horizontal margin, it instructs the element to fill up all the available space within its container (source: ).

Exploring Why 'display: inline-block' Does Not Center Elements:

In an inline setting, there is limited or no available horizontal space as other inline elements (such as characters) occupy their own space before and after. This results in the element acting as if it has zero horizontal margin set.

Understanding How 'display: block' Achieves Centering:

Elements with display: block set will utilize the full width of their parent container minus their own width for horizontal positioning. This behavior makes sense as display: block reserves this horizontal space. It's important to note that elements with display: block cannot be placed next to each other unless you use float, which also disables the availability of horizontal space.

Recommendation for Centering 'inline-block' Elements:

For elements styled with display: inline-block, consider them similar to characters when centering. One way to center these elements is by applying text-align: center to their parent container (which you might already be familiar with).

Answer №3

When using the CSS property display: inline-block, any 'auto' computed value for 'margin-left' or 'margin-right' will be rendered as '0'. This behavior is specified in CSS2§10.3.9.

Answer №4

margin-left:50%;
transform: translateX(-50%);

.container{
  border:solid 1px red;
}

.container img{
  display:inline-block;
  margin-left:50%;
  transform: translateX(-50%);
}
<div class="container">
  <img src="https://placekitten.com/100/300" />
</div>

Answer №5

I needed to create two inline divs on the same line, with one positioned at the far right. To achieve this, I set the width of the first div to 70% and let the second div take up the remaining space. Additionally, I applied white-space: nowrap; to ensure that the divs do not wrap onto the next line (making them responsive).

Here is an example:

<div style="border:1px solid black;white-space:nowrap;">
  <div style="border:1px solid black;display:inline-block;width:70%;max-width:70%;word-break:break-word;">
  <div style="border: 1px solid blue;word-break: break-word;white-space: normal;">  
  hello hi hi hi;hello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hihello hi hi hi</div>
  </div>
  <div style="border:1px solid black;display:inline-block;width:28%;height:100%;vertical-align:top;word-break:break-word;white-space;">Right Side Inline-Div</div>
</div>

Note: The use of white-space: normal prevents inherited white-space and allows for word-break in the child containers.

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

Steps for selecting checkboxes according to database values in AngularJSWould you like to learn how to automatically check checkboxes based on the

I am experiencing an issue where I can successfully insert values into the database based on what is checked, but I am encountering difficulty retrieving the values when fetching from the database. Can anyone provide me with some assistance? Thank you. Th ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

What is the best way to navigate down a page using the <a> tag?

I'm working on creating a mini wiki page on my website that will have a table of contents at the top. Users can click on a link in the table of contents and it will automatically scroll down to the relevant section of the page. I know this involves us ...

Font size determined by both the width and height of the viewport

I'll be brief and direct, so hear me out: When using VW, the font-size changes based on the viewport width. With VH, the font-size adjusts according to the viewport height. Now, I have a question: Is there a way to scale my font-size based on ...

Completion status of circle loader: 100%

I am currently facing some challenges getting the circle loader to work correctly. Although I can handle basic animations, the code I found on CodePen is a bit more advanced than what I am used to. I am using it as a learning experience to understand how i ...

Python Selenium: Troubleshooting the get_elements method not retrieving li items within ul tags

I am facing an issue while trying to retrieve li items within a ul element using the following code: driver.get('https://migroskurumsal.com/magazalarimiz/') try: select = WebDriverWait(driver, 10).until( EC.presence_of_element_locate ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

Tips on getting your card to stay at the top

Screenshot of webpage HTML (EJS): In the HTML file, look for the section after <%- include("navbar.ejs") -%> inside the body tag. The ".cardholder" contains "anime-card" templates (card.ejs). The first and last div within ".cardholder" are ...

Reduce the transparency of the overlay picture

I have been utilizing the 'Big Picture' template to design a webpage. The overlay.png file adds a lighter background effect by overlaying intro.jpg with a partially transparent blue graphic. Despite my efforts to adjust the opacity and gradient/R ...

Get a polished look using HTML and CSS exclusively

Allow me to illustrate what I am intending to accomplish with an example. div{ width:100px;height:100px; border:3px solid #900; border-radius:0px 0px 140px 0px; } <div></div> I am seeking a method to ...

Can you explain the significance of 'height: 0;' in CSS coding?

I've recently come across some outdated CSS code that sets the height to height: 0; in multiple places. I'm curious about what this 0 signifies and what unit it is measured in. If anyone could provide some insight, I would greatly appreciate it. ...

Issues with select options not functioning correctly in knockout framework

Currently, I am engaged in a project where data is being retrieved from an API. The main task at hand is to create a dropdown list using select binding. In order to do so, I have defined an observable object to hold the selected value within my data model. ...

"Taking a break" and indulging in Sass

When transferring Sass classes from another project, I wanted to create a wrapper to contain these styles locally. Here is how my wrapper is currently set up: .my-wrapper { @include "framework-main" } Initially, everything seemed fine. However, I soo ...

Tips on inserting text into form input fields

I need some guidance on how to enhance my form input functionality. The form currently consists of an input box and a submit button. When the user clicks submit, the form content is submitted and processed using javascript. What I am aiming for is to autom ...

Exploring "Additional resources" using Selenium and Python

I am looking to extract text from the "Further reading" section of a Wikipedia article. My current code can open Google, input a request (such as 'Bill Gates'), and then find the URL of the corresponding Wikipedia page. Now I need help parsing th ...

Solving the issue of unnecessary white space when printing from Chrome

Whenever I print from Chrome, there seems to be extra space between lines in the middle of a paragraph! Here is an image showing the difference between print emulation and print preview This excess space is always in the same position on various pages an ...

Struggling to position a div below another div within a flex box layout

When trying to make the Information & advice header appear above the other div elements, I encountered an issue with flexbox. Despite setting the container div to have a flex direction of column, the header div continued to align to the left of the oth ...

Utilizing JQuery's $(document).ready() function following a window.location change

I'm having trouble getting a JavaScript function to run after being redirected to a specific page like "example.com" when the DOM is ready for it to work with. Unfortunately, it seems that $(document).ready() is running before "example.com" finishes l ...

Quiz12 - The Influence of Inheritance on CSS

How will the text size of "Universe" be affected by the following declaration? <div style=”font-size:12px;”>World is<div style=”font-size:0.5em;”>VERY small in <div style=”font-size:100%;”>Universe</div></div>< ...

CSS - Apply a captivating background to specific columns within a grid layout

Wow, I'm really struggling with CSS and this particular issue has me perplexed. I need to add background colors to specific columns but not all of them. The problem is that the padding gets messed up when I do this. Imagine having a headache while try ...