Show button only if sibling is not empty - html

I want to implement a list to which new items can be added with buttons. One button at the top of the list to prepend new items, another one at the bottom to append new items.
HTML:
<div>
<div id="list">
<button class="addbtn">prepend item</button>
<ul id="items"></ul>
<button class="addbtn">append item</button>
</div>
</div>
The buttons are only visible when hovering the div containing the list (via css :hover selector).
CSS:
.addbtn {
display: none;
}
#list:hover .addbtn {
display: block;
}
Is there a way (without using JS, only with CSS) to only show the append button, but not the prepend button, when the list is empty?
If the list contains at least one item, both buttons should be shown.
JSFiddle: https://jsfiddle.net/uLmzom18/
Edit: here's the JSFiddle with the working solution: https://jsfiddle.net/khu7bahm/

You can not influence with CSS an element that is upwards of the other in the DOM flow.
But you can trick it: inverse the order of the elements in the HTML, and rearrange them using flex
ul:empty ~ .addbtn {
background-color: lightgreen;
}
#list, #list2 {
display: inline-flex;
flex-direction: column-reverse;
}
<div>
<div id="list">
<button class="addbtn">prepend item</button>
<ul id="items"></ul>
<button class="addbtn">append item</button>
</div>
</div>
<div>
<div id="list2">
<button class="addbtn">prepend item</button>
<ul id="items">
<li>1</li>
<li>2</li>
</ul>
<button class="addbtn">append item</button>
</div>
</div>

If you may use display:flex for your #list, you could reverse the order of the buttons and use the css sibling selector +
See this fiddle: https://jsfiddle.net/sb7zewp6/1/
If you need more info about this I can extend this answer, but I think this is only needed if display:flex is an option.
More info about display:flex usage at caniuse.com
$(".addbtn").click(function() {
if($(this).hasClass("prepend"))
$("#items").prepend($("<li>").text("prepended item " + $("#items").children().length));
else
$("#items").append($("<li>").text("appended item " + $("#items").children().length));
});
#list {
width: 300px;
min-height: 10em;
background-color: #fff;
display:flex;
}
.addbtn {
display: none;
}
#items:empty+.addbtn {
display:none !important;
}
#list:hover .addbtn {
display: block;
}
.addbtn.append {
order:3;
}
#items {
order:2;
outline:1px solid red;
}
.addbtn.prepend {
order:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div id="list">
<button class="addbtn append">append item</button>
<ul id="items"></ul>
<button class="addbtn prepend">prepend item</button>
</div>
</div>

You cannot test the emptiness of an element using CSS as it doesn't have the possibility to traverse up. What you can do is, if you use jQuery, it is possible.
If you want a pure CSS based solution for your current HTML, then no!
However, if you are using some invalid HTML, it might work:
#items .addbtn:last-child {
display: none;
}
<div>
<div id="list">
<ul id="items">
<button class="addbtn">prepend item</button>
</ul>
<button class="addbtn">append item</button>
</div>
</div>
Here, I have given the addbtn directly inside the <ul>, which is wrong. But if you are determined, we can make it better:
#items .btnAdd {
margin-left: -40px;
margin-bottom: 20px;
}
#items .btnAdd:last-child {
display: none;
}
<p>No Prepend Button</p>
<div>
<div id="list">
<ul id="items">
<li class="btnAdd"><button class="addbtn">prepend item</button></li>
</ul>
<button class="addbtn">append item</button>
</div>
</div>
<hr />
<p>Prepend Button Shown</p>
<div>
<div id="list">
<ul id="items">
<li class="btnAdd"><button class="addbtn">prepend item</button></li>
<li>One item</li>
</ul>
<button class="addbtn">append item</button>
</div>
</div>
The final snippet is a pure CSS based solution.

Related

label element multiple inner span text truncation

Is it possible to truncate one specific inner span of a label, that contains multiple span elements, so that the label in total does not overflow into the next line?
I have prepared a JSFiddle for it on https://jsfiddle.net/keltik/k18892xe/3/, but for completeness I will also supply a part of the HTML/CSS here:
<html>
<body>
<div class="container">
<!-- First Spectre Accordion -->
<div class="container">
<div class="accordion">
<input id="accordion-1" name="accordion-radio" type="checkbox" hidden="">
<label class="accordion-header c-hand" for="accordion-1">
<i class="fas fa-angle-down"></i>
<span class="headline">some headline that needs to be truncated1, so that everything in the parent 'label' element remains in one line</span>
<span class="spacer"></span>
<span class="date">dont truncate me1</span>
</label>
<div class="accordion-body">
<ul class="menu menu-nav">
<li class="menu-item">Element 1</li>
<li class="menu-item">Element 2</li>
<li class="menu-item">Element 3</li>
</ul>
</div>
</div>
<!-- more accordions, same structure -->
</body>
</html>
The CSS file is like this, on my development machine I am using Scss though:
.container {
max-width: 400px;
}
I am using the spectre.css framework and it is already included in the aforementioned JSFiddle link.
I have already tried out these approaches, but could not get any of them working with label, multiple span elements and the specific spectre.css classes:
https://scottwhittaker.net/flexbox/2017/02/05/flexbox-and-text-truncation.html
https://westerndevs.com/css/Using-Overflow-Ellipsis-in-Inline-Flex/
How to use "text-overflow: ellipsis" with a label element?
I am looking for approaches using HTML/CSS without Javascript, if it is possible.
I would appreciate your help.
You can try the use of white-space:nowrap and flexbox like this:
label {
display: flex;
max-width: 400px;
border: 1px solid;
overflow: hidden;
}
label > span {
white-space: nowrap;
flex-shrink: 0; /*This span will never shrink*/
margin: 0 5px;
}
span.tru {
flex-shrink: 1;/*allow this one to shrink*/
/*Hide the overflow*/
overflow: hidden;
text-overflow: ellipsis;
}
<label>
<span>lorem don't truncate</span>
<span class="tru"> truncate me truncate me truncate me truncate me truncate me v truncate me truncate me truncate me</span>
<span>don't truncate me</span>
</label>

Hover button change css of other class

In the following html the .pricing-item has a border which appears when it is hovered (.pricing-item:hover::after). I am trying to set it so that when the button (pricing-button btn-primary) is hovered, the css for the .pricing-item border is set to '0px;'.
<div class="pricing-item">
<div class="pricing-icon"></div>
<h3 class="pricing-title">Title</h3>
<div class="pricing-price"><span class="pricing-currency">$</span>200<span class="pricing-period">/ year</span></div>
<ul class="pricing-feature-list">
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
</ul>
<button class="pricing-button btn btn-primary">Choose plan</button>
In css file tried using ~ or + to make one class affect the other but just not getting it right. Any help would be appreciated. Thanks.
Here is a trick you can do, to achieve what you want.
.pricing-item-wrapper {
position: relative;
}
.pricing-item {
padding-bottom: 30px;
border: 1px solid red;
z-index: 1;
}
.pricing-button {
position: absolute;
bottom: 10px;
left: 1px;
z-index: 2;
}
.pricing-button:hover + .pricing-item {
border: 1px solid transparent;
}
<div class="pricing-item-wrapper">
<button class="pricing-button btn btn-primary">Choose plan</button>
<div class="pricing-item">
<div class="pricing-icon"></div>
<h3 class="pricing-title">Title</h3>
<div class="pricing-price">
<span class="pricing-currency">$</span>
200
<span class="pricing-period">/ year</span>
</div>
<ul class="pricing-feature-list">
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
<li class="pricing-feature">Feature</li>
</ul>
</div>
</div>
With your current HTML code, the short answer is : you can't.
The reason is : you try to access a parent element with CSS, which is not possible.
<div class="pricing-item">
...
<button class="pricing-button btn btn-primary">Choose plan</button>
</div>
You'll have to change something in your HTML, e.g :
<div class="pricing-item">
...
<button class="pricing-button btn btn-primary">Choose plan</button>
<div class="pricing-item-border"></div>
</div>
So it will make things a lot easier :
.pricing-item:hover .pricing-item-border { /* Display the border */}
.pricing-item:hover .pricing-button:hover + .pricing-item-border { /* Hide the border */}
Unfortunately no CSS selectors can go up the dom.. For this you'd have to use JS.
You could add that hover effect on pricing-item but not from just hovering on the button within that element.
A simple JS version would be:
$('.pricing-button.btn-primary').hover(
function() {
$(this).parent().css('border', '2px solid black');
},
function() {
$(this).parent().css('border', 'none');
}
);
CSS
.pricing-item.no-border{
border: 0px;
}
JS
$(".pricing-button".on({
mouseenter: function () {
//stuff to do on mouse enter
$(this).parents(".pricing-item").addClass("no-border");
},
mouseleave: function () {
//stuff to do on mouse leave
$(this).parents(".pricing-item").removeClass("no-border");
}
});

Overlay a div inside a list item

I'm trying to implement auto-complete inside a form item, where as the user types it creates a dropdown menu with a list of suggestions, which are clickable. This is done inside the Ionic Framework.
I've made a codepen to demonstrate what I want. (look at the auto-complete field, and the grey hidden box below it)
http://codepen.io/pbernasconi/pen/Cgobi
My dropdown:
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">License #</span>
<input type="text" placeholder="AUTO COMPLETE FIELD">
<div class="input-dropdown">
<ul class="input-dropdown-menu">
<li>...</li>
</ul>
</div>
</label>
</div>
My CSS:
.input-dropdown {
position: absolute;
background: grey;
border: solid 1px #000;
z-index: 1001;
overflow: visible;
}
.input-dropdown-menu {
}
This issue is that position: absolute doesn't allow me to overlay over the list item below the auto-complete field, as you can see in the codepen.
Here's an example of a solution, which for some reason doesn't work for me.
Does anyone know how to implement this dropdown to overlay over it's parent's?
The label item overflow is hidden and the dropdown list is inside it, so you can't see it.
// jquery code
$(document).ready(function() {
$("#test").focus(function(){
$(".input-dropdown-menu").show();
});
$("#test").mouseleave(function(){
$(".input-dropdown-menu").hide();
});
});
//use css
input-dropdown {
position: absolute;
background: grey;
border: solid 1px #000;
z-index: 1001;
overflow: visible;
margin-left:65px;
}
.input-dropdown-menu {
display:none;
}
//use html
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">License #</span>
<input type="text" placeholder="AUTO COMPLETE FIELD" id ="test">
<div class="input-dropdown">
<ul class="input-dropdown-menu">
<li>111</li>
<li>111</li>
<li>111</li>
</ul>
</div>

How to disable div or jstree inside div?

Can anyone tell me how to disable a div or elements inside a div?
I have a jstree in my div and I want to disable the div/jstree.
Thanks!
I made a simple example for you here
Basically, it creates a div over your jstree's one so that it is disabled from user interactions.
I guess you can make it visually better, but i think this gives you the idea.
I also checked that there is no strigth way to disable a jstree, even if it could be usefull.
Maybe you'd want to ask the dev in google group...
HTML Code:
<button id="disable">Disable</button>
<button id="enable">Enable</button>
<div id="jstree-wrapper">
<div id="demo" style="height:100px;">
<ul>
<li id="node_1_id">
<a>Root node 1</a>
<ul>
<li id="child_node_1_id">
<a>Child node 1</a>
</li>
<li id="child_node_2_id">
<a>Child node 2</a>
</li>
</ul>
</li>
</ul>
<ul>
<li><a>Team A's Projects</a>
<ul>
<li><a>Iteration 1</a>
<ul>
<li><a>Story A</a></li>
<li><a>Story B</a></li>
<li><a>Story C</a></li>
</ul>
</li>
<li><a>Iteration 2</a>
<ul>
<li><a>Story D</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div id="disabler"></div>
</div>​
CSS code:
#jstree-wrapper {
position: relative;
}
#disabler {
position: absolute;
top: 0px;
left: 0px;
background-color: black;
opacity: 0.5;
}​
JS Code:
$(document).ready(function() {
$("#demo").jstree();
$("#disable").on("click", function() {
$("#disabler").css("width", $("#demo").width());
$("#disabler").css("height", $("#demo").height());
});
$("#enable").on("click", function() {
$("#disabler").css("width", "0px");
$("#disabler").css("height", "0px");
});
});
Here's a very good example I didn't find done so simple and good anywhere else. In this example you can simulate the 'disabled' attribute only by adding CSS style as I entered in the code snippet. It disables by using the CSS "visible:hidden", and adds a translucent mask to cover the whole div area and disable anything inside it. You can choose to comment out the 'Visibility:hidden' to be able to see the elements behind the mask, but then they will be tabable, if you don't mind them hidden then uncomment that style.
function disable(elementId, enabling) {
el = document.getElementById(elementId);
if (enabling) {
el.classList.remove("masked");
} else
{
el.classList.add("masked");
}
}
.masked {
position: relative;
pointer-events: none;
display: inline-block;
//visibility:hidden; /* Uncomment this for complete disabling */
}
.masked::before {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
visibility: visible;
opacity: 0.2;
background-color: black;
content: "";
}
<button onclick="alert('Now, click \'OK\' then \'Tab\' key to focus next button.\nThen click \'Enter\' to activate it.');">Test</button>
<div id="div1" style="display:inline-block" class="masked">
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<br/>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
</div>
<button>Dummy</button>
<br/>
<br/>
<br/>
<button id="enableBtn" onclick="disable('div1',true);disable('enableBtn',false);disable('disableBtn',true);">Enable</button>
<button id="disableBtn" onclick="disable('div1',false);disable('enableBtn',true);disable('disableBtn',false);" class="masked">Disable</button>

How to make UL Tabs with only HTML CSS

Trying to figure out how to do this. I have the style but I'd like something to happen after I click the tabs. I would like the div with the tab class names to show and hide when i click the tabs. I'm assuming how that would work. Right now when I click the tabs nothing happens.
Here's my HTML
<style type="text/css">
ul.tabs {
display: table;
list-style-type: none;
margin: 0;
padding: 0;
}
ul.tabs>li {
float: left;
padding: 10px;
background-color: lightgray;
}
ul.tabs>li:hover {
background-color: lightgray;
}
ul.tabs>li.selected {
background-color: lightgray;
}
div.content {
border: 1px solid black;
}
ul { overflow: auto; }
div.content { clear: both; }
</style>
<body>
<ul class="tabs">
<li>Description</li>
<li>Specs</li>
</ul>
<div class="pane">
<div class="tab1">
<div><h2>Hello</h2></div>
<div />
<div>Hello hello hello.</div>
<div />
<div>Goodbye goodbye, goodbye</div>
<div />
<div />
</div>
<div class="tab2" style="display:none;">
<div><h2>Hello2</h2></div>
<div />
<div>Hello2 hello2 hello2.</div>
<div />
<div>Goodbye2 goodbye2, goodbye2</div>
<div />
</div>
</div>
<div class="content">
This should really appear on a new line.
</div>
</body>
Standard answer: you can't. There is no way to do this with purely HTML/CSS2, unfortunately. We can make drop-downs in CSS with the :hover psuedo-class, but there's no equivalent for clicks. Look into one of these Javascript-based solutions.
Secret answer: CSS3 [kind of] supports this. But you have to create radio buttons [weird], and it's not supported in IE7/8. If you dare...
And if you don't mind using Javascript, here's a quick solution. Reformatted your HTML, first of all. No need to put <h2>s in <div>s, and use <br /> for breaks—that's what it's there for. Also, I changed the tab <div>s to use id's instead of classes. If you have unique identifiers for an element, use id.
<ul class="tabs">
<li>Description</li>
<li>Specs</li>
</ul>
<div class="pane">
<div id="tab1">
<h2>Hello</h2>
<p>Hello hello hello.</p>
<p>Goodbye goodbye, goodbye</p>
</div>
<div id="tab2" style="display:none;">
<h2>Hello2</h2>
<p>Hello2 hello2 hello2.</p>
<p>Goodbye2 goodbye2, goodbye2</p>
</div>
</div>
<div class="content">This should really appear on a new line.</div>
Didn't touch your CSS.
For Javascript, I recommend using jQuery. It really simplifies things.
All you need are these lines of code:
$(document).ready(function() {
$("ul.tabs a").click(function() {
$(".pane div").hide();
$($(this).attr("href")).show();
});
})
Basically, once the page is ready [has loaded], look for every link that's a child of a tabs ul. Attach a function that runs each time this link is clicked. When said link is clicked, hide all the tabs in the .pane div. Then, use the link's href to find the proper tab div and show it.
fiddle: http://jsfiddle.net/uFALn/18/
Because of the floated <li> elements your <ul> element is zero height.
Try adding ul { overflow: auto; } and div.content { clear: both; } to your CSS
Thanks benesch. It helped me too.
One can also add return false to prevent that jerky jump to the anchor. For instance:
$("ul.tabs a").click(function() {
$(".pane div").hide();
$($(this).attr("href")).show();
return false;
});