250 likes | 267 Views
Learn what CSS is and its benefits, explore three ways to insert CSS into your HTML pages, and understand CSS rule structure and how to style HTML elements using IDs and classes. Discover useful elements like tables and layers, and learn about different position properties and display options.
E N D
SEEM4570Tutorial 03: CSS Cao Yuewen ywcao@se.cuhk.edu.hk 2017.10.12
What is CSS ? • CSS stands for Cascading Style Sheets • CSS describes how HTML elements are to be displayed
Why CSS ? • Separates content from form • Easy maintenance of multiple pages • Faster page loading • Easy to learn
Three Ways to Insert CSS • Inline: the “style” attribute • Internal: the <style> markup tag • External: the .css stylesheet file <p style=“font-color:red;font-size:10px;”>Content</p> <html><head><style> p {background-color: Red;font-family: serif;font-color: White; } </style></head><body> <p>Content</p></body></html> <link rel="stylesheet" type="text/css" href=“mystylesheet.css" />
CSS Rule Structure CSS rule-set consists of a selector and a declaration block: • The selector points to the HTML element you want to style • The declaration block contains one or more declarations • Each declaration includes a CSS property name and a value • Each declaration separates with a semi-colon
ID in CSS • Specify a style for a unique element by referring to the “id” in the HTML element. • The style is defined by “#” in CSS <head> <style> #para1{ color:red; } </style> </head> <body> <p id="para1">A style paragraph</p> <p id="para1">Another style paragraph</p> </body>
Class in CSS • Specify a style for a unique of elements by referring to the “class” in the HTML element. • The style is defined by “.” in CSS <head> <style> .para1{ color:red; } </style> </head> <body> <p class="para1">A style paragraph</p> <p class="para1">Another style paragraph</p> </body>
Class in CSS • You can also specify the specific HTML elements should be affected by a class. <head> <style> div.para1{ color:red; } </style> </head> <body> <p class="para1">No effect</p> <div class="para1">Affected</p> </body>
Some Useful Elements - Table • Table <table> • <th> head <tr>table row <td>table data • Table border • Table width and height • Table color • Alignment • Table Padding • Horizontal Dividers • Hoverable Table • Apply style to a specific table …
Some Useful Elements - Layer • We use <div> to represent it. • You need to understand the layout first:
Some Useful Elements - Position • Position • Position properties allow you to position an element. It can also specify what should happen if an element’s content is too big or which element overlaps with one another. • There are four different position value: • Static(default) • Relative • Fixed • Absolute
Some Useful Elements - Position • Position: relative • A relative positioned element is positioned relative to its normal position. • Example: • div.relative{ position: relative; left: 30px; border: 3px; }
Some Useful Elements - Position • Position: fixed • A fixed positioned element is positioned relative to the browser window. • Example: • div.fixed{ position: fixed; bottom: 0; right: 0; width: 300px; border:3px; }
Some Useful Elements - Position • Position: absolute • An absolute positioned element is positioned relative to the nearest positioned ancestor. If no such ancestor is found, it uses the document body, and move along with page scrolling. • Example: div.relative { position: relative; width: 400px; height: 200px; border: 3px; } div.absolute { position: absolute; bottom: 0; right: 0; width: 200px; height: 100px; border: 3px; } <div class="relative">This div element has position: relative; <div class="absolute">This div element has position: absolute;</div> </div>
Some Useful Elements - Position • Position: overlapping • Elements can overlap other elements if needed. • The z-index property specifies the stack order of an element. • Example: img { position: absolute; left: 0px; top: 0px;z-index: -1;}
Some Useful Elements - Display • Display property specifies how an element is displayed. • Commonly used values: div { display: inline; /* Default of all elements*/ display: inline-block; /* Characteristics of block, but sits on a line */ display: block; /* Display block-level element */ display: run-in; /* Not particularly well supported */ display: none; /* Hide */ }
Some Useful Elements - Display • Inline • Don’t start on a new line and only take up as much width as necessary. It sits right inline with the natural flow of the text. • e.g. <span>、<em> 、<b>、<a> • Inline Block • Similar to inline element but is able to set a width and height • Block • Don’t sit inline but break past them. Take up as much horizontal space as they can. • e.g. <div>、<section>、<ul>、<h1> • Run-in • Make a header element to sit inline with the text below it. • Don’t work in Firefox
Some Useful Elements - Display • Table Values • Force non-table elements to behave like table-elements • Browser Support: • IE8 supports the "table-" values only if a !DOCTYPE is specified. div { display: table; /* like a <table> element*/ display: table-cell; /* like a <td> element*/ display: table-column; /* like a <col> element*/ display: table-column-group; /* like a <colgroup> element*/ display: table-header-group; /* like a <thead> element */ display: table-row-group; /* like a <tbody> element*/ display: table-footer-group; /* like a <tfoot> element*/ display: table-row; /*like a <tr> element */ display: table-caption; /* like a <caption> element*/ }
Some Useful Elements - Display Put together a multi-column layout
Resources • http://www.w3schools.com/css/ • http://www.html.net/tutorials/css/ • http://www.barelyfitz.com/screencast/html-training/css/positioning/