css - 在鼠标悬停时展开整个包装,宽度自动和高度自动

标签 css superfish

我正在使用一个菜单,它有一个 ID 为“menu_unidades”的包装器。我希望此 div 在鼠标悬停时展开后调整其尺寸。

这里是 FIDDLE

我正在使用 superfish 修改过的 css

CSS

/*** ESSENTIAL STYLES ***/
body {
font-family: Arial,Helvetica,sans-serif;
font-size: 11px;
}

#menu_unidades {
    width: auto;
    height: auto;
    position: relative;
    float: left;
padding: 10px 15px 10px 10px;
box-shadow: 0px 2px 2px rgb(102, 102, 102);
    left: 50px;
    margin-top: 10px;
    margin-bottom: 20px;
background-color: rgba(0,53,146,0.7);
}

.sf-menu, .sf-menu * {
    margin: 0;
    padding: 0;
list-style: none;
}
.sf-menu li {
position: relative;
}
  .sf-menu ul {
    position: absolute;
    display: none;
    top: 100%;
    left: 0;
z-index: 99;
}
.sf-menu > li {
float: left;
}
.sf-menu li:hover > ul,
.sf-menu li.sfHover > ul {
display: block;
}

.sf-menu a {
display: block;
position: relative;
}
.sf-menu ul ul {
top: 0;
left: 100%;
}

.sf-menu li ul {
padding: 10px 15px 10px 10px;
background-color: rgba(0,53,146,0.7);
}

.sf-menu li li {
    padding-left:10px;
    padding-right:10px;
    padding-top:1px;
padding-bottom:1px;
}

.sf-menu li ul a{
    padding:2px;
    font-size: 12px;
text-transform:none;
}

/*** DEMO SKIN ***/
.sf-menu {
    /*float: left;*/
margin-bottom: 1em;
}
.sf-menu ul {
box-shadow: 2px 2px 6px rgba(0,43,146,0.7);
min-width: 12em; /* allow long menu items to determine submenu width */
*width: 12em; /* no auto sub width for IE7, see white-space comment below */
}

.sf-menu li {
border-bottom:1px solid #fff;
white-space: nowrap; /* no need for Supersubs plugin */
*white-space: normal; /* ...unless you support IE7 (let it wrap) */
-webkit-transition: background .2s;
transition: background .2s;
}
.sf-menu ul li {
}
.sf-menu ul ul li {
}
.sf-menu li:hover,
.sf-menu li.sfHover {
background-color: rgba(0,53,146,0.7);
/* only transition out, not in */
-webkit-transition: none;
transition: none;
}

/*** arrows (for all except IE7) **/
.sf-arrows .sf-with-ul {
padding-right: 2.5em;
*padding-right: 1em; /* no CSS arrows for IE7 (lack pseudo-elements) */
}
 /* styling for both css and generated arrows */
  .sf-arrows .sf-with-ul:after {
    content: '';
    position: absolute;
    top: 50%;
    right: 1em;
    margin-top: -3px;
    height: 0;
width: 0;
/* order of following 3 rules important for fallbacks to work */
border: 5px solid transparent;
border-top-color: #dFeEFF; /* edit this to suit design (no rgba in IE8) */
border-top-color: rgba(255,255,255,.5);
 }
.sf-arrows > li > .sf-with-ul:focus:after,
.sf-arrows > li:hover > .sf-with-ul:after,
.sf-arrows > .sfHover > .sf-with-ul:after {
border-top-color: white; /* IE8 fallback colour */
}
/* styling for right-facing arrows */
.sf-arrows ul .sf-with-ul:after {
margin-top: -5px;
margin-right: -3px;
border-color: transparent;
border-left-color: #dFeEFF; /* edit this to suit design (no rgba in IE8) */
border-left-color: rgba(255,255,255,.5);
}
.sf-arrows ul li > .sf-with-ul:focus:after,
.sf-arrows ul li:hover > .sf-with-ul:after,
.sf-arrows ul .sfHover > .sf-with-ul:after {
border-left-color: white;
}

HTML

最佳答案

你有这个 css 选择器,.sf-menu ul 起初我的印象是它添加了 absolute 到所有ul 的。

然后我将其更改为 .sf-menu > ul 因此它专门针对 .sf-menu 中的 ul .

.sf-menu > ul {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 99;
}

在查看了所有 css 之后,我不是100% 确定它为什么会准确修复它。你的类在你的 css 中有一些重复,所以我不确定。

不能正确扩展的一个原因是 position: absolute。它们被带到元素之外,就好像它根本不在那里一样漂浮但更糟。

我最有根据的猜测是,如果没有 >,它会将 position: absolute 应用于类 .sf-menu。这会将整个导航从工作流程中取出,并且不会使父项扩展到内容。

现在绝对 div 是相对于 .sf-menuli 的,因为 li 是相对定位的,父级可以填充给他们。

这是 JSFIDDLE看看它的实际效果。

关于css - 在鼠标悬停时展开整个包装,宽度自动和高度自动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986235/

相关文章:

javascript - 自动打开 btn-group 下拉列表并选择第一个元素

css - 在css中将safari中的背景颜色设置为透明

html - 下拉菜单(Superfish)出现在 IE 7/8 中的 Flex Slider 后面

css - Superfish 子菜单文字和下拉菜单在右侧的定位

仅在 IE8 兼容模式和 IE7 中出现 JavaScript 错误

html - 如何对齐 div 中的 span 元素

css - 在 css 菜单上添加图标

css - 获取 xslt 中的最后一项

jquery - super 鱼 CSS : Make submenu dropdown vertically instead of horizontally