If you have an image inside a div element and you want it to be displayed exactly at the center of it then you must apply display=block, vertical-align=middle, and text-align=center to that div.
Example: We have a div with width and height as 400px with an image located within it. If you do not add anycss properties, the image will be displayed at the top-left corner of the div element.
<html>
<head>
<title>Image displayed at top-left corner of the div</title>
<style>
.my-div {
width:400px;
height:400px;
border:1px dotted #888;
}
</style>
</head>
<body>
<div class="my-div">
<img src="http://code2care.org/tools/base64-decoder-encoder/images/code2care-logo.png" />
</div>
<body>
</html>
Solution:
.my-div {
display:table-cell;
vertical-align:middle;
text-align:center;
width:400px;
height:400px;
border:1px dotted #888;
background:#fefefe;
border-radius:15px
}
The image is now aligned at absolute center Vertically as well as Horizontally within the div element.
Browser Support test :
Property : text-align
IE : Internet Explorer 8 or above
Firefox : version 38 or above
Chrome : version 31 or above
Safari : version 8 or above
Property : vertical-align
It is supposed by all browsers : IE6+, Firefox 2+, Chrome 1+
Property : display
IE : Internet Explorer 8 or above
Firefox : version 38 or above
Chrome : version 31 or above
Safari : version 8 or above
More Posts related to CSS,
- How to create a circular Image using pure CSS Code
- Horizontally Center Align
- Add scroll to div element in HTML Css
- Change CSS Background Opacity with Examples
- How to make text bold using CSS
- How to place two div elements next to each other
- Vertically Center Text in a DIV Element
- align image at middle of div element
- Tailwind CSS Hello World Example
- How to make div or text in html unselectable using CSS
- Only Chessboard using HTML and CSS Code Example
- Simple CSS Grid Example
- How to set CSS background-Image Property
- CSS Background Opacity with Examples
- reCAPTCHA Implementation Tutorial
- List of 32 CSS cursors web developers must know about
- How to Vertically Center Align Text in a Div using CSS Code Example
- 10 Must Know CSS Border Styles with Examples
- Tutorial : Simple Lightweight Pure CSS based Vertical Navigation Menu
- CSS: Apply opacity only for div background and not text
- Set Falling Show on Website for Christmas using Pure CSS Code
- How to make jsfiddle bootstrap ready
- Elegant CSS Box Shadows Ideas
- Responsive Web Design with CSS Media Queries: A Beginner's Tutorial [Updated for 2023]
- How to Center Align Image in Bootstrap
More Posts:
- How to hide or cancel Toast message in Android Programming - Android
- Java JDK 21 - JEP 440 - Record Patterns - Java-JDK-21
- Get the current timestamp in Java - Java
- Export aborted because fatal lint error were found - Android
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end users experience - Java
- Locate MainActivity Java or Kotlin file in Android Studio - Android-Studio
- How to Make Your Own Cross Platform Custom Notepad Application [Windows/Mac/Linux] - Python
- [Fix] java: incompatible types: java.lang.String cannot be converted to int - Java