how to center content in a div code example
Example 1: center a div in css
.container {
display: flex;
justify-content: center;
align-items: center;
}
Example 2: css align center
<div class="parent">
<span>Hello World</span>
</div>
.parent {
display: flex;
justify-content: center;
align-items: center;
}
Example 3: css center vertically
<style>
.container {
height: 200px;
position: relative;
border: 3px solid green;
}
.center {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
<div class="container">
<div class="center">
<p>I am vertically and horizontally centered.</p>
</div>
</div>
Example 4: html css center text on page
<style>
body { background: white }
section {
background: black;
color: white;
border-radius: 1em;
padding: 1em;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
</style>
<section>
<h1>Nicely centered</h1>
<p>This text block is vertically centered.
<p>Horizontally, too, if the window is wide enough.
</section>
Example 5: how to center anything inside your container
display: grid;
place-items: center;
height: 100vh;
Example 6: how to align contents of div in center
1. Enclose the div that you want to center with a parent element
(commonly known as a wrapper or container)
2. Set “text-align: center” to parent element.
3. Then set the inside div to “display: inline-block”