Reputation: 27
I have a css onclick dropdown menu that will have dynamic content in it, so when it is a very long string, I need it to truncate or hide. I tried 'overflow: hidden' but it breaks the dropdown so that the submenu doesn't dropdown anymore.
I hope I've explained my issue properly!
My 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>
My 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);
}
EDIT:
Sorry, my first time posting on this site... hopefully this is more clear.... Please ignore the above code and check out my fiddle here instead: https://jsfiddle.net/xw7hzmky/6/
I want to apply overflow: hidden; and text-overflow: ellipsis; (or clip) to the ul#menu, and while it does what I want, it ends up hiding my dropdown as well. I need to keep the width: auto; and max-width: 85%; so that it resizes depending on the string, looking nice when its short, and not going beyond a certain point (85%) if its long... at that point, I am hoping to truncate it, hence my issues. Any help would be greatly appreciated! Thank you so much in advance!
EDIT:
Thanks so much for the help everyone, @Mridul Kashyap solved it for me. Thanks so much for all your time!
Upvotes: 0
Views: 1399
Reputation: 700
check if this works for you:
/*----- 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>
I just added widths to necessary elements. and some overflow
, whitespace
, and text-overflow
properties.
Upvotes: 0
Reputation: 3868
You can use CSS3 text-overflow property
#div1 {
white-space: nowrap;
width: 12em;
overflow: hidden;
text-overflow: clip;
border: 1px solid #000000;
}
#div2 {
white-space: nowrap;
width: 12em;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid #000000;
}
<p>The following two divs contains a long text that will not fit in the box. As you can see, the text is clipped.</p>
<p>This div uses "text-overflow:clip":</p>
<div id="div1">This is some long text that will not fit in the box</div>
<p>This div uses "text-overflow:ellipsis":</p>
<div id="div2">This is some long text that will not fit in the box</div>
Added for your drop down https://codepen.io/anon/pen/dXALwg
Upvotes: 2
Reputation: 209
Try a fixed "width" for "ul.submenu" and add "text-overflow" to "ul.submenu li a". Like this,
ul.submenu {
width : 150px;
}
ul.submenu li a {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Upvotes: 0