Hello everyone! I run into a problem while trying to position some texts on “banner-img”.
I can’t put texts (“Happy Kitchen”) on the center of “banner img” without affecting links in , so I comment out CSS code to keep the links safe. but you can see the texts are under the image due to CSS is commented out. I also find out that the problem is “position: relative” of “.banner-container” . Please help me understand this issue. Thank you!
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
The position: relative is creating a new stacking context that puts the banner-container on top of the nav. Try giving the element a background color so you can better see what is happening.
The reason in this case it is a problem is because you are floating the image and links so they are taken out of document flow and their container (the nav) collapses and does not push down the content below it.
If you clear the container (using a clear fix or just overflow: hidden) it should fix it.
nav {
overflow: hidden;
}
Edit: BTW, I would suggest using something other than float for doing layout. Look into Flexbox and CSS Grid. Using floats is an old and bug-prone way of doing layout that really is not needed anymore.
Wow, it’s magic! Problem solved. Thank you once again!
Ah, yes, I’ve finished all the challenges in CSS part, but it takes time to understand and apply those knowledge to a real project. So at this moment, I’m trying to understand float and other basic positioning properties. I will move to Grid and Flexbox after these.
It’s not magic I’m afraid. If you are going to use (or learn about) floats you need to know about all the different issues they cause and how to work around and fix them.
It’s good to know a little bit about floats so you can work on old code, or if you have to support really old browsers. But for new projects, I would not use float for layout. They are simply not needed. I’d suggest limiting the use of floats for their intended purpose which is floating text around elements.