I cannot understand this.. kindly help

I want the blue and red boxes to align with like colours beneath but there seems to be this gap that always persist (even with gap 0). and it seems impossible to center this entire layout vertically (the entire layout , without 100% height of viewport). Can someone explain why and suggest some possible solutions with explanations? I hope you kindly cooperate since i am still on my learning phase.
No description
No description
No description
51 Replies
goatmilk2030
goatmilk203010mo ago
@Cole Thank you for sharing the link, it was helpful would u mind explaining the problem here to me? (the problem with my code above)
Nick
Nick10mo ago
You don't have a height declared. You can fix if you declare height: 400px in the body but I recommend against that. I think it is bad practice to use absolute positioning on the body element That's why instead I gave those divs a container and gave them classes. https://jsfiddle.net/c1wfuby2/ But this is your code (with very minor edits) with height declared on line 14
goatmilk2030
goatmilk203010mo ago
ah thanks bro appreciate it was helpful
MrkaramSingh
MrkaramSingh10mo ago
Use z-index instead of absolute if possible
goatmilk2030
goatmilk203010mo ago
the problem persists even if i have height declared... (i am talking about the method that i chose) and the elements seem to be positioned without any gap if I dont declare any height at all.. basically allowing the children to take up and dictate the space for themselves) @MrkaramSingh didnt quite get how z index is related to absolute positoning here...
Nick
Nick10mo ago
Do you have the full code? If you check the jsfiddle I linked most recently, that doesn't have that issue.
MrkaramSingh
MrkaramSingh10mo ago
@Cole is right about bad practice absolute on body. Create a div container then put all four in that
goatmilk2030
goatmilk203010mo ago
i rechecked by refeshing the browser... so the main problem is working with body tag and not working on any wrapper container other than the body?
Nick
Nick10mo ago
We don't have access to your code. Plug it in in a code sharing site like jsfiddle.
goatmilk2030
goatmilk203010mo ago
ok but how do i share?
Nick
Nick10mo ago
That's more of a best practice issue. We are recommending alterntaive ways because that is considered bad practice, and since you are new to this, it will be best to use alternative solutions because absolute body will create issues for more complex pages.
goatmilk2030
goatmilk203010mo ago
ah i see thank you
Nick
Nick10mo ago
You can get the job done with the code like that if this is all you are doing, but this is not the recommended method for this. There are specific layout elements that are better, and you should instead use div and div classes than modifying all that to the body since everything inside has to follow those rules.
MrkaramSingh
MrkaramSingh10mo ago
Body is what contain everything its bad practice to edit body ... if body is edit then what is behide boday having white background @Cole do u know?
goatmilk2030
goatmilk203010mo ago
interesting question thank you for the guidance heres my code and its absolutely broken in jsfiddle for some reason
Nick
Nick10mo ago
It is? Share screenshot. It looks fine to me!
goatmilk2030
goatmilk203010mo ago
wait what
Nick
Nick10mo ago
You said its broken.
Nick
Nick10mo ago
This is what I see
No description
goatmilk2030
goatmilk203010mo ago
bro same but mine was messed up bot now looks all right the browser must have had some problem allright then this problem is resolved
MrkaramSingh
MrkaramSingh10mo ago
Okay
goatmilk2030
goatmilk203010mo ago
one last question
goatmilk2030
goatmilk203010mo ago
No description
goatmilk2030
goatmilk203010mo ago
if i increase browser height then the boxes space out why is that? but in shorter heights they remain joined this is where all the confusion must have stemmed from
Nick
Nick10mo ago
You declared them to have 200px height. So they will have 200px height and the body / container will fill up the space available unless otherwise declared (like telling body to be 400px) alternatively, you can give these divs the height of 50%
Nick
Nick10mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
goatmilk2030
goatmilk203010mo ago
OH i see
Nick
Nick10mo ago
This I have removed the rule for the body to be 400px and instead told the divs to be 50%
goatmilk2030
goatmilk203010mo ago
because i gave them static values not dynamic values that are subject to change right?
Nick
Nick10mo ago
Yeah.
goatmilk2030
goatmilk203010mo ago
ty ty
Nick
Nick10mo ago
You can also keep the body declaration to be 400 and have the divs be 50% and it will keep that like before.
goatmilk2030
goatmilk203010mo ago
yes it does
Nick
Nick10mo ago
But try to wrap them in a div with a container class and give them classes that are the same instead. Because if you add any other divs, they all automatically will apply the same exact rules regardless of where they are structurally because you declared the rules for the divs to be what they are
Nick
Nick10mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
Nick
Nick10mo ago
example with how it looks with more divs nested
goatmilk2030
goatmilk203010mo ago
yeah it does work that way thanks how would you suggest positioning this to the centre of the screen? let me give you an example
Nick
Nick10mo ago
The very first jsfiddle link I sent is how I would do it personally
goatmilk2030
goatmilk203010mo ago
No description
Nick
Nick10mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
goatmilk2030
goatmilk203010mo ago
i am aware of the method you demostrated i am probing with position absolute and margins to center them because i want to undertsand how they work properly ... margins are very cnfusing after all for me so i am asking for suggestions in this case to further improve my understanding about how these behave
Nick
Nick10mo ago
ah I see
goatmilk2030
goatmilk203010mo ago
i heard it is not recommended to use magic numbers to center elements (bad practise), i used margin auto to center horizontally , but without applyign any magic number i cant postion it to the center vertically.... also the layout squishes with change in page dimensions
goatmilk2030
goatmilk203010mo ago
No description
goatmilk2030
goatmilk203010mo ago
how would u suggest centering it vertically (using margins, padding etc )? I hope I dont come across as confusing
Nick
Nick10mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
Nick
Nick10mo ago
See CSS comments I made And then here:
Nick
Nick10mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
Nick
Nick10mo ago
I named the divs "child", adjusted the div rules previously to instead be the rules of .child, and wrapped them inside a container and applied the max width of 400 to the container and told it to be flexbox with a wrap, but the body is what is telling it to be centered both vertically and horizontally.
goatmilk2030
goatmilk203010mo ago
clean and crisp ty if i dont declare any dimensions for my container, is it automatically set to width: auto and height: auto? @Cole
Want results from more Discord servers?
Add your server