Search
 
SCRIPT & CODE EXAMPLE
 

CSS

how to center items in css

{{-- 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
--}}


Comment

center items

something {
	display: block;
    margin-left: auto;
    margin-right: auto;
}
Comment

center item

something {
	display: grid;
    place-items: center;
}
Comment

PREVIOUS NEXT
Code Example
Css :: centos 8 cron on reboot 
Css :: pointer events none and cursor not allowed 
Css :: html list over three columns 
Css :: ::after cant see 
Css :: texting getting out of box css 
Css :: change icon color to gradient css 
Css :: postcss-preset-env: end value has mixed support, consider using flex-end instead 
Css :: add css file to html 
Css :: adding border to text css 
Css :: change bootstrap input focus glow 
Css :: form styling 
Css :: how to hide the scrollbar in css 
Css :: add backdrop to modal css 
Css :: scss breakpoints 
Css :: photshop flip layer 
Css :: style scroll react csss 
Css :: how to set fallback font in css 
Css :: how to add a background color in css 
Css :: css center alignment 
Css :: responsive font-size 
Css :: css border different sides 
Css :: background properties css 
Css :: text indent css 
Css :: css anchor fill parent 
Css :: radial gradient css 
Css :: how to add background image in a container css 
Css :: box model css 
Css :: linear-gradient 
Css :: superscript css 
Css :: bootstrap 4 input error 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =