agoston Novice Member Feb 21, 2020 #1 Hello, How can I horizontally center a <div> within another <div> using CSS? Code: <div id="outer"> <div id="inner">Hello World!</div> </div>
Hello, How can I horizontally center a <div> within another <div> using CSS? Code: <div id="outer"> <div id="inner">Hello World!</div> </div>
R rigsby Novice Member Feb 24, 2020 #2 You can apply this CSS to the inner <div>: Spoiler You don't have permission to view the spoiler content. Log in or register now. Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering. If you are targeting IE8+, it might be better to have this instead: Spoiler You don't have permission to view the spoiler content. Log in or register now. It will make the inner element center horizontally, and it works without setting a specific width. Working example here: Spoiler You don't have permission to view the spoiler content. Log in or register now. Spoiler You don't have permission to view the spoiler content. Log in or register now. Upvote 0 Downvote
You can apply this CSS to the inner <div>: Spoiler You don't have permission to view the spoiler content. Log in or register now. Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering. If you are targeting IE8+, it might be better to have this instead: Spoiler You don't have permission to view the spoiler content. Log in or register now. It will make the inner element center horizontally, and it works without setting a specific width. Working example here: Spoiler You don't have permission to view the spoiler content. Log in or register now. Spoiler You don't have permission to view the spoiler content. Log in or register now.