Prevent elements from breaking down within an overflow

Asked

Viewed 95 times

0

How to leave a div with Divs daughters with overflow, without breaking the content?

I’m setting up a binary tree viewer, and I need the Ivs to never break the line. In the code I’m working on, by reducing the window size, the div with float, goes down to div sister.

I would like, when reducing the window, to create an overflow-x with scroll so as not to lose the Divs hierarchy, including the sisters' side-by-side positioning.

.root {
    position: absolute !important;
    margin:0;
    padding:0;
    overflow-x: auto;
    display:block;
    text-align:center;
    background-color: #ddd;
}
.node {
    width: auto;
    height:auto;
}
.lft, .rgt {
    margin:5px;
    margin-top: 20px;
    min-width:60px;
    min-height:60px;
    position:relative;
}
.rgt {
    border:1px solid #000;
    float:right;
    background-color: #ccc;
}
.lft {
    border:1px solid #fff;
    float:left;
    background-color: #eee;
}
<div class='container'>
    <div class='row'>
        <div class='root node'>
            <p><i class="fa fa-user"></i></p>
            <p>root name</p>
            <p>150</p>
            <div class='lft'>
                <p><i class="fa fa-user"></i></p>
                <p>lft name</p>
                <p>200</p>
                <div class='node'>
                    <div class='lft'>
                        <p><i class="fa fa-user"></i></p>
                        <p>lft name</p>
                        <p>100</p>
                        <div class='lft'>
                            <p><i class="fa fa-user"></i></p>
                            <p>lft name</p>
                            <p>200</p>
                            <div class='node'>
                                <div class='lft'>
                                    <p><i class="fa fa-user"></i></p>
                                    <p>lft name</p>
                                    <p>50</p>
                                    <div class='lft'>
                                        <p><i class="fa fa-user"></i></p>
                                        <p>lft name</p>
                                        <p>50</p>
                                    </div>
                                    <div class='rgt'>
                                        <p><i class="fa fa-user"></i></p>
                                        <p>rgt name</p>
                                        <p>100</p>
                                    </div>
                                </div>
                                <div class='rgt'>
                                    <p><i class="fa fa-user"></i></p>
                                    <p>rgt name</p>
                                    <p>150</p>
                                </div>
                            </div>
                        </div>
                        <div class='rgt'>
                            <p><i class="fa fa-user"></i></p>
                            <p>rgt name</p>
                            <p>50</p>
                        </div>
                    </div>
                    <div class='rgt'>
                        <p><i class="fa fa-user"></i></p>
                        <p>rgt name</p>
                        <p>0</p>
                    </div>
                </div>
            </div>
            <div class='rgt'>
                <p><i class="fa fa-user"></i></p>
                <p>rgt name</p>
                <p>100</p>
                <div class='node'>
                    <div class='lft'>
                        <p><i class="fa fa-user"></i></p>
                        <p>lft name</p>
                        <p>50</p>
                    </div>
                    <div class='rgt'>
                        <p><i class="fa fa-user"></i></p>
                        <p>rgt name</p>
                        <p>100</p>
                        <div class='node'>
                            <div class='lft'>
                                <p><i class="fa fa-user"></i></p>
                                <p>lft name</p>
                                <p>50</p>
                                <div class='lft'>
                                    <p><i class="fa fa-user"></i></p>
                                    <p>lft name</p>
                                    <p>150</p>
                                </div>
                                <div class='rgt'>
                                    <p><i class="fa fa-user"></i></p>
                                    <p>rgt name</p>
                                    <p>200</p>
                                </div>
                            </div>
                            <div class='rgt'>
                                <p><i class="fa fa-user"></i></p>
                                <p>rgt name</p>
                                <p>50</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Alternative to testing on jsfiddle.

  • Good evening Marcelo, I would like to know if my answer helped you or not.

1 answer

2


The elements don’t break down because of overflow:;, they break because the child elements are limited to the width.

A quick solution is to use the css property white-space: nowrap; in its parent element combined with display: inline-block; in the child elements (No need to apply to grandchildren)

The result must be something like:

div.parent {
  width: 300px;
  overflow: auto;
  white-space: nowrap;
}

div.child {
  width: 100px;
  height: 35px;
  margin: 0 5px 5px 0;
  background: #fc0;
  display: inline-block;
}
<div class="parent">
   <div class="child">test</div>
   <div class="child">test</div>
   <div class="child">test</div>
   <div class="child">test</div>
   <div class="child">test</div>
</div>

In the case of your code, you must:

  • Add white-space: nowrap; for .node
  • Remove the floats from .lft and .rgt
  • And add display:inline-block; combined with vertical-align: top; (vertical-align only works with display: inline; and inline-block) for .lft and .rgt

The code should look like this:

.root {
    position: absolute !important;
    margin:0;
    padding:0;
    overflow-x: auto;
    display:block;
    text-align:center;
    background-color: #ddd;
}
.node {
    width: auto;
    height:auto;
	white-space: nowrap;
}
.lft, .rgt {
    margin:5px;
    margin-top: 20px;
    min-width:60px;
    min-height:60px;
    display: inline-block;
    vertical-align: top;
}
.rgt {
    border:1px solid #000;
    background-color: #ccc;
}
.lft {
    border:1px solid #fff;
    background-color: #eee;
}
        <div class="root node">
            <p><i class="fa fa-user"></i></p>
            <p>root name</p>
            <p>150</p>
            <div class="lft">
                <p><i class="fa fa-user"></i></p>
                <p>lft name</p>
                <p>200</p>
                <div class="node">
                    <div class="lft">
                        <p><i class="fa fa-user"></i></p>
                        <p>lft name</p>
                        <p>100</p>
                        <div class="lft">
                            <p><i class="fa fa-user"></i></p>
                            <p>lft name</p>
                            <p>200</p>
                            <div class="node">
                                <div class="lft">
                                    <p><i class="fa fa-user"></i></p>
                                    <p>lft name</p>
                                    <p>50</p>
                                    <div class="lft">
                                        <p><i class="fa fa-user"></i></p>
                                        <p>lft name</p>
                                        <p>50</p>
                                    </div>
                                    <div class="rgt">
                                        <p><i class="fa fa-user"></i></p>
                                        <p>rgt name</p>
                                        <p>100</p>
                                    </div>
                                </div>
                                <div class="rgt">
                                    <p><i class="fa fa-user"></i></p>
                                    <p>rgt name</p>
                                    <p>150</p>
                                </div>
                            </div>
                        </div>
                        <div class="rgt">
                            <p><i class="fa fa-user"></i></p>
                            <p>rgt name</p>
                            <p>50</p>
                        </div>
                    </div>
                    <div class="rgt">
                        <p><i class="fa fa-user"></i></p>
                        <p>rgt name</p>
                        <p>0</p>
                    </div>
                </div>
            </div>
            <div class="rgt">
                <p><i class="fa fa-user"></i></p>
                <p>rgt name</p>
                <p>100</p>
                <div class="node">
                    <div class="lft">
                        <p><i class="fa fa-user"></i></p>
                        <p>lft name</p>
                        <p>50</p>
                    </div>
                    <div class="rgt">
                        <p><i class="fa fa-user"></i></p>
                        <p>rgt name</p>
                        <p>100</p>
                        <div class="node">
                            <div class="lft">
                                <p><i class="fa fa-user"></i></p>
                                <p>lft name</p>
                                <p>50</p>
                                <div class="lft">
                                    <p><i class="fa fa-user"></i></p>
                                    <p>lft name</p>
                                    <p>150</p>
                                </div>
                                <div class="rgt">
                                    <p><i class="fa fa-user"></i></p>
                                    <p>rgt name</p>
                                    <p>200</p>
                                </div>
                            </div>
                            <div class="rgt">
                                <p><i class="fa fa-user"></i></p>
                                <p>rgt name</p>
                                <p>50</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.