.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
{{-- this is my code
there is two ways to do it
the first method using flex box
--}}
<html>
<body>
<style>
.center {
display: flex;
align-items: center;
justify-content: center;
}
</style>
<div>
<h1 class="center">title</h1>
<h4 class="center">body</h4>
<div class="center">
<img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" height="400px" width="400px" >
</div>
</div>
</body>
</html>
{{--
but the problem with this method that you need to give each element the center class
and in the case of some elments such as an image you need to wrap it inside a div
and pass the center class to it
--}}
{{-- the other way is using css grid --}}
<style>
.center {
display: grid;
place-items: center;
}
</style>
<div class="center">
<h1 >title</h1>
<h4>body</h4>
<img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg" height="400px" width="400px" >
</div>
{{--
the cool thing about this method that you just need to pass the center class,
in the parent div and evreything inside this div will be centered
and for example you dont need to put the image elemnt in a div in order for it to work
--}}
display: flex;
align-items: center;
justify-content: center;
/* order: vertical, horizontal */
/* If you need to access this quickly, just search for "fc" */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Center child horizontally*/
display: flex;
justify-content: center;
}
.child {
width: 50px;
height: 50px;
background-color: red;
}
// example 1
div { display: grid; place-items: center; }
// example 3
div{ display:flex; align-items:center; }
// example 3
div { width: 100%; margin: 0 auto; }
element {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
/* the simple solution*/
table {
text-align: center;
}
.center {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
div.container4 {
height: 10em;
position: relative }
div.container4 p {
margin: 0;
background: yellow;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%) }
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Center child horizontally*/
display: flex;
justify-content: center;
}
.child {
width: 50px;
height: 50px;
background-color: red;
}
element {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
Code Example |
---|
Css :: line spacing css |
Css :: css selector every child except last |
Css :: css div went over top menu |
Css :: border-radius cross browser |
Css :: html watermark background |
Css :: how center div in css |
Css :: text overflow |
Css :: center li in ul css |
Css :: css text align |
Css :: css position element in the middle of a header |
Css :: html text not wrapping |
Css :: transform scale |
Css :: css strike through |
Css :: display inline-block not working |
Css :: td wrap text without space |
Css :: move up on hover css |
Css :: horizontal scroll with css grid |
Css :: input type date all css style |
Css :: how to remove bullets from li |
Css :: wrap none css |
Css :: delete after content css |
Css :: css especifico para safari |
Css :: how to add a photo using css beside a paragraph in html |
Css :: li not first child |
Css :: color code green |
Css :: css gradient 3 color |
Css :: ng-deep |
Css :: css grid row height with repeat |
Css :: flex justify-content |
Css :: how to hange the color of your cursor in css |