Typography in CSS and Google Fonts
Typography in CSS and Google Fonts
Google Fonts
</>
Topics Covered
Height and widt
Min and max-heigh
Min and max-widt
Box sizing
Note: In every example, we will use the most common CSS absolute unit called a pixel, denoted by "px." In the
next section, we will see each CSS unit (absolute unit and relative unit) with an example.
The "height" and "width" properties are used to set the size of an element. The "height" property sets the
height of the content area of an element, not including any padding, borders, or margins. The "width"
property sets the width of the content area of an element, again not including any padding, borders, or
margins.
Here's an example of how to use the "height" and "width" properties in CSS:
div {
width: 300px;
height: 200px;
The "min-height" property sets the minimum height that an element can have. If the content inside the
element requires more height than the specified minimum, the element will expand to accommodate the
content. If the content inside the element requires less height than the specified minimum, the element will
still have the minimum height.
Example-
index.html
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper
diam at erat pulvinar, at pulvinar felis blandit...</p>
<h2>min-height: 200px:</h2>
.minHeight {
min-height: 100px;
Browser Output-
If the content is smaller than the minimum height, the minimum height will be applied.
If the content is larger than the minimum height, the min-height property has no effect.
max-height
The "max-height" property sets the maximum height that an element can have. If the content inside the
element requires less height than the specified maximum, the element will shrink to fit the content. If the
content inside the element requires more height than the specified maximum, the element will still have the
maximum height.
Example
Index.html
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper
diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat
tellus diam, consequat gravida libero rhoncus ut. Maecenas imperdiet felis
nisi, fringilla luctus felis hendrerit sit amet. Pellentesque interdum,
nisl nec interdum maximus, augue diam porttitor lorem, et sollicitudin
felis neque sit amet erat.</p>
<h2>max-height: 50px:</h2>
.maxHeight {
max-height: 50px;
Browser output:
If the content is larger than the maximum height, it will overflow. If the content is smaller than the maximum
height, the max-height property has no effect.
The "min-width" property sets the minimum width that an element can have. If the content inside the
element requires more width than the specified minimum, the element will expand to accommodate the
content. If the content inside the element requires less width than the specified minimum, the element will still
have the minimum width.
Index.html
<h1>The min-width Property</h1>
<h2>min-width: 500px:</h2>
span {
background-color: yellow;
.minWidth {
min-width: 500px;
display: inline-block;
}
Browser output:
If the content is smaller than the minimum width, the minimum width will be applied.
If the content is larger than the minimum width, the min-width property has no effect.
max-width
The "max-width" property sets the maximum width that an element can have. If the content inside the
element requires less width than the specified maximum, the element will shrink to fit the content. If the
content inside the element requires more width than the specified maximum, the element will still have the
maximum width.
Index.html
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper
diam at erat pulvinar, at pulvinar felis blandit.</p>
<h2>max-width: 150px:</h2>
.maxWidth {
max-width: 150px;
Browser output:
If the content is larger than the maximum width, it will automatically change the height of the element.
If the content is smaller than the maximum width, the max-width property has no effect.
Box sizing
The box-sizing property sets how the total width and height of an element are calculated.
By default box-sizing is set to “content-box”, When using this value, the element's width and height
properties will only consider the content, and will not include the margin, padding, or border values.
For example, if we specify the width of an element as 200px, the content will be exactly 200px wide, while
the border and padding will be added to the final display, making the element wider than 200px.
The formula for calculating box size in the case of box-sizing: ”content-box” is
It means if we create a box of some specific height and width and then add padding and a border to it, it
will look wider than the actual width.
Index.html
Style.css
.withoutPadding {
height: 50px;
width: 100px;
.withPadding {
height: 50px;
width: 100px;
padding: 10px;
Browser output:
The "border-box" value for the box-sizing property is commonly used to tell the browser to fit the
element's border and padding within its specified width and height.
For example, if we set an element's width to 200px, the border and padding we specify will be included in
that 200px, and the content box will adjust to any extra width.
Example: Let's compare border-box and content-box with the below example.
In the below example, we have two containers, one with box-sizing: ”border-box” and another with
default box-sizing:”content-box”(no need to set explicitly)
index.html
<div class="withBoxSizing">box-sizing:border-box</div>
style.css
.withBoxSizing {
height: 100px;
width: 200px;
padding: 10px;
box-sizing: border-box;
.withoutBoxSizing {
height: 100px;
width: 200px;
padding: 10px;