CSS | Height and Width

Height and Width in CSS are used to set the height and width of boxes. It’s value can be set using length, percentage or auto.

Width and height

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
      <title>width and height</title>
      <style>
         .GFG{
         height: 120px;
         width: 50%;
         border: 5px solid black;
         padding-left: 50px;
         padding-top: 50px;
         font-size:42px;
         font-weight:bold;
         color:green;
         margin-left:50px;
         margin-top:50px;
         }
      </style>
   </head>
   <body>
      <div class = "GFG"> GeeksforGeeks </div>
   </body>
</html>

chevron_right


Output:

width and height

Height and width of Image: It is used to set the height and width of an image. It’s value can be in px, cm, percent, … etc.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
      <title>Height and width of image</title>
      <style>
         .GFG {
         width:300px;
         height:200px;
         border:3px solid black;
         }
      </style>
   </head>
   <body>
      <h3>Set the width and height of an Image</h3>
      <img class="GFG" src="4.jpg">
   </body>
</html>

chevron_right


Output:

Height and width of an image

Set max-width and min-width of an element:

  • max-width: It is used to set the maximum width of the box. It’s effect can be seen by resizing the browsers.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
       <head>
          <title>max-width of element</title>
          <style>
             .GFG {
             max-width:500px;
             border:2px solid black;
             }
          </style>
       </head>
       <body>
          <div class="GFG">
          <h3>GeeksforGeeks</h3>
          <p>GeeksforGeeks is a computer science platform
             where you can learn programming. It is a Computer 
             Science portal for geeks. It contains well written,
             well thought and well explained computer science 
             and programming articles, quizzes etc.
          </p>
       </body>
    </html>

    chevron_right

    
    

    Output:

    max-width

  • min-width: It is used to set the minimum width of the box. It’s effect can be seen by resizing the browsers.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
       <head>
          <title>min-width of element</title>
          <style>
             .GFG {
             min-width:400px;
             border:2px solid black;
             }
          </style>
       </head>
       <body>
          <div class="GFG">
          <h3>GeeksforGeeks</h3>
          <p>GeeksforGeeks is a computer science platform
             where you can learn programming. It is a Computer 
             Science portal for geeks. It contains well written,
             well thought and well explained computer science 
             and programming articles, quizzes etc.
          </p>
       </body>
    </html>

    chevron_right

    
    

    Output:

    min-width

Set max-height and min-height of an element:

  • max-height: It is used to set the maxumum height of the box. It’s effect can be seen by resizing the browsers.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
       <head>
          <title>max-height of element</title>
          <style>
             .GFG {
             max-height:100px;
             border:2px solid black;
             }
          </style>
       </head>
       <body>
          <div class="GFG">
          <h3>GeeksforGeeks</h3>
          <p>GeeksforGeeks is a computer science platform
             where you can learn programming. It is a Computer 
             Science portal for geeks. It contains well written,
             well thought and well explained computer science 
             and programming articles, quizzes etc.
          </p>
       </body>
    </html>

    chevron_right

    
    

    Output:

    max-height

  • min-height: It is used to set the minimum height of the box. It’s effect can be seen by resizing the browsers.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
       <head>
          <title>min-height of element</title>
          <style>
             .GFG {
             min-height:50px;
             border:2px solid black;
             }
          </style>
       </head>
       <body>
          <div class="GFG">
          <h3>GeeksforGeeks</h3>
          <p>GeeksforGeeks is a computer science platform
             where you can learn programming. It is a Computer 
             Science portal for geeks. It contains well written,
             well thought and well explained computer science 
             and programming articles, quizzes etc.
          </p>
       </body>
    </html>

    chevron_right

    
    

    Output:

    min-height



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.