在 CSS 中使用百分比边距但想要最小边距(以像素为单位)?
我已经设置了 margin: 0 33% 0 0;
但我还想确保边距至少某个 px
金额。我知道 CSS 中没有 min-margin
所以我想知道这里是否有任何选项?
I have set a margin: 0 33% 0 0;
however I would also like to make sure the margin is at least a certain px
amount. I know there is no min-margin
in CSS so I was wondering if I have any options here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这里真正的解决方案是使用媒体查询断点来确定 33% 何时不再适合您,并且应该被最小边距(以像素为单位)覆盖。
在上面的代码中,将
max-width
更改为任意屏幕宽度,33% 的右边距不再适合您。The true solution here is to use a media query break-point to determine when 33% no longer works for you and should be overridden by the minimum margin in pixels.
In the above code, change the
max-width
to whatever screen width the 33% right margin no longer works for you.在元素右侧放置一个带有 %
width
和静态min-width
的div
。Place a
div
with a %width
and a staticmin-width
to the right of your element.你可以试试这个
you can try this
我知道游戏已经很晚了,但是你尝试过吗?
其中 20px 是您想要的至少一定数量的像素。因此边距将保持流动,但永远不会低于20px。
希望有帮助!
I know it's late in the game, but have you tried this?
where 20px is whatever you want to be at least a certain number of pixels. So the margin will stay fluid but will never fall under 20px.
Hope it helps!
你可以使用这样的东西:
You may use something like this:
这个怎么样?
How about this?
如果您使用
span
,那么您必须这样做:工作演示
如果你使用
div
,你可以这样做:if you are using
span
than u have to do like this :working demo
if you are using
div
than u can do this :我已经使用了上述几个解决方案,但在流畅且真正响应的设置中,我相信最好的选择是在相应的容器/包装器上设置适当的填充。例子:
样式:
现在尝试各种窗口宽度,并尝试各种字体大小。
另请尝试工作演示。
I've played with a couple of the aforementioned solutions, but in a fluid and truly responsive setting, I believe the best option is to set the proper padding on the respective container/wrapper. Example:
Style:
Now play around with various window widths, and also try various font sizes.
Also try the working demo.
您可以将您的项目保存在“容器”div 中,并将填充设置为您想要的“最小边距”。它可能不完全是您正在寻找的,但它给您一种最小边距大小的感觉。
然后是CSS:
You could keep your items in a "container" div and set a padding exact to "min-margin" you'd like to have. It might not be exactly what you're looking for, but it gives you that sense of minimum margin size.
Then the CSS:
据我了解,您可以在元素周围放置一个 div,它定义了填充。外部元素的填充类似于内部元素的边距。
想象一下你至少想要 1px 的边距:
编辑:这就像 Imigas 的答案,但我认为更容易理解。
As far as I understand, you can place a div around your element, that defines a padding. A padding of an outer element is like the margin of an inner element.
Imagine you want at least a margin of 1px:
edit: this is like Imigas's answer, but I think easier to understand.
还可以测试屏幕宽度/高度的一定百分比是否小于像素长度。
下面是一个使用 JavaScript 的简单解决方案:
It is also possible to test if a certain percentage of the screen width/height is smaller than a length in pixels.
Here is a simple solution for this using JavaScript: