{"id":3086,"date":"2020-10-26T04:05:23","date_gmt":"2020-10-26T09:05:23","guid":{"rendered":"https:\/\/cssnewbie.com\/?p=2586"},"modified":"2022-07-15T06:31:03","modified_gmt":"2022-07-15T06:31:03","slug":"how-to-center-an-image-in-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-center-an-image-in-css\/","title":{"rendered":"How To Center An Image In CSS"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"300\" height=\"300\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/12\/centre-of-gravity-300x300-1.png\" alt=\"\" class=\"wp-image-3090\"\/><\/figure><\/div>\n\n<p>\u00a0<\/p>\n<p>A picture speaks a thousand words.<\/p>\n<p><a href=\"http:\/\/www.thoughtmechanics.com\/the-importance-of-images-in-web-design\/#:~:text=An%20image%20can%20also%20communicate,immersive%20experience%20than%20writing%20alone.\">ThoughtMechanics<\/a> explains the importance of images in a webpage and how they can impact your views, ideas, and perceptions. Thus, while learning web development it is important to learn how to format images.<\/p>\n<p>Images are the major ingredient of modern webpages. Without using them the page isn\u2019t eye-catching. Using the right image to express adds value to the content of the page.<\/p>\n<p>We shall learn how to achieve it using CSS. We will use the <em>margin-right &amp; margin-left <\/em>property to center the image with values in both the property set to <em>auto. <\/em>But the important property is <em>display <\/em>which treats the image as a block element and that helps us move the image around the webpage. The value of the <em>display <\/em>property shall be set as \u201cblock\u201d. Remember this is the important property for aligning the image.<\/p>\n<p>Let\u2019s have a look with the help of a code.<\/p>\n<p>HTML Code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n&lt;title&gt; How to center an image &lt;\/title&gt;\n&lt;link rel= \"style-sheet\" href= \"centerimage.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h2&gt; How to Center an Image &lt;\/h2&gt;\n&lt;p&gt; The image has been added and it is in the center because margin-left and right are set to auto. &lt;\/p&gt;\n&lt;img src=\"https:\/\/cdn.pixabay.com\/photo\/2020\/09\/09\/13\/03\/bike-riding-5557589_960_720.png\" alt=\"Paris\" style=\"width:50%;\"&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>CSS Filename: centerimage.css<\/p>\n<pre>img {\n\ndisplay: block;\n\nmargin-left: auto;\n\nmargin-right: auto;<\/pre>\n<pre>}<\/pre>\n<p>Here&#8217;s what the output will be:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2589\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/CenterImage-1024x428.png\" alt=\"How To Center An Image In CSS\" width=\"1024\" height=\"428\" \/><\/p>\n<h3>See Also:\u00a0<strong><a class=\"c-link\" tabindex=\"-1\" href=\"https:\/\/www.techiediaries.com\/css-centering\/\" target=\"_blank\" rel=\"noopener noreferrer\" data-stringify-link=\"https:\/\/www.techiediaries.com\/css-centering\/\" data-sk=\"tooltip_parent\" data-remove-tab-index=\"true\">CSS Centering (Text and Images) with Angular 11 Example<\/a><\/strong><\/h3>\n<h3>How To Create Rounded Images<\/h3>\n<p>Modern webpages show the profile images inside a circle. I\u2019m sure you would have noticed it. Well, to achieve it on our webpage we use the <em>border-radius <\/em>property with different values. We will have a look with two same images side-by-side. One using the property and the other without. The style attribute used in the img tag sets the width of the image to 20px and the id given to img tag will help in differentiating the shape of the image.<\/p>\n<p>Let&#8217;s take a look:<\/p>\n<p>The HTML code will be as follows:-<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n\u00a0 &lt;title&gt; How to create rounded images &lt;\/title&gt;\n\u00a0 &lt;link rel=\"stylesheet\" href=\"roundedimage.css\"&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;h2&gt;Rounded Images&lt;\/h2&gt;\n&lt;img src=\"https:\/\/cdn.pixabay.com\/photo\/2020\/08\/12\/09\/42\/dog-5482171_960_720.jpg\" alt=\"Avatar\" style=\"width:200px\" id=\"i1\"&gt;\n&lt;img src=\"https:\/\/cdn.pixabay.com\/photo\/2020\/08\/12\/09\/42\/dog-5482171_960_720.jpg\" alt=\"Avatar\" style=\"width:200px\" id=\"i2\"&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>CSS File:Roundedimage.css<\/p>\n<pre>#i1 {\n  border-radius: 50%;\n  margin-left: 100px;\n}\n\n#i2 {\n  margin-left: 250px;\n}\n<\/pre>\n<p>The output of the above code will be as follows:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2590\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/Rounded-Image-1024x225.png\" alt=\"Center Image In CSS\" width=\"1024\" height=\"225\" \/><\/p>\n<h3>How To Border And Center An Image<\/h3>\n<p>Now let us learn how to use the property <em>border<\/em> to create a border around the image which can be styled to give a look like a photo-frame. The values of <em>border <\/em>used here are \u201c10px\u201d which gives the thickness of the border, \u201csolid\u201d defines the type of border &amp; \u201cgreen\u201d adds color to the thickness defined earlier. You can replace these values by appropriate ones to get the desired change. Further, other properties used here are to center the image after converting it as a block element.<\/p>\n<p>HTML Code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt; How to add border to the image &lt;\/title&gt;\n  &lt;link rel= \"stylesheet\" href= \"borderimage.css\"&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n  &lt;h2&gt; Border Around Image &lt;\/h2&gt;\n  &lt;p&gt; Using the border property to create a border around the image.&lt;\/p&gt;\n  &lt;img src=\"https:\/\/cdn.pixabay.com\/photo\/2020\/08\/16\/23\/22\/hills-5494021_960_720.jpg\" alt=\"Snow\"&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>CSS File:Borderimage.css<\/p>\n<pre>img {\n  border: 10px solid green;\n  display: block;\n  margin-right: auto;\n  margin-left: auto;\n  width: 350px;\n}<\/pre>\n<p>Here&#8217;s what the output will be:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2591\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/BorderImage-1024x268.png\" alt=\"Center An Image In CSS\" width=\"1024\" height=\"268\" \/><\/p>\n<p>\u00a0<\/p>\n<h3>Conclusion:<\/h3>\n<p>In this tutorial, we have learned how to center an image at first using margin-right &amp; left properties. We also learned how to round the image and how to add a border to the image. You can change the values and shall see a variety of possible borders and different sizes of rounded images. You can write your own code and explore different styles at <a href=\"https:\/\/codepen.io\/\">CodePen<\/a>.<\/p>\n<p>To keep learning such CSS codes, you can subscribe to our <a href=\"http:\/\/www.cssnewbie.com\">blog<\/a>!<\/p>\n<p>Here are some other filters and effects that can be added to an image in CSS:<\/p>\n<p><a title=\"Permalink to Using Images as Backgrounds with CSS3\" href=\"https:\/\/cssnewbie.com\/using-images-as-backgrounds-with-css3\/\" rel=\"bookmark\">Using Images as Backgrounds with CSS3<\/a><\/p>\n<p><a title=\"Permalink to How To Alter Images Using CSS Filters\" href=\"https:\/\/cssnewbie.com\/how-to-alter-images-using-css-filters\/\" rel=\"bookmark\">How To Alter Images Using CSS Filters<\/a><\/p>\n<p><a title=\"Permalink to Create a Flash Effect on Images on Hover Using CSS3\" href=\"https:\/\/cssnewbie.com\/create-a-flash-effect-on-images-on-hover-using-css3\/\" rel=\"bookmark\">Create a Flash Effect on Images on Hover Using CSS3<\/a><\/p><div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>\u00a0<\/p>\n<p>A picture speaks a thousand words.<\/p>\n<p>ThoughtMechanics explains the importance of images in a webpage and how they can impact your views, ideas, and perceptions. Thus, while learning web development it is important to learn how to format images.<\/p>\n<p>Images are the [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-center-an-image-in-css\/\" title=\"Click to read 'How To Center An Image In CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[183],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3086"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=3086"}],"version-history":[{"count":3,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3468,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3086\/revisions\/3468"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}