how do width and height work in CSS the width and height properties are used to control the dimensions of elements on a web page these properties can be defined in different units such as pixels percentages viewport units and more you will learn more about these types of units in future videos the width property specifies the width of an element if you do not specify a width then the default is set to Auto this means the element will take up the full width of its parent container the height property specifies the height of an element similarly
the height is Auto by default which means it will adjust to the content inside here is an example using the width and height properties in this example we have a div element with a class named box this element will be occupying 100 pixels in height and width whereas the background color will be sky blue pixels are a fixed size unit of measurement in CSS providing precise control over Dimensions you will learn more about those in later videos here is the output result on the screen the minan width property specifies the minimum width an element can
take even if the content inside it is smaller the element won't shrink below this value the minan height specifies the minimum height an element can take it ensures that the element does not become shorter than the set value here is an example this example demonstrates how Min width and Min Height work even though the box has a width and height set to 50 pixels it will actually be 100 pixels by 100 pixels this is because the minan width and minan height are set to 100 pixels which are larger than the specified width and height remember
if mean width or mean height are larger than the width or height they will override the smaller values this ensures that elements don't become too small which is important for maintaining a consistent and usable design here's what the result will look like displayed on screen the max width specifies the maximum width an element can grow to even if there is enough space for it to be wider the max height specifies the maximum height an element can grow to regardless of the content size here is an example this example demonstrates how Max width and Max height
override width and height even though the box is set to 200 pixels by 200 pixels it will actually be 150 pixels by 150 pixels this is because the max width and Max height are set to 150 pixels which is smaller remember when Max width or Max height are smaller than width or height they take precedence this is important for controlling the maximum size of elements in your layout here's what the result will look like displayed on the screen CSS prioritizes minan width and minan height over width and height Max width and Max height restrict Dimensions
if values exceed their limits for example if you said the width to 600 pixels and Max width to 500 pixels the element will be limited to 500 pixels wide the max width overrides The Wider setting keeping the element within the specified maximum size this ensures elements stay within the desired size ranges regard regardless of standard width and height values