Basic Style (CSS)
Basics
Some example text
- To change the style of content in an element, use the "style" attribute in the start tag, then provide a property (e.g. color) and a value (e.g. red)
- You can set multiple properties at the same time (e.g. style="color:blue; font-size:12px;")
Style element
- You can define multiple style specs for various kinds of elements, including those indicated by ID, and class name
- To assign a class to an element, the element must have the attribute "class" with the specified class name (e.g. <p class="myClass">)
- The "style" element goes within the "head" element (which is in the "html" element, but before the "body" element)
- To be clear, you must put "#" to indicate an ID name, and "." to indicate a class name
- Text between /* and */ is considered a comment within a style element
Linking External Stylesheet Files
CSS file:
Link within the HTML file (head element):
- To make a css file, just create a txt file in a text editor, add the content, and then save it with the *.css extension
- The link goes within the "head" element (which is in the "html" element, but before the "body" element)
- The "rel" attribute specifies the relationship to the file
- If the stylesheet file is in the same folder as the HTML file that calls it, just specify the css file name without a folder path
- If the stylesheet file path can only be access through a parent folder, you can use ".." in the filepath to go back one level (e.g. "../MyCSSFolder/myStyle.css")
- You can include multiple stylesheets
Color
- With HSL, the first value ranges from 0 and 360 and specifies the angle on the color wheel
- You can use the background-color property to change the color of the element with the text
Font Size and Style
- For more size options, go to https://www.w3schools.com/cssref/css_units.php
- A font name should have "quotes" if it has spaces in it
- It is recommended to list multiple font names so that the others can work as a backup if the browser doesn't support the previous ones
Alignment
- In addition to padding-top and margin-top, you can specify for -left, -right, or -bottom
Position
- You can also set the position from the right or bottom
Display
- Certain elements are inline by default (e.g. span), while others are block by default (e.g. p and div)
- With flex, the elements are set in a line for as many as will fit, then any more will continue on the next line
- Flex and grid elements may adjust position with window resize, including shifting to different lines
Challenge
Create a stylesheet and include it in a web page. The web page should include a header that stays at the top of the page when you scroll down, and the page should have at least two sections, each with different font, color, and text size. Create a style class that makes the background color light green, and apply that class to three separate phrases on the page.