gpt4 book ai didi

html - 更改按钮大小时的行为

转载 作者:行者123 更新时间:2023-12-01 23:05:57 24 4
gpt4 key购买 nike

我正在研究创建一个拨动开关。我希望它能够响应,以防将来我想调整它的大小。拨动开关中有一个红色方 block (用于演示),问题是当我更改拨动开关的宽度和高度时,红色方 block 会拉伸(stretch)并改变其尺寸。我想要的喜欢它做的是保持其形状并且无论拨动开关有多大或多小都不会移动。

这是一个演示:

width: 9em;
height: 4.5em;

enter image description here

/*half the size*/    
width: 4.5em;
height: 2.25em;

enter image description here

由于某种原因,红色方 block 变成了矩形并向左移动。下面是我希望它在更改宽度和高度时的样子(又名完全相同,只是更小)

enter image description here

我已经尝试过百分比、vh、最小宽度、最大宽度、em、rem、px 等等。有什么想法吗?

* {
box-sizing: border-box;
}
button {
/*the height is 50% of the width*/
width: 9em;
height: 4.5em;
border-radius: 100px 100px;
border:none;
padding-left: 1em;
}

.switch {
display: block;
width: 40%;
height: 70%;
background-color: red;
}
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet">
</head>
<body>
<button>
<span class="switch"></span>
</button>
</body>
</html>

最佳答案

如果你想保持 1:1 的纵横比,你应该尝试为 widthpadding-bottom 使用相同的值,因为你可以在这里阅读这个答案:How to style a div to be a responsive square?

这是您修改后的示例:

* {
box-sizing: border-box;
}

button {
/*the height is 50% of the width*/
width: 18em;
height: 9em;
border-radius: 100px 100px;
border: none;
}

.switch {
display: block;
height: 0;
width: 35%;
padding-bottom: 35%;
margin-left: 10%;
background-color: red;
}
<!DOCTYPE html>
<html>

<head>
<link href="style.css" rel="stylesheet">
</head>

<body>
<button>
<span class="switch"></span>
</button>
</body>

</html>

关于html - 更改按钮大小时的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70866745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com