简体   繁体   English

CSS3列中的绝对定位

[英]Absolute positioning within CSS3 columns

I'm trying to style a popup-menu that shows a submenu on hover, popping out to the right of the hovered item. 我正在尝试设置一个弹出菜单,它在悬停时显示一个子菜单,弹出到悬停项目的右侧。 My main items are split into two columns using column-count , and this is where the misery begins. 我的主要项目使用column-count分成两列,这就是痛苦开始的地方。

In Firefox, everything behaves as expected: the submenu pops out where the hovered item is. 在Firefox中,一切都按预期运行:子菜单弹出悬停项目的位置。 In Chrome, the submenu pops up relative to the leftmost column. 在Chrome中,子菜单会相对于最左侧的列弹出。

The four cases (hovered items 3 and 9, Firefox and Chrome) are shown in the attached screen. 四个案例(悬停的项目3和9,Firefox和Chrome)显示在附加的屏幕中。 Try the demo both in Firefox and Chrome to see what I mean. 在Firefox和Chrome中试用这个演示来看看我的意思。

第四个案例显示了弹出窗口的位置

Is there a neat solution for this? 对此有一个简洁的解决方案吗? I tried column-span, but that doesn't work. 我尝试了列跨度,但这不起作用。 I cannot make the item's li relative because I want the popup to fill the complete height. 我不能让这个项目的li相对,因为我希望弹出窗口填满整个高度。

 ul.first { border:1px solid #888; position:relative; display:inline-block; margin:5px; padding:0; column-count:2; -moz-column-count:2; -webkit-column-count:2; column-rule:1px solid #888; -moz-column-rule:1px solid #888; -webkit-column-rule:1px solid #888; background-color:#eee; } ul.first li { list-style:none; display:block; width:200px; background-color:#eee; margin:2px; padding:5px; } ul.first li:hover { background-color:#ddd; } ul.first > li.hassub > ul { display:none; position:absolute; margin-left:100px; top:0; bottom:0; background-color:#ddd; padding:0 5px; } ul.first > li.hassub:hover > ul { display:inline-block; } ul.first > li.hassub > ul > li { background-color:#ddd; } ul.first > li.hassub > ul > li:hover { background-color:#eeffee; } 
 <ul class="first"> <li>Item 1</li> <li>Item 2</li> <li class="hassub">Item 3 <ul> <li>Subitem 3-1</li> <li>Subitem 3-2</li> <li>Subitem 3-3</li> <li>Subitem 3-4</li> </ul> </li> <li>Item 4</li> <li>Item 5</li> <li>Item 6</li> <li>Item 7</li> <li>Item 8</li> <li class="hassub">Item 9 <ul> <li>Subitem 9-1</li> <li>Subitem 9-2</li> <li>Subitem 9-3</li> <li>Subitem 9-4</li> </ul> </li> <li>Item 10</li> </ul> 

http://jsfiddle.net/cfckw5jz/6/ http://jsfiddle.net/cfckw5jz/6/

There are many solutions: 有很多解决方案:

Solution 1: A quick solution is to Give the sub-menu inside the right column different class (hassub2), and give it different margin-left 解决方案1:快速解决方案是将右侧列中的子菜单设置为不同的类(hassub2),并给它不同的边距 - 左边

.hassup2{
  left: 390px;
}

Solution 2: A smarter solution is to give all sub-menus inside li's above 5 a different margin, this can be achieved by using nth-child: 解决方案2:更智能的解决方案是为li中的所有子菜单提供5个不同的边距,这可以通过使用nth-child来实现:

ul.first > li:nth-child(n+5) > ul{
  left: 390px;
}

n + 5 = any element above 5 (in that case all li's above 5) n + 5 =任何高于5的元素(在这种情况下,所有li都高于5)

Solution 3: You can also separate it into 2 UL's and float them left (or use display: inline-block), and assign position relative to the UL's to be the reference point for the sub-menu: 解决方案3:您也可以将它分成2个UL并向左浮动(或使用display:inline-block),并将相对于UL的位置指定为子菜单的参考点:

li{ list-style: none; }

ul.left,
ul.right{
  float: left;
  width: 200px;
  position: relative;
}

.right li,
.left li{
  float: left;
  width: 100%;
  background-color: #eee;
  margin: 2px;
  padding: 5px;
}

.right li ul,
.left li ul{
  display: none;
  position: absolute;
  left: 200px;
  top:0;
  height:100%;
}

.right li ul li,
.left li ul li{
  width: 100%;
  padding: 5px;
  margin: 2px;
}

.right li:hover ul,
.left li:hover ul{
  display: block;
}

Tip: use Left instead of margin-left for a consistent design, using left will always make the element 200px away from left side, it will not depend or get effected on any element placed before it, like what margins behave. 提示:使用Left而不是margin-left来获得一致的设计,使用left将始终使元素距离左侧200px,它将不会依赖于或者对放在它之前的任何元素产生影响,就像边缘的行为一样。

Tip: Absolute positioned element will look for the first father with a defined position and make it its reference point. 提示:绝对定位元素将查找具有已定义位置的第一个父元素,并将其作为参考点。 So to make an absolute div refer to the direct father div, the father must be given position (relative, fixed, or absolute). 因此,为了使绝对div指向直接父div,父亲必须被赋予位置(相对,固定或绝对)。

Today I realized that this issue was fixed in the latest Chrome release 55 and now Chrome behaves as the other modern browsers. 今天我意识到此问题已在最新的Chrome版本55修复,现在Chrome的行为与其他现代浏览器一样。

So, no need to make modifications to css nor html. 所以,不需要修改css或html。 Yay! 好极了!

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

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