Horizontal scroll only if child element's width property exceeds parent element's max-width property

ctwheels

I'm wondering how this can be achieved...

I have code similar to the code in this jsfiddle.

I have an unordered list with a set max-width (inherited from parent) that contains many list items that should have automatic widths. The issue I am having is that when width:auto is set, the element conforms to its parent's max-width property and when width:*length* is set to a specific length that is larger than the parent's max-width property (i.e. 400px in the attached fiddle), the element's output is similar to what is desired, but not exactly what is desired.

The desired result is for the list items to have automatic widths, but not conforming to the parent's max-width property. The list items should:

  1. Have a width equal to the total width of the content it provides (which is undetermined for each element)
  2. Not be multiline (each element should be 1 line max)
  3. Not overlap the parent element (i.e. using position:absolute;)
  4. Only be scrollable if its width exceeds the parent's width

I would prefer a purely HTML/CSS solution, however I will use a JS/jQuery solution if need be


HTML

<div id="scroll1">
    <ul>
        <li>
            <div>I have some text here that is longer than 300px width, but its width is undefined (width can be between 0 and whatever)</div>
        </li>
        <li>
            <div>I have text here less than 300px wide</div>
        </li>
    </ul>
</div>
<div id="scroll2">
    <ul>
        <li>
            <div>I have some text here that is longer than 300px width, but its width is undefined (width can be between 0 and whatever)</div>
        </li>
        <li>
            <div>I have text here less than 300px wide</div>
        </li>
    </ul>
</div>

CSS

#scroll1, #scroll2 {
    max-width:300px;
    margin-bottom:100px;
    overflow:hidden;
}
ul {
    max-width:inherit;
    list-style-type:none;
    padding:0px;
}
li {
    max-width:inherit;
    overflow:auto;
}
li:nth-of-type(even) {
    background-color:#ddd;
}
#scroll1 li div {
    width:auto;
}
#scroll1 li div {
    width:400px;
}

Thank you for your help!

P.S. I am sorry if this question has already been asked, I was not able to find a duplicate of it. If there is an existing answer out there, I am more than willing to accept it as an answer or remove this question if a result that is given fully answers this question.

Brian McCall

this will get you part way there. White-space:nowrap will keep everything on the same line

#scroll1 li>div {
width:400px;
white-space: nowrap;
overflow:scroll

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Child element 100% width of it's parent with overflow: scroll

From Dev

How to distribute parent element's width among child element

From Dev

Issue adding child max width to parent element

From Dev

How do I override a parent element's width with an implicit child element's width?

From Dev

<ul> - float li's - parent element width

From Dev

<ul> - float li's - parent element width

From Dev

how to prevent child element from widening parent element's width in css

From Dev

scroll property in a child element of a parent div whose position is fixed

From Dev

how to bind width of child element to width of parent element in silverlight

From Dev

Is it possible in HTML to set child element width based on parent element width?

From Dev

Intelligently reducing width of parent element when child text wraps due to max-width

From Dev

Append element to parent and position based on parent's current width

From Dev

How to hide child element entirely if it is cut off by it's parent's overflow:hidden property?

From Dev

Child element auto width (larger than parent)

From Dev

How to make the width of an element the full width of the screen and not the parent's width - css

From Dev

Using only CSS, can I limit a child to the parent width, without previously fixing the parent's width?

From Dev

Table-cell element's not accepting max-width value

From Dev

why css width property is working with input element

From Dev

Blank element.style.width property in jsFiddle

From Dev

CSS Positioned Absolute Element, automatic width outside of parent's bounds?

From Dev

Animate marginLeft to element's -width

From Java

How to make horizontal line span entire width of overflow: scroll element?

From Dev

How do you disable the set width of parent element for child element?

From Dev

Make an element's width the same as another element

From Dev

Binding an element in a child View to a property of the parent ViewModel

From Dev

How to fix the text inside the parent element so it wont expand beyond the parent element's width

From Dev

Set width of a form inside a div, to the max-width of it's parent

From Dev

Sum of horizontal properties always equals width of the parent element? Is this true?

From Dev

How to set element's width equal to half of the window's width

Related Related

  1. 1

    Child element 100% width of it's parent with overflow: scroll

  2. 2

    How to distribute parent element's width among child element

  3. 3

    Issue adding child max width to parent element

  4. 4

    How do I override a parent element's width with an implicit child element's width?

  5. 5

    <ul> - float li's - parent element width

  6. 6

    <ul> - float li's - parent element width

  7. 7

    how to prevent child element from widening parent element's width in css

  8. 8

    scroll property in a child element of a parent div whose position is fixed

  9. 9

    how to bind width of child element to width of parent element in silverlight

  10. 10

    Is it possible in HTML to set child element width based on parent element width?

  11. 11

    Intelligently reducing width of parent element when child text wraps due to max-width

  12. 12

    Append element to parent and position based on parent's current width

  13. 13

    How to hide child element entirely if it is cut off by it's parent's overflow:hidden property?

  14. 14

    Child element auto width (larger than parent)

  15. 15

    How to make the width of an element the full width of the screen and not the parent's width - css

  16. 16

    Using only CSS, can I limit a child to the parent width, without previously fixing the parent's width?

  17. 17

    Table-cell element's not accepting max-width value

  18. 18

    why css width property is working with input element

  19. 19

    Blank element.style.width property in jsFiddle

  20. 20

    CSS Positioned Absolute Element, automatic width outside of parent's bounds?

  21. 21

    Animate marginLeft to element's -width

  22. 22

    How to make horizontal line span entire width of overflow: scroll element?

  23. 23

    How do you disable the set width of parent element for child element?

  24. 24

    Make an element's width the same as another element

  25. 25

    Binding an element in a child View to a property of the parent ViewModel

  26. 26

    How to fix the text inside the parent element so it wont expand beyond the parent element's width

  27. 27

    Set width of a form inside a div, to the max-width of it's parent

  28. 28

    Sum of horizontal properties always equals width of the parent element? Is this true?

  29. 29

    How to set element's width equal to half of the window's width

HotTag

Archive