CS 80: Internet Programming
Instructor: Mark Edmonds
style
attribute
style="font-size: 45pt;"
which
would set the font size to 45pt.<h2>
in the documentinline_styles.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Inline CSS Styling</title>
</head>
<body>
<p style="font-size: 45pt">We can apply styling to one HTML tag</p>
<p>But take notice it doesn't persist in the document</p>
<h2 style="font-family: helvetica, tahoma, sans-serif; font-size: 10pt; color: blue">We can overrride default settings for tags, notice the bold is still applied! </h2>
<p>Note: browser will attempt to use font-family specified in order of the comma-separated list. The last entry in the list should be a generic font style (serif sans-serif, cursive, fantasy, monospace)</p>
</body>
</html>
<head>
tag of the HTML document/* css to modify all p tags */
p {
font-size: x-large;
font-family: arial, sans-serif;
}
<p>
tag in the
document to have an extra large font and use Arial
fontfont-size
)x-large
):
and the key-value pair is terminated
with a ;
embedded_styles.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Embedded CSS Styling</title>
<style type="text/css">
p { font-size: x-large;
font-family: arial, sans-serif; }
</style>
</head>
<body>
<p>This tag is begin styled by the global styling of <p></p>
<p style="font-size: x-small;">But notice that inline styling <strong>always</strong> takes precendence over global styling</p>
<p>This tag is begin styled by the global styling of <p></p>
</body>
</html>
font-family
serif
,
sans-serif
, monospace
,
cursive
, or fantasy
font-family: helvectica, tahoma,
sans-serif;
font-size
NUMBERpt)
or with a
relative sizexx-small``,
x-small, small, smaller, medium, large, ``larger``,
x-large, xx-large
)pt
will result
in different effects based on the user’s
displayfont-size: x-small;
font-style
normal, italic,
oblique
font-style: italic
background-color
background-color:
#668B8B
background_color.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Background Color</title>
<!-- style defaults to "text/css" -->
<style>
body { background-color: #668B8B;
font-style: italic}
p { background-color: black;
color: #0198E1; }
</style>
</head>
<body>
Here's <span style="background-color: coral">some text no</span> in a <p> tag
<div style="background-color: #F0F8FF;">
<p>
Here's some text overriding the background
</p>
And here's some ever more text overriding both settings!
</div>
<p>
Here's some text overriding the background
</p>
</body>
</html>
background-image
url('image')
and uses it as
the backgroundurl()
’s to fetchbackground-image:
url('http://eskipaper.com/images/sand-dune-pictures-1.jpg')
background_image.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Background Image</title>
<!-- style tag defaults to CSS -->
<style>
body { background-image: url(http://www.noupe.com/wp-content/uploads/2009/10/pattern-13.jpg);
}
p { background: #000000 url('https://i.stack.imgur.com/pMAiU.jpg') center center no-repeat;
color: white;
text-align: center; }
</style>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam nec lectus et diam rutrum efficitur id non risus. Pellentesque vitae velit lacinia, sollicitudin turpis in, lacinia enim. Cras in leo a nisl ullamcorper lacinia. Aliquam eu dignissim arcu. Ut ultricies orci quis sollicitudin malesuada. Morbi in mauris sed nisl gravida lobortis. Vestibulum tempor justo consectetur, pharetra lorem eget, posuere quam. Mauris leo libero, vestibulum sed viverra sed, ornare non urna. Suspendisse accumsan a libero a ullamcorper. Sed eu hendrerit est, ut consectetur neque. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer diam justo, sodales a dolor sed, laoreet blandit urna. Vestibulum vestibulum ac arcu quis ornare. Nulla luctus tristique nisl vel tristique. Sed vel massa eget mauris pharetra ultrices.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam nec lectus et diam rutrum efficitur id non risus. Pellentesque vitae velit lacinia, sollicitudin turpis in, lacinia enim. Cras in leo a nisl ullamcorper lacinia. Aliquam eu dignissim arcu. Ut ultricies orci quis sollicitudin malesuada. Morbi in mauris sed nisl gravida lobortis. Vestibulum tempor justo consectetur, pharetra lorem eget, posuere quam. Mauris leo libero, vestibulum sed viverra sed, ornare non urna. Suspendisse accumsan a libero a ullamcorper. Sed eu hendrerit est, ut consectetur neque. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer diam justo, sodales a dolor sed, laoreet blandit urna. Vestibulum vestibulum ac arcu quis ornare. Nulla luctus tristique nisl vel tristique. Sed vel massa eget mauris pharetra ultrices.
</p>
</body>
</html>
background-repeat
repeat, repreat-x,
repeat-y, no-repeat
repeat
will repeat in both the x
and y directionbackground-repeat:
repeat-x
background-position
left, right, center,
for X top,
bottom, center
for Ybackground-position: center
left;
background
background: bg-color bg-image position
bg-repeat
text-align
center
, left
, or
right
to align text<div>
<p>
, but doesn’t denote a
paragraph<h1>
-<h6>
and <p>
are also block
elements (consider their behavior!)<span>
<div>
and
<span>
matter?
<em>some
text</em>
as <span
style="font-style: italic;">some
text</span>
div
and
span
can be a very general way to
apply styling to a specific sectionclass
attributeUsage: all classes start with .
Example:
.myemph { font-style: italic; }
Apply this class using the class
attribute:
<p class="myemph">some_text</p>
We can even apply certain classes only to specific HTML tags
Example:
p.myemph { font-style: italic; }
myemph
class for
use within a <p>
HTML tagSelectors are a bit more general (classes are a type of selector)
id
’s with
#id
Example:
#htmlid { color: blue }
Corresponding HTML:
<section id="htmlid">
#id
)p.myemph
)myemph
)p
)background_color.html
)
myemph
both as a CSS
class and a CSS class for <p>
<p>
(no class specified) inside of
a <div class="myemph">
?conflicting_styles.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Classes and Selectors</title>
<style>
.class1 { font-style: oblique; color: red;}
#blueele { color: blue; }
p.class1 { font-style: normal; color: green;}
</style>
</head>
<body>
<div class="class1">
This text adheres to the global CSS emph class
<p>
But with no class specified in the paragraph tag, the child is still inheriting from the parent
</p>
<p class="class1">
Now we have a p tag with class1 directly applied, so text is normal and green.
</p>
<p class="class1" id="blueele">
But now that we apply the more-specific class, the div is overridden
</p>
</div>
</body>
</html>
background_color.html
using normal nesting)
<link>
HTML tag is used to
import external stylingAttributes required:
rel="stylesheet"
specifies the
relationship between this document and the external
one, in this case, we want to link to a
stylesheettype="text/css"
just like how we
specified with <style
type="text/css">
href="style.css"
is the hyperlink
reference to the external documentExample:
<link rel="stylesheet" type="text/css" href="style.css">
external_styles.html
<!DOCTYPE html>
<!-- Fig. 4.8: external.html -->
<!-- Linking an external style sheet. -->
<html>
<head>
<meta charset="utf-8">
<title>Linking External Style Sheets</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<ul>
<li>Milk</li>
<li>Bread
<ul>
<li>white bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Carrots</li>
<li>Yogurt</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<p><em>Go to the</em>
<a class="nodec" href="http://www.deitel.com">
Grocery store</a>
</p>
</body>
</html>
/* This is a CSS comment. start with /* and end with */ */
#em
amount relative to the normal
size#cm
amount in centimeters#px
amount in pixels#pt
amount in pointsposition
property controls the location of
document elementsposition
property
left: value;
and top: value;
which control the amount of
positioning to apply
Absolute positioning
position: absolute;
sepcifies this
elements position relative to it’s first
positioned (not static) ancestor elementRelative positioning
position: relative;
specifies the
element’s position relative to its normal
positioningFixed positioning
position: fixed;
specifies the element’s
position relative to the browser
windowStatic positioning
position: static;
the default behavior,
positioning is determined by document flow/orderingpositioning.html
<!DOCTYPE html>
<!-- Based on: http://learnlayout.com/position-example.html -->
<html>
<head>
<meta charset="utf-8">
<title>CSS positioning</title>
<link href="https://dl.dropbox.com/s/p6zt773weewbvzs/styles.css" rel="stylesheet" type="text/css" />
<style>
.container {
position: relative;
}
nav {
position: absolute;
width: 200px;
}
section {
/* position is static by default */
margin-left: 200px;
padding: 5px;
}
footer {
position: fixed;
padding: 20px;
bottom: 0;
left: 0;
height: 70px;
background-color: white;
width: 100%;
}
body {
margin-bottom: 120px;
}
.div1 {
height: 100px;
width: 100px;
overflow: hidden;
}
.div2 {
height: 415px;
width: 715px;
overflow: scroll;
}
/* increase the height and width (koala.jpg is 710 x 408) to see the scroll disappear */
.div3 {
height: 415px;
width: 715px;
overflow: auto;
}
</style>
</head>
<body>
<!-- positioned relative to its normal setting; has no effect size we didn't move it (play around with it!) -->
<h3>CSS Positioning and Element Dimensions</h3>
<div class="container">
<!-- our nav CSS applies; width is 200px -->
<nav>
<ul>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
</ul>
</nav>
<!-- our section CSS applies; margin is 200px (will be on the right of nav)-->
<section>
The margin-left style for sections makes sure there is room for the nav. Otherwise the absolute and static elements would overlap
</section>
<section>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
</section>
<section>
Notice what happens when you resize your browser. It works nicely!
</section>
<section>
<div class="div1">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
<br>
<div class="div2">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
<div class="div3">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
</section>
<footer>
If you use a fixed header or footer, make sure there is room for it! I put a margin-bottom on the body.
</footer>
</div>
</body>
</html>
width: value;
to specify widthheight: value;
to specify
heightoverflow
property defines what to do if
the content of an element goes over the specified
dimensions
overflow: visible;
overflow will
still be rendered (overflowing into over
elements)overflow: hidden;
overflow will
be clipped (overflowing content will be
invisible)overflow: scroll;
scroll bars
are used to view the rest of the contentoverflow: auto;
if the overflow
is clipped, a scroll bar will appearpositioning.html
<!DOCTYPE html>
<!-- Based on: http://learnlayout.com/position-example.html -->
<html>
<head>
<meta charset="utf-8">
<title>CSS positioning</title>
<link href="https://dl.dropbox.com/s/p6zt773weewbvzs/styles.css" rel="stylesheet" type="text/css" />
<style>
.container {
position: relative;
}
nav {
position: absolute;
width: 200px;
}
section {
/* position is static by default */
margin-left: 200px;
padding: 5px;
}
footer {
position: fixed;
padding: 20px;
bottom: 0;
left: 0;
height: 70px;
background-color: white;
width: 100%;
}
body {
margin-bottom: 120px;
}
.div1 {
height: 100px;
width: 100px;
overflow: hidden;
}
.div2 {
height: 415px;
width: 715px;
overflow: scroll;
}
/* increase the height and width (koala.jpg is 710 x 408) to see the scroll disappear */
.div3 {
height: 415px;
width: 715px;
overflow: auto;
}
</style>
</head>
<body>
<!-- positioned relative to its normal setting; has no effect size we didn't move it (play around with it!) -->
<h3>CSS Positioning and Element Dimensions</h3>
<div class="container">
<!-- our nav CSS applies; width is 200px -->
<nav>
<ul>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
<li><a href="http://google.com">Google</a></li>
</ul>
</nav>
<!-- our section CSS applies; margin is 200px (will be on the right of nav)-->
<section>
The margin-left style for sections makes sure there is room for the nav. Otherwise the absolute and static elements would overlap
</section>
<section>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
</section>
<section>
Notice what happens when you resize your browser. It works nicely!
</section>
<section>
<div class="div1">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
<br>
<div class="div2">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
<div class="div3">
<img src="koala.jpg" alt="A koala eating a leaf">
</div>
</section>
<footer>
If you use a fixed header or footer, make sure there is room for it! I put a margin-bottom on the body.
</footer>
</div>
</body>
</html>
p, div, h1-h6, section
), but
let’s formalize itControlling borders
border-width
propertyborder-style
propertyborder-color
propertyborder: width style
color;
box_model.html
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 25px solid green;
padding: 20px;
margin: 25px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding, margins, and the actual content.</p>
<div>This text is the actual content of the box. We have added a 25px padding, 25px margin and a 25px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</body>
</html>
p, div, section, h1-h6
) to fill the width of
the screen.float: none;
default, element is not
floatedfloat: right;
element is floated to
the right. Other content can flow on the left
sidefloat: left;
element is floated to
the left. Other content can flow on the right
sideclear
allows you to specify that an
element should not flow with a float (e.g. will
not flow with an element on right/left; it will start
below the floated element)clear: left;
do not allow floating
to the leftclear: right;
do not allow floating
to the rightclear: both;
do not allow floating
to the light or rightfloating.html
<!DOCTYPE html>
<!-- Example from: http://www.w3schools.com/css/tryit.asp?filename=trycss_layout_clear -->
<html>
<head>
<style>
.div1 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
border: 3px solid #73AD21;
}
.div2 {
width: 500px;
margin-left: 150px;
border: 1px solid red;
}
.div3 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
border: 3px solid #73AD21;
}
.div4 {
border: 1px solid red;
clear: left;
}
</style>
</head>
<body>
<h2>Without clear</h2>
<div class="div1">div1</div>
<div class="div2">div2 - Notice that the div2 element is after div1, in the HTML code. However, since div1 is floated to the left, this happens: the text in div2 is floated around div1, and div2 surrounds the whole thing.</div>
<h2>Using clear</h2>
<div class="div3">div3</div>
<div class="div4">div4 - Using clear moves div4 down below the floated div3. The value "left" clears elements floated to the left. You can also clear "right" and "both".</div>
</body>
</html>
margin
property specifies the required
space between the border and the next element (see box
model above)margin
attribute is shorthand for
margin-top
, margin-right
,
margin-bottom
, margin-left
margin: 50px 40px 100px 25px
(what does this mean?)margin-top: 100px
padding
property specifies the amount of
space between the content and the border (see box model
above)padding
attribute is shorthand
for padding-top
,
padding``-right
,
padding``-bottom
,
padding-left
padding: 50px 40px 100px
25px
(what does this mean?)padding-top: 100px
media_types_queries.html
or
positioning.html
Media Types
screen
, which
stands for computer screenhandheld
(cell
phone/mobile), speech
(read out old),
print
(printers)./* media query example */
@media all {
body { background-color: black; }
}
@media print{
body { background-color: white; }
}
Media Queries
height
- height of display area (the
browser)width
- width of display area (the
browser)resolution
- resolution of the
output deviceorientation
- landscape or
portraitMedia Queries
CSS Syntax
/* media query syntax */
@media not|only mediatype and (media feature) {
CSS-Code;
}
not
negates the media query
only
applies the style only if the
query matches
Note: you may omit the mediatype
and
only specify a (media feature)
if you
wish
media_types_queries.html
<!DOCTYPE html>
<!-- Example from: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_mediaquery -->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
body {
font-family: "Lucida Sans", Verdana, sans-serif;
}
.main img {
width: 100%;
}
h1{
font-size: 1.625em;
}
h2{
font-size: 1.375em;
}
.header {
padding: 1%;
background-color: #f1f1f1;
border: 1px solid #e9e9e9;
}
.menuitem {
margin: 4%;
margin-left: 0;
margin-top: 0;
padding: 4%;
border-bottom: 1px solid #e9e9e9;
cursor: pointer;
}
.main {
padding: 2%;
}
.right {
padding: 4%;
background-color: #CDF0F6;
}
.footer {
padding: 1%;
text-align: center;
background-color: #f1f1f1;
border: 1px solid #e9e9e9;
font-size: 0.625em;
}
.gridcontainer {
width: 100%;
}
.gridwrapper {
overflow: hidden;
}
.gridbox {
margin-bottom: 2%;
margin-right: 2%;
float: left;
}
.gridheader {
width: 100%;
}
.gridmenu {
width: 23%;
}
.gridmain {
width: 50%;
}
.gridright {
width: 23%;
margin-right: 0;
}
.gridfooter {
width: 100%;
margin-bottom: 0;
}
@media only screen and (max-width: 500px) {
.gridmenu {
width: 100%;
}
.menuitem {
margin: 1%;
padding: 1%;
}
.gridmain {
width: 100%;
}
.main {
padding: 1%;
}
.gridright {
width: 100%;
}
.right {
padding: 1%;
}
.gridbox {
margin-right: 0;
float: left;
}
}
</style>
</head>
<body>
<div class="gridcontainer">
<div class="gridwrapper">
<div class="gridbox gridheader">
<div class="header">
<h1>The Pulpit Rock</h1>
</div>
</div>
<div class="gridbox gridmenu">
<div class="menuitem">The Drive</div>
<div class="menuitem">The Walk</div>
<div class="menuitem">The Return</div>
<div class="menuitem">The End</div>
</div>
<div class="gridbox gridmain">
<div class="main">
<h1>The Walk</h1>
<p>The walk to the Pulpit Rock will take you approximately two hours, give or take an hour depending on the weather conditions and your physical shape.</p>
<img src="pulpitrock.jpg" alt="Pulpit rock" width="" height="">
</div>
</div>
<div class="gridbox gridright">
<div class="right">
<h2>What?</h2>
<p>The Pulpit Rock is a part of a mountain that looks like a pulpit.</p>
<h2>Where?</h2>
<p>The Pulpit Rock is in Norway</p>
<h2>Price?</h2>
<p>The walk is free!</p>
</div>
</div>
<div class="gridbox gridfooter">
<div class="footer">
<p>This web page is a part of a demonstration of fluid web design made by www.w3schools.com. Resize the browser window to see the content response to the resizing.</p>
</div>
</div>
</div>
</div>
</body>
</html>
display
property:
block
(like p
,
section
etc) or inline
(like span
, em
etc)display: none;
element is not
displayeddisplay: block;
displays a block
level elementdisplay: inline;
displays as an
inline elementdisplay: inline-block;
display is
like an inline element, but can have a specified
height and widthdrop_down_menu.html
<!DOCTYPE html>
<html>
<head>
<link href="https://dl.dropbox.com/s/p6zt773weewbvzs/styles.css" rel="stylesheet" type="text/css" />
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>
<p>Note that because <p>
<div class="dropdown">
<button class="dropbtn">Navigation</button>
<div class="dropdown-content">
<a href="http://google.com">Google</a>
<a href="http://facebook.com">Facebook</a>
<a href="http://cnn.com">CNN</a>
</div>
</div>
</body>
</html>