FAQ: The Box Model - Overflow


#1

This community-built FAQ covers the “Overflow” exercise from the lesson “The Box Model”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn CSS

FAQs on the exercise Overflow

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Why does the class. “share” height change when I type in the #main ID a height of 1000px?


#3

Not clear on your question. .share is independent of #main so should see no effect. Can you illustrate your question with a before and after screen grab?


#4

I believe the above poster is asking why when there is no overflow property declared on #main, and the height is set to 1000px, that the position of .share changes to the center of our #main div. I too was wondering why this seems to so drastically change the placement of our .share div.


#5

That is a positioning problem we should be able to sort out once we see your style sheet. Please post style.css.

Belay, belay. I have reproduced your example and will need to ponder awhile the reason why we see this behavior.


Here’s one thought… Because the height is set to 1000px on #main, and both it and .share have position: relative; properties, the latter is positioning itself 1000px down from the banner.

Recall that HTML is greedy when it comes to page length. The DIV will stretch to whatever length it needs to accomodate the content. However once we introduce height we exceed this paradigm by imposing artificial limits. Strange behavior should be expected, which we witness here. Take the height property away from #main and the behavior will be natural.

Bottom line, we should always impose an overflow rule if we expect the content to exceed our prescribed height.


#6

I tried to play about with this too, I tried to change the positioning of the elements to try and overcome the issue to no avail. The only rule set that moved to element was position: fixed; which moved the element out of sight, essentially hidden. Is this down to the same issue (The unexpected results of html trying to compensate for the overspill?


#7

We should not have to change the position property for this demo to work. Simply impose a height that is less than what is needed to contain all the content in #main, and add an overflow:scroll property. Save and examine the output.


#8

Sorry i think you misunderstood my question. I had already completed the lesson and succeeded. I just wanted to see if I could get the element to stay at the bottom of the page… I’ve learnt a bit more about CSS now and understand that “fixed: position;” is fixed relative to the viewport and the values one sets. At the time i misunderstood what position: fixed; rule actually did. I was going on the assumption that it fixed it in the position where it was placed in the code…


#9

Had I tried to explain what you discovered, would it have been as effective? I didn’t misunderstand your question, but evaded it. Now of your own volition you have a better understanding of position. No doubt static came into that.

When we wish to fix something relative to a parent container that is not the body, then that container must be position: relative. A position: absolute property on a child of that container will adhere to the parent top and left boundaries.

Fixing relates to the browser viewport, not just the element viewport. It has plenty of uses, though a design will naturally work around the constraints of a fixed element. We can fix elements to the left and right side, as well, not just top and bottom.


#10

Hi @mtf, would you kindly explain why the component with .share class overlaps the one with #main id when I set its height to 1000px even though the I have set the margin of .share to 10px?


#11

In this exercise it says that the overflow property is set on a parent element. I understand why and when that would be.

However, when the parent element contains multiple children, and say we only want the overflow to affect the content of one particular child, can we set the overflow directly on this child element?

e.g.

<div id="main">
  <h2>Title</h2>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <h3>Quote</h3>
  <p>Paragraph 3</p>
  <p>Paragraph 4</p>
</div>
#main {
  padding: 20px;
  margin: 30px;
  max-width: 700px;
  max-height: 2000px;
  overflow: scroll;
}

h2{
  text-align: center;
}

h3 {
  border: 1px solid;
  padding: 10px 20px;
  margin: 0 10%;
  text-align: center;
  max-height: 200px;
  overflow: hidden;
}

p {
  text-align: justify;
}

As you can see from the CSS, there is a scroll bar to tackle the overflow from ALL of the child elements (title, paragraphs and quote combined).

The declaration overflow: hidden; with an h3 selector hides any part of the quote which would spill outside of its border when the screen size is narrowed. This code works, but would it be better practice to create an additional parent element around the h3 and set the overflow on this instead?

e.g.

(NB: full code from above applies, but with the following amendments)

  <p>Paragraph 2</p>
  <div class="quote">
    <h3>Quote</h3>
  </div>
  <p>Paragraph 3</p>
.quote h3 {
  max-height: 250px;
  overflow: hidden;
}

h3 {
  border: 1px solid;
  padding: 10px 20px;
  margin: 0 10%;
  text-align: center;
}

#12

We can set overflow on any element that we wish to preserve size and or shape of the visible region within it. There is no set rule that it can only apply to a parent. Every case may be different. In an adaptive or responsive page, elements may be changing size according to media queries, which rulesets may also contain their own overflow properties.


#13

With the variety of display resolutions on various devices, in what situation would it be worth NOT using overflow?