CSS
Center Div In Div Using CSS Example
Ever wanted to center Div in Div using CSS? In this article, I am going to show you a very simple & easy little trick to use CSS so that you can easily center a div in another div.
How to Center Div in Div using CSS – Example
For the sake of example, let’s assume that we have 2 Divs & we would like to center the inner div inside the outer (main) div. Here is the CSS & HTML code:
<style type="text/css"> #content { width: 100%; } #text { width: 80%; /*Change this value to a suitable one*/ margin-right: auto; margin-left: auto; } </style> <div id="content"> <div id="text">Try selecting all this text. You will see that all of it is unselectable.</div> </div>
Very simple, isn’t it?
Do you know of any other ways to center div in div using CSS? Feel free to suggest by commenting below.