/    Sign up×
Community /Pin to ProfileBookmark

overflow: auto; makes <main> background color visible

This is a passage from some instructional powerpoint slides on HTML and CSS:

[QUOTE]

“If all elements inside an element are set to float, then the height of the containing element becomes 0. To fix this, set overflow to auto.”

[/QUOTE]

For some reason, no matter what height I specify for the children divs, the main div auto-adjusts its height to theirs when overflow is set to auto. Is this default behavior? When I set the height of the <main> div to less than that of its containing children, then I see the scrollbars because of the overflow auto setting. I understand that if I set height of <main> to anything greater than 0, its background color becomes visible again.

How does overflow work in this case? How is overflow related to the <main> background color being visible? That’s what I don’t understand. Does the overflow auto setting automatically adjusts the containing element’s height to that of its children? I don’t understand what’s going on in the internals.

[code=html]
<html>
<head>
<style>
main {
width: 500px;
margin: auto;
background: gray;
border: 2px solid blue;
overflow: auto; /* the fix */
}

nav {
border: 3px solid black;
width: 100px;
float: right;
background-color: yellow;
}

nav p {
margin: 0;
padding: 0;
}

p.content {
margin: 0;
float: left;
width: 388px;
border: 3px solid red;
}
</style>
</head>

<body>
<main>

<nav>
<p><a href=”link1″>link1</a></p>
<p><a href=”link2″>link2</a></p>
<p><a href=”link3″>link3</a></p>
<p><a href=”link4″>link4</a></p>
</nav>

<p class=”content”>Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.</p>

</main>

</body>
</html>

[/code]

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Oct 18.2015 — In the comments to this post:

https://css-tricks.com/left-align-and-right-align-text-on-the-same-line/

See a question by "coderob" and an answer by "csser".

Stumbled across this today as a partial answer to your question. Although I still don't really understand the "internals" as you put it. ?
Copy linkTweet thisAlerts:
@33303authorOct 20.2015 — Thank you, Kevin.

As I understand it, the background color becomes visible when the containing parent has a height greater than zero.

However, even after reading the reply of 'csser', the functionality that causes the height of the divs to be discovered is still mysterious to me. Though, I understand that overflow: hidden; causes a recalculation (but what kind?).

If the floating divs are effectively out of the flow, then how does a recalculation of the parent's contents with the overflow setting result to a correct height calculation? The divs are out of the flow, but then overflow somehow makes them go back into the flow? I'm not sure I understand it.
Copy linkTweet thisAlerts:
@Kevin2Oct 20.2015 — Your understanding is my understanding, and your question is my question. I don't get it either. But that's why I said it was only a partial answer. Maybe someone smart will come along with the rest of the answer. ?
×

Success!

Help @33303 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...