Container shrinking below content width

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Mlynarczyk

    Container shrinking below content width

    Hi!

    I have noticed something strange: I have an element styled to have padding,
    a border and some margin. The element contains preformatted text. When the
    browser window is made narrow enough, a horizontal scrollbar appears as the
    preformatted text becomes too wide and is not wrapped. So far so good. But
    why is the element itself shrunk to fit in the window (still having its full
    margin, padding and border visible) as if it's content was not too wide?
    Instead of staying "around" the preformatted text (i.e. fully enclosing it),
    the latter seems to "overflow" the element's boundaries. This also seems to
    happen with other content (like an image). Is this standard behaviour? Or
    should I look for what I must have done wrong?

    Illustration:

    * = window edge

    +---+
    ! ! = element border
    +---+

    ##::: = scrollbar

    Window wide enough:

    * +----------+ *
    * ! ! *
    * ! ABCDEFGH ! *
    * ! ! *
    * +----------+ *

    Window not wide enough:

    - That's what I expect:

    * +------*----+
    * ! * !
    * ! ABCDE*FGH !
    * ! * !
    * +------*----+
    ######::::

    - But that's what happens:

    * +----+ *
    * ! ! *
    * ! ABCDE*FGH
    * ! ! *
    * +----+ *
    ######::::


    Confused,
    Thomas


  • Neal

    #2
    Re: Container shrinking below content width

    Thomas Mlynarczyk <blue_elephant5 5@hotmail.com> wrote:
    [color=blue]
    > Hi!
    >
    > I have noticed something strange:[/color]

    A URL would have saved you a lot of creative artwork!

    Look up the CSS property overflow.

    Comment

    • Thomas Mlynarczyk

      #3
      Re: Container shrinking below content width

      Also sprach Neal:
      [color=blue]
      > Look up the CSS property overflow.[/color]

      The only value that could possibly change something here would be "visible",
      but that's the default anyway. Besides, the problem is not what happens to
      the content, but what happens to the area the containing element is supposed
      to fill with its background and padding.


      Comment

      • Markus Ernst

        #4
        Re: Container shrinking below content width

        Thomas Mlynarczyk wrote:[color=blue]
        > Hi!
        >
        > I have noticed something strange: I have an element styled to have
        > padding, a border and some margin. The element contains preformatted
        > text. When the browser window is made narrow enough, a horizontal
        > scrollbar appears as the preformatted text becomes too wide and is
        > not wrapped. So far so good. But why is the element itself shrunk to
        > fit in the window (still having its full margin, padding and border
        > visible) as if it's content was not too wide? Instead of staying
        > "around" the preformatted text (i.e. fully enclosing it), the latter
        > seems to "overflow" the element's boundaries. This also seems to
        > happen with other content (like an image). Is this standard
        > behaviour? Or should I look for what I must have done wrong?[/color]

        I encountered the same phenomenon, but only in Gecko browsers, not in IE. It
        made me guess that Gecko sets the priority on the calculated element width,
        allowing the content to break out of the element, while IE sets the priority
        on wrapping the element arount the content.

        You might find some workaround working with min-width or max-width; but note
        that it is not supported by IE.

        --
        Markus


        Comment

        • Thomas Mlynarczyk

          #5
          Re: Container shrinking below content width

          Also sprach Markus Ernst:
          [color=blue]
          > I encountered the same phenomenon, but only in Gecko browsers, not in
          > IE. It made me guess that Gecko sets the priority on the calculated
          > element width, allowing the content to break out of the element,
          > while IE sets the priority on wrapping the element arount the content.[/color]

          Indeed, the problem does not occur in IE.
          [color=blue]
          > You might find some workaround working with min-width or max-width;[/color]

          My element has no explicit width set as I want it to take up all the
          available horizontal space and still give it padding, border and margin.
          Maybe an inner element set to width: 100% would do the job? I must try that.
          [color=blue]
          > but note that it is not supported by IE.[/color]

          Wouldn't matter in that case.



          Comment

          • Markus Ernst

            #6
            Re: Container shrinking below content width

            Thomas Mlynarczyk wrote:[color=blue]
            > Also sprach Markus Ernst:
            >[color=green]
            >> I encountered the same phenomenon, but only in Gecko browsers, not in
            >> IE. It made me guess that Gecko sets the priority on the calculated
            >> element width, allowing the content to break out of the element,
            >> while IE sets the priority on wrapping the element arount the
            >> content.[/color]
            >
            > Indeed, the problem does not occur in IE.
            >[color=green]
            >> You might find some workaround working with min-width or max-width;[/color]
            >
            > My element has no explicit width set as I want it to take up all the
            > available horizontal space and still give it padding, border and
            > margin. Maybe an inner element set to width: 100% would do the job? I
            > must try that.
            >[color=green]
            >> but note that it is not supported by IE.[/color]
            >
            > Wouldn't matter in that case.[/color]

            Just an idea that came to my mind, maybe it's crap, but you could try
            something like that:

            <div id="container" >
            <div id="innerelemen t">
            <pre>Your text goes here</pre>
            </div>
            </div>
            <script type="text/javascript">
            var c = document.getEle mentById("conta iner");
            var i = document.getEle mentById("inner element");
            if (c.width < i.width) c.width = i.width;
            </script>

            --
            Markus


            Comment

            • Thomas Mlynarczyk

              #7
              Re: Container shrinking below content width

              Also sprach Markus Ernst:
              [color=blue]
              > Just an idea that came to my mind, maybe it's crap, but you could try
              > something like that:
              >
              > <div id="container" >
              > <div id="innerelemen t">
              > <pre>Your text goes here</pre>
              > </div>
              > </div>
              > <script type="text/javascript">
              > var c = document.getEle mentById("conta iner");
              > var i = document.getEle mentById("inner element");
              > if (c.width < i.width) c.width = i.width;
              > </script>[/color]

              I guess I'd prefer the content leaking out...
              I tried to use an inner element with width: 100%, but it didn't seem to
              change anything, nor could I achieve anything using min-width or max-width.
              Any idea what the specs say about that?


              Comment

              Working...