Center in element :after

Asked

Viewed 647 times

2

I have the code below:

.divCheckList .valorBool, 
.divCheckList .divCheckBox {
    height: 34px;
    display: inline-block;
    border:#CCC .1px solid;
    vertical-align:middle;
}

.divCheckList .valorBool {
    line-height: 34px;
}
.divCheckList .divCheckBox .checkBox {
    display: none;
}

.divCheckList .labelCheckBox {
    display:block;
    width:60px;
    height:34px;
    background-color:#CCC;
    border:none;
    border-radius:34px;
}

.divCheckList .labelCheckBox:after {
    display:block;
    width:26px;
    height:26px;
    background-color:#FFF;
    border:#000 .1px solid;
    border-radius:50%;
    content:"";
}

html

  <div class="divCheckList">
    <label class="valorBool">NÃO</label>
    <div class="divCheckBox">
      <input type="checkbox" name="cb" id="cb" class="checkBox">
      <label for="cb" class="labelCheckBox"></label>
    </div>
    <label class="valorBool">SIM</label>
  </div>

That shows the following picture:

inserir a descrição da imagem aqui

However, I would like to center the white ball so that it is spaced in the corners and on top.

I tried with margin: 4px;but the image distorts.

inserir a descrição da imagem aqui

How to correct?

  • I was able to correct by placing position: relative in the most external div and position Absolute in the label with after. Is it the right way?

  • Will that button change position by clicking "YES"? You have to see how it will look with this.

  • 1

    The way you are, adding top: 10%; left: 5%; position: relative; in the :after was great.

  • I will continue the code with margin:4px; even. I’ll see if it will be a problem when placing the clickable button

  • Boto fé........

  • other 2 things I could not understand is because both after and before, the created element is always on the left side of the creator object and because the white ball only appears if we add content: ""?

  • The left side is default if you do not arbitrarily position the element. "content" is the content of the element, which may or may not be empty. If you don’t put "content", CSS "understands" that the element doesn’t exist and does nothing.

  • mean to align the element at right for example? In this case the ball goes right?

  • To align the ball to the right has several shapes.

  • got it. Thanks. Just did not understand why before and after if any text placed in the div next to the element created for example as after, does not "push" the ball to the right. the text is as background.

  • It is because the ball has fixed size of 26px.

  • and this makes the text stay deep in the div? Shouldn’t push the ball?

  • It is not in the bottom. It is inside the div. It does not push the ball because the size is fixed. When you specify a size for div, it doesn’t change even if you fill text inside it.

  • That’s not what I meant no. Look at this: we have the div, right? so inside it I put the object before, a text and an after object. Okay? So, what is happening is that the text is appearing at the bottom of the div and the object before or after seems to be soibresai to the text. Do the following, take the code that is posted and put a text of 50 carcters for example <label for="cb" class="labelCheckBox">aaaaaaaaaaaaaaaa</label> and see what happens.

  • But there’s before there.

Show 10 more comments

1 answer

0

You can, as said in the comments, add the position: relative in .labelCheckBox and position: absolute in the :after.

.divCheckList .labelCheckBox {
  position: relative;
  ...
}
.divCheckList .labelCheckBox:after {
  position: absolute;
  top: 3px;
  left: 3px;
  ...
}

And when selected, apply the position change.

.divCheckList .checkBox:checked + .labelCheckBox:after {
  right: 3px;
  left: initial;
}

Jsfiddle

  • Your code was excellent, only I would put top=12% pq is right in the center. With top=3px decentralized.

  • To be right in the center, automatically: top: 50%; -webkit-transform: translate(0, -50%); -moz-transform: translate(0, -50%); transform: translate(0, -50%);.

  • But the right value would be top=4px. (34-26)/2 = 4.

  • also possible just by doing . divCheckList . divCheckBox . checkbox:checked + . labelCheckBox:before { right:0;}, Without the Translate and maintaining margin: 4px; However, this way we can not make the transition to right:0 without the slow effect on the transition.

  • Take a look at this example from W3schools https://www.w3schools.com/howto/howto_css_switch.asp

  • My starting line was exactly this example. But I wanted to do step-by-step to understand what really makes each line. But in the course of learning, I realized that I could exclude some lines.

Show 1 more comment

Browser other questions tagged

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