Cascading Style Sheet (CSS)

Junghoo Cho

cho@cs.ucla.edu

CSS (Cascading Style Sheet)

CSS Example

p {                 /* p element */
    background-color: grey;
}
p.notes {           /* p element of notes class */
    background-color: yellow;
}
p .notes {          /* notes-class element that is a descendant of p */
    background-color: blue;
}
#text3372 {         /* id text3372 */
    background-color: green;
}
img[src$=".svg"] {  /* attr src ends with .svg */
    width: 20em; 
}

Adding CSS Rules to Page

Inheritance

Cascading Rule

CSS Custom Properties

Page Layout via CSS

CSS Box Model

overflow Property

Positioning Element

Overlapping Elements

Block vs Inline Elements

CSS Layout Example

Q: How can we create the following layout?

CSS Layout

CSS Grid

CSS Grid Example

#container {
    display: grid;
    grid-template-rows: 2em 2em 2em; 
        /* height of each row */
    grid-template-columns: 100pt 100pt; 
        /* width of each column */
}
<div id="container">
    <div>Cell 1</div>
    <div>Cell 2</div>
    <div>Cell 3</div>
    <div>Cell 4</div>
    <div>Cell 5</div>
    <div>Cell 6</div>
</div>


Cell 1
Cell 2
Cell 3
Cell 4
Cell 5
Cell 6

CSS Grid Example: Spanning

#container {
    display: grid;
    grid-template-rows: 2em 2em 2em; 
    grid-template-columns: 100pt 100pt 100pt; 
}
#c1 {
    grid-column-start: 1; 
    grid-column-end: 3; /* spans columns 1-2 */
    grid-row-start: span 2; /* spans 2 rows */
}
<div id="container">
    <div id="c1">Cell 1</div><div>Cell 2</div>
    <div>Cell 3</div><div>Cell 4</div>
    <div>Cell 5</div><div>Cell 6</div>
</div>


Cell 1
Cell 2
Cell 3
Cell 4
Cell 5
Cell 6

Fixed vs Fluid Layout

Responsive Web Design (RWD)

Viewport (1)

Default iPhone

Viewport (2)

Without Viewport Before

With Viewport After

Media Queries

Media Query Conditions

Media Query Example

CSS Flexbox

Flex Container and Flex Item

Flexbox: Changing Size

Flexbox: Rearranging Items

Flexbox Example

Animation

JavaScript Animation

Animation Demo

Animation: Another Example

<body>
    <div id="box" style="border: solid 5px black;"></div>
</body>
<script type="text/javascript">
let box  = document.getElementById("box");
let size = 0;
let timer = setInterval(callback, 100);

function callback() {
    box.style.width = String(size) + "px";
    box.style.height = String(size) + "px";
    size = (size + 10) % 200;
}
</script>

Relevant API

CSS Animation

CSS transition property

CSS transition Example

CSS @keyframes Rule

Relevant CSS Properties

CSS Preprocessor

CSS Frameworks

What We Learned

References