The min-height Property in CSS

We can define a fixed min-height for an element?s content box using CSS min-height property which does not allows the element?s content box to be smaller even if height is lesser than min-height.

Syntax

The syntax of CSS min-height property is as follows −

Selector {
   min-height: /*value*/
}

The value can be −

  • length − Set the min height in px, cm, em, etc.

  • % − Set the min height in %

Example

In this example, we have set the minimum height for the <p> element −

p.demo {
   min-height: 120px;
   background-color: green;
}

Let us see the example −

<!DOCTYPE html>
<html>
<head>
   <style>
      p {
         color: white;
         background-color: blue;  
      }
      p.demo {
         min-height: 120px;
         background-color: green;
      }
   </style>
</head>
<body>
   <h1>Demo Heading</h1>
   <h2>Learn Java</h2>
   <p>This is a demo text.</p>
   <h2>mLearn Python</h2>
   <p class="demo">This is a demo text.</p>
   <h2>mLearn Python</h2>
   <p>This is a demo text.</p>
</body>
</html>

Example

Let?s see another example for the CSS min-height property. When the button is clicked, the minim height is set to 100px for the content div −

<script>
   function add() {
      document.querySelector('#contentDiv').style.minHeight = "100px";
   }
</script>

Let us see the example −

<!DOCTYPE html>
<html>
<head>
   <title>CSS min-height Property</title>
   <style>
      * {
         padding: 2px;
         margin:5px;
      }
      button {
         border-radius: 10px;
      }
      #containerDiv {
         width:70%;
         margin: 0 auto;
         padding:20px;
         background-image: linear-gradient(135deg, #dc3545 0%, #9599E2 100%);
         text-align: center;
         border-radius: 10px;
      }
      #contentDiv{
         min-height:150px;
      }
   </style>
</head>
<body>
   <div id="containerDiv">
      <div id="contentDiv">
         This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
         This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
         This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
      </div>
      <button onclick="add()" class="btn">Set minHeight</button>
   </div>
   <script>
      function add() {
         document.querySelector('#contentDiv').style.minHeight = "100px";
      }
   </script>
</body>
</html>

Example

Let?s see another example for the CSS min-height property. When the button is clicked, the minim height is set for the 1st child div −

<script>
   function add() {
      document.querySelector('#contentDiv1').style.minHeight = "95px";
   }
</script>

Let us see the example −

<!DOCTYPE html>
<html>
<head>
   <title>CSS min-height Property</title>
   <style>
      * {
         padding: 2px;
         margin:5px;
      }
      button {
         border-radius: 10px;
      }
      #containerDiv {
         width:70%;
         margin: 0 auto;
         padding:20px;
         background-image: linear-gradient(135deg, #dc3545 0%, #9599E2 100%);
         text-align: center;
         border-radius: 10px;
      }
      #contentDiv1, #contentDiv2{
         width:50%;
         border: 2px solid black;
         margin: 0 auto;
      }
   </style>
</head>
<body>
   <div id="containerDiv">
      <div id="contentDiv1">
         This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
      </div>
      <div id="contentDiv2">
         This is paragraph 1 with some dummy text.
         This is paragraph 1 with some dummy text.
         This is paragraph 1 with some dummy.
      </div>
      <button onclick="add()" class="btn">Set minHeight</button>
   </div>
   <script>
      function add() {
         document.querySelector('#contentDiv1').style.minHeight = "95px";
      }
   </script>
</body>
</html>
Updated on: 2023-12-29T15:39:44+05:30

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements