align elements to center css code example
Example 1: center with css
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Example 2: centering css elements
// add to the parent element
.parent {
display: grid;
place-items: center;
}
Example 3: how to center a div element
/*ADD MARGIN auto to left and right*/
.box1{
width:80%;
margin:0 auto;
}
Example 4: center text in div container
text-align: center
Example 5: css center div
<div class="parent">
<div class="child">
<p>
Eh Up Me Duck!
</p>
</div>
</div>
<style>
.parent {
border: 1px solid black;
padding: 2px;
}
.child {
border: 1px solid red;
width: 50%;
margin-left: auto;
margin-right: auto;
}
p {
text-align: center;
}
</style>
Example 6: center elemnts css
margin-left: auto;
margin-right: auto;