With CSS3 you can give your users a greater level of control over how they view your pages without having to employ complex JavaScript functions. Using the resize property in CSS3, you can set elements to be automatically resizable. These elements appear within the browser with a subtle indicator at the bottom right corner letting users know that the element is resizable. On clicking and dragging the corner, the user can resize your element.

The resize property in itself is not complex, but can have complex effects on your other elements and on the content of the element being dragged. In this tutorial we will run through the basics of using the CSS3 resize property, looking at how it functions in a page with multiple elements and using it for an image. CSS3 resizing does not have full browser support yet, but functions with varying degrees of success in recent versions of Firefox, Safari and Chrome. Internet Explorer and Opera are not yet supporting the property.

Create a Web Page

Since we are using CSS3 properties, let’s go ahead and create an HTML5 page using the following structure:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">

</style>
</head>
<body>

</body>
</html>

We will place HTML elements in the body and a style section in the head.

Add Some Elements

Let’s start with a few simple elements, adding the following markup to your page body section:

<div class="textContent">

<div>CONTENT A</div>
<div>CONTENT B</div>
<div>CONTENT C</div>
<div>CONTENT D</div>
<div>CONTENT E</div>
<div>CONTENT F</div>

</div>

As you can see these elements contain some simple text indicating which is which, so that you can get a good sense of what the browser does when you resize. The container element is just there so that we can identify the group of elements within our CSS code using the class attribute.

Add Some Style

Style the “div” elements with the following declarations in your head CSS code:

.textContent div {
background-color:#dedede;
float:left;
margin:10px;
padding:10px;
resize:both;
overflow:auto;
}

We set a variety of default styling properties in addition to the resize property – you must set the overflow property to a value other than “visible” for resizing to function correctly. The other properties are just there to make the demonstration clearer.

Open your page in a browser and experiment with clicking and dragging to resize the elements. Notice what happens when you resize an element so that it prevents the other elements from fitting alongside it horizontally. The float property will determine how the browser rearranges the elements to fit on the page.

Options

In certain browsers, the default behavior is to prevent you from resizing the elements so that they become smaller, but in others you can resize larger and smaller. The support level for the property will of course change in the near future.

Notice that in the code above, we used the “both” value, meaning that the element can be resized by the user both horizontally and vertically. You can optionally choose to specify “horizontal” or “vertical” if you only want the element to resize along one axis. This can be helpful when attempting to strike a balance between allowing the user to customize the appearance of your page and still remaining faithful to your original visual design.

Resizing Images

Strictly speaking, the resize property is not really designed to be used with images. However, using a combination of CSS properties we can begin to see how this level of customization may develop. Add an image inside a “div” element in your page body as follows:

<div class="imgContent">
<img src="picture.jpg" alt="pic"/>
</div>

In this case we are assuming the image is 400 pixels wide and 300 pixels in height. You can of course alter the file-name to point to an image of your own. Now style the containing element for the image in your CSS area:

.imgContent {
width:400px;
height:300px;
resize:both;
overflow:hidden;
background-color:#ffff99;
padding:5px;
}

We set the dimensions of the containing “div” to match the dimensions of the image. By setting the resize property, overflow to hidden and adding a small padded area, the user will be able to resize the containing div element. Now we style the image:

img {
width:100%;
height:100%;
}

Using relative size values for the image means that its size will stretch and scale to fill the specified percentage of the parent element. The parent element can be resized, so the image will automatically resize along with it. Test your page in a browser to see the effect. Adding a small amount of padding on the parent element makes dragging the element easier.

Conclusion

CSS3 resizing is one of the many techniques that is not being widely used yet, mainly due to a poor level of support. Although it’s early days to start relying on this function, it does give us an exciting glimpse into the level of customization future Web users will enjoy while browsing.

See what the finished product looks like here.

By Sue Smith
Sue Smith works as a Web/ software developer and technical writer based in the UK: see benormal.info for details. Sue has written for various clients including Smashing Magazine and Mobiletuts+. She also does a little Android development and some comedy writing.
  • Kenzo Minang

    wow… I used to know its just resize the div element. But with this, I can resize images 🙂
    Thank U…

  • Thats a great tip, thanks. I know you mention support being an issue in the conclusion, but it would be nice to have a little meta data section to the article showing what the support is for different browsers. Just a suggestion. Thanks again

  • Thanks for tips, very usefully 🙂

  • Jonathan Scialpi

    Once a user has resized a div, how can we save those new height and width values so that when they refresh the page, the divs remain the same size

Home XML WordPress Web Services Web Development Web Design Underscore Uncategorized Tutorial Tools SQL Server Social Apps Snippet SEO Security RoR Responsive Design Resources Python PHP News MySQL Mobile Miscellaneous jQuery JavaScript Java J2EE HTML5 HTML Design Patterns