The target of CSS selectors with pseudo-element ::before

Asked

Viewed 122 times

1

Hello, I’m studying CSS3 and, in a given tutorial, I analyzed it all, and did not understand the point where the ::before was used together with the checkbox, in which the property was added content.

Why in the first use of ::before with the pseudo-class :checked it stylizes the value of content and does not style the checkbox itself?

Follows code:

body {
  margin: 5px;
  padding: 0;
  background: #262626;
}

#botoes {
  position: absolute;
  width: 100px;
  color: #fff;
  margin: 5px;
}

#botoes input[type=checkbox]::before {
  content: '';
  position: absolute;
  width: 40px;
  height: 40px;
  top: 0;
  left: 0;
  border-radius: 50%;
  background: #000;
  transition: .1s;
  transform: scale(1.1);
  box-shadow: 0 0px 15px #ffffff;
}

#botoes input[type=checkbox] {
  position: relative;
  background: #b9b9c8;
  width: 80px;
  height: 40px;
  border: none;
  float: right;
  border-radius: 20px;
  -webkit-appearance: none;
  margin-bottom: 15%;
  outline: none;
}

#botoes input:checked[type=checkbox]::before {
  left: 40px;
}

#botoes input:checked[type=checkbox] {
  background: dodgerBlue;
}
<html lang="pt-br">

<head>
  <title>Test Toogle With Pure CSS</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="CSS/xxxxxxxx.css">
</head>
<div class="box" id="box">
</div>

<body>
  <div id="botoes">
    Hidden Rings
    <input type="checkbox"> Collision Rings
    <input type="checkbox">
  </div>
</body>

</html>

  • You want to know why on elemento::before need to put the content and in the elemento in itself not? If that is so, it is because the content of the element itself is defined in HTML, but the tag input is a little different

  • No, Guilherme, look at the second paragraph of my question: why does CSS style the content of the element (the content) and not the element itself, understand? See all the css code posted (with scrool), ok?

1 answer

1


Fernandes if I understand you well you want to know why he used the content correct?

Let’s explain.

A CSS pseudo-element is a keyword added to a selector that allows you to style a specific part of the element selected.

https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elementos

::before creates a pseudo-element who is the first child of element hit. It is often used to add content decorative to the one element using the property content. This element is inline by default.

https://developer.mozilla.org/en-US/docs/Web/CSS/:before

Note in this image that when HTML and CSS are rendered on the page is created the pseudo-element within the input, which in turn gains a closing tag added by the browser itself. As you can see in the image.

inserir a descrição da imagem aqui

But this behavior of input types (checkbox, radio, text, numeber, email, etc...) is not standard, and are not all inputs that may receive a pseudo-element ::after and ::before as you can see in this question: Pseudo elements ::after and ::before work on which input types

Note that in HTML we only have the tag <input type="checkbox"> which corresponds to the gray background of the switch-buttom already the toggle which is the ball that goes from side to side she "doesn’t exist" in HTML, she is built with the pseudo-element

inserir a descrição da imagem aqui

And why you need to put the content:"" in the pseudo elements?

The CSS property content is used with the pseudo elements ::before and ::after to generate content in an element. Objects inserted using property content sane replaced elements anonymous.

https://developer.mozilla.org/en-US/docs/Web/CSS/content

"In CSS, you can insert a lie element (pseudo-element) into objects in HTML. These elements can help you at various times of development, preventing the creation of empty HTML elements to produce some detail of the layout that can be mixed with the actual content. ... The coolest thing is that you can format the :after and ::before elements with CSS as if they were HTML elements, using the normal properties we already use daily."

This article can help you better understand the subject: https://tableless.com.br/geracao-estatica-de-conteudo-via-css/

A short direct explanation in the code:

/* cor do btn sem estar checado "cinza" */
#botoes input[type=checkbox] {
  background: #b9b9c8;
}
/* cor do btn quando está checado "azul" */
#botoes input:checked[type=checkbox] {
  background: dodgerBlue;
}

/* quando vc declara um pseudo-elemento vc precisa declarar que ele tem um "conteúdo" (content) */
/* esse "conteúdo" antes de ser checado esta com left 0 */
#botoes input[type=checkbox]::before {
  content: '';
  left: 0;
}

/* depois de checado o "conteúdo" afasta 40px pra direita */
/* nesse momento vc não precisa declarar novamente o content, pois independe do input está checado ou não  o contente já foi declarado no ::before anteriormente */
#botoes input:checked[type=checkbox]::before {
  left: 40px;
}

  • All right, Hugocsl, let’s go. If I understand correctly (confirm), when I declare a pseudo-element with ::before and content, then I can style only this content, also via ::before, is that it? That is, on the line: #input buttons:checked[type=checkbox]:before I’m telling CSS to style the input checkbox content when checked, not the input[type=checkbox] element, that’s it?

  • PS: I have already scored your answer. Very detailed. Very grateful!

  • @Fernandes speaks Jove, I’ll try to simplify. Imagine that you have 4 states css in the same element. The first 2 are input and input:checked where you have input with gray background and then input:checked with blue background. And the other 2 are the input::before and the input:checked::before, where before the check the ball is on one side, and after the checked the ball is on the other side. This is the structure for you to assemble CSS. Now about the contet is as I said in the explanation, every element ::after and ::before must have the property content=" " to be rendered by the browser

  • You may have an empty content or with some content within type content=" btn n. 1", but placing things within the content is not mandatory. Mandatory is just placing the property content=" " no :after or ::before. #botoes input[type=checkbox]::before {&#xA; content: ''; } without the content the pseudo element will not be "created" in HTML. @Fernandes When you have a little time to look at the links I quoted in the answer, they will clarify many things

  • hugocsl, all right, I get it. You were very practical in answering when you complemented noting, in this case, that are four states of css in the same element! Good, got it. Grateful!

  • @Fernandes cool that it became clear what is what rss, sometimes it is kind of confusing even. Any doubt is only give the touch, good studies!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.