Cascading Style Sheet

Similar documents
Understanding this structure is pretty straightforward, but nonetheless crucial to working with HTML, CSS, and JavaScript.

As we design and build out our HTML pages, there are some basics that we may follow for each page, site, and application.

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

To Ponder. Which one wins? .draft div.warning li { }.draft div #main li {!important; } div #main ul li { }.draft.warning ul li { }

CSS Cont'd: Cascading Style Sheets

CSS.

Chapter 3 Style Sheets: CSS

Index. CSS directive, # (octothorpe), intrapage links, 26

HTML and CSS COURSE SYLLABUS

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

Introduction to WEB PROGRAMMING

2. Write style rules for how you d like certain elements to look.

COPYRIGHTED MATERIAL. Contents. Chapter 1: Creating Structured Documents 1

HTML & CSS. SWE 432, Fall 2017 Design and Implementation of Software for the Web

CSS Selectors. element selectors. .class selectors. #id selectors

Zen Garden. CSS Zen Garden

BIM222 Internet Programming

HTML Summary. All of the following are containers. Structure. Italics Bold. Line Break. Horizontal Rule. Non-break (hard) space.

HTML: Parsing Library

Deccansoft Software Services

HTML: Parsing Library

<style type="text/css"> <!-- body {font-family: Verdana, Arial, sans-serif} ***set font family for entire Web page***

The [HTML] Element p. 61 The [HEAD] Element p. 62 The [TITLE] Element p. 63 The [BODY] Element p. 66 HTML Elements p. 66 Core Attributes p.

CSS

Data Visualization (CIS/DSC 468)

COMP519 Web Programming Lecture 6: Cascading Style Sheets: Part 2 Handouts

HTML TAG SUMMARY HTML REFERENCE 18 TAG/ATTRIBUTE DESCRIPTION PAGE REFERENCES TAG/ATTRIBUTE DESCRIPTION PAGE REFERENCES MOST TAGS

Styles, Style Sheets, the Box Model and Liquid Layout

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

1/6/ :28 AM Approved New Course (First Version) CS 50A Course Outline as of Fall 2014

COPYRIGHTED MATERIAL. Contents. Introduction. Chapter 1: Structuring Documents for the Web 1

Downloads: Google Chrome Browser (Free) - Adobe Brackets (Free) -

CSS. Shan-Hung Wu CS, NTHU

APPLIED COMPUTING 1P01 Fluency with Technology

HTML and CSS a further introduction

Symbols INDEX. !important rule, rule, , 146, , rule,

"utf-8";

Chapter 2:- Introduction to XHTML. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Web Designing HTML5 NOTES

CSS: Cascading Style Sheets

COMS 359: Interactive Media

Wireframe :: tistory wireframe tistory.

Web Site Design and Development Lecture 5

CMPT 165 Advanced XHTML & CSS Part 3

Static Webpage Development

CSS Cascading Style Sheets

WEBSITE PROJECT 2 PURPOSE: INSTRUCTIONS: REQUIREMENTS:

HTML + CSS. ScottyLabs WDW. Overview HTML Tags CSS Properties Resources

CS193X: Web Programming Fundamentals

CSS: formatting webpages

CSS cont. October 5, Unit 4

Programmazione Web a.a. 2017/2018 HTML5

Data Visualization (DSC 530/CIS )

CSS BASICS. selector { property: value; }

CSS - Cascading Style Sheets

Introduction to using HTML to design webpages

Cascading Style Sheets (CSS)

8a. Cascading Style Sheet

WML2.0 TUTORIAL. The XHTML Basic defined by the W3C is a proper subset of XHTML, which is a reformulation of HTML in XML.

CSS for Page Layout Robert K. Moniot 1

Web development using PHP & MySQL with HTML5, CSS, JavaScript

QUICK REFERENCE GUIDE

Web Engineering CSS. By Assistant Prof Malik M Ali

Review of HTML. Chapter Pearson. Fundamentals of Web Development. Randy Connolly and Ricardo Hoar

The Benefits of CSS. Less work: Change look of the whole site with one edit

First Name Last Name CS-081 March 23, 2010 Midterm Exam

HTML CS 4640 Programming Languages for Web Applications

FEWD START SCREENCAST!!!!

Certified HTML Designer VS-1027

INTRODUCTION TO HTML5! HTML5 Page Structure!

Creating and Building Websites

Index. alt, 38, 57 class, 86, 88, 101, 107 href, 24, 51, 57 id, 86 88, 98 overview, 37. src, 37, 57. backend, WordPress, 146, 148

Session 4. Style Sheets (CSS) Reading & References. A reference containing tables of CSS properties

CIS 228 (Fall, 2012) Exam 2, 11/20/12

3.1 Introduction. 3.2 Levels of Style Sheets. - The CSS1 specification was developed in There are three levels of style sheets

- The CSS1 specification was developed in CSS2 was released in CSS2.1 reflects browser implementations

Block & Inline Elements

Website Development with HTML5, CSS and Bootstrap

CSS 1: Introduction. Chapter 3

Certified HTML5 Developer VS-1029

Interview Question & Answers

Text and Layout. Digital Multimedia, 2nd edition Nigel Chapman & Jenny Chapman Chapter 11. This presentation 2004, MacAvon Media Productions

ICT IGCSE Practical Revision Presentation Web Authoring

WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5

Co. Louth VEC & Co. Monaghan VEC. Programme Module for. Web Authoring. leading to. Level 5 FETAC. Web Authoring 5N1910

Web Design and Development Tutorial 03

CSS: The Basics CISC 282 September 20, 2014

.hedgehog { background-image: url(backyard.jpg); color: #ffff99; height: 6in; width: 12in; } .tube {

CS193X: Web Programming Fundamentals

Lecture : 3. Practical : 2. Course Credit. Tutorial : 0. Total : 5. Course Learning Outcomes

Lab Introduction to Cascading Style Sheets

Assignments (4) Assessment as per Schedule (2)

HTML Hyper Text Markup Language

NEW WEBMASTER HTML & CSS FOR BEGINNERS COURSE SYNOPSIS

Cascading Style Sheets Level 2

Basics of Web Design, 3 rd Edition Instructor Materials Chapter 2 Test Bank

A HTML document has two sections 1) HEAD section and 2) BODY section A HTML file is saved with.html or.htm extension

CSS. Selectors & Measurments. Copyright DevelopIntelligence LLC

Welcome Please sit on alternating rows. powered by lucid & no.dots.nl/student

CSS: Beyond the Code. Karen Perone Rodman Public Library.

Transcription:

Extra notes - Markup Languages Dr Nick Hayward CSS - Basics A brief introduction to the basics of CSS. Contents Intro CSS syntax rulesets comments display Display and elements inline block-level CSS selectors classes pseudoclasses complex selectors Cascading rules Inheritance Fonts relative and em relative and rem custom web fonts References Intro A Cascading Style Sheet, or CSS, allows us to define stylistic characteristics for our HTML. In effect, it helps us define how our HTML is displayed and rendered. The colours used, font sizes, borders, padding, margins, links, and so on. CSS syntax CSS follows a defined syntax pattern, which may be defined as follows selector e.g. body or p declaration property and value pairing

For example, color: black; font-family: "Times New Roman", Georgia, Serif; So, in this example body is the selector, color is the property, and black is the value. Image - CSS Syntax rulesets In essence, a CSS file is a group of rules for styling our HTML documents. These rules form rulesets, which can be applied to elements within the DOM of our HTML documents. Rulesets consist of the following, a selector eg p an opening brace { a set of rules color: blue a closing brace So, each rule can be considered as containing a specific property, followed by a colon, plus our required value (or list of values separated by spaces), followed by a semi-colon. So, as seen earlier, our ruleset might look as follows, width: 900px; color: #444; font-family: "Times New Roman", Georgia, Serif;

Our example includes a selector for the body element, and our required rules. This set of rules will be applied to all content of the body element, including any elements contained within the body element. This ruleset, therefore, contains three rules, each respectively specifying a width property, a value for the colour property, and the available fonts for our text content. As you've probably noticed, colour can be set to a named value (e.g. blue ) or HEX value (e.g. #444 ). There's a fun and useful colour picker at the following URL, HTML Colour Picker comments We can also add comments to help describe the selector and its properties, /* add styling to paragraphs */ color: blue; font-size: 14px; Comments can be added before the selector or within the braces. display For the majority of cases, we can display HTML elements in one of two ways. We can use inline, which applies to elements such as <a> or <span>, to display content that will appear contiguously on the same line as the surrounding content. For example, <div class="content"> <p> <a href="...">philae</a> is a <span>ptolemaic</span> era temple in Egypt. </p> </div> However, it's more common to display elements as block-level instead of inline elements. Basically, this means that the element's content will be rendered on a new line outside the normal flow of our content. A few sample block elements include article, div, figure, main, nav, p, section... It's interesting to note that block-level is not technically defined for new elements in HTML5. Display and elements We may consider the display of elements using CSS relative to the following: inline Current inline elements include: b big i small tt abbr acronym cite code dfn em kbd strong samp var a bdo br img map object q script span sub sup button input label select textarea Source - MDN - Inline Elements

n.b. not all inline elements supported in HTML5 block-level Current block-level elements include: address article aside blockquote canvas dd div dl fieldset figure figcaption footer form h1 h2 h3 h4 h5 h6 header hgroup hr main nav noscript ol output p pre section table tfoot ul video Source - MDN - Block-level Elements n.b. block-level is not technically defined for new elements in HTML5 CSS selectors Effective use and manipulation of selectors is a crucial aspect of working with CSS and JavaScript as well. We've already encountered basic selectors, such as color: #444; Don't forget, p is the selector, color is the property, and #444 is the value The above ruleset is adding basic styling to our paragraphs, setting the text colour to HEX value 444, a dark grey. This is simple, and easy to apply, but it applies the same properties and values to all paragraphs. If we want to be more specific, for example, we need to use classes, psuedoclasses, and other selectors. classes We can add a class attribute to an element, such as a <p> or a <span>, thereby helping us differentiate elements. We can also add a class to any DOM element. For example, we could now add different classes to multiple <p> elements, <p class="p1">paragraph one...</p> <p class="p2">paragraph two...</p> By adding these classes, we can now select our paragraphs by class name within the DOM. We can then apply a ruleset for each class. We can be specific, and style this class for a specific element p.p1 { color: #444; Or, we could simply style the class itself.p1 { color: #444; The above example will style all elements with the class p1, and not just <p> elements with that class. Classes are very useful for styling groups of elements together, or abstracting styling to one ruleset and applying to multiple different elements.

pseudoclasses As with classes in the previous example, we can add a class to links or anchors, thereby styling all links with the same ruleset. However, we might also want to add specific styles for different link states. For example, styling links with a different colour depending upon whether a link has already been visited or not. CSS allows us to add an additional ruleset for such an example, a { color: blue; a:visited { color: red; In the above example, visited is a CSS pseudoclass applied to the <a> element. The main difference is that a browser implicitly adds this pseudoclass for us, we just need to provide the styling in the CSS. We don't need to add anything to the HTML for the link, for example. Likewise, we can use another pseudoclass to help us style our links when a user hovers their cursor over an <a> element. So, we can now style our links for hover a:hover { color: black; text-decoration: underline; Again, the browser will implicitly set this pseudoclass in the HTML for us. complex selectors As we build our websites and applications, our DOM will often become more complicated and detailed. The depth and complexity will require more complicated selectors as well. If we consider lists, and their varying depths <ul> <li>unordered first</li> <li>unordered second</li> <li>unordered third</li> </ul> <ol> <li>ordered first</li> <li>ordered second</li> <li>ordered third</li> </ol> So, we now have two lists, one unordered and the other ordered. We can style each list, and the list items, as we've already seen. We simply create our rulesets for the given selectors.

For example, ul { border: 1px solid #ddd; ol { border: 1px solid #ccc; We could also add a single ruleset for the list items, <li>, but we'd be applying the same style properties to both types of lists. If we wanted to be more specific, we could apply a ruleset to each list item for the different lists. For example, ul li { color: blue; ol li { color: green; Now, it might also be useful to set the background for specific list items in each list, therefore making it easier to visualise the first list item, for example li:first-child { background: #eee; We can take this another step, and set a pseudoclass of nth-child thereby allowing us to specify a style for the second, fourth &c. child in the list. li:nth-child(2) { background: #ddd; For a bit of fun, we could take this even further and style odd and even list items to create a useful alternating pattern. This is particularly useful, as you might imagine, for styling tables. For example, li:nth-child(odd) { background: #bbb; li:nth-child(even) { background: #ddd; For a table, we could simply update the selectors for each pseudoclass to reflect a <tr>. We can take this yet another step, and select only certain list items, or rows in a table &c., such as selecting every fourth list item, starting at the first one. For example, our CSS would be as follows li:nth-child(4n+1) { background: green;

In fact, for the even and odd children we are simply leveraging the above in a pre-configured package. Other examples include and so on. last-child nth-last-child() Cascading rules As the name suggests, CSS, or cascading style sheet, employs a set of cascading rules, which are applied by each browser if and when a ruleset conflict arises. For example, consider the simple issue of specificity. If we create a ruleset for a <p> element, and then another for a <p> element with a class p1, the more specific rule, the class, will take precedence. color: blue; p.p1 { color: red; However, there is also the issue of possible duplication in rulesets. If we had two rulesets for the same element, for example h3 { color: black; h3 { color: blue; the cascading rules of CSS dictate that the later ruleset in the CSS list will be the one applied. For this example, we would end up with <h3> elements styled in blue. So, hopefully, you can already see how simple styling and rulesets can quickly become compounded and complicated. Different styles, specified in different places, can interact and affect each other in complex ways. This can become a powerful feature of CSS, but it can also create many issues with logic, maintenance, and design. Therefore, we can often consider three primary sources of style information, relative to our documents, that form this cascade. They include, default styles applied by the browser for a given markup language e.g. colours for links, size of headings, and so on... styles specific to the current user of the document often affected by browser settings, device, mode... styles linked to the document by the designer as we've seen, such styles can be linked in three ways including an external file, embedded in a definition at the beginning of the document, and as inline styles per element.

The basic cascading nature of these options means that the following applies, browser's style will be default user's style will modify the browser's default style the styles of the document's designer will then modify the overall styles further So, as we read documents in a browser, our styles might be applied in a cascading nature from multiple sources. The whole becomes the document that we see and use within our browser. For example, a rendered document may include some styles from the browser's defaults for HTML. Then another part of the rendered style might come from customised browser settings or style definition files. For example, a user may customise their browser preferences or specify custom behaviours, which are then applied by the browser for rendering documents. Finally, we will also see styles applied from stylesheets linked to the document itself, or other embedded styles. It's not quite as simple as just looking at the linked CSS files. So, to reiterate, for our styles in a cascade, a designer's stylesheets have priority, then user's stylesheets, and then the browser's own defaults. Inheritance CSS also includes an interpretation of the concept of inheritance for its styles. So, descendants will inherit properties from their ancestors. For example, if we create a style on an element, all descendants of that element within the DOM will also inherit that style. This will apply unless the style is then overridden by another ruleset that specifically targets that element. background: blue; color: white; As p is a descendant of body in the DOM, it will inherit the background colour of the body. So, we can set our paragraphs' text colour to correctly show against the specified background colour. White text colour against a blue background. This characteristic of CSS is an important feature, and it helps to reduce redundancy and repetition of styles. It is also another reason why it is useful to maintain an outline of the DOM structure for a given HTML document. Again, however, there is a small caveat to this characteristic of CSS. Whilst most styles will happily follow this pattern, not all properties are inherited by default. For example, properties related to block-level elements are a notable issue with inheritance. Margin, padding, and border rules are not inherited from ancestors. Fonts Fonts for our HTML document can be set for the body or within an element's specific ruleset. The first thing we need to do is specify our font-family, font-family: "Times New Roman", Georgia, Serif; The value for the font-family property specifies preferred and fall-back fonts for our document. If Times New Roman is not available, then the browser will try Georgia and Serif. We add quotation marks to "Times New Roman" because the font name includes spaces.

However, "" is now added due to the CSS validator requesting this standard. It's believed to be a legacy error with the validator itself, and not an actual requirement of the CSS standard. relative and em We've already seen how we can change the colour of our text, but it's also useful to be able to modify the size of our fonts as well. For example, font-size: 100%; h3 { font-size: x-large; font-size: larger; p.p1 { font-size: 1.1em; With the above, we begin by setting the base font size to 100% of the font size for a user's web browser. This allows us to scale our other fonts relative to this base size. So, we could use CSS absolute size values, such as x-large, which scales the size accordingly. Or, we could try relative sizes, such as larger, to help make our font sizes larger relative to the current context. However, if we need better control of our font sizes, we can use em. These are meta-units, which represent a multiplier on the current font-size. They're derived from standard typography practices, which are based upon the standard width of an uppercase M in printing. So, if the current font size has been set to 12px, a font-size of 1.5em will make the font actually an equivalent 18px. However, this current font size will be relative to the element itself. So, cascading and inheritance will be an important factor in how a font-size is calculated. It might be as simple as referencing the root font size for the body, or an inheritance from a parent element. You'll need to check in the CSS and the rendering. The obvious benefit of this approach, em, is that our text will scale according to the base font size. If we modify the size of the base, all font sizes set to em will also adjust accordingly. Try different examples at W3 Schools Further examples as follows, JSFiddle - CSS Fonts relative and rem Another option is the rem unit for font sizes. This is simpler in concept, but can often provide an easier way to manage font sizes. It's particularly useful for websites and applications that are designed to be responsive or progressive in design and usage. e.g. font-size: 100%; font-size: 1.5rem;

However, there are issues with using both em and rem specified font-sizes. custom web fonts Using Fonts with CSS has often been a limiting experience, problematic at best, and reliant upon the installed fonts on a given user's local machine. There were workarounds, such as wrapping font files in JavaScript, and then serving from a remote server with the applicable HTML documents, but these were notoriously slow and buggy. They only tended to be employed, at best, as a final solution to a difficult problem. However, with the advent of web fonts, the process of rendering with custom fonts is now considerably easier for designers and developers. We can deliver required fonts via the internet, using services such as Google's custom fonts. Google Fonts From this site, we can pick and choose our custom fonts by selecting the Quick-use button. This loads a new page with options and instructions for using your chosen custom font. We then select our required character sets, add a <link> reference for the font to our HTML document, and then specify the fonts in our CSS. We need to add this new font as a font-family in our style sheet, font-family: 'Roboto'; We can then style our document's fonts as normal. An example may be found at the following URL, JSFiddle - CSS Custom Fonts References CSS Tricks - nth child recipes JSFiddle - CSS Basics MDN - CSS Perishable Press - Barebones Web Templates W3 CSS W3 Schools - CSS W3 Schools - HTML Colour Picker W3 Web Style Sheets - Even & Odd