Learn CSS (part 1)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Specificity

Specificity is the order by which the browser decides which CSS styles will be displayed. A best practice in CSS is to style elements while using the lowest degree of specificity, so that if an element needs a new style, it is easy to override. IDs are the most specific selector in CSS, followed by classes, and finally, tags. For example, consider the following HTML and CSS: In the example code above, the color of the heading would be set to firebrick, as the class selector is more specific than the tag selector. If an ID attribute (and selector) were added to the code above, the styles within the ID selector's body would override all other styles for the heading. The only way to override an ID is to add another ID with additional styling.

Inline Styles

Although CSS is a different language than HTML, it's possible to write CSS code directly within HTML code using inline styles. To style an HTML element, you can add the style attribute directly to the opening tag. After you add the attribute, you can set it equal to the CSS style(s) you'd like applied to that element.

More on Classes and IDs

CSS can select HTML elements by their tag, class, and ID. CSS classes and IDs have different purposes, which can affect which one you use to style HTML elements. CSS classes are meant to be reused over many elements. By writing CSS classes, you can style elements in a variety of ways by mixing classes on HTML elements. For instance, imagine a page with two headlines. One headline needs to be bold and blue, and the other needs to be bold and green. Instead of writing separate CSS rules for each headline that repeat each other's code, it's better to write a .bold CSS rule, a .green CSS rule, and a .blue CSS rule. Then you can give one headline the bold green classes, and the other the bold blue classes. While classes are meant to be used many times, an ID is meant to style only one element. As we'll learn in the next exercise, IDs override the styles of tags and classes. Since IDs override class and tag styles, they should be used sparingly and only on elements that need to always appear the same.

Tag name

CSS can select HTML elements by using an element's tag name. A tag name is the word (or character) between HTML angle brackets. For example, in HTML, the tag for a paragraph element is <p>. The CSS syntax for selecting <p> elements is: p { }

Class name

CSS is not limited to selecting elements by tag name. HTML elements can have more than just a tag name; they can also have attributes. One common attribute is the class attribute. It's also possible to select an element by its class attribute.

What is CSS

CSS, or Cascading Style Sheets, is a language that web developers use to style the HTML content on a web page. If you're interested in modifying colors, font types, font sizes, shadows, images, element positioning, and more, CSS is the tool for the job!

The CSS File

Developers avoid mixing code by storing HTML and CSS code in separate files (HTML files contain only HTML code, and CSS files contain only CSS code). You can create a CSS file by using the .css file name extension, like so: style.css With a CSS file, you can write all the CSS code needed to style a page without sacrificing the readability and maintainability of your HTML file.

Dedicated Style Section

Fortunately, HTML allows you to write CSS code in its own dedicated section with the <style> element. CSS can be written between opening and closing <style> tags. To use the <style> element, it must be placed inside of the <head> element. Note how the syntax of the CSS code matches (for the most part) the syntax you used for inline styling. The main difference is that you can specify which elements to apply the styling to.

ID name

If an HTML element needs to be styled uniquely (no matter what classes are applied to the element), we can add an ID to the element. To add an ID to an element, the element needs an id attribute:

Inline Style Limitations

Inline styles are a fast way of styling HTML, but they also have limitations. If you wanted to style, for example, multiple <h1> elements, you would have to add inline styling to each element manually. In addition, you would also have to maintain the HTML code when additional <h1> elements are added.

Specificity (cntd.)

Over time, as files grow with code, many elements may have IDs, which can make CSS difficult to edit, since a new, more specific style must be created to change the style of an element. To make styles easy to edit, it's best to style with a tag selector, if possible. If not, add a class selector. If that is not specific enough, then consider using an ID selector.

ID name (cntd.)

Then, CSS can select HTML elements by their id attribute. To select an id element, CSS prepends the id name with a hashtag (#). For instance, if we wanted to select the HTML element in the example above, it would look like this:

Multiple Classes (cntd.)

Then, you could include both of these classes on one HTML element like this: We can add multiple classes to an HTML element's class attribute by separating them with a space. This enables us to mix and match CSS classes to create many unique styles without writing a custom class for every style combination needed.

Multiple Classes

We can use CSS to select an HTML element's class attribute by name. So far, we've selected elements using only one class name per element. If every HTML element had a single class, all the style information for each element would require a new class. Luckily, it's possible to add more than one class name to an HTML element's class attribute. For instance, perhaps there's a heading element that needs to be green and bold. You could write two CSS rules like so:

Chaining Selectors

When writing CSS rules, it's possible to require an HTML element to have two or more CSS selectors at the same time. This is done by combining multiple selectors, which we will refer to as chaining. For instance, if there was a .special class for h1 elements, the CSS would look like: The code above would select only the h1 elements that have a class of special. If a p element also had a class of special, the rule in the example would not style the paragraph.

Linking HTML and CSS files

You can use the <link> element to link HTML and CSS files together. The <link> element must be placed within the head of the HTML file. It is a self-closing tag and requires the following three attributes: href — like the anchor element, the value of this attribute must be the address, or path, to the CSS file. type — this attribute describes the type of document that you are linking to (in this case, a CSS file). The value of this attribute should be set to text/css. rel — this attribute describes the relationship between the HTML file and the CSS file. Because you are linking to a stylesheet, the value should be set to stylesheet.


Conjuntos de estudio relacionados

Chapter 8- Vascular Access and Medication Administration

View Set

chapter 13 15 16 che 4 homework falshcards continued

View Set

Exam 5 Women and Children Chapters: 12, 13, 14, 15, 19, 20, 21 PrepU

View Set

chapter 2 EMT, chapter 3 EMT, chapter 4 EMT, EMT chap1, EMT test, EMT, EMT Final Exam practice, Chapter 31 quiz EMT, Acronyms just names, EMT test1, EMT test2, EMT test5, EMT test3, EMT test4, EMT vocabs, Chapter 16 quiz EMT, Chapter 15 quiz EMT, Cha...

View Set