Introduction
Center alignment of divs is one of the most important aspects of front-end development. In this article, we will look at the technique of placing a div inside another div using HTML and CSS.
In this tutorial we will have a parent div which should have child divs. Our task is to position the child div in the center of the parent div.
Use Transform translation and positional syntax
This is not a very popular way to center align one div into another div
grammar
left:50%; top:50%; Transform: translate(-50%, -50%);
The above syntax performs the following operations -
The CSS rule "left:50%;" sets the horizontal position of an element to 50% of the left side of its container.
The rule "top:50%;" sets the vertical position of an element to 50% from the top of its container.
The rule "transform: translate(-50%, -50%);" moves the element -50% horizontally and -50% vertically, effectively positioning the center of the element a distance from the left and Top 50% position.
However, this is not a popular way to center a div within another div. This is due to the following reasons -
This requires an additional five lines of code, more than the other methods.
The positions of parent and child divs must be defined, which may cause inconvenience in designing other associated divs in the future.
Example
<!DOCTYPE html> <html lang="en"> <head> <style> .container{ background-color: red; width:50vw; height:50vh; position: relative; } .child{ background-color: yellow; Width: 25vw; Height: 25vh; position: absolute; left:50%; top:50%; transform:translate(-50%, -50%); } </style> </head> <body> <div class="container"> <div class="child"> </div> </div> </body> </html>
illustrate
In the above example, we first declare that the child's position is absolute and the parent's position is relative. Next, we move the child 50% from the top and left of the parent div. Next, we use the CSS transform property to center the child div.
The translate(x, y) function takes two values as parameters, where x is the number of pixels to move the element horizontally and y is the number of pixels to move the element vertically. In this example, the element will move -50% of its width and -50% of its height, centering it vertically and horizontally.
Using Grid Properties
The more popular way to center align a div is to use CSS's grid property; however, for this, you first need to declare the div as a grid.
grammar
place-items: center;
The place-items property aligns items horizontally and vertically with the grid container. We can only use this property with grid containers.
Example
<!DOCTYPE html> <html lang="en"> <head> <style> .container{ background-color: blue; width:50vw; height:50vh; display: grid; place-items: center; } .child{ background-color: yellow; width:25vw; height:25vh; } </style> </head> <body> <div class="container"> <div class="child"> </div> </div> </body> </html>
Using Flex Box properties
Another method we can use is the flexbox property of CSS. We first need to declare the parent as a flexbox. Flex box is a widely used element in CSS. They are very convenient to use because they are responsive elements and programmers generally have good control over Flexbox properties.
Example
<!DOCTYPE html> <html lang="en"> <head> <style> .container{ background-color: purple; width:50vw; height:30vh; display: flex; flex-direction: row; align-items: center; justify-content: center; } .child{ background-color: green; width:25vw; height:10vh; } </style> </head> <body> <div class="container"> <div class="child"> </div> </div> </body> </html>
Center multiple nested divs
Putting multiple nested divs into a parent div is also a simple task. Suppose there are three divs, namely container, which is the parent div, and first-child, which is the child div of the container; the second child is the child of the first child. We can then first center align the first child element into the container div using the method we discussed. Next, we can make the first child the parent of the second child and apply the same technique.
As an illustration, we will use one of these methods to demonstrate the technique. Readers should try the other two methods to perform similar tasks.
Example
<!DOCTYPE html> <html lang="en"> <head> <style> .container { background-color: red; width: 50vw; height: 30vh; display: flex; flex-direction: row; align-items: center; justify-content: center; } .first-child { background-color: green; width: 25vw; height: 20vh; display: flex; flex-direction: row; align-items: center; justify-content: center; } .second-child { background-color: yellow; height: 10vh; width: 20vw; } </style> </head> <body> <div class="container"> <div class="first-child"> <div class="second-child"></div> </div> </div> </body> </html>
in conclusion
In this article, we learned how to center align divs inside other divs using HTML and CSS. We looked at three different techniques for center-aligning divs. They use the position property, grid property and flexbox property. Among them, the flexbox attribute is the most widely used and the most convenient.
The above is the detailed content of How to center a div inside another div?. For more information, please follow other related articles on the PHP Chinese website!

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
