Loading course content...
Loading course content...
Code Playground is only enabled on larger screen sizes.
After understanding the box model, let's discuss how to change the size of the box using width and height properties.
<p>Lorem ipsum dolor . . .</p>p {
border: 2px solid black;
width: 200px;
height: 50px;
}Notice that the content is too big to fit inside the 200 x 50px box. By setting an overflow property, you can customize how the overflow is handled.
When set to hidden, the overflow will be cropped off.
When set to scroll, the overflow will be cropped off, but a scrollbar will be added, allowing you to scroll down to see the full content.
Or you can simply set it to auto, which adds a scrollbar only when necessary.
<p class="hidden">Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
<p class="scroll">Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
<p class="auto">Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>p {
border: solid 2px;
width: 150px;
height: 150px;
}
.hidden {
overflow: hidden;
}
.scroll {
overflow: scroll;
}
.auto {
overflow: auto;
resize: both; /* make element resizable */
}There is one more issue with this box. Consider this example:
<p>Lorem ipsum dolor . . .</p>p {
border: 2px solid black;
width: 200px;
height: 50px;
}Try to do this on your own machine and inspect this paragraph element using the Developer Tools, and you will see that even though we defined this element to be 200px wide and 50px tall, it is, in fact, 204 x 54px.
This is because when you define the box size, the browser assumes you refer to the size of the content, meaning it ignores the padding and the border itself. For example, let's make the border thicker and add a padding. Notice that the size of the content box remains the same.
p {
border: 10px solid black;
padding: 10px;
width: 200px;
height: 50px;
}This can be counterintuitive sometimes because when we define the box size, we usually mean the size of the border, not just the content.
To fix this issue, you can overwrite the browser's default actions by setting the box-sizing property to border-box.
p {
box-sizing: border-box;
border: 10px solid black;
padding: 10px;
width: 200px;
height: 50px;
}By default, this property is set to content-box, so the browser will count the content size as the box size.
When you set it to border-box, the browser will take padding and border into account when calculating the box size.
Besides setting a fixed dimension for the elements, you can also be more flexible by specifying the maximum or minimum sizes or both. For example, you can define the maximum width of the <p> element as 400px.
p {
border: solid 2px;
max-width: 400px;
}Try this demo in a new browser tab, and resize your browser window. Make sure its width is less than 400px.
Slowly make the window wider, and notice that the <p> element grows with it until it reaches 400px and stops growing.
The minimum value works the same way, just in the opposite direction.
p {
border: solid 2px;
min-width: 400px;
}You can specify both maximum and minimum values to ensure the element size is always within the defined limit.
This is crucial for a design concept called responsive design, which we will discuss later.
Finally, when it comes to resizing elements, we can't overlook the importance of CSS units. We've already covered some commonly used ones, such as px, em, and rem. Now, let's take this opportunity to summarize all the units available in CSS.
Units in CSS can be divided into two groups, absolute units and relative units.
First of all, we have the absolute units:
cm: Centimeters.mm: Millimeters.in: Inches.px: Pixels.pt: Points, 1pt equals 1/72in.pc: Picas, 1pc equals 12pt.These units define a fixed value, and they will not be affected by other elements ot the screen size. They are useful when you need precise control over the size of an element.
Besides the absolute units, there are also relative units, whose values are relative to some other elements or the screen size.
em: Relative to the font size of the parent element. If not set, the default font size will be 16px. 2em is two times the current font size, which is 32px.ex: Relative to the x-height of the current font. This is rarely used.ch: Relative to the width of the character 0. The value may be different for different fonts.rem: Relative to the font size of the root element, <html>. This provides a consistent base size across the entire document, and as a result, this is the most commonly used unit for fonts.vw: Relative to 1% of the width of the viewport. Viewport is the visible area of the webpage, such as the browser window.vh: Relative to 1% of the height of the viewport.vmin: Relative to 1% of the smaller dimension (width or height) of the viewport. This ensures the element is at least visible on the screen.vmax: Relative to 1% of the larger dimension (width or height) of the viewport.%: Relative to the parent element.These units adapt based on the context, and are more flexible and useful for responsive design.
Among these options, there are only 6 units that are most commonly used: px, em, rem, %, vw, and vh.
px is often used for fine-grained, pixel-level control when you need to specify the exact size of an element.em and rem are often used for font size and text spacing. em is relative to the font size of its closest parent element, and rem is relative to the document's root element.%, vw, and vh are the three most popular relative units used for defining dimensions and creating layouts that adapts to either the parent element (%), or the view port (vw, and vh).Each of these units plays a unique role in web design, providing different levels of flexibility and control depending on the layout and responsiveness requirements.
To summarize, when you need responsive and flexible dimensions and layouts, use %, vw, and vh; when you are working with texts, use em and rem; when you need to define the exact size of an element, use px.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="/styles.css"> </head> <body> <p>Hello, World!</p> </body> </html>