Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML width Attribute
The width attribute in HTML is specifically used with the <input type="image"> element to control the display width of image-based submit buttons. This attribute allows you to resize images used as form submission buttons without affecting their original file dimensions.
Syntax
Following is the syntax for using the width attribute with input elements −
<input type="image" src="image.jpg" width="pixels" height="pixels">
Here, width represents the desired width in pixels. The height attribute is often used together to maintain proper image proportions.
How It Works
The width attribute is only applicable to <input type="image"> elements. When you specify a width value, the browser resizes the image to that width while maintaining its aspect ratio unless a height is also specified. The image functions as a submit button, sending the form data when clicked.
The width attribute accepts positive integer values representing pixels. If the specified width differs from the original image dimensions, the browser scales the image accordingly.
Example − Registration Form with Image Submit Button
Following example demonstrates using the width attribute to resize an image submit button in a registration form −
<!DOCTYPE html>
<html>
<head>
<title>Width Attribute Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>User Registration</h2>
<form action="/submit" method="post">
<label>User ID:</label><br>
<input type="text" name="id" placeholder="Enter User ID..." size="25"><br><br>
<label>Password:</label><br>
<input type="password" name="pwd" placeholder="Enter password..."><br><br>
<label>Date of Birth:</label><br>
<input type="date" name="dob"><br><br>
<label>Phone:</label><br>
<input type="tel" name="tel" placeholder="Enter phone number..."><br><br>
<label>Email:</label><br>
<input type="email" name="email" placeholder="Enter email..." size="35"><br><br>
<input type="image" src="/images/submit-button.png" alt="Submit Form" width="100" height="40">
</form>
</body>
</html>
The form includes various input fields and uses an image submit button with a width of 100 pixels and height of 40 pixels. The image is resized to these dimensions regardless of its original size.
Example − Multiple Image Buttons with Different Widths
Following example shows how different width values affect image submit buttons −
<!DOCTYPE html>
<html>
<head>
<title>Different Width Values</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Image Submit Buttons with Different Widths</h2>
<form action="/process" method="post">
<p>Choose an action:</p>
<p>Small Button (50px width):</p>
<input type="image" src="/images/save-icon.png" alt="Save" width="50" name="action" value="save">
<p>Medium Button (80px width):</p>
<input type="image" src="/images/edit-icon.png" alt="Edit" width="80" name="action" value="edit">
<p>Large Button (120px width):</p>
<input type="image" src="/images/delete-icon.png" alt="Delete" width="120" name="action" value="delete">
</form>
</body>
</html>
Each image button has a different width value, demonstrating how the attribute controls the display size. All buttons function as submit buttons while maintaining their visual differences.
Key Points
Following are important points about the width attribute for input elements −
-
Exclusive to image inputs − The width attribute only works with
<input type="image">elements. -
Pixel values only − The attribute accepts positive integers representing pixels, not percentages or other units.
-
Maintains functionality − Resizing the image does not affect its role as a submit button.
-
Works with height − Often used together with the height attribute for precise control over image dimensions.
-
Responsive considerations − Fixed pixel widths may not be ideal for responsive designs; CSS is preferred for flexible layouts.
Example − Width with Responsive Design
Following example shows how to combine the width attribute with CSS for better responsive behavior −
<!DOCTYPE html>
<html>
<head>
<title>Responsive Image Submit Button</title>
<style>
.submit-btn {
max-width: 100%;
height: auto;
}
.form-container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
}
</style>
</head>
<body style="font-family: Arial, sans-serif;">
<div class="form-container">
<h2>Contact Form</h2>
<form action="/contact" method="post">
<label>Name:</label><br>
<input type="text" name="name" style="width: 100%; padding: 5px; margin-bottom: 10px;"><br>
<label>Message:</label><br>
<textarea name="message" style="width: 100%; height: 80px; padding: 5px; margin-bottom: 15px;"></textarea><br>
<input type="image" src="/images/send-message.png" alt="Send Message" width="90" height="35" class="submit-btn">
</form>
</div>
</body>
</html>
The image submit button has a fixed width attribute but uses CSS classes for responsive behavior on smaller screens.
Conclusion
The width attribute for <input type="image"> elements provides precise control over image submit button dimensions. While useful for specific sizing needs, consider CSS-based approaches for responsive designs. The attribute works exclusively with image input types and accepts pixel values to resize images while maintaining their submit button functionality.
