CSS: select last div with specific class [duplicate] - html

This question already has answers here:
CSS: How to say .class:last-of-type [classes, not elements!] [duplicate]
(2 answers)
Closed 3 years ago.
<ul>
<li class="class1">
<div class="custom">text1</div>
</li>
<li class="class1">
<div class="custom">text2</div>
</li>
<li class="class1">
<div class="custom">text3</div>
</li>
<li class="class1">
<div>text4</div>
</li>
<li class="class1">
<div>text5</div>
</li>
</ul>
How do I select last div with class="custom"? I have to add border-bottom to div with text3. I tried:
.class1 .custom:last-of-type {
border-bottom: 1px solid;
}
...But then border-bottom is added to all div's with class="custom".
I need CSS solution only, no JS. Also there could be any amount of <li> so I can't use :nth-child().
Update:
This layout is generated from Prime NG component <p-autocomplete>
The code is:
<p-autocomplete ....>
<ng-template let-item pTemplate="item">
<div [ngClass]="{'custom'}: item.isCustom">
{{item.name}}
</div>
</ng-template>
</p-autocomplete>

I agree with the comments saying that a pure-CSS solution is not existing, because it is not possible to aggregate special selectors like :last-of-type as for example (.class1 .custom):last-of-type (not working).
What i propose is a simple JQuery solution:
$(".class1 .custom").last().parents(".class1").addClass("custom2");
.custom2{
border-bottom: 1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="class1">
<div class="custom">text1</div>
</li>
<li class="class1">
<div class="custom">text2</div>
</li>
<li class="class1">
<div class="custom">text3</div>
</li>
<li class="class1">
<div>text4</div>
</li>
<li class="class1">
<div>text5</div>
</li>
</ul>

I'm not proud of this, but it's CSS only and it works.
.class1 .custom {
border-bottom: 1px solid;
position: relative;
}
.class1 .custom::before {
display: block;
content: '';
position: absolute;
border-top: 1px solid white;
width: 100%;
top: -1px;
}
<ul>
<li class="class1">
<div class="custom">text1</div>
</li>
<li class="class1">
<div class="custom">text2</div>
</li>
<li class="class1">
<div class="custom">text3</div>
</li>
<li class="class1">
<div>text4</div>
</li>
<li class="class1">
<div>text5</div>
</li>
</ul>

Related

CSS Full Height Sidebar

I've been researching for hours trying to figure out this issue. I want to display sidebar with 100% height responsively without position: fixed/absolute, is this possible to do it?
.sidebar {
height: 100%;
width: 40%;
border-right: none;
background: blue;
}
<div class="sidebar">
<p class="menu-section-head text-left">MENU UTAMA</p>
<nav class="sidebar-nav">
<ul>
<li>
<span>Menu 1</span>
<hr class="underline">
</li>
<li>
<span>Menu 2</span>
<hr class="underline">
</li>
<li>
<span>Menu 3</span>
<hr class="underline">
</li>
</ul>
</nav>
</div>
Use 100vh in place 100% in height.
.sidebar {
height: 100vh;
width: 40%;
border-right: none;
background: blue;
}
<div class="sidebar">
<p class="menu-section-head text-left">MENU UTAMA</p>
<nav class="sidebar-nav">
<ul>
<li>
<span>Menu 1</span>
<hr class="underline">
</li>
<li>
<span>Menu 2</span>
<hr class="underline">
</li>
<li>
<span>Menu 3</span>
<hr class="underline">
</li>
</ul>
</nav>
</div>
does switching from 100% to 100vh (viewer height) accomplish what you are looking for? % mimics the parent contianer, vh uses the window.
.sidebar {
height: 100vh;
width: 40%;
border-right: none;
background: blue;
}
<div class="sidebar">
<p class="menu-section-head text-left">MENU UTAMA</p>
<nav class="sidebar-nav">
<ul>
<li>
<span>Menu 1</span>
<hr class="underline">
</li>
<li>
<span>Menu 2</span>
<hr class="underline">
</li>
<li>
<span>Menu 3</span>
<hr class="underline">
</li>
</ul>
</nav>
</div>

How to store the | in HTML?

I was wondering what will be the best practice for me to use the | i.e. do I wrap it in a div class or store it in a span tag? Basically what I want to achieve is the following.
Ask a question | Privacy | Statement
<!DOCTYPE html>
<html>
<body>
<footer role="contentinfo">
<div class="footer-bottom hidden-print">
<div class="Container">
<div class="row">
<div class="col-12">
<ul class="list list--inline" role="menu" aria-label="Navigation">
<li role="menuitem" aria-label="Ask a question">
Ask a question |
</li>
<li role="menuitem" aria-label="Privacy">
Privacy |
</li>
<li role="menuitem" aria-label="Statement">
Statement
</li>
</ul>
</div>
</div>
</div>
</div>
</footer></body>
</html>
Please advise.
No need to have special characters at all.
Just use a right-border...
ul {
display:flex;
justify-content:center;
list-style:none;
}
li {
padding:0 1em;
}
li:not(:last-child) {
border-right:3px solid red;
}
<footer role="contentinfo">
<div class="footer-bottom hidden-print">
<div class="Container">
<div class="row">
<div class="col-12">
<ul class="list list--inline" role="menu" aria-label="Navigation">
<li role="menuitem" aria-label="Ask a question">
Ask a question
</li>
<li role="menuitem" aria-label="Privacy">
Privacy
</li>
<li role="menuitem" aria-label="Statement">
Statement
</li>
</ul>
</div>
</div>
</div>
</div>
</footer>
Alternatively, use a pseudo-element
ul {
display: flex;
justify-content: center;
list-style: none;
}
li {
padding: 0 1em;
position: relative;
}
li:not(:last-child):after {
content: "";
position: absolute;
right: 0;
top: 10%;
height: 80%;
width: 3px;
background: green;
}
<footer role="contentinfo">
<div class="footer-bottom hidden-print">
<div class="Container">
<div class="row">
<div class="col-12">
<ul class="list list--inline" role="menu" aria-label="Navigation">
<li role="menuitem" aria-label="Ask a question">
Ask a question
</li>
<li role="menuitem" aria-label="Privacy">
Privacy
</li>
<li role="menuitem" aria-label="Statement">
Statement
</li>
</ul>
</div>
</div>
</div>
</div>
</footer>
You can use CSS border-right with pseudo-selectors for this so you don't have to have the pipe (|) in your markup.
Pseudo-Selectors
:not() -- exclude what is inside the brackets
:first-child -- first of this type of descendant
:last-child -- last of this type of descendant
descendant, as in your markup, as li is the child (descendant) of ul.
ul[role="menu"].list {
list-style: none;
display: flex;
}
li[role="menuitem"]:not(:first-child) {
padding-left: 0.5rem;
}
li[role="menuitem"]:not(:last-child) {
padding-right: 0.5rem;
border-right: 1px solid grey;
}
<footer role="contentinfo">
<div class="footer-bottom hidden-print">
<div class="Container">
<div class="row">
<div class="col-12">
<ul class="list list--inline" role="menu" aria-label="Navigation">
<li role="menuitem" aria-label="Ask a question">
Ask a question
</li>
<li role="menuitem" aria-label="Privacy">
Privacy
</li>
<li role="menuitem" aria-label="Statement">
Statement
</li>
</ul>
</div>
</div>
</div>
</div>
</footer>

Target specific element if it contains another element only with CSS [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 5 years ago.
I have the following HTML setups and I'm trying to target the class="chosen-with-children" with CSS2 or CSS3 only if the div also contains the class="children". Any idea how to solve this? I hope this is possible.
Thanks in advance.
1.
<div class="wcapf-layered-nav">
<ul>
<li class="chosen-with-children">
<a class="" href=""></a>
</li>
</ul>
</div>
2.
<div class="wcapf-layered-nav">
<ul>
<li class="chosen-with-children">
<a class="" href=""></a>
<ul class="children">
<li class="chosen">
<a class="" href=""></a>
</li>
</ul>
</li>
</ul>
</div>
It is not possible using pure CSS, as explained here.
If you wanted to use jQuery you could do this:
$(".chosen-with-children").each(function() {
if($("ul", this).hasClass("children")) {
$(this).addClass("has-child-class");
}
})
.chosen-with-children.has-child-class > a {
color: red;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wcapf-layered-nav">
<ul>
<li class="chosen-with-children">
<a class="" href="">Link 1</a>
<ul class="children">
<li class="chosen">
<a class="" href="">Link 1a</a>
</li>
</ul>
</li>
<li class="chosen-with-children">
<a class="" href="">Link 2</a>
</li>
</ul>
</div>

Tree structure in html [duplicate]

This question already has answers here:
HTML tree full width hover effect
(3 answers)
Closed 6 years ago.
I need to create a tree structure using html and css.
Structure should be like this.
Current css and html for this:
ul{
list-style: none;
}
ul li{
background: #F4F4F4;
}
li.active{
background: #B7E9FB!important;
}
<ul>
<li class="drag-folder">
<a href="javascript:void(0);">
<span>TEST1.1</span>
</a>
<div class="nested-list">
<ul style="display:block">
<li class="drag-folder">
<a href="javascript:void(0);">
<span>TEST1.2</span>
</a>
<div class="nested-list">
<ul style="display:block">
<li class="drag-folder">
<a href="javascript:void(0);">
<span>TEST1.3</span>
</a>
<div class="nested-list">
<ul style="display:block">
<li class="drag-folder">
<a href="javascript:void(0);">
<span>TEST1.4</span>
</a>
<div class="nested-list">
<ul style="display:block">
<li class="drag-folder active">
<a href="javascript:void(0);">
<span>TEST1.5</span>
</a>
<div class="nested-list">
<ul style="display:block">
<li class="drag-folder">
<a href="javascript:void(0);">
<span>TEST1.6</span>
</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
This structure can have n number of nodes.
Problem:
nested elements width is less than root element width because of left padding (applied padding to display hierarchy), so on hover background color changes only for that particular width.
I want to make width of all li element to 100% of root element so on active background color will change for 100% width of display area, but need to maintain hierarchy.
I would like to have css solution for it.
Thanks in advance.
You can change your CSS like this:
ul{
list-style: none;
}
ul li{
background: #F4F4F4;
}
ul li a{
display:block;
}
li.active a{
background: #B7E9FB!important;
}

Breadcrumb in single line

I am using Twitter Breadcrumb in my html page. But the breadcrumb keeps appearing in a new line. How can I make it come next to the glyphicon?
This is the div section:
<div class="col-md-3 well pre-scrollable scroll-pane">
<span class="glyphicon glyphicon-folder-close"></span>
<ul class="breadcrumb list-unstyled">
<li ng-repeat="crumb in breadcrumb track by $index">
{{crumb}}
</li>
</ul>
<hr>
<p>
<ul ng-repeat="node in nodes" class="list-unstyled">
<li ng-repeat="value in node">{{value}}</li>
</ul>
</p>
</div>
How it looks now:
There are unwanted spaces in the top and bottom. But I want the list to appear next to the glyphicon as if they are in a new line.
Just add some CSS inline. <ul> elements are automatically block-level elements. I believe that Bootstrap automatically adds glyphicons to be display: inline-block as well so it can apply with and margins to the icon.
Fiddle for you
span.glyphicon-folder-close,
ul.breadcrumb {
display: inline;
}
<div class="col-md-3 well pre-scrollable scroll-pane">
<span class="glyphicon glyphicon-folder-close"></span>
<ul class="breadcrumb list-unstyled">
<li>node1
</li>
<li>node2
</li>
<li>node3
</li>
<li>node4
</li>
</li>
</ul>
.....
.....
Addition:
Here's another way of doing it. Wrap the span and ul in it's own separate container so they float together. You have to remove the padding for the ul.breadcrumb as it's auto applied.
Add containers fiddle
ul.breadcrumb {
padding: 0;
}
<div class="col-md-3 well pre-scrollable scroll-pane">
<div class="col-xs-1">
<span class="glyphicon glyphicon-folder-close"></span>
</div>
<div class="col-xs-11">
<ul class="breadcrumb list-unstyled">
<li>node1
</li>
<li>node2
</li>
<li>node3
</li>
<li>node4
</li>
</li>
</ul>
</div>
....
....
Addition #3 (worked best for OP)
Here's an even better way. Just use psuedo selector for the breadcrumb and remove the span altogether. Also remove the <hr> tag and add border to the breadcrumb itself
Fiddle w/ psuedo
ul.breadcrumb {
position: relative;
padding-bottom: 10px;
border-bottom: 1px solid #ccc;
}
ul.breadcrumb:before {
position: absolute;
content: '\e117';
font-family: 'Glyphicons Halflings';
left: -10px;
top: 8px;
width: 20px;
height: 20px;
}
<div class="col-md-3 well pre-scrollable scroll-pane">
<ul class="breadcrumb list-unstyled">
<li>node1
</li>
<li>node2
</li>
<li>node3
</li>
<li>node4
</li>
</li>
</ul>
....
....
It may be due to the parent div width is not enough for show all.