Target class in html

Type, class, and ID selectors

In this lesson, we examine some of the simplest selectors, which you will probably use most frequently in your work.

Prerequisites: Basic computer literacy, basic software installed, basic knowledge of working with files, HTML basics (study Introduction to HTML), and an idea of how CSS works (study CSS first steps.)
Objective: To learn about the different CSS selectors we can use to apply CSS to a document.

Type selectors

A type selector is sometimes referred to as a tag name selector or element selector because it selects an HTML tag/element in your document. In the example below, we have used the span , em and strong selectors.

Try adding a CSS rule to select the element and change its color to blue.

The universal selector

The universal selector is indicated by an asterisk ( * ). It selects everything in the document (or inside the parent element if it is being chained together with another element and a descendant combinator). In the following example, we use the universal selector to remove the margins on all elements. Instead of the default styling added by the browser — which spaces out headings and paragraphs with margins — everything is close together.

This kind of behavior can sometimes be seen in «reset stylesheets», which strip out all of the browser styling. Since the universal selector makes global changes, we use it for very specific situations, such as the one described below.

Using the universal selector to make your selectors easier to read

One use of the universal selector is to make selectors easier to read and more obvious in terms of what they are doing. For example, if we wanted to select any descendant elements of an element that are the first child of their parent, including direct children, and make them bold, we could use the :first-child pseudo-class. We will learn more about this in the lesson on pseudo-classes and pseudo-elements, as a descendant selector along with the element selector:

article :first-child  font-weight: bold; > 

However, this selector could be confused with article:first-child , which will select any element that is the first child of another element.

To avoid this confusion, we can add the universal selector to the :first-child pseudo-class, so it is more obvious what the selector is doing. It is selecting any element which is the first-child of an element, or the first-child of any descendant element of :

article *:first-child  font-weight: bold; > 

Although both do the same thing, the readability is significantly improved.

Class selectors

The class selector starts with a dot ( . ) character. It will select everything in the document with that class applied to it. In the live example below we have created a class called highlight , and have applied it to several places in my document. All of the elements that have the class applied are highlighted.

Targeting classes on particular elements

You can create a selector that will target specific elements with the class applied. In this next example, we will highlight a with a class of highlight differently to an heading with a class of highlight . We do this by using the type selector for the element we want to target, with the class appended using a dot, with no white space in between.

This approach reduces the scope of a rule. The rule will only apply to that particular element and class combination. You would need to add another selector if you decided the rule should apply to other elements too.

Target an element if it has more than one class applied

You can apply multiple classes to an element and target them individually, or only select the element when all of the classes in the selector are present. This can be helpful when building up components that can be combined in different ways on your site.

In the example below, we have a that contains a note. The grey border is applied when the box has a class of notebox . If it also has a class of warning or danger , we change the border-color .

We can tell the browser that we only want to match the element if it has two classes applied by chaining them together with no white space between them. You’ll see that the last doesn’t get any styling applied, as it only has the danger class; it needs notebox as well to get anything applied.

ID selectors

An ID selector begins with a # rather than a dot character, but is used in the same way as a class selector. However, an ID can be used only once per page, and elements can only have a single id value applied to them. It can select an element that has the id set on it, and you can precede the ID with a type selector to only target the element if both the element and ID match. You can see both of these uses in the following example:

Warning: Using the same ID multiple times in a document may appear to work for styling purposes, but don’t do this. It results in invalid code, and will cause strange behavior in many places.

Note: As we learned in the lesson on specificity, an ID has high specificity. It will overrule most other selectors. In most cases, it is preferable to add a class to an element instead of an ID. However, if using the ID is the only way to target the element — perhaps because you do not have access to the markup and cannot edit it — this will work.

Summary

That wraps up Type, class, and ID selectors. We’ll continue exploring selectors by looking at attribute selectors.

Found a content problem with this page?

This page was last modified on Jun 30, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

CSS :target Pseudo Class

The :target pseudo-class is used to highlight the section of a page linked to from a table of contents. It styles an element that is the target of an internal link in a document.

The :target pseudo-class represents the target element with an id matching the URL’s fragment.

Pure-CSS lightbox

The :target pseudo-class is used to create a lightbox without any JavaScript part. This method uses the ability of anchor links to point to those elements that are hidden on the page, by default. When they are targeted, CSS changes their display so as to be shown.

Version

Syntax

Example of the :target selector:

html> html> head> title>Title of the document title> style> :target < border: 2px solid #1c87c9; background-color: #eeeeee; > style> head> body> h2>:target selector example h2> p> a href="#example1">Jump to Paragraph 1 a> p> p> a href="#example2">Jump to Paragraph 2 a> p> p id="example1"> strong>Paragraph 1 strong> p> p id="example2"> strong>Paragraph 2 strong> p> body> html>

Example of using the :target psudo-class to create a tabbed menu:

html> html> head> style> .tab-menu div < display: none; background-color: #f5f5f5; padding: 0 20px 20px; > .tab-menu a < text-decoration: none; padding: 10px; margin: 20px 0; display: inline-block; > .tab-menu div:target < display: block; > style> head> body> h1>:target selector example h1> div class="tab-menu"> a href="#html">HTML a> a href="#css">CSS a> a href="#php">PHP a> div id="html"> h2> a href="https://www.w3docs.com/learn-html.html">Lean HTML a> h2> p>HTML-Hyper Text Markup Language p> div> div id="css"> h2> a href="https://www.w3docs.com/learn-css.html">Learn CSS a> h2> p>CSS-Cascading Style Sheets p> div> div id="php"> h2> a href="https://www.w3docs.com/learn-php.html">Learn PHP a> h2> p>PHP-Hyertext Preprocessor p> div> div> body> html>

Example of using the :target pseudo-class to create a modal box:

html> html> head> style> h1+a < text-decoration: none; padding: 10px 20px; background-color: #8ebf42; color: #ffffff; font-family: sans-serif; > /* Add animation (Chrome, Safari, Opera) */ @-webkit-keyframes example < from < top: -100px; opacity: 0; > to < top: 0px; opacity: 1; > > /* Add animation (Standard syntax) */ @keyframes example < from < top: -100px; opacity: 0; > to < top: 0px; opacity: 1; > > /* The modal's background */ .modal < display: none; position: fixed; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); > /* Display the modal when targeted */ .modal:target < display: table; position: absolute; > /* The modal box */ .modal-dialog < display: table-cell; vertical-align: middle; > /* The modal's content */ .modal-dialog .modal-content < margin: auto; background-color: #f3f3f3; position: relative; padding: 0; outline: 0; border: 1px #777 solid; text-align: justify; width: 80%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); /* Add animation */ -webkit-animation-name: example; /* Chrome, Safari, Opera */ -webkit-animation-duration: 0.5s; /* Chrome, Safari, Opera */ animation-name: example; animation-duration: 0.5s; > /* The button used to close the modal */ .closebtn < text-decoration: none; float: right; font-size: 35px; font-weight: bold; color: #fff; > .closebtn:hover, .closebtn:focus < color: #000; text-decoration: none; cursor: pointer; > .container < padding: 2px 16px; text-align: center; line-height: 1.6; > header < background-color: #337ab7; font-size: 25px; color: white; > header h2 < text-align: left; > footer < background-color: #337ab7; font-size: 20px; color: white; > footer p < text-align: right; > style> head> body> h1>:target selector example h1> a href="#modal">Open Modal a> div id="modal" class="modal"> div class="modal-dialog"> div class="modal-content"> header class="container"> a href="#" class="closebtn">× a> h2>Header h2> header> div class="container"> p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. p> div> footer class="container"> p>Footer p> footer> div> div> div> body> html>

There is a slight difference between modal boxes and lightboxes. Lightboxes can be closed by clicking outside the pop-up, while modal boxes can only be closed by interacting inside the pop-up.

Example of using the :target pseudo-class to create a lightbox:

html> html> head> style> h1+a < background-color: #8ebf42; padding: 20px 40px; color: #ffffff; text-decoration: none; font-size: 20px; margin: 15px 0; display: inline-block; > /* Unopened lightbox */ .lightbox < display: none; > /* Opened lightbox */ .lightbox:target < position: absolute; left: 0; top: 0; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; > /* Lightbox content */ .lightbox figcaption < width: 25rem; position: relative; padding: 1.5em; background-color: #8ebf42; > /* Close button */ .lightbox .close < position: relative; display: block; > .lightbox .close::after < right: -1rem; top: -1rem; width: 2rem; height: 2rem; position: absolute; display: flex; z-index: 1; align-items: center; justify-content: center; background-color: black; border-radius: 50%; color: white; content: "×"; cursor: pointer; > /* Lightbox overlay */ .lightbox .close::before < left: 0; top: 0; width: 100%; height: 100%; position: fixed; background-color: rgba(0, 0, 0, .7); content: ""; cursor: default; > style> head> body> h1>:target selector example h1> a href="#lightbox">Open Lightbox a> div class="lightbox" id="lightbox"> figure> a href="#" class="close"> a> figcaption>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec felis enim, placerat id eleifend eu, semper vel sem. figcaption> figure> div> body> html>

Browser support

Источник

Читайте также:  How to use def in python
Оцените статью