Prop Webcontrol Image Imagealign
## ASP.NET Web Control: Image ImageAlign Property
The `ImageAlign` property is a key styling property of the ASP.NET `Image` web control. It allows developers to specify or retrieve the alignment of an image relative to its surrounding text and other elements on a web page.
---
## Definition and Usage
The `ImageAlign` property determines how an image is positioned vertically or horizontally in relation to adjacent text and elements. Under the hood, ASP.NET translates this property into the corresponding HTML `align` attribute or inline CSS styles, depending on the target browser rendering.
---
## Syntax
```xml
```
### Property Values
The `ImageAlign` property accepts values defined by the `ImageAlign` enumeration. Below is the detailed list of supported values:
| Value | Description |
| :--- | :--- |
| `NotSet` | The alignment is not set. The image will render using the browser's default alignment behavior. |
| `AbsBottom` | Aligns the middle of the image with the absolute bottom of the current line of text (including descenders like 'g' or 'y'). |
| `AbsMiddle` | Aligns the middle of the image with the absolute middle of the current line of text. |
| `BaseLine` | Aligns the bottom of the image with the baseline of the current line of text. |
| `Bottom` | Aligns the bottom of the image with the bottom of the current line of text. |
| `Left` | Aligns the image to the left margin, allowing surrounding text to wrap around its right side. |
| `Middle` | Aligns the middle of the image with the baseline of the current line of text. |
| `Right` | Aligns the image to the right margin, allowing surrounding text to wrap around its left side. |
| `TextTop` | Aligns the top of the image with the top of the tallest text in the current line. |
| `Top` | Aligns the top of the image with the top of the tallest element in the current line. |
---
## Code Examples
### Example 1: Setting ImageAlign Declaratively in Markup
The following example demonstrates how to align an image to the right side of the page, allowing text to wrap around its left side.
```xml
<%@ Page Language="C#" %>
ASP.NET Image ImageAlign Example
```
### Example 2: Setting ImageAlign Programmatically in Code-Behind
You can also dynamically change the alignment of an image at runtime using C#.
```csharp
// Code-behind file: Default.aspx.cs
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set the image alignment to Middle programmatically
Img1.ImageAlign = ImageAlign.Middle;
}
}
}
```
---
## Technical Considerations
1. **HTML5 Compatibility**: The `align` attribute on HTML `
` tags is deprecated in HTML5. Modern web standards recommend using CSS (`float`, `vertical-align`, or Flexbox/Grid layouts) for element alignment.
2. **CSS Alternatives**:
* Instead of `ImageAlign="Left"`, you can use CSS: `float: left;`.
* Instead of `ImageAlign="Middle"`, you can use CSS: `vertical-align: middle;`.
3. **Accessibility**: Always pair your `asp:Image` control with an `AlternateText` property to ensure screen readers can describe the image to visually impaired users, regardless of how it is aligned.
YouTip