Is there a css cross-browser value for "width: -moz-fit-content;"?

FirefoxCssCross Browser

Firefox Problem Overview


I need some divs to be center-positioned and to fit their content width at the same time.

I am now doing it like this:

.mydiv-centerer{

  text-align: center;

  .mydiv {
    background: none no-repeat scroll 0 0 rgba(1, 56, 110, 0.7);
    border-radius: 10px 10px 10px 10px;
    box-shadow: 0 0 5px #0099FF;
    color: white;
    margin: 10px auto;
    padding: 10px;
    text-align: justify;
    width: -moz-fit-content;
  }
}

Now, the last command "width: -moz-fit-content;" is exactly what I need!

Only problem is.. it works only on Firefox.

I also tryed with "display:inline-block;", but I need these divs to behave like divs. Namely, every next div should be under, and not inline, the previous.

Do you know any possible cross-browser solution?

Firefox Solutions


Solution 1 - Firefox

At last I fixed it simply using:

display: table;

Solution 2 - Firefox

Mozilla's MDN suggests something like the following [source]:

 p {
  width: intrinsic;			  /* Safari/WebKit uses a non-standard name */
  width: -moz-max-content;	  /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
}

Solution 3 - Firefox

In similar case I used: white-space: nowrap;

Solution 4 - Firefox

Is there a single declaration that fixes this for Webkit, Gecko, and Blink? No. However, there is a cross-browser solution by specifying multiple width property values that correspond to each layout engine's convention.

.mydiv {  
  ...
  width: intrinsic;           /* Safari/WebKit uses a non-standard name */
  width: -moz-max-content;    /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
  ...
}

Adapted from: [MDN][1]

[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/width "MDN"

Solution 5 - Firefox

width: intrinsic;           /* Safari/WebKit uses a non-standard name */
width: -moz-max-content;    /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */

Solution 6 - Firefox

I use these:

.right {display:table; margin:-18px 0 0 auto;}
.center {display:table; margin:-18px auto 0 auto;}

Solution 7 - Firefox

I was looking for a way to prevent a long line of text from outgrowing past its container, and max-width: fit-content worked in Chrome, but not in Firefox.

I found a workaround: if the element is the last displayed subelement, setting display: table-caption; and caption-side: bottom; does have the same effect, together with display: table; on the parent object.

Solution 8 - Firefox

Why not use some brs?

<div class="mydiv-centerer">
    <div class="mydiv">Some content</div><br />
    <div class="mydiv">More content than before</div><br />
    <div class="mydiv">Here is a lot of content that
                       I was not anticipating</div>    
</div>

CSS

.mydiv-centerer{
    text-align: center;
}

.mydiv{
    background: none no-repeat scroll 0 0 rgba(1, 56, 110, 0.7);
    border-radius: 10px 10px 10px 10px;
    box-shadow: 0 0 5px #0099FF;
    color: white;
    margin: 10px auto;
    padding: 10px;
    text-align: justify;
    display:inline-block;
}

Example: http://jsfiddle.net/YZV25/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarmeView Question on Stackoverflow
Solution 1 - FirefoxDarmeView Answer on Stackoverflow
Solution 2 - FirefoxJustin GeeslinView Answer on Stackoverflow
Solution 3 - Firefoxuser570605View Answer on Stackoverflow
Solution 4 - Firefoxb_archerView Answer on Stackoverflow
Solution 5 - FirefoxIhor PavlykView Answer on Stackoverflow
Solution 6 - FirefoxdiyismView Answer on Stackoverflow
Solution 7 - FirefoxIS4View Answer on Stackoverflow
Solution 8 - FirefoxJason GennaroView Answer on Stackoverflow