{"id":756,"date":"2008-07-07T19:28:11","date_gmt":"2008-07-08T01:28:11","guid":{"rendered":"http:\/\/www.mrc-productivity.com\/techblog\/?p=756"},"modified":"2023-07-05T14:29:14","modified_gmt":"2023-07-05T20:29:14","slug":"css-tutorial-2-style-sheet-syntax","status":"publish","type":"ht_kb","link":"https:\/\/www.mrc-productivity.com\/techblog\/?ht_kb=css-tutorial-2-style-sheet-syntax","title":{"rendered":"CSS Tutorial #2: Style Sheet Syntax"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>This post follows up the previous post titled, \u201c<a href=\"\/techblog\/?ht_kb=css-tutorial-style-sheet-types\" data-type=\"ht_kb\" target=\"_blank\" rel=\"noreferrer noopener\">CSS Tutorial: Style Sheet Types<\/a>\u201d.\u00a0 It is meant for CSS beginners who want to learn the basics of style sheet syntax, and how it interacts with HTML.\u00a0 If you haven\u2019t read the previous post, it is important that you read it before this one.<\/p>\n\n\n\n<p>You will find that style sheet syntax is relatively easy to learn, once you understand the basic principles.\u00a0 Style sheets establish rules that declare how certain elements are displayed on the webpage.\u00a0 Every style sheet rule has two parts: A selector and a declaration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Selector<\/h2>\n\n\n\n<p>The selector is connected to a particular style within the HTML.\u00a0 The selector defines which element will be affected by the declaration.\u00a0 Any element used in html can be a selector, such as \u2018p\u2019, \u2018h1\u2019, or \u2018ul\u2019<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Declaration<\/h2>\n\n\n\n<p>The declaration applies a style to the selector and is split up into two parts: property and value.\u00a0 The property is the aspect of the selector that is being defined.\u00a0 The value lays out the exact specification of the property.<\/p>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-ee120a7c wp-block-group-is-layout-flex\">\n<p>Here is an example of the different parts that make up a rule:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.mrc-productivity.com\/forum\/images\/selectorimage.jpg\" alt=\"\"\/><\/figure>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Rules<\/h2>\n\n\n\n<p>Look at the image above and you\u2019ll notice a few basic rules to follow when using style sheets:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The declaration is always wrapped in curly braces.\u00a0<\/li>\n\n\n\n<li>The property is followed by a colon.<\/li>\n\n\n\n<li>The value is followed by a semi-colon<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Grouping Properties<\/h2>\n\n\n\n<p>More often than not, you will want to declare a few properties for the same selector. Grouping properties follows the same format as above, and can be displayed like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h1 { font-family: arial; color: red; font-weight: bold; }&lt;\/pre>\r\n&lt;\/div><\/code><\/pre>\n\n\n\n<p>When defining multiple properties for the same selector, make sure that every value is followed with a semicolon.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Visual Formatting<\/h2>\n\n\n\n<p>When dealing with a selector containing multiple properties as shown above, it is good practice to lay out the code in such a way that is simple to read.\u00a0 Obviously, placing 5 properties on the same line can look messy and be difficult to read.\u00a0 The key to readability is proper use of white space.\u00a0 When using style sheets, white space is a matter of preference.\u00a0 You can add as little or as much space in between properties as you like.\u00a0 With that in mind, it is a good practice to place multiple properties on separate lines, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p   {\r\n     color: red;\r\n     font-family: arial;\r\n     font-weight: bold;\r\n     background-color: blue;\r\n     }<\/code><\/pre>\n\n\n\n<p>Do you see how much easier that is to read than a linear format?\u00a0 Just like HTML, it is important that you write clean code with CSS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Grouping Selectors<\/h2>\n\n\n\n<p>Let\u2019s say that you want a few different elements to have the same style. Rather than declaring each style separately, you can group similar selectors together and save space. Simply separate each selector with a comma, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p, h1, th   {\r\n               color: red;\r\n               font-family: arial;\r\n               background-color: blue;\r\n               }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Using the fundamentals laid out above, below is an example of actual CSS code and the corresponding HTML.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html>\r\n&lt;head>\r\n&lt;style type=\"text\/css\">\r\np  {\r\n    font-family: arial;\r\n    color: blue;\r\n    background-color: gray;\r\n    border: 2px solid black;\r\n    font-weight: bold;\r\n    width: 200px;\r\n    }\r\n&lt;\/style>\r\n&lt;\/head>\r\n&lt;body>\r\n&lt;p>\rThis is blue text on a gray background.&lt;\/p>\n&lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<p>Click this <a href=\"https:\/\/www.mrc-productivity.com\/forum\/example.html\" target=\"_blank\" rel=\"noreferrer noopener\">link<\/a> to see what that code would look like in a browser window.<\/p>\n\n\n\n<p>This has been a basic overview of CSS syntax. I hope that you now have a good understanding of style sheet formatting, and how to lay out a basic style sheet. The next tutorial will detail two of the most commonly used aspects of CSS: Class and ID selectors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview This post follows up the previous post titled, \u201cCSS Tutorial: Style Sheet Types\u201d.\u00a0 It is meant for CSS beginners who want to learn the basics of style sheet syntax, and how it interacts with HTML.\u00a0 If you haven\u2019t read the previous post, it is important that you read it&#8230;<\/p>\n","protected":false},"author":2,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[160],"ht-kb-tag":[],"class_list":["post-756","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-css"],"_links":{"self":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=756"}],"version-history":[{"count":8,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/756\/revisions"}],"predecessor-version":[{"id":11958,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/756\/revisions\/11958"}],"wp:attachment":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=756"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-category&post=756"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-tag&post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}