Štěpán Stenchlák, KSI
stepan.stenchlak@matfyz.cuni.cz
Today's goal is to finish the HTML & CSS part of the course.
Download the HTML file we will work with: cats.html.
Familiarize yourself with it.
Used to remove unwanted default behavior and to ensure consistency across browsers.
Can be highly opinionated, there is no one true reset.
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
}
/* Inherit fonts for form controls */
input, button, textarea, select {
font: inherit;
}
Example of more aggressive reset: Tailwind.
Easier sizing of elements because it includes their border and padding.
width: 100%; padding: 1em; border: .5em solid darkgoldenrod;
box-sizing: border-box;
margin: 0 1em;
Approach to web design that makes web pages render well on a variety of screen sizes (mobile, tablet, ultra wide monitors, ...).
<meta name="viewport" content="width=device-width, initial-scale=1.0,
maximum-scale=1.0, user-scalable=no, minimal-ui">
Check out this example.
Control how elements are arranged on the page.
There are different CSS properties that control which layout mode is used: position, display, float.
Row elements are placed one after another in the same style as a text in a paragraph.
Block elements, such as div or p, start on a new line and take the full width available.
Long lines are wrapped to the next line.
line-height plays a role.
Use only for text-like content.
word-break: break-alloverflow-wrap: break-wordYou might need to modify HTML.
position: relative - Element is rendered normally and then moved (relatively to itself).position: absolute - Element is rendered after the normal flow and then moved (relatively to its last positioned ancestor).position: fixed - Like absolute, but is moved relatively to the viewport.position: sticky - Like relative, but is moved relatively to its last scrolling ancestor.Relative position
(top, left)
Absolute position
(margin-top, margin-left)
Absolute position
(top, left)
Sticky position
(top)
Positions elements in a one-dimensional way (row or column).
You can use it for:
Check CSS Flexbox Layout Guide by CSS-Tricks.
flex-wrap: wrap; min-width: 7em; flex-grow: 1;
Positions elements in a 2D table.
You can use it for:
Check CSS Grid Layout Guide by CSS-Tricks.
Download this HTML document and fix TODOs in the source code.
Apply different styles based on window width.
Order of statements matters!
/* Styles for the smallest devices */
@media (min-width: 576px) {
/* Overrides for sm devices */
}
@media (min-width: 768px) {
/* Overrides for md devices */
}
@media (min-width: 992px) {
/* Overrides for lg devices */
}
@media (min-width: 1200px) {
/* Overrides for xl devices */
}
Modify the website to look good on mobile phones by:
Page is split to 12 "virtual" columns. Each element in a row specifies how many columns it occupies on which device size.
col-12 col-lg-6 (on large devices two columns, on small devices one column)col-12 col-md-6 col-xl-3 (one column, two columns on wider and 4 columns on large)<!-- Equal width -->
<div class="row">
<div class="col"></div>
<div class="col"></div>
</div>
<!-- Custom width based on 12 column system -->
<div class="row">
<div class="col-12 col-lg-6"></div>
<div class="col-12 col-lg-3"></div>
<div class="col-12 col-lg-3"></div>
</div>
Download 03-columns.html and fix TODOs in the source code.
The idea is to implement column system for selected screens. Since we have limited time we implement support only for two breakpoints: 768px and 1200px. This leave us with three screens: xs < 768px, md ≥ 768px, and xl ≥ 1200px.
Try to resize the windows to see the change.
CSS can animate changes of selected properties using a transition property.
Typically combined with something that changes the style of the property such as :hover.
.element {
transition: all 1s ease; /* The transition property must be applied here, not in :hover only! */
transform: rotate(0); /* You need to set the initial state! */
}
.element:hover {
transform: rotate(360deg);
}
More complex animations can be created using keyframes.
@keyframes bounce {
0% { left: 0; top: 0; }
25% { left: 2em; top: 0; }
50% { left: 2em; top: 2em; }
75% { left: 0; top: 2em; }
100% { left: 0; top: 0; }
}
.target-element {
position: relative;
animation: bounce 0.5s infinite alternate;
}
Then add at least one animation (perpetual) and transtion (on hover).
Upload the file to your public_html directory on webik page as cats.html.