- Take the entire line width by default.
- Even if width is reduced, they still start on a new line.
Examples:
<p></p>
<div></div>- Behave like text.
- Stay within the same line.
- Width/height usually don't work properly.
Examples:
<strong></strong>
<u></u>
<span></span>- Take only the space they need.
- Can stay on the same line.
- Width and height work properly.
Examples:
<img>Used to change element behavior.
display: block;
display: inline;
display: inline-block;
display: flex;
display: grid;- A generic container/box.
- By default:
display: block;
width: 100%;Suppose we have:
- video title
- channel name
- video stats
We want:
- them stacked vertically
- but the whole section should not take the entire page width
So we wrap them inside a container.
<div class="video-info">
<p>Title</p>
<p>Channel</p>
<p>Stats</p>
</div>Then:
.video-info {
display: inline-block;
}Now the container only takes required space.
Images keep their original size and may overflow containers.
img {
width: 100%;
}Now the image fits the container width.
Entire image visible inside container.
object-fit: contain;Container fully filled by image.
object-fit: cover;- Works
- But alignment becomes messy
- Better alignment
- Cleaner layouts
.container {
display: flex;
flex-direction: row;
}Equivalent to:
1frin Grid.
Controls horizontal spacing.
justify-content: center;
justify-content: start;
justify-content: space-between;Controls vertical alignment.
align-items: center;
align-items: start;
align-items: end;
align-items: stretch; /* default */Prevents shrinking.
flex-shrink: 0;margin-left: -1px;box-shadow: inset 1px 2px 3px rgba(0,0,0,0.05);Format:
box-shadow: inset x y blur color;Useful in flex layouts when an input has a default minimum width.
width: 0;Allows shrinking properly.
overflow: hidden
Hides overflowing content.
Useful for:
- rounded corners
- clipping content
overflow: hidden;Whenever an element appears on top of another element, positioning is involved.
Default behavior.
position: static;- Removed from normal page flow
- Floats above page
- Stays fixed during scrolling
position: fixed;
top: 20px;
left: 0;If both left and right are used:
left: 0;
right: 0;the element stretches to satisfy both.
Fixed elements cover page content.
Solution:
body {
padding-top: 60px;
}Similar to fixed, but positioned relative to the page.
position: absolute;- Relative to browser window
- Doesn't move while scrolling
- Relative to page
- Moves while scrolling
Behaves normally unless absolute elements are placed inside it.
position: relative;Then:
.child {
position: absolute;
}Now the child becomes relative to the parent instead of the page.
We want:
- duration fixed on thumbnail
- but thumbnail itself should scroll
Solution:
.thumbnail {
position: relative;
}
.duration {
position: absolute;
}If positioned elements overlap:
- the element written later appears on top.
To fix:
z-index: 1;Higher z-index appears above lower ones.
Hide initially:
opacity: 0;Show on hover.
pointer-events: none;transition: opacity 0.15s;white-space: nowrap;.thumbnail{
width: 300px;
height: 300px;
object-fit: contain;
object-position: bottom;
border-width: 2px;
border-color: red;
border-style: solid;
}.search-bar{
font-size: 33px;
margin-left: 34px;
}.channel-picture {
display: inline-block;
vertical-align: top;
width: 50px;
}
.video-info {
display: inline-block;
vertical-align: top;
width: 200px;
}