Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

CSS Width


The CSS width property is used to set the width of the content area of an element.

It does not include padding borders or margins. It sets width of the area inside the padding, border, and margin of the element.

CSS width values

Value Description
auto It is a default value. it is used to calculate the width.
length It is used to define the width in px, cm etc.
% It defines the width of the containing block in %.
initial It is used to set the property to its default value.
inherit It is used to inherit the property from its parent element.

CSS Width Example: width in px

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. img.normal {
  6.     width: auto;
  7. }
  8. img.big {
  9.     width: 150px;
  10. }
  11. p.ex {
  12.     height: 150px;
  13.     width: 150px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <img class=“normal” src=“good-morning.jpg” width=“95” height=“84”><br>
  19. <img class=“big” src=“good-morning.jpg” width=“95” height=“84”>
  20. <p class=“ex”>The height and width of this paragraph is 150px.</p>
  21. <p>This is a paragraph.</p>
  22. </body>
  23. </html>

Test it Now

Output:

good-morning

good-morning

The height and width of this paragraph is 150px.

This is a paragraph.


CSS Width Example: width in %

The percent width is a measurement unit for the containing block. It is great for images.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. img.normal {
  6.     width: auto;
  7. }
  8. img.big {
  9.     width: 50%;
  10. }
  11. img.small {
  12.     width: 10%;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <img class=“normal” src=“good-morning.jpg” width=“95” height=“84”><br>
  18. <img class=“big” src=“good-morning.jpg” width=“95” height=“84”><br>
  19. <img class=“small” src=“good-morning.jpg” width=“95” height=“84”>
  20. </body>
  21. </html>

Output:

good-morning

good-morning

good-morning

Note: You can also use the “min-width” and “max-width” property to control the size of image.

Comments

comments