Sunday, 20 September 2015

CSS Width

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

ValueDescription
autoIt is a default value. it is used to calculate the width.
lengthIt is used to define the width in px, cm etc.
%It defines the width of the containing block in %.
initialIt is used to set the property to its default value.
inheritIt 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>  

Output:


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:

 

No comments:

Post a Comment