html - align text at same horizontal align as image

Html - align text at same horizontal align as image

To align text at the same horizontal position as an image, you can use the vertical-align property in combination with the line-height property. Here's an example:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> /* Container for image and text */ .container { display: inline-block; vertical-align: middle; } /* Image styling */ .container img { width: 50px; /* Adjust the width as needed */ height: auto; /* Maintain aspect ratio */ vertical-align: middle; } /* Text styling */ .container p { display: inline-block; margin: 0; /* Remove default margin */ vertical-align: middle; line-height: 1.2; /* Adjust line height as needed */ } </style> </head> <body> <!-- Container for image and text --> <div class="container"> <!-- Image --> <img src="your-image.jpg" alt="Image"> <!-- Text --> <p>Your text goes here.</p> </div> </body> </html> 

In this example:

  • The .container class is used to wrap both the image and the text. It is set to display: inline-block to allow the image and text to be on the same line, and vertical-align: middle is applied for vertical alignment.
  • The image (container img) is styled with a fixed width (adjust as needed), and vertical-align: middle is applied to ensure it is vertically centered.
  • The text (container p) is set to display: inline-block and vertical-align: middle to align it with the image. line-height is adjusted to control the spacing between lines.

Replace "your-image.jpg" with the actual path to your image.

Adjust the styling based on your specific design requirements.

Examples

  1. "HTML image and text side by side"

    • Description: Basic query for aligning an image and text horizontally.
    <div class="image-text-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .image-text-container { display: flex; align-items: center; } 
  2. "HTML image and text center alignment"

    • Description: Demonstrates center alignment of an image and text horizontally.
    <div class="center-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .center-align-container { text-align: center; } .center-align-container img { display: inline-block; } 
  3. "HTML image and text float alignment"

    • Description: Illustrates using the float property for aligning an image and text.
    <div class="float-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .float-align-container img { float: left; } 
  4. "HTML image and text vertical alignment"

    • Description: Shows how to vertically align an image and text within a container.
    <div class="vertical-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .vertical-align-container { display: flex; align-items: center; height: 100px; /* Adjust height as needed */ } 
  5. "HTML image and text inline-block alignment"

    • Description: Demonstrates using inline-block for aligning an image and text.
    <div class="inline-block-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .inline-block-align-container img, .inline-block-align-container p { display: inline-block; vertical-align: middle; } 
  6. "HTML image and text absolute positioning"

    • Description: Illustrates absolute positioning for aligning an image and text.
    <div class="absolute-positioning-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .absolute-positioning-container { position: relative; } .absolute-positioning-container img, .absolute-positioning-container p { position: absolute; top: 0; } .absolute-positioning-container img { left: 0; } .absolute-positioning-container p { left: 50px; /* Adjust left position as needed */ } 
  7. "HTML image and text flexbox alignment"

    • Description: Shows how to use flexbox for aligning an image and text.
    <div class="flexbox-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .flexbox-align-container { display: flex; align-items: center; } 
  8. "HTML image and text table-cell alignment"

    • Description: Demonstrates using table-cell for aligning an image and text.
    <div class="table-cell-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .table-cell-align-container { display: table; } .table-cell-align-container img, .table-cell-align-container p { display: table-cell; vertical-align: middle; } 
  9. "HTML image and text grid alignment"

    • Description: Illustrates using CSS grid for aligning an image and text.
    <div class="grid-align-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .grid-align-container { display: grid; grid-template-columns: auto auto; } 
  10. "HTML image and text position absolute center"

    • Description: Shows how to position an image and text at the absolute center of the container.
    <div class="absolute-center-container"> <img src="your-image.jpg" alt="Your Image"> <p>Your Text Here</p> </div> 
    .absolute-center-container { position: relative; height: 200px; /* Adjust height as needed */ text-align: center; line-height: 200px; /* Adjust line-height to match the container height */ } .absolute-center-container img, .absolute-center-container p { position: absolute; top: 50%; transform: translateY(-50%); } 

More Tags

graph usart amazon-rekognition compiler-construction osx-elcapitan memory excel-2011 perforce encodable photo-upload

More Programming Questions

More Retirement Calculators

More Gardening and crops Calculators

More Auto Calculators

More Financial Calculators