Introduction
When creating the UI part in any Web Application we encounter many situations where we have to center a DIV inside another DIV. We have a lot of ways to center the child Div but some are not responsive and change their position according to the screen size which is not a good practice. Here I will show you three different ways to center a Div inside another Div.
What is DIV
Div is a division tag in HTML. The div tag is used to create a section in our content. If we need to put our HTML content in different sections then we use a div tag to divide them. It is a block-level element. Block-level elements take up the full width of the page and the next content starts from the next line just like a new paragraph start from the next line. The division tag is used with the div keyword inside angular braces <div> and ends with the forward-slash div keyword with angular braces </div>.
What is HTML
HTML stands for Hyper Text Markup Language. It is a markup language that is used to create content on web pages. When we first load any website on the browser all content we see comes from the HTML file. The boilerplate code for any HTML file is given below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
The three important and compulsory tags are <html>, <head>, and <body>. The HTML tag is the main tag and contains all the tags within it. The Head tag contains all the information about the page and the title of the page. The content of the head tag will not be visible to the user. The body tag contains all the content which will appear to the user on the webpage. To learn more about HTML, please refer to html-5-interview-questions article.
What is CSS
CSS stands for Cascading Style Sheet. CSS is used to style web pages. All the styling like forecolor, background color, font size, position, etc. All the styling is done by CSS on a web page. To learn more about CSS, please refer to css-interview-questions-and-answers article.
Steps to Position a Child DIV
- Create an HTML file with two DIV tags.
- Use CSS to give them height, width, and background color.
- Now center the child DIV inside the parent DIV using three different ways.
Step 1
Create two DIVs as per the below-given code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="parent_div">
<div class="child_div">
</div>
</div>
</body>
</html>
We have two DIV, one with the class name parent_div and the other with the class name child_div respectively. The class attribute is used to uniquely identify them during the styling with CSS.




Ishika TiwariPosted Jun 7, 2022, 6:28 AM
Nice explanation Vijay Pratap Singh