CSS Dasar - 7 - Selector

CSS Dasar - 7 - Selector

Introduction to CSS Selectors

In this video, the speaker introduces the concept of CSS selectors and explains their role in styling HTML elements. The video will cover different types of selectors, including HTML elements, IDs, and classes.

CSS Selectors

  • CSS selectors are used to identify HTML elements for styling purposes.
  • Different types of selectors can be used:
  • HTML element selector: Targets all instances of a specific HTML element.
  • ID selector: Targets a specific element with a unique ID attribute.
  • Class selector: Targets multiple elements with the same class attribute.
  • Multiple selectors can be grouped together using commas or spaces to apply the same style to different elements.

Using HTML Element Selectors

  • To target all instances of an HTML element, such as changing the font family for all text within the body tag, use the element name as the selector (e.g., body font-family: Arial; ).
  • Specific styles can be applied to individual HTML elements by using their respective selectors (e.g., h1 color: green; ).

Grouping Selectors

  • Multiple selectors can be grouped together using commas to apply the same style (e.g., h1, h2 color: green; ).
  • Styles can also be applied to specific combinations of elements by using spaces between selectors (e.g., ul li a color: orange; ).

Using IDs and Classes as Selectors

  • IDs and classes provide more specific targeting options.
  • Use "#" followed by the ID name for ID selectors (e.g., #p1 color: orange; ).
  • Use "." followed by the class name for class selectors (e.g., .p2 color: red; ).

Summary

CSS selectors are used to target specific HTML elements for styling. Different types of selectors, such as HTML element selectors, IDs, and classes, provide various ways to apply styles. Selectors can be grouped together or used individually to achieve the desired styling effects.

Understanding IDs and Classes

In this section, the speaker explains the usage of IDs and classes in HTML elements. They discuss how IDs can be used as unique identifiers for elements and caution against using the same ID for multiple elements. The speaker also mentions that IDs can be used as page anchors for creating hyperlinks to different sections of a webpage.

Usage of IDs and JavaScript

  • IDs are also commonly used in JavaScript.
  • However, it is not recommended to use IDs for CSS styling due to specificity issues.
  • Using an ID selector carries more weight than using an element selector or a class selector.
  • It is advised to prioritize using element selectors over IDs whenever possible.

Performance Impact of Using IDs

  • Using an ID selector has a performance impact that is 10 times heavier than using an element selector.
  • Additionally, using an ID selector is 100 times heavier than using a class selector.
  • Therefore, it is best practice to first try using element selectors and resort to classes if necessary.

Recommendations for Selectors

  • The speaker suggests utilizing HTML elements whenever possible.
  • If HTML elements cannot fulfill the requirements, then classes should be used instead of IDs.

Applying CSS Styles with Classes

In this section, the speaker demonstrates how to apply CSS styles selectively by targeting specific paragraphs with a class. They explain that by assigning a specific class name to certain paragraphs, only those paragraphs will be affected by the defined styles.

Applying Bold Styling with Class Selector

  • To apply bold styling selectively, a class name such as "bold" can be assigned to desired paragraphs.
  • By defining CSS rules specifically for the "bold" class, only paragraphs with that class will have bold formatting applied.

Selector Hierarchy and Specificity

  • The speaker emphasizes that this approach is possible due to the specificity of CSS selectors.
  • Other paragraphs without the "bold" class will not be affected by the bold styling.
  • This demonstrates the basic concept of using classes to selectively apply styles.

Final Recommendations

In this final section, the speaker provides some concluding recommendations regarding selector usage in HTML and CSS.

Best Practices for Selector Usage

  • It is advised to prioritize using HTML elements whenever possible.
  • If HTML elements cannot fulfill the requirements, then classes should be used instead of IDs.
  • IDs should generally be avoided for CSS styling due to their higher specificity and performance impact.

Conclusion

  • The speaker concludes by encouraging viewers to experiment with different selector combinations, such as using element selectors, IDs, or classes.
  • They also remind viewers to subscribe to their channel for future videos on related topics like pseudo-classes.
Video description

Masih ingat anatomi dari sebuah sintaks CSS kan? di dalamnya terdapat bagian yang dinamakan selector. Selector digunakan untuk memilih elemen spesifik pada HTML yang akan diberi style. Ada 3 jenis selector yaitu elemen HTML, id dan class. -- BGM : "Z One Hundred" by. Robert Larow www.freeplaymusic.com/volumedetail.aspx?volume=1973 -- Jangan lupa untuk follow juga akun media sosial kita yang lain : - https://www.facebook.com/WebProgrammingUNPAS - http://twitter.com/pw1unpas - http://instagram.com/webprogrammingunpas - http://codepen.io/webprogrammingunpas - http://www.unpas.ac.id terimakasih dan selamat #ngoding! @sandhikagalih