How to center div inside the div
Easy and quick way to center div using display: inline-block;
Thanx for watching, your comments are welcome.
Project Resources
-----------------------------------------------------------------------------------------------------------
[ Ссылка ]
Tools
-----------------------------------------------------------------------------------------------------------
Brackets, FREE code editor
[ Ссылка ]
Emmet, super speed coding addon
[ Ссылка ]
Beautify, turning your ugly code into beautiful script
[ Ссылка ]
Transcription
-----------------------------------------------------------------------------------------------------------
Hi guys and welcome to another tutorial how zo center a div on a website. For this purpose we will center again another div in another div, one div in another div, but this time we will use inline block instead of margin 0 auto, so follow me. All the files, you can download this file in the description, so you don’t have to write, but I encourage you to write code so to be faster and more precise. Ok, let’s make the outer div. Outer div, ok, and inside this outer div we will make inner div. And in the styles.css we will style this outer div a bit ok. Let’s make inner div. Let’s apply some styling, I prepared some styling for you guys about this background colors, and about this background colors, so we can see, ok, we don’t see anything now, but we will see shortly. Outer div it will be height of 400px ok, we can see our outer div, ok? Let’s make our inner div. Height 200px ok, we can see our inner div, let’s change color for a bit. And let’s make width of the inner div to be also 200px. Ok, we have our divs prepared, as you can see, so let’s center this inner bad boy in the outer bad boy. Ok, inner div we just said it will be inline-block. So we say display inline block. Ta da, and nothing happens, why? Why nothing happens? Can you imagine the problem? OK, because this is the inline block element, we treat our inline-block as a inline element not block element, forget about this block, it is just like inline and block element simultaneously, so we must do add something here. It will be text-align: center; Ta da, we just center our inner div by adding to our outer div text-align center. What this means? This means that outer div, if we add text-align center, it will align all the divs inside this outer div. For example, if we add few more divs as you can see: I add three more divs. Let’s make this bit smaller, about 100px, ok. You can see that all the divs are sorted in the center of the website. Pretty cool, right? Ok, and it’s very simple. If we use margin 0 auto, like this, we remove this, add margin 0 auto, our divs will be stacked. Will be stacked one on another. As you can see, one two, three, four, they all stacks, but if we add, just undo the, if we add, text-align: center; and display inline block. Our divs will be centered in the middle of the page, horizontally. And that’s it guys, I will just a little bit beautify this code, ok?, look how I beautifully write my code, and I can also beautify html, of course as you can see. What is this? I don’t know guys what did just happen, oh my god, this making, I don’t know what just happened, I’m sorry must be my code editor. Ok , It was awkward I just refresh the view, ok, it was something with the brackets code editor I use, usually it’s pretty docile, not this wild, nevermind. Ok guys in the next tutorial we will center our divs horizontally and vertically. That’s from me and bye bye.
Ещё видео!