Week 02 - CSS Petr Wehrenberg PB138 - Basics of web development and markup languages Outline Intro to CSS and how it works Layouting vs. styling BEM Responsivity Alternatives, tips and tricks PB138 - Basics of web development and markup languages What is CSS? Stands for Cascading Style Sheets Basic HTML is readable in a browser, but isn't aesthetically pleasing Basic styling (colors, fonts) and advanced styling (animations, 3D transforms) Allows to style different viewport widths (mobile vs desktop) within a document PB138 - Basics of web development and markup languages An example of CSS .btn { font-size: 1.15rem; color: white; padding: 1rem 1.5rem; text-align: center; background-color: #36393f; } PB138 - Basics of web development and markup languages selector { property: value; } PB138 - Basics of web development and markup languages How to include styles PB138 - Basics of web development and markup languages Link /style.css .cool-navbar { display: flex; color: #343434; } /index.html
PB138 - Basics of web development and markup languages Style element /index.html PB138 - Basics of web development and markup languages Inline /index.html PB138 - Basics of web development and markup languages So how it works PB138 - Basics of web development and markup languages "Cascading" meaning PB138 - Basics of web development and markup languages CSS Selectors #identifier { } .class { } div { } PB138 - Basics of web development and markup languages Specificity element < class < identifier < inline PB138 - Basics of web development and markup languages CSS Pseudo elements State: hover , visited , or focus Ancestor relationship: first-child , last-child , only-child , nth-of-type , empty , etc. .class:hover { } input:focus { } tr:nth-child(even) { } PB138 - Basics of web development and markup languages CSS Atribute selectors the presence of an attribute specific value a[title] { } a[href="https://example.org"] { } PB138 - Basics of web development and markup languages CSS Selectors .comment.btn { font-size: 2rem; } .comment h2 { font-size: 2rem; } .comment > .content { font-size: 2rem; } PB138 - Basics of web development and markup languages Messy CSS .header .navigation ul li a.active { color: white; background-color: blue; } body.homepage .content .article > p:first-child { font-size: 20px; } .footer .widget-area .widget:first h3 { font-weight: bold; } .sidebar .widget, .footer .widget-link-list, .header .dropdown-menu { background-color: #f0f0f0; } PB138 - Basics of web development and markup languages CSS Priority 1. !important 2. Specificity 3. Source (author, user, broswer) 4. Order in source file PB138 - Basics of web development and markup languages Browser Support Visit caniuse.com to see what browsers support what features PB138 - Basics of web development and markup languages Checkpoint - Intro to CSS Anatomy Source (css files, css element, inline) Selectors (identifier, class, element) Pseudo selectors Specificity Priority PB138 - Basics of web development and markup languages Layouting and styling PB138 - Basics of web development and markup languages Layouting and styling PB138 - Basics of web development and markup languages Styling Reusable Components: Buttons, cards, avatars. Visual Attributes: Color, font, rounded corners. Theming: Dark/light mode, brand colors. Interaction Feedback: Hover, active, disabled states. .btn { border-radius: 1rem; background-color: #33aa22; font-size: 1.2rem; } PB138 - Basics of web development and markup languages CSS Variables :root { --radius-sm: 0.8rem; --radius-md: 1rem; /* ... */ } .btn { border-radius: var(--radius-md); background-color: var(--bg-accent); font-size: var(--text-lg); } PB138 - Basics of web development and markup languages CSS Units body { font-size: 12px; /* NOT Recommended */ font-size: 2em; /* 2x current font */ font-size: 2rem; /* 2x root font */ width: 10rem; width: 10vh; width: 10vw; } PB138 - Basics of web development and markup languages Layouting Component Positioning: Arrange elements spatially. Content Flow: Logical structure, easy navigation. Responsivity: Media queries, flexible grids. Main tools: Grid, Flexbox PB138 - Basics of web development and markup languages Flexbox PB138 - Basics of web development and markup languages Flex container .parent-element { display: flex; flex-direction: row; flex-wrap: no-wrap; justify-content: space-between; align-items: center; } PB138 - Basics of web development and markup languages Flex item .child-element { flex: 1 0 100px; /* grow, shrink, basis */ } PB138 - Basics of web development and markup languages Flex Guide Pictures are worth more than 1000 words of the lecturer: css-tricks.com/snippets/css/a-guide-to-flexbox/ PB138 - Basics of web development and markup languages Grid PB138 - Basics of web development and markup languages Grid container .parent-element { display: grid; grid-template-columns: 1fr 1fr 50px; grid-template-rows: 30px 30px 30px; } PB138 - Basics of web development and markup languages Grid item .child-element { grid-column-start: 2; grid-column-end: 3; grid-row: 1 / span 2; } PB138 - Basics of web development and markup languages Grid Guide Pictures are worth more than 1000 words of the lecturer: css-tricks.com/snippets/css/complete-guide-grid/ PB138 - Basics of web development and markup languages Positioning PB138 - Basics of web development and markup languages Static .parent-element { position: static; } PB138 - Basics of web development and markup languages Relative .parent-element { position: relative; left: 30px; } PB138 - Basics of web development and markup languages Absolute .parent-element { position: relative; } .child-element { position: absolute; top: 10px; left: 30%; } PB138 - Basics of web development and markup languages Fixed .child-element { position: fixed; top: 10px; left: 30%; } PB138 - Basics of web development and markup languages Sticky .parent-element { position: relative; } .child-element { position: sticky; top: 20px; } PB138 - Basics of web development and markup languages Positioning: DEMO PB138 - Basics of web development and markup languages Checkpoint - Styling and layouting Difference between styling and layouting Styling (CSS variables, units) Layouting (Flex, Grid, Positioning) PB138 - Basics of web development and markup languages BEM PB138 - Basics of web development and markup languages Problem .header .navigation ul li a.active { color: white; background-color: blue; } body.homepage .content .article > p:first-child { font-size: 20px; } .footer .widget-area .widget:first-of-type h3 { font-weight: bold; } .sidebar .widget, .footer .widget-link-list, .header .dropdown-menu { background-color: #f0f0f0; } PB138 - Basics of web development and markup languages Solution .nav { background-color: #ee42fd; } .nav__item { margin: 5px 0; } .nav__link { color: black; text-decoration: none; } .nav__link--active { color: white; background-color: blue; } PB138 - Basics of web development and markup languages What is BEM? The BEM methodology helps to think with components: Block - a standalone entity that is meaningful on its own Element - a part of a block with no meaning on its own, semantically tied to block Modifier - a flag on block/element used to modify appearance or behavior PB138 - Basics of web development and markup languages BEM - Block A BEM Block consists of a singular component, which can potentially be reused throughout the page It encapsulates an entity on the page (search bar, navbar, button, avatar, ...) CSS: .avatar { display: flex; clip-path: circle(50% at center); overflow: hidden; } HTML:Click me