What are width and max-width properties in CSS layout?
Overview
Typically, any block-level element uses all the available space to place an element in a container and stretches itself to fill it. However, we might not want this to happen because we may lose our control while working with a small screen.
CSS allows us to set layouts using width and max-width features to have more control over the element’s placement and spreading.
Difference between width and max-width
The width property
The width property allows us to set a specific width of the element, allowing it to expand to that size.
Syntax
width: auto or initial or specific value(50%, 50px, etc) or inherit;
Parameters
auto: This is a default value and depends on the size of the browser.initial: This is a keyword that is used to set any CSS property to its default value.specific value: This would be any number that you would write with thepx,cm,%, and more. This value will dictate the element to have this amount of width.inherit: This is a keyword that tells the property of an element to inherit its value from the parent element.
Note: Using the width property alone may lead to an issue while working with small or different screen sizes. If you have specified any value for the width and the screen size changes, it will automatically add a scroll bar for that element.
The max-width property
The max-width property allows us to set a boundary or a limit for the element. This property will restrict the component to not exceeding a particular value in any case.
The max-width property makes elements more attractive and presentable when moving to small screens.
Syntax
max-width: none or initial or specific value(50%, 50px, etc.) or inherit;
Parameters
none: By default, there is nomax-widthfor the elements. Therefore,nonerepresents that value.initial: This is a keyword that is used to set any CSS property to its default value.specific value: This is any particular value that we'll write withpx,cm,%, and more. This value would dictate that the element should not exceed the maximum width value.
inherit: This is a keyword that tells the property of an element to inherit its value from the parent element.
Example
Explanation
- Line 4: We open the
styletag. - Line 5–9: We define the class,
widthExample, fordiv. Inside the class, thewidthproperty is assigned a value of 600px, along with auto margins and some aesthetics to beautifydiv. - Lines 10–14: We define the class,
max-widthExample, fordiv. Themax-widthproperty is assigned a value of 600px along with auto margins and some aesthetics to beautifydiv. - Line 15: We close the
styletag before theheadtag. - Line 19: Inside the
divtag, we add our classwidthExample. This class will add colors to thedivtag and thewidthproperty value. Thisdivwill now expand to 600 pixels regardless of the screen size. - Line 20: We add a
breaktag for moving the next div to the following line. - Line 21: Inside the
divtag, we add our class,max-widthExample. This class will add colors todivalong with the max-width property value, restricting thisdivto not stretch over 600px.