Minulý týždeň dostala tvoja stránka obsah. Dnes dostane vzhľad — a formulár, ktorý o pár týždňov napojíme na PHP. Všetko potrebné je na tejto stránke; odkazy na W3Schools sú navyše, keď chceš viac.
Last week your page got content. Today it gets its looks — and a form that we will connect to PHP in a few weeks. Everything you need is on this page; the W3Schools links are extra, for when you want more.
TeóriaTheory
Čo je CSS a ako ho pripojiť
HTML hovorí, čo obsah je; CSS (Cascading Style Sheets) hovorí, ako má vyzerať. Štýl sa dá zapísať tromi spôsobmi: priamo do atribútu style prvku (inline), do značky <style> v hlavičke stránky, alebo do samostatného súboru. My používame výhradne samostatný súbor css/style.css — jeden súbor oštýluje všetky stránky webu naraz a zmeny robíš na jednom mieste. Do hlavičky každej stránky patrí:
<link rel="stylesheet" href="css/style.css">
Viac: W3Schools: CSS How To
Selektory na elementy
Pravidlo CSS má tvar selektor { vlastnosť: hodnota; }. Selektor vyberá prvky, na ktoré sa pravidlo použije. Najjednoduchší selektor je názov značky — zasiahne všetky jej výskyty na všetkých stránkach, ktoré štýl pripájajú.
| Zápis | Čo vyberie |
|---|---|
h2 { } |
všetky nadpisy h2 |
h1, h2 { } |
viac druhov prvkov naraz (čiarka) |
nav a { } |
len odkazy vo vnútri nav (medzera = zanorenie) |
Viac: W3Schools: CSS Selectors
Trieda (class)
Trieda označuje ľubovoľnú skupinu prvkov, ktoré majú vyzerať rovnako — bez ohľadu na to, aké sú to značky. V HTML je to atribút class, v CSS sa selektor triedy píše s bodkou. Jedna trieda môže byť na mnohých prvkoch a jeden prvok môže mať viac tried (oddelených medzerou): class="zvyraznene aktualne".
Viac: W3Schools: The class Selector
id — a kedy použiť čo
id označuje jeden konkrétny prvok: každé id smie byť na stránke len raz. V CSS sa píše s mriežkou #. Pravidlo palca: vzhľad rieš triedami, id si nechaj pre jedinečné prvky — zíde sa ti aj na kotvy v URL (form.html#kontakt) a o pár týždňov v JavaScripte, keď bude treba chytiť presne jeden prvok.
Viac: W3Schools: The id Selector
Box model
Každý prvok na stránke je obdĺžnik so štyrmi vrstvami: obsah → padding (vnútorný odstup) → border (rámik) → margin (vonkajší odstup od okolia). Keď niečo „nesedí o pár pixelov”, takmer vždy je to box model. Nakreslený si ho pozrieš v prehliadači cez F12 (nižšie).
Viac: W3Schools: CSS Box Model
Pozicovanie
Normálne idú prvky za sebou, ako sú zapísané v HTML (position: static). Ostatné hodnoty to menia:
| Hodnota | Čo robí |
|---|---|
relative |
posunie prvok voči jeho pôvodnému miestu a vytvorí „kotvisko” pre potomkov |
absolute |
umiestni prvok presne voči najbližšiemu rodičovi s position: relative |
fixed |
pripne prvok k oknu prehliadača — ideálne na menu, ktoré má byť stále viditeľné |
Obsah stránky sa centruje obmedzením šírky: max-width + margin: 0 auto (Ukážka 2).
Viac: W3Schools: CSS Position
Pseudotriedy :hover a :focus
Pseudotrieda štýluje stav prvku: :hover platí, keď je nad prvkom myš, :focus, keď je pole formulára aktívne (klikol si doň alebo si sa doň dostal tabulátorom). Práve nimi stránka „ožije” — a :focus je dôležitý aj pre prístupnosť: používateľ vidí, kde práve píše.
Viac: W3Schools: CSS Pseudo-classes
Formulár
Formulár zbiera údaje od návštevníka. Dve veci si zapamätaj už teraz:
labelhovorí, čo do poľa patrí; jeho atribútforukazuje naidpoľa, takže klik na popisok aktivuje pole.nameje meno, pod ktorým údaj odíde zo stránky. O pár týždňov si tieto údaje vyzdvihneme v PHP presne podľaname— preto nesmie chýbať pri žiadnom poli.
method="get" pripája odoslané údaje do URL: po odoslaní si adresný riadok pozri, svoje dáta tam uvidíš. Bežné typy polí: text, email, number, date, select (rozbaľovací zoznam), radio (jedna možnosť z viacerých — skupina má spoločné name), checkbox, textarea, tlačidlo submit. Celý formulár je v Ukážke 3.
Viac: W3Schools: HTML Forms
Preskúmaj si CSS v prehliadači (F12)
Stlač F12 a na karte Elements (Chrome/Edge) či Inspector (Firefox) klikni na ľubovoľný prvok: vpravo vidíš všetky pravidlá, ktoré naň platia; prečiarknuté sú tie, ktoré prehralo silnejšie pravidlo, a pri každom je napísané, z ktorého súboru a riadka prišlo. Dole nájdeš nakreslený box model prvku so skutočnými hodnotami. Toto je tvoj hlavný nástroj na ladenie CSS.
What CSS is and how to attach it
HTML says what the content is; CSS (Cascading Style Sheets) says how it should look. Styles can be written in three places: directly in an element’s style attribute (inline), in a <style> tag in the page head, or in a separate file. We use a separate file css/style.css only — one file styles all pages of the site and you make changes in one place. The head of every page gets:
<link rel="stylesheet" href="css/style.css">
More: W3Schools: CSS How To
Element selectors
A CSS rule has the form selector { property: value; }. The selector picks the elements the rule applies to. The simplest selector is a tag name — it hits all its occurrences on every page that attaches the stylesheet.
| Notation | What it selects |
|---|---|
h2 { } |
all h2 headings |
h1, h2 { } |
several kinds of elements at once (comma) |
nav a { } |
only links inside nav (space = nesting) |
More: W3Schools: CSS Selectors
Class
A class marks any group of elements that should look the same — no matter what tags they are. In HTML it is the class attribute, in CSS the class selector is written with a dot. One class can be on many elements and one element can have several classes (separated by spaces): class="zvyraznene aktualne".
More: W3Schools: The class Selector
The id — and when to use which
An id marks one specific element: each id may appear on a page only once. In CSS it is written with a hash #. Rule of thumb: style with classes, keep id for unique elements — it is also useful for anchors in the URL (form.html#kontakt) and in a few weeks in JavaScript, when you need to grab exactly one element.
More: W3Schools: The id Selector
The box model
Every element on a page is a rectangle with four layers: content → padding (inner spacing) → border → margin (outer spacing from the surroundings). When something is “off by a few pixels”, it is almost always the box model. You will look at it drawn in the browser via F12 (below).
More: W3Schools: CSS Box Model
Positioning
Normally elements follow each other as written in the HTML (position: static). The other values change that:
| Value | What it does |
|---|---|
relative |
shifts the element relative to its original place and creates an “anchor” for descendants |
absolute |
places the element precisely relative to the nearest parent with position: relative |
fixed |
pins the element to the browser window — ideal for a menu that should stay visible |
Page content is centred by limiting its width: max-width + margin: 0 auto (Example 2).
More: W3Schools: CSS Position
The :hover and :focus pseudo-classes
A pseudo-class styles the state of an element: :hover applies when the mouse is over it, :focus when a form field is active (you clicked into it or reached it with Tab). This is what brings a page to life — and :focus also matters for accessibility: the user sees where they are typing.
More: W3Schools: CSS Pseudo-classes
The form
A form collects data from the visitor. Remember two things right away:
labelsays what belongs in a field; itsforattribute points to the field’sid, so clicking the label activates the field.nameis the name under which the value leaves the page. In a few weeks we will pick these values up in PHP exactly by theirname— so no field may be missing one.
method="get" appends the submitted data to the URL: after submitting, look at the address bar and you will see your data there. Common field types: text, email, number, date, select (drop-down), radio (one option of several — the group shares one name), checkbox, textarea, a submit button. The whole form is in Example 3.
More: W3Schools: HTML Forms
Explore CSS in the browser (F12)
Press F12 and on the Elements tab (Chrome/Edge) or Inspector (Firefox) click any element: on the right you see all the rules that apply to it; the crossed-out ones lost to a stronger rule, and each shows which file and line it came from. Below you will find the element’s box model drawn with real values. This is your main tool for debugging CSS.
UkážkyExamples
Ukážka 1: selektory, trieda a id
h2 { color: #a3274f; } /* všetky nadpisy h2 */
h1, h2 { font-family: Georgia, serif; } /* viac selektorov naraz */
nav a { text-decoration: none; } /* len odkazy vo vnútri nav */
.zvyraznene { background-color: #f3e1e8; } /* trieda: bodka */
#kontakt { border-top: 3px solid #a3274f; } /* id: mriežka */
<p class="zvyraznene">Dôležitý odsek.</p>
<section id="kontakt"> ... </section>
Ukážka 2: box model, pozicovanie a centrovanie
img {
padding: 4px; /* odstup obsahu od rámika */
border: 2px solid #a3274f; /* rámik */
margin: 16px; /* odstup od okolitých prvkov */
max-width: 100%; /* obrázok nepretečie mimo stránku */
}
nav { position: fixed; top: 0; left: 0; right: 0; }
body { padding-top: 60px; } /* aby pripnuté menu neprekrylo obsah */
.sekcia { position: relative; } /* kotvisko */
.sekcia .stitok { position: absolute; top: 8px; right: 8px; }
main { max-width: 800px; margin: 0 auto; } /* centrovanie obsahu */
nav a:hover { background-color: #a3274f; color: white; }
input:focus, textarea:focus { outline: 2px solid #a3274f; }
Ukážka 3: formulár so všetkými bežnými poľami
<form method="get">
<label for="meno">Meno</label>
<input type="text" id="meno" name="meno">
<label for="email">E-mail</label>
<input type="email" id="email" name="email">
<label for="pocet">Počet</label>
<input type="number" id="pocet" name="pocet" min="1">
<label for="termin">Termín</label>
<input type="date" id="termin" name="termin">
<label for="kategoria">Kategória</label>
<select id="kategoria" name="kategoria">
<option>prvá</option>
<option>druhá</option>
<option>tretia</option>
</select>
<fieldset>
<legend>Hodnotenie</legend>
<label><input type="radio" name="hodnotenie" value="1"> dobré</label>
<label><input type="radio" name="hodnotenie" value="2"> lepšie</label>
<label><input type="radio" name="hodnotenie" value="3"> najlepšie</label>
</fieldset>
<label><input type="checkbox" name="suhlas"> Súhlasím so spracovaním</label>
<label for="sprava">Správa</label>
<textarea id="sprava" name="sprava"></textarea>
<button type="submit">Odoslať</button>
</form>
Takto vyzerá kód z ukážky v prehliadači. Vyskúšaj: klikni do poľa (:focus), prejdi myšou na tlačidlo (:hover), vyplň, stlač Odoslať a pozri si adresný riadok.
Po odoslaní sa pozri do adresného riadka: form.html?meno=Jana&email=... — presne tieto dvojice name=hodnota si o pár týždňov prečíta PHP.
Ukážka 4: prompt pre AI na dvojstránkový web
Web nepíšeš ručne, zadáš ho AI. Dobrý prompt presne povie, čo chceš dostať:
Vytvor mi jednoduchý web v HTML5 v slovenčine (lang="sk", kódovanie UTF-8),
dve stránky a jeden samostatný súbor so štýlmi. Bez JavaScriptu a bez
frameworkov (žiadny Bootstrap).
Štruktúra súborov, presne takto:
- index.html
- form.html
- css/style.css (jediné miesto so štýlmi, žiadne inline štýly ani <style>)
- img/obrazok.jpg (obrázok dodám sám)
index.html obsahuje:
- hlavičku s nadpisom h1 a vodorovné menu s odkazmi na index.html a form.html
(menu je rovnaké na oboch stránkach),
- aspoň dve sekcie s nadpismi h2 a odsekmi textu, jedna sekcia má id,
- obrázok img/obrazok.jpg s atribútom alt,
- pätičku s menom a rokom.
form.html obsahuje to isté menu a formulár s method="get" (zatiaľ bez
spracovania — neskôr ho napojíme na PHP, preto KAŽDÉ pole musí mať atribút
name a vlastný label):
- textové pole (meno), e-mail (type="email"), číslo (type="number"),
dátum (type="date"),
- select s aspoň tromi možnosťami,
- skupinu troch radio tlačidiel s rovnakým name,
- dva checkboxy,
- textarea na správu,
- odosielacie tlačidlo.
CSS v css/style.css: čitateľné písmo, obsah s obmedzenou šírkou centrovaný cez
max-width a margin auto, farebne odlíšené nadpisy, menu položky vedľa seba,
vzhľad formulára (polia pod sebou, label nad poľom, odsadenia), obrázok nesmie
pretiecť mimo stránku. Použi selektory na elementy, aspoň jednu triedu a aspoň
jeden id selektor.
Do HTML aj CSS napíš krátke slovenské komentáre ku každému bloku, ktoré
vysvetlia, čo robí a prečo — učím sa z nich.
Téma webu: [DOPLŇ tému svojho projektu]
Prečo je prompt napísaný práve takto:
- presná štruktúra súborov:
css/aimg/priečinky ťa učia poriadok a URL na sigme bude čistá, - žiadne inline štýly: všetko CSS je na jednom mieste — tak sa dá meniť a ladiť,
namealabelpri každom poli: formulár o pár týždňov napojíme na PHP a beznameby údaje neprišli,- komentáre v kóde: kód vieš prečítať a vysvetliť — to budeš robiť pri obhajobe,
- téma projektu: každý má iný web.
Keď výsledok nesedí, celý prompt nepíš znova. Napíš AI, čo presne treba opraviť, napríklad: „Menu nie je vedľa seba, oprav CSS.”
Example 1: selectors, a class and an id
h2 { color: #a3274f; } /* all h2 headings */
h1, h2 { font-family: Georgia, serif; } /* several selectors at once */
nav a { text-decoration: none; } /* only links inside nav */
.zvyraznene { background-color: #f3e1e8; } /* class: a dot */
#kontakt { border-top: 3px solid #a3274f; } /* id: a hash */
<p class="zvyraznene">An important paragraph.</p>
<section id="kontakt"> ... </section>
Example 2: box model, positioning and centring
img {
padding: 4px; /* space between content and border */
border: 2px solid #a3274f; /* the border */
margin: 16px; /* space from surrounding elements */
max-width: 100%; /* the image must not overflow the page */
}
nav { position: fixed; top: 0; left: 0; right: 0; }
body { padding-top: 60px; } /* so the pinned menu does not cover content */
.sekcia { position: relative; } /* the anchor */
.sekcia .stitok { position: absolute; top: 8px; right: 8px; }
main { max-width: 800px; margin: 0 auto; } /* centring the content */
nav a:hover { background-color: #a3274f; color: white; }
input:focus, textarea:focus { outline: 2px solid #a3274f; }
Example 3: a form with all the common fields
<form method="get">
<label for="meno">Name</label>
<input type="text" id="meno" name="meno">
<label for="email">E-mail</label>
<input type="email" id="email" name="email">
<label for="pocet">Count</label>
<input type="number" id="pocet" name="pocet" min="1">
<label for="termin">Date</label>
<input type="date" id="termin" name="termin">
<label for="kategoria">Category</label>
<select id="kategoria" name="kategoria">
<option>first</option>
<option>second</option>
<option>third</option>
</select>
<fieldset>
<legend>Rating</legend>
<label><input type="radio" name="hodnotenie" value="1"> good</label>
<label><input type="radio" name="hodnotenie" value="2"> better</label>
<label><input type="radio" name="hodnotenie" value="3"> best</label>
</fieldset>
<label><input type="checkbox" name="suhlas"> I agree</label>
<label for="sprava">Message</label>
<textarea id="sprava" name="sprava"></textarea>
<button type="submit">Send</button>
</form>
This is what the code from the example looks like in the browser. Try it: click into a field (:focus), hover over the button (:hover), fill it in, press Send and look at the address bar.
After submitting, look at the address bar: form.html?meno=Jana&email=... — exactly these name=value pairs will be read by PHP in a few weeks.
Example 4: an AI prompt for a two-page website
You do not write the site by hand, you brief an AI. A good prompt says precisely what you want to get:
Create a simple two-page website in HTML5 (set lang to the page language,
UTF-8 encoding) with one separate stylesheet. No JavaScript and no frameworks
(no Bootstrap).
File structure, exactly like this:
- index.html
- form.html
- css/style.css (the only place with styles, no inline styles and no <style>)
- img/obrazok.jpg (I will supply the image)
index.html contains:
- a header with an h1 heading and a horizontal menu linking to index.html and
form.html (the same menu on both pages),
- at least two sections with h2 headings and paragraphs, one section has an id,
- the image img/obrazok.jpg with an alt attribute,
- a footer with my name and the year.
form.html contains the same menu and a form with method="get" (no processing
yet — we will connect it to PHP later, so EVERY field must have a name
attribute and its own label):
- a text field (name), an e-mail (type="email"), a number (type="number"),
a date (type="date"),
- a select with at least three options,
- a group of three radio buttons sharing one name,
- two checkboxes,
- a textarea for a message,
- a submit button.
CSS in css/style.css: readable font, content width limited and centred with
max-width and margin auto, coloured headings, menu items side by side, form
styling (fields below each other, label above the field, spacing), the image
must not overflow the page. Use element selectors, at least one class and at
least one id selector.
Add short comments to both the HTML and the CSS for every block, explaining
what it does and why — I learn from them.
Website topic: [FILL IN your project topic]
Why the prompt is written this way:
- the exact file structure: the
css/andimg/folders teach you order and the URL on sigma stays clean, - no inline styles: all CSS is in one place — that is what makes it editable and debuggable,
nameandlabelon every field: we will connect the form to PHP in a few weeks and withoutnameno data would arrive,- comments in the code: you can read and explain the code — that is what you will do at the defence,
- your project topic: everyone gets a different site.
When the result is not right, do not rewrite the whole prompt. Tell the AI what exactly to fix, for example: “The menu items are not side by side, fix the CSS.”
Úlohy na cvičenieLab tasks
Úlohy dostaneš aj ako zadanie cv02 v Classroom 50, sú v súbore README.md v tvojom repozitári.
Link na zadanie cv02: prijať zadanie
(platí pre skupiny B. Stehlíkovej; ostatné skupiny dostanú link od svojho cvičiaceho)
Úloha 3.1: Zadanie a klon. Priprav si Kubeflow podľa návodu 04 Odovzdávanie, časť 1 (Explorer má hore WEB, rozšírenia na mieste). Prijmi zadanie cv02 v Classroom 50 (link je vyššie) a repozitár naklonuj do ~/web/ti/cv02. Ak sa po reštarte Kubeflow stratilo rozšírenie SFTP, vráti ho tento príkaz v termináli:
code-server --install-extension Natizyskunk.sftp --install-extension yandeu.five-server --install-extension bmewburn.vscode-intelephense-client --install-extension mblode.twig-language-2
Potom F1, Developer: Reload Window. Nastavenie pripojenia na sigmu zostáva.
Úloha 3.2: Vygeneruj web. Prompt z Ukážky 4 vlož do AI a na koniec doplň tému svojho projektu. Výsledok ulož do ~/web/ti/cv02 presne v štruktúre z promptu. Do img pretiahni vlastný obrázok a premenuj ho na obrazok.jpg. Stránky si pozri cez Five Server. Prečítaj komentáre v kóde — každý riadok musíš vedieť vysvetliť.
Úloha 3.3: Selektory. Jedným pravidlom v style.css zmeň farbu všetkých nadpisov h2 na oboch stránkach. Vytvor triedu .zvyraznene (napr. farba pozadia alebo rámik) a použi ju aspoň na dvoch rôznych prvkoch na dvoch rôznych stránkach. Prvku, ktorý má id, zmeň štýl cez #id selektor.
Úloha 3.4: Box model. Sekciám nastav vnútorný odstup (padding) a obrázku rámik (border) s vonkajším odstupom (margin). V prehliadači cez F12 si na obrázku pozri diagram box modelu a over, že hodnoty sedia.
Úloha 3.5: Pozicovanie. Pripni menu na vrch okna (position: fixed) a zariaď, aby neprekrývalo obsah (odsadenie obsahu zhora). V jednej sekcii nastav rodiča ako position: relative a umiestni do jej rohu malý prvok (štítok alebo obrázok) cez position: absolute.
Úloha 3.6: Formulár. Pridaj do formulára jedno nové pole typu, ktorý tam ešte nie je — aj s label a name. Odošli formulár a v adresnom riadku nájdi svoje údaje.
Úloha 3.7: Oživenie. Položkám menu pridaj :hover a poliam formulára :focus s viditeľnou zmenou (farba, rámik).
Úloha 3.8: Odovzdanie. Commit a push podľa návodu 04 Odovzdávanie. Klikni pravým na priečinok cv02 a v menu úplne dole zvoľ Upload Folder — na sigma tým doplníš aj pretiahnutý obrázok (pri ukladaní v editore sa nahráva samo, obrázok nie). Over v prehliadači https://sigma.tuke.sk/student/meno.priezvisko/ti/cv02/ — obe stránky aj obrázok.
Hodnotenie: 1 bod. V repozitári cv02 na GitHube aj na sigme je funkčný web: index.html, form.html, css/style.css, img/obrazok.jpg, splnené úlohy, commit a push do konca cvičenia (najneskôr do nasledujúceho cvičenia).
You also get the tasks as the assignment cv02 in Classroom 50, in the README.md file in your repository.
Link to the assignment cv02: accept the assignment
(valid for B. Stehlíková’s groups; other groups get the link from their own teacher)
Task 3.1: Assignment and clone. Get Kubeflow ready following guide 04 Submitting, part 1 (Explorer shows WEB at the top, the extensions are in place). Accept the assignment cv02 in Classroom 50 (the link is above) and clone the repository into ~/web/ti/cv02. If the SFTP extension disappeared from Kubeflow after a restart, this terminal command brings it back:
code-server --install-extension Natizyskunk.sftp --install-extension yandeu.five-server --install-extension bmewburn.vscode-intelephense-client --install-extension mblode.twig-language-2
Then F1, Developer: Reload Window. The sigma connection settings stay.
Task 3.2: Generate the web. Paste the prompt from Example 4 into an AI and add the topic of your project at the end. Save the result into ~/web/ti/cv02 exactly in the structure from the prompt. Drag your own image into img and rename it to obrazok.jpg. View the pages with Five Server. Read the comments in the code — you must be able to explain every line.
Task 3.3: Selectors. With one rule in style.css change the colour of all h2 headings on both pages. Create a class .zvyraznene (e.g. background colour or a border) and use it on at least two different elements on two different pages. Change the style of the element that has an id via a #id selector.
Task 3.4: Box model. Give the sections inner spacing (padding) and the image a border with outer spacing (margin). In the browser, open F12, look at the box model diagram on the image and check that the values match.
Task 3.5: Positioning. Pin the menu to the top of the window (position: fixed) and make sure it does not cover the content (offset the content from the top). In one section set the parent to position: relative and place a small element (a label or an image) in its corner with position: absolute.
Task 3.6: The form. Add one new field of a type that is not in the form yet — with a label and a name. Submit the form and find your data in the address bar.
Task 3.7: Bring it to life. Add :hover to the menu items and :focus to the form fields with a visible change (colour, border).
Task 3.8: Submitting. Commit and push following guide 04 Submitting. Right-click the cv02 folder and at the very bottom of the menu choose Upload Folder — this also gets the dragged image to sigma (saves in the editor upload themselves, the image does not). Check in the browser https://sigma.tuke.sk/student/name.surname/ti/cv02/ — both pages and the image.
Grading: 1 point. The repository cv02 on GitHub and on sigma contains a working website: index.html, form.html, css/style.css, img/obrazok.jpg, the tasks done, commit and push by the end of the lab (by the next lab at the latest).