简体   繁体   English

设置具有绝对位置的屏幕宽度的子宽度最大值

[英]Set Child width maximum of Screen width having absolute position

I have 3 drop down menus with dynamic width and position like these 我有3个下拉菜单,它们的动态宽度和位置都像这样

在此处输入图片说明

I want to make each drop down width to maximum of Screen width starting from the current position leaving 10px from right side like this. 我想使每个下拉宽度从当前位置开始,直到屏幕宽度的最大值,这样从右侧离开10px。

在此处输入图片说明

The drop down list is positioned absolute to its parent container. 下拉列表位于其父容器的绝对位置。 I want to make a single css class and apply the each drop down list so it can act like the above one which can have maximum of screen width subtracting 10px from it. 我想制作一个单独的CSS类,并应用每个下拉列表,以便它可以像上面的那样运行,最大屏幕宽度可以从中减去10px。

By searching SO, I found many questions but could not get it work in my problem. 通过搜索SO,我发现了很多问题,但是无法解决我的问题。 I don't want to do anything with Javascript but with pure CSS only. 我不想使用Javascript做任何事情,而只使用纯CSS。

Many of SO Questions suggest using vw unit of css but I don't understand how to use vw in this problem. 许多SO问题建议使用css的vw单位,但我不明白如何在此问题中使用vw。 Any help from you experts will be really appreciated. 您的专家的任何帮助将不胜感激。 Thanks. 谢谢。

Some suggest this but does not work 有些人建议这样做,但不起作用

width:100vw;
margin-left:100%;
transform: translate(-50vw);

Because above could is good if I want to make drop down in center of screen but here the situation is starting from parent position. 因为如果我想在屏幕中央下拉菜单,上面的方法可能会很好,但是这里的情况是从父位置开始的。 I have tried replacing it with margin-right and tried as much I could but no use. 我尝试用保证金权利代替它,并尽我所能,但没有用。

i don't think this can be made with a single class, you should be using a single class for each menu item. 我不认为这可以用一个类来完成,您应该为每个菜单项使用一个类。

You can first set a fixed width to top menu item, something like 100px. 您可以先为顶部菜单项设置固定宽度,例如100px。 So each item next will be 100px further from the left side of the page. 因此,下一个项目距页面左侧的距离为100px。

Now you have to make a calculation of 100% of the viewport with 100vw , and then subtract the pixels from the previous items, if the item menu has 2 items before then it will be 200 pixels. 现在,您必须使用100vw来计算视口的100%,然后从以前的项目中减去像素,如果项目菜单之前有2个项目,则它将为200像素。

This substraction can be made with calc like this: width: calc(100vw - 200px); 可以使用calc这样进行减法: width: calc(100vw - 200px);

The result with this technique would be something like this: 这种技术的结果将是这样的:

在此处输入图片说明 ` `

Here is an example: https://jsfiddle.net/52jab0t9/ 这是一个示例: https : //jsfiddle.net/52jab0t9/

I hope this help you, excuse me by my bad english :) 希望这对您有所帮助,请问我的英语不好:)

PD: I took the code for the menu from here: http://www.cssscript.com/demo/simple-clean-pure-css3-dropdown-menu/ PD:我从这里获取了菜单代码: http : //www.cssscript.com/demo/simple-clean-pure-css3-dropdown-menu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM