How To Set Image At The Center Of Div Element Using CSS

admin_img Posted By Bajarangi soft , Posted On 03-11-2020

Using CSS we can center an image, set left and right margin to auto and make it into a block element.So today we discuss how to do it.

How To Set Image At The Center Of Div Element Using CSS

Step 1:Create index.html file and implement as below code in it.

<img src="../image/demo4.jpg" alt="Paris" style="width:40%">

Step 2:Implement CSS code as below to set image center at the div element.

<style>
    body {
        background: #c7254e;
    }
    img {
        display: block;
        margin-left: auto;
        margin-right: auto;
    }
</style>

Complete Code For Setting Image At The Center Of Div Element Using CSS.

<!DOCTYPE html>
<html>
<head>
    <title>How To Set Image At The Center Of Div Element Using CSS</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<style>
    body {
        background: #c7254e;
    }
    img {
        display: block;
        margin-left: auto;
        margin-right: auto;
    }
</style>
<body>
<br/><br/>
<div class="container">
    <br>
    <div class="text-center">
        <h1 id="color" style="color: white;">Set Image At The Center Of Div Element Using CSS</h1>
    </div>
    <br>
    <div class="well">
        <img src="../image/demo4.jpg" alt="Paris" style="width:40%">
    </div>
    <br>
</div>
</body>
</html>

Related Post