css 需要帮助识别和消除元素周围的间距

m1m5dgzv  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(104)

我面临着一个关于“开关”元素周围奇怪间距的问题。它使div的“check-lower”变得比实际内容更大。当我在我当前的项目中使用它时,它会导致溢出,这是不能通过使用溢出隐藏来改变的,因为它会扰乱“开关”的缩放。

<!DOCTYPE html>
<html>
<head>
<style>

.check-lower {
display: flex;
align-items: center;
border: 2px solid black;
}

.check-display {
  background-color: #000000;
  color: #19ba00;
  font-family: "AminDigitalCaps";
  font-size: 20px;
  padding: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 170px;
  border: outset 3.5px;
}

.switch {
display: block;
background-color: black;
width: 150px;
height: 195px;
box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.2), 0 0 1px 2px black, inset 0 2px 2px -2px white, inset 0 0 2px 15px #47434c, inset 0 0 2px 22px black;
border-radius: 5px;
padding: 20px;
perspective: 700px;
transform: scale(0.25);
}

.switch input {
display: none;
}

.switch input:checked + .button {
transform: translateZ(20px) rotateX(25deg);
box-shadow: 0 -10px 20px #ff1818;
}

.switch input:checked + .button .light {
animation: flicker 0.2s infinite 0.3s;
}

.switch input:checked + .button .shine {
opacity: 1;
}

.switch input:checked + .button .shadow {
opacity: 0;
}

.switch .button {
display: block;
transition: all 0.15s cubic-bezier(1, 0, 1, 1);
transform-origin: center center -20px;
transform: translateZ(20px) rotateX(-25deg);
transform-style: preserve-3d;
background-color: #9b0621;
height: 100%;
position: relative;
cursor: pointer;
background: linear-gradient(#980000 0%, #6f0000 30%, #6f0000 70%, #980000 100%);
background-repeat: no-repeat;
}

.switch .button::before {
content: "";
background: linear-gradient(rgba(255, 255, 255, 0.8) 10%, rgba(255, 255, 255, 0.3) 30%, #650000 75%, #320000) 50% 50%/97% 97%, #b10000;
background-repeat: no-repeat;
width: 100%;
height: 50px;
transform-origin: top;
transform: rotateX(-90deg);
position: absolute;
top: 0;
}

.switch .button::after {
content: "";
background-image: linear-gradient(#650000, #320000);
width: 100%;
height: 50px;
transform-origin: top;
transform: translateY(50px) rotateX(-90deg);
position: absolute;
bottom: 0;
box-shadow: 0 50px 8px 0px black, 0 80px 20px 0px rgba(0, 0, 0, 0.5);
}

.switch .light {
opacity: 0;
animation: light-off 1s;
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(#ffc97e, #ff1818 40%, transparent 70%);
}

.switch .dots {
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(transparent 30%, rgba(101, 0, 0, 0.7) 70%);
background-size: 10px 10px;
}

.switch .characters {
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(white, white) 50% 20%/5% 20%, radial-gradient(circle, transparent 50%, white 52%, white 70%, transparent 72%) 50% 80%/33% 25%;
background-repeat: no-repeat;
}

.switch .shine {
transition: all 0.3s cubic-bezier(1, 0, 1, 1);
opacity: 0.3;
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(white, transparent 3%) 50% 50%/97% 97%, linear-gradient(rgba(255, 255, 255, 0.5), transparent 50%, transparent 80%, rgba(255, 255, 255, 0.5)) 50% 50%/97% 97%;
background-repeat: no-repeat;
}

.switch .shadow {
transition: all 0.3s cubic-bezier(1, 0, 1, 1);
opacity: 1;
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(transparent 70%, rgba(0, 0, 0, 0.8));
background-repeat: no-repeat;
}

@keyframes flicker {
0% {
  opacity: 1;
}

80% {
  opacity: 0.8;
}

100% {
  opacity: 1;
}
}

@keyframes light-off {
0% {
  opacity: 1;
}

80% {
  opacity: 0;
}
}

</style>
</head>
<body>

<div class="check-lower">
                <label class="check-display" id="display-right">Use lowercase</label>
                
<label class="switch">
    <input type="checkbox" id="lowercase" class="checks">
    <div class="button">
      <div class="light"></div>
      <div class="dots"></div>
      <div class="characters"></div>
      <div class="shine"></div>
      <div class="shadow"></div>
    </div>
  </label>
</div>

</body>
</html>

我已经寻找任何可能的保证金或填充,但我找不到它。我还尝试了在switch元素上transform translate x,但是“check-lower”div中的间距仍然存在,所以这也不能完成任务。

wvyml7n5

wvyml7n51#

您需要放弃缩放并更改开关的宽度/高度值。Transform会导致div认为它的子元素比它们本身要大。请参阅下面的示例(您可能需要调整开关/边框以满足您的需求)

.check-lower {
display: flex;
align-items: center;
border: 2px solid black;
}

.check-display {
  background-color: #000000;
  color: #19ba00;
  font-family: "AminDigitalCaps";
  font-size: 20px;
  padding: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 170px;
  border: outset 3.5px;
}

.switch {
display: block;
background-color: black;
width: 47.5px;
height: 58.75px;
box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.2), 0 0 1px 2px black, inset 0 2px 2px -2px white, inset 0 0 2px 15px #47434c, inset 0 0 2px 22px black;
border-radius: 5px;
padding: 20px;
perspective: 700px;
}

.switch input {
display: none;
}

.switch input:checked + .button {
transform: translateZ(20px) rotateX(25deg);
box-shadow: 0 -10px 20px #ff1818;
}

.switch input:checked + .button .light {
animation: flicker 0.2s infinite 0.3s;
}

.switch input:checked + .button .shine {
opacity: 1;
}

.switch input:checked + .button .shadow {
opacity: 0;
}

.switch .button {
display: block;
transition: all 0.15s cubic-bezier(1, 0, 1, 1);
transform-origin: center center -20px;
transform: translateZ(20px) rotateX(-25deg);
transform-style: preserve-3d;
background-color: #9b0621;
height: 100%;
position: relative;
cursor: pointer;
background: linear-gradient(#980000 0%, #6f0000 30%, #6f0000 70%, #980000 100%);
background-repeat: no-repeat;
}

.switch .button::before {
content: "";
background: linear-gradient(rgba(255, 255, 255, 0.8) 10%, rgba(255, 255, 255, 0.3) 30%, #650000 75%, #320000) 50% 50%/97% 97%, #b10000;
background-repeat: no-repeat;
width: 100%;
height: 50px;
transform-origin: top;
transform: rotateX(-90deg);
position: absolute;
top: 0;
}

.switch .button::after {
content: "";
background-image: linear-gradient(#650000, #320000);
width: 100%;
height: 50px;
transform-origin: top;
transform: translateY(50px) rotateX(-90deg);
position: absolute;
bottom: 0;
box-shadow: 0 50px 8px 0px black, 0 80px 20px 0px rgba(0, 0, 0, 0.5);
}

.switch .light {
opacity: 0;
animation: light-off 1s;
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(#ffc97e, #ff1818 40%, transparent 70%);
}

.switch .dots {
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(transparent 30%, rgba(101, 0, 0, 0.7) 70%);
background-size: 10px 10px;
}

.switch .characters {
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(white, white) 50% 20%/5% 20%, radial-gradient(circle, transparent 50%, white 52%, white 70%, transparent 72%) 50% 80%/33% 25%;
background-repeat: no-repeat;
}

.switch .shine {
transition: all 0.3s cubic-bezier(1, 0, 1, 1);
opacity: 0.3;
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(white, transparent 3%) 50% 50%/97% 97%, linear-gradient(rgba(255, 255, 255, 0.5), transparent 50%, transparent 80%, rgba(255, 255, 255, 0.5)) 50% 50%/97% 97%;
background-repeat: no-repeat;
}

.switch .shadow {
transition: all 0.3s cubic-bezier(1, 0, 1, 1);
opacity: 1;
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(transparent 70%, rgba(0, 0, 0, 0.8));
background-repeat: no-repeat;
}

@keyframes flicker {
0% {
  opacity: 1;
}

80% {
  opacity: 0.8;
}

100% {
  opacity: 1;
}
}

@keyframes light-off {
0% {
  opacity: 1;
}

80% {
  opacity: 0;
}
}
<!DOCTYPE html>
<html>
<head>
<style>

</style>
</head>
<body>

<div class="check-lower">
                <label class="check-display" id="display-right">Use lowercase</label>
                
<label class="switch">
    <input type="checkbox" id="lowercase" class="checks">
    <div class="button">
      <div class="light"></div>
      <div class="dots"></div>
      <div class="characters"></div>
      <div class="shine"></div>
      <div class="shadow"></div>
    </div>
  </label>
</div>

</body>
</html>

相关问题