used to style html documents, it controls the appearance of web pages
-
inline css:
<p style="color: red;">Text in red</p> -
internal css: used inside
<style>tag -
external css: separate
.cssfile linked to html:<link rel="stylesheet" href="style.css"> -
comment with
/* */ -
viewport: visible part of a web page in the browser window, it's what the users sees without scrolling
syntax of a css property:
selector {
property1: value1;
property2: value2;
}
- declaration:
property1: value1; - declaration block:
{ ... }
example:
h1.highlight {
color: white;
text-align: center;
}
p {
background-color: lightblue;
}
div#profile {
font-family: verdana;
font-size: 20px;
}- element selector
p { color: red; }pstyles all<p>tags
- class selector
.myClass { color: red; }
- id selector
#myID { color: red; }
- attribute selector
a[target="_blank"] { color: red; }- will apply the style to all
<a>elements withtarget="_blank"
- will apply the style to all
- global selector (
*)- selects every element in the web page
- select multiple elements:
div, p { ... }
Important
if multiple rules apply to an element, the most specific rule wins inline > id > class > tag
color: color of text- can be specified with
rgb(255, 99, 71)or hex codes#ff6347background: rgba (0, 128, 0, 0.3)
border-colorbackground-color
- can be specified with
background-image:opacity:font-size: size of textfont-family: sets font typefont-weight: normal, boldfont-style: normal, italicmargin:margin: 25px 50px 75px 100px;(sets, in order: top margin, right margin, bottom margin and left margin)margin-top,margin-right,margin-bottom,margin-left
padding:border:border-style: can bedotted,dashed,solidborder-radius: round the corner of an element's border box
display: can beblock,inline,flex,gridinline-block: no newline, allows settign width and heightflex:
position:relative: positions the element relative to its normal positionabsolute: positions the element relative to the nearest positioned ancestor- ancestor: any element that wraps or contains another
fixed: positions the element relative to the viewport, so it stays in place when scrolling
width:height:float: position element to the left or right of its containerz-index: element with higher value will be displayed on top when overlapping
<div class="container">
<p>Sample text</p>
</div>.container {
display: flex;
justify-content: center; /* horizontal alignment */
align-items: center; /* vertical alignment */
}flex-direction: defines main-axis (default is horizontal)justify-content: positions content in the main axisalign-items: items are aligned in the cross-axis (default is vertical)
.parent {
display: grid;
place-content: center;
place-items: center;
}Tip
Use px when you want precise control (e.g. borders, icons). Use rem and em for text, so it scales nicely across devices. Use %, vw, and vh for layout that adapts to screen size.
| Unit | Meaning | Example |
|---|---|---|
px |
Pixels | font-size: 16px; |
pt |
Points (1/72 inch) | font-size: 12pt; |
cm |
Centimeters | width: 10cm; |
mm |
Millimeters | width: 50mm; |
in |
Inches | width: 1in; |
| Unit | Meaning | Example |
|---|---|---|
% |
Relative to parent element | width: 50%; |
em |
Relative to the font-size of the parent element | font-size: 2em; |
rem |
Relative to the root element font-size (<html>) |
font-size: 1.5rem; |
vw |
1% of viewport width | width: 50vw; |
vh |
1% of viewport height | height: 100vh; |
vmin |
1% of smaller of vw or vh |
font-size: 5vmin; |
vmax |
1% of larger of vw or vh |
font-size: 5vmax; |
allow css to apply rules based on screen size, resolution or device type
@media (max-width: 600px) {
body { font-size: 14px; }
}describes how elements are rendered
- each element is made up of four layers, listed below from inner to outer:
- content in the center, inner layer
- padding: surrounds the content
- border
- margin: outer layer
keywords added to selectors that specify a special state of the selected elements
:hover: when the user hovers over an element:active: while the element is being clicked:focus: when an element (like an input) gains focus
button:hover {
background-color: blue;
}style specific parts of elements without needing additional markup
::before: inserts content before an element::after: inserts content after an element::first-letter: styles the first letter of a block::first-line: styles the first line of text
p::first-letter {
font-size: 200%;
color: red;
}defines priority of application of hierarchy in case of conflict
HIGHEST PRIORITY
- inline style:
<h1 style="color: pink;"> - id selectors:
#navbar - classes and pseudo-classes:
.container - attributes
- elements and pseudo-elements:
h1
LOWEST PRIORITY
display: grid;: turn a container into a grid