{{-- 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
--}}
.center{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Assume the div has a class of "center"
The below would center it horizontally
*/
.center {
margin-left: auto;
margin-right: auto;
}
/* There is a shorthand and it is given below */
.center {
margin: 0 auto;
}
margin-left: auto;
margin-right: auto;
https://stackoverflow.com/questions/1725759/css-how-to-center-box-div-element-directly-in-center
/* CSS */
.mx-auto {margin-left: auto;margin-right: auto;}
<!-- HTML -->
<div class="mx-auto">
<!-- content -->
</div>
element {display: block; float: left; vertical-align: middle; text-align: center}
Not so easy huh?
.container {
display: grid;
place-items: center;
}
Code Example |
---|
Css :: aspect-ratio css |
Css :: fixed div with scrollable content |
Css :: css transform origin |
Css :: align-self in css |
Css :: box model properties |
Css :: custom scrollbar css |
Css :: font-face html |
Css :: png image background transparent css |
Css :: rotate favicon css |
Css :: ionic mobile always dark theme in web |
Css :: html file upload without browse button |
Css :: width not responding to css in table |
Css :: list decoration none |
Css :: importing scss into vue component |
Css :: font face |
Css :: css translate x and y |
Css :: how to change svg image color on hover using css |
Css :: not focus css |
Css :: css erase text |
Css :: line cap css |
Css :: css not full width |
Css :: media min height css |
Css :: watch scss |
Css :: bottom gradient |
Css :: prevent bounce scroll css |
Css :: bootstrap modal overflow |
Css :: fading bottom image css |
Css :: remove outline on button click |
Css :: css text slider animation |
Css :: array_filter use keys |