{"id":881,"date":"2023-03-13T14:02:31","date_gmt":"2023-03-13T14:02:31","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=881"},"modified":"2023-03-13T15:30:15","modified_gmt":"2023-03-13T15:30:15","slug":"css-introduction","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-2\/css-introduction\/","title":{"rendered":"CSS Introduction"},"content":{"rendered":"\n<p>Cascading Style Sheets (CSS) can be used to style web pages.<\/p>\n\n\n\n<p>While HTML tells the browser what to display on a page, CSS tells the browser how to display it.<\/p>\n\n\n\n<p>CSS rules can be added to an HTML file using the &lt;style&gt; tag, and\/or it can be in a separate file included using a &lt;link&gt; tag. Either way this would be done in the header section of the HTML.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;style&gt;\n      ...\n    &lt;\/style&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    ...\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>This method is useful if you have certain rules only on a single page.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"\/style.css\"&gt;\n    &lt;script type=\"text\/javascript\" src=\"\/script.js\"&gt;&lt;\/script&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta author=\"Your name here\"&gt;\n    &lt;title&gt;Forms&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    ...\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>This method is best where the styles are consistent across the whole website.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>A CSS rule set consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a selector \u2013 points to the HTML element you want to style<\/li><li>a declaration block \u2013 defines the properties and values of the style to be used<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"115\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13152956\/HTML13-e1652188276391.png\" alt=\"Image from BBC Bitesize showing the CSS selector and a declaration with two property value pairs. \" class=\"wp-image-900\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13152956\/HTML13-e1652188276391.png 428w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/03\/13152956\/HTML13-e1652188276391-300x81.png 300w\" sizes=\"auto, (max-width: 428px) 100vw, 428px\" \/><figcaption>Image from BBC Bitesize<\/figcaption><\/figure><\/div>\n\n\n\n<p>In the case above the selector shows that the style will be applied to all h1 tags.<\/p>\n\n\n\n<p>The declaration block contains one or more declarations. These are separated by semicolons.<\/p>\n\n\n\n<p>Each declaration includes a property name and a value. These are separated by a colon. The entire group of declarations is surrounded by curly brackets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Style declarations<\/h2>\n\n\n\n<p>Whitespace in a declaration is ignored. Therefore, sometimes the declarations are shown on a single line like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h1 {color:blue;font-size:12px;}<\/code><\/pre>\n\n\n\n<p>They can also appear on separate lines, so the same rule could appear as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h1 {\n  color: blue;\n  font-size: 12px;\n}<\/code><\/pre>\n\n\n\n<p>One property value pair per line is the most readable once you start applying complex styles. In either form, the heading would show on screen as blue text with a font size of 12.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Selectors<\/h2>\n\n\n\n<p>The selector is how you choose the HTML elements that are affected by the CSS properties. You can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The HTML tag<\/li><li>A class assigned to a tag<\/li><li>An id assigned to a tag<\/li><li>Nested HTML tags<\/li><li>Pseudo classes<\/li><li>Pseudo elements<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">HTML tag selectors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>h1 { \n  color: blue;\n  font-size: 12px;\n}\n<\/code><\/pre>\n\n\n\n<p>This means apply these CSS properties to all h1 elements in the page. Using a HTML tag as the selector is how you change the default appearance of the whole site.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Class selectors<\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>&lt;p class=\"intro\"&gt;Some text to display.&lt;\/p&gt;<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>.intro {\n  color: #ff0000;\n  padding: 10px 5px 10px 5px;\n}\np.intro {\n  background-color: #000000;\n}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>You use a period then the class name to apply a class based selection. A class selector applies the CSS properties to any elements with the matching class assigned. You can also use it in combination with a tag selector. In the above example any tag with an intro class has their color and padding change, but paragraphs with the intro class also have their background-color altered. Classes are useful ways to assign styles to different sections of a website.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">ID selectors<\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>&lt;p id=\"userfeedback\"&gt;Some text here.&lt;\/p&gt;<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>#userfeedback {\n  color: #ff0000;\n  text-align: center;\n}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>To use an ID based selector you use the hash symbol then the ID of the HTML element to match. Because IDs are unique, ID based selectors are used mainly for forms or menus where it has to behave differently from the rest of the site.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Nested selectors<\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>&lt;div&gt;&lt;p&gt;Some text here&lt;\/p&gt;&lt;\/div&gt;<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>div p {\n  background-color: #33cc33;\n}<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>You can use nested selectors to limit CSS properties to only affect tags that occur beneath other tags. In the above example we only want paragraphs that are within div tags to be altered, not all paragraphs.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Pseudo class selectors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>a:hover {\n  color: #ff3333;\n}<\/code><\/pre>\n\n\n\n<p>To use a pseudo class you use a colon followed by the pseudo class name. A pseudo class is used to define a specific state of an element such as when a mouse is hovering over it or if a link has already been visited. Some of the more common pseudo classes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>link  (every link that hasn&#8217;t been visited)<\/li><li>visited   (every link that has been visited)<\/li><li>hover  (hovering the mouse of the element)<\/li><li>active  (every link to the current active page)<\/li><li>first-child  (the first HTML element within the tag that this is applied to)<\/li><li>nth-child(n)  (the n<sup>th<\/sup> HTML element within the tag that this is applied to)<\/li><li>lang(language)  (every element with a lang attribute matching the language passed into the selector)<\/li><li>checked  (every input that is checked)<\/li><li>enabled  (every input that is enabled)<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Pseudo element selectors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>h1::after {\n ...\n}<\/code><\/pre>\n\n\n\n<p>To use them you use two colons followed by the pseudo element. Pseudo element selectors can style specific parts of the content such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>first-letter  (Changes the style only for the first letter of the element)<\/li><li>first-line  (Changes the style only for the first line of the element)<\/li><li>before  (insert content before the element)<\/li><li>after  (insert content after the element)<\/li><\/ul>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-2\/css-basic-rules\/\" data-type=\"page\" data-id=\"896\">Next: CSS Basic rules<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cascading Style Sheets (CSS) can be used to style web pages. While HTML tells the browser what to display on a page, CSS tells the browser how to display it. CSS rules can be added to an HTML file using the &lt;style&gt; tag, and\/or it can be in a separate file included using a &lt;link&gt;&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/authoring-a-website\/week-2\/css-introduction\/\">Continue reading <span class=\"screen-reader-text\">CSS Introduction<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":56,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-881","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=881"}],"version-history":[{"count":13,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/881\/revisions"}],"predecessor-version":[{"id":901,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/881\/revisions\/901"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/56"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}