How to align a text and an image in CSS

An inline element (display value of inline-block) is a <img> element.
Adding the text-align: center; CSS property to the parent element containing it makes it simple to center.
An element at the block level, like a div, must have the <img> in order to center an image using text-align: center.
You can accomplish a horizontally centered <img> by applying text-align: center; to the wrapping block-level element, as the text-align attribute is only applicable to block-level components.
A block element is required for the parent element. In addition to the text-align property, you should add the display: block; CSS attribute if the element isn’t a block.
You may want to resize your image after it has been centered. The way an element reacts to the width and height of its parent box is defined by the object-fit attribute.
Any type of material, including images and videos, can use this attribute. To have additional control over how the media is shown, it can also be used in conjunction with the object-position attribute.

Basically, we specify how it will stretch or compress an inline media by using the object-fit attribute.
The majority of contemporary browsers support object-fit; for the most recent information on compatibility, visit http://caniuse.com/#search=object-fit.
See our CSS Images lesson to get more styling tips for images.
See our CSS Position tutorial to find out more about CSS positioning.

However, what about aligning text and graphics together?

An inline element (display value of inline-block) is a <img> element.
Adding the text-align: center; CSS property to the parent element containing it makes it simple to center.
An element at the block level, like a div, must have the <img> in order to center an image using text-align: center.
You can accomplish a horizontally centered <img> by applying text-align: center; to the wrapping block-level element, as the text-align attribute is only applicable to block-level components.
A block element is required for the parent element. In addition to the text-align property, you should add the display: block; CSS attribute if the element isn’t a block.
You may want to resize your image after it has been centered. The way an element reacts to the width and height of its parent box is defined by the object-fit attribute.
Any type of material, including images and videos, can use this attribute. To have additional control over how the media is shown, it can also be used in conjunction with the object-position attribute.

However, what about using CSS to center an image and text?

The CSS text-align property can be used to center an image horizontally.
We must enclose the image in a block element since this feature only applies to block-level components—not inline elements.
As follows: Launch your HTML file, find the image on the page (it can be identified by its <IMG> element), enclose it in a DIV, add a style declaration to the div’s opening tag (e.g., <DIV STYLE="[INSERT STYLE HERE]">), and alter the text-align: center; attribute to CENTER (i.e.
Using a block element like as a div and a combination of the CSS position, left, and top attributes along with the transform property, you may center an image vertically.
As follows: Find the image you wish to center in your HTML file, then wrap it in a div element. In your CSS file, locate your DIV selector or type in your ID or class selector (#example or .example), inside the curly brackets. Set the div’s POSITION property to ABSOLUTE to remove it from the normal document flow. Set the div’s LEFT and TOP properties to 50% to instruct the browser to align the top and left edges of the div containing the image with the center of the page both horizontally and vertically (i.e. 50% to the right and down the page).

Now let’s explore how to align an image to the right and text to the left using CSS.

Use the float or text-align declaration in CSS to show images on your website in the alignment that you want.
An image in a paragraph can be floated to the left or right using the CSS FLOAT property. The paragraph’s text will around the picture.
An image can be positioned on the left, right, or center of the page using the CSS TEXT-ALIGN property. Depending on the alignment setting, accompanied text will behave as a block and appear either before or after the image.
This property will float the picture to the left or right in the paragraph when you want to wrap the text around an image like you would in a newspaper article. It won’t produce a lot of white space either.
Using the TEXT-ALIGN property is another method to align an image to the left, center, or right of the page.
ALERTS TO THE LEFT
The image will align with the text block’s left alignment.
CENTER LINEUPTION
The image will align with the text block’s center.
Accurate Alignment
The image will align with the text block at the proper position.
To better align your image, you can utilize the "float" or "text-align" properties, depending on what you need.