Web Development Tutorials.com

How to set the width of an HTML element with CSS using a pixel value


Click Here for CoffeeCup Website Design Software   GoDaddy.com Hosting & Servers

The below HTML and CSS code example shows you how to set the width of an HTML element with CSS using a pixel value. In this example, the HTML element is an image.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>this is the title of the web page</title>

<style type="text/css">
img.normal
{
width: auto;
}
img.small
{
width: 100px;
}
</style>

</head>
<body>

<p>
<img class="normal" src="image.jpg" width="500" height="44" alt="css image tutorial" />
</p>
<p>
<img class="small" src="image.jpg" width="500" height="44" alt="css image tutorial" />
</p>

</body>
</html>

The XHTML and CSS code above makes the images below:

css image tutorial

css image tutorial

You can the following image file (image2-w500h44.jpg) and the text editor below to practice setting the width of an HTML element with CSS.