需要仅在CSS onclick下拉菜单中隐藏溢出文本,但这样做会破坏功能

梅尔

我有一个css onclick下拉菜单,其中包含动态内容,因此当它是一个很长的字符串时,我需要它来截断或隐藏。我尝试了“溢出:隐藏”,但它中断了下拉菜单,因此子菜单不再下拉。

我希望我已经正确解释了我的问题!

我的html:

<ul id="menu">
    <li>
        <input id="check01" type="checkbox" name="menu"/>
        <label for="check01">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</label>
        <ul class="submenu">
            <li><a href="#">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</a></li>
            <li><a href="#">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</a></li>
        </ul>
    </li>
    </ul>

我的CSS:

/*Style for the first level menu bar*/
ul#menu{
    position:fixed;
    top:0;
    height:3em;
    border: 1px solid #fff;
    border-radius: 3px;
    color: #fff;
    float: left;
    margin-right: 1em;
    position: relative;
    display: inline-block;
    vertical-align: middle;
    font-size: .9em;
    max-width: 85%;
    width: auto;
}

ul#menu > li{
    float:left;
    list-style-type:none;
    position:relative;
}

label{
    position:relative;
    display:block;
    padding:0 18px 0 12px;
    line-height:3em;
    transition:background 0.3s;
    cursor:pointer;
}

label:after{
    content:"";
    position:absolute;
    display:block;
    top:50%;
    right:5px;
    width:0;
    height:0;
    border-top:4px solid rgba(255,255,255,.5);
    border-bottom:0 solid rgba(255,255,255,.5);
    border-left:4px solid transparent;
    border-right:4px solid transparent;
    transition:border-bottom .1s, border-top .1s .1s;
}

label:hover,
input:checked ~ label{
    background:rgba(0,0,0,.3);
}

input:checked ~ label:after{
    border-top:0 solid rgba(255,255,255,.5);
    border-bottom:4px solid rgba(255,255,255,.5);
    transition:border-top .1s, border-bottom .1s .1s;
}

/*hide the inputs*/
input{
    display:none
}

/*show the second levele menu of the selected voice*/
input:checked ~ ul.submenu{
    max-height:300px;
    transition:max-height 0.5s ease-in;
}

/*style for the second level menu*/
ul.submenu{
    max-height:0;
    padding:0;
    overflow:hidden;
    list-style-type:none;
    background:#444;
    box-shadow:0 0 1px rgba(0,0,0,.3);
    transition:max-height 0.5s ease-out;
    position:absolute;
    min-width:100%;
}

ul.submenu li a{
    display:block;
    padding:12px;
    color:#ddd;
    text-decoration:none;
    box-shadow:0 -1px rgba(0,0,0,.5) inset;
    transition:background .3s;
    white-space:nowrap;
}

ul.submenu li a:hover{
    background:rgba(0,0,0,.3);
}

编辑:

抱歉,我第一次在此站点上发布...希望这更加清晰...。请忽略上面的代码,而是在这里查看我的小提琴:https : //jsfiddle.net/xw7hzmky/6/

我想应用溢出:隐藏;和文本溢出:省略号;(或剪辑)到ul#菜单,虽然它可以执行我想要的操作,但最终也隐藏了我的下拉菜单。我需要保持宽度:自动;最大宽度:85%;以便根据字符串调整大小,短时看起来不错,并且长时不会超出某个点(85%)...在那一点上,我希望截断它,因此是我的问题。任何帮助将不胜感激!提前非常感谢您!

编辑:

非常感谢大家的帮助,@ Mridul Kashyap为我解决了这个问题。非常感谢您的光临!

Mridul Kashyap

检查是否适合您:

/*----- Dropdown -----*/
/*Style for the first level menu bar*/
ul#menu{
  position:fixed;
  top:0;
  max-width:85%;	
  height:3em;
  padding:0;
  color: #141c30;
  display: inline-block;
  vertical-align: middle;
  font-size: .9em;
  -webkit-transition: all 0.10s ease-in-out;
  -moz-transition: all 0.10s ease-in-out;
  transition: all 0.15s ease-in-out;
  background: #cbced0;
}
ul#menu > li{
  width:100%;
  float:left;
  list-style-type:none;
}
ul#menu:hover{
	color: #38a3af;
}
label{
	position:relative;
	display:block;
	line-height:3em;
	transition:background 0.3s;
	cursor:pointer;
  padding-left: 12px;
  padding-right: 20px;
  max-width:85%;
  overflow:hidden;
  whitespace:nowrap;
  text-overflow:ellipsis;
}

label:after{
	content:"";
	position:absolute;
	display:block;
	top:50%;
	right:5px;
	width:0;
	height:0;
	border-top:4px solid rgba(0,0,0,.5);
	border-bottom:0 solid rgba(0,0,0,.5);
	border-left:4px solid transparent;
	border-right:4px solid transparent;
	transition:border-bottom .1s, border-top .1s .1s;
}


input:checked ~ label:after{
	border-top:0 solid rgba(255,255,255,.5);
	border-bottom:4px solid rgba(255,255,255,.5);
	transition:border-top .1s, border-bottom .1s .1s;
}

/*hide the inputs*/
input{display:none}

/*show the second levele menu of the selected voice*/
input:checked ~ ul.submenu{
	max-height:300px;
	transition:max-height 0.5s ease-in;
}

/*style for the second level menu*/
ul.submenu{
  max-width:85%;
	max-height:0;
	padding:0;
	overflow:hidden;
	list-style-type:none;
	background:#26162f;
	box-shadow:0 0 1px rgba(0,0,0,.3);
	transition:max-height 0.5s ease-out;
	position:absolute;
	-webkit-transition: all 0.10s ease-in-out;
	-moz-transition: all 0.10s ease-in-out;
	transition: all 0.15s ease-in-out;
}

ul.submenu li{
  max-width:100%;
}

ul.submenu li a{
  max-width:100%;
	display:block;
	padding:12px;
	color:#ddd;
	text-decoration:none;
	box-shadow:0 -1px rgba(0,0,0,.5) inset;
	transition:background .3s;
	white-space:nowrap;
}

ul.submenu li a:hover{
	background:#371f44;
	color: #38a3af;

}

ul.submenu li a{
  overflow:hidden;
  whitespace:nowrap;
  text-overflow:ellipsis;
}
<ul id="menu">
							<li>
								<input id="check01" type="checkbox" name="menu"/>
								<label for="check01">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</label>
								<ul class="submenu">
								<li><a href="#">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</a></li>
								<li><a href="#">fadfadfadakldjfkwpoerikawekrlqkawejndzay6yai1keysb5tz6ptpcjxr1rwcadfa56fgsdf98hrjsijsgfsfgi</a></li>
								</ul>
							</li>
							</ul>

我只是在必要的元素上增加了宽度。有的overflowwhitespacetext-overflow性能。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CSS溢出滚动仅在需要时显示

来自分类Dev

CSS溢出Y:如何仅在需要时显示

来自分类Dev

CSS溢出Y:如何仅在需要时显示

来自分类Dev

CSS仅在需要时截断嵌入式文本

来自分类Dev

使用JavaScript隐藏CSS下拉菜单

来自分类Dev

CSS中的下拉菜单

来自分类Dev

需要jQuery下拉菜单才能仅在有人单击时关闭

来自分类Dev

Lua“需要”,但文件仅在内存中

来自分类Dev

CSS下拉菜单中的第3级需要更智能的垂直对齐

来自分类Dev

我需要下拉菜单分别显示和隐藏列

来自分类Dev

容器导航下拉菜单的onClick问题(需要双击下拉菜单)

来自分类Dev

CSS仅在Chrome中可用

来自分类Dev

下拉菜单在CSS中崩溃

来自分类Dev

CSS下拉菜单-子菜单悬停文本颜色继承

来自分类Dev

需要使用JavaScript选择所选下拉菜单的文本

来自分类Dev

CSS下拉菜单-Radius仅在最左边缘和右边缘

来自分类Dev

为什么我的CSS不能仅在移动设备的Opera中下拉菜单工作?

来自分类Dev

CSS下拉菜单

来自分类Dev

CSS下拉菜单无法正确隐藏/显示

来自分类Dev

带有折叠/隐藏选项的纯 CSS 下拉菜单

来自分类Dev

下拉菜单需要协助

来自分类Dev

将CSS Hover转换为JS onClick的下拉菜单?

来自分类Dev

将CSS Hover转换为JS onClick的下拉菜单?

来自分类Dev

CSS或jQuery:<select>下拉菜单中的两种不同颜色的文本

来自分类Dev

CSS菜单下拉菜单

来自分类Dev

仅CSS和HTML的功能下拉菜单?

来自分类Dev

SwiftUI-文本minimumScaleFactor不会仅在需要时缩放

来自分类Dev

uilabel文本仅在需要时使用换行符

来自分类Dev

下拉菜单文本对齐,css 工作但引导程序失败?