using + with > selector - css

I am learning responsive menus and by googling, i got the hamburger checkbox hack.
What i am trying to do is show only direct descendants by clicking the hamburger and hide the sub menus.
#toggle-menu {
cursor: pointer;
}
#primary-nav,
#menu-toggle,
#primary-nav>ul {
display: none;
}
#menu-toggle:checked+#primary-nav {
display: block;
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet" />
<div class="menu">
<a href="#">
<h1>Company</h1>
</a>
<label for="menu-toggle" id="toggle-menu"><i class="far fa-bars"></i></label>
<input type="checkbox" id="menu-toggle">
<ul id="primary-nav">
<li>home</li>
<li>dropdown
<ul>
<li>sub1</li>
<li>sub2</li>
</ul>
</li>
</ul>
</div>
Any help will be appreciated

To only show the ul after the input, you need to hide all uls, then only show the ul directly after a checked input
You can then add a class on all the inputs and do the same for the submenu.
#toggle-menu {
cursor: pointer;
}
.toggler, /*checkboxes a class of toggler and hide */
ul { /* hide all menus (you may want to give them all a class) */
display: none;
}
.toggler:checked+ul { /* only show a ul if it is directly after a checked toggler input */
display: block;
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet" />
<div class="menu">
<a href="#">
<h1>Company</h1>
</a>
<label for="menu-toggle" id="toggle-menu"><i class="far fa-bars"></i></label>
<input type="checkbox" id="menu-toggle" class="toggler">
<ul id="primary-nav">
<li>home</li>
<li>
<label for="sub-menu1">dropdown</label> <!-- use a label and target the checkbox below -->
<input type="checkbox" class="toggler" id="sub-menu1"> <!-- add this -->
<ul>
<li>sub1</li>
<li>sub2</li>
</ul>
</li>
</ul>
</div>

Related

How to highlight selected menu item in css only?

I need to get the menu item to remain highlighted when a user clicks on it, of each different page..? It must be CSS only.
CSS:
.topplinker span:hover, .index .shjem, .kontakt .skontakt, .byggdrifter .sbyggdrifter, .om .som {
background-color: #fffcd9;
border-radius: 0 10px 0 0;}
HTML:
<body class="index"><p class="topplinker">
<span class="shjem">Hjem</span>
<span class="skontakt">Kontakt</span>
<span class="sbyggdrifter">Byggdrifter</span>
<span class="som">Om</span>
</p>
So now the code depends on the body class to be unique for each page to work. But I wants it to be working without using the body class to highlight each menu item. Any suggestions..?
Best regards
use ul and li.
then use onclick function to make li active.
Try this one with CSS, If you click on the URL i just change color.
FYI, if pages loads while clicking those url means you have right based on Router URL only.
#toggle1:checked ~ .control-me,
#toggle2:checked ~ .control-me,
#toggle3:checked ~ .control-me,
#toggle4:checked ~ .control-me {
background: violet;
}
.menuitem {
visibility: hidden;
}
<body class="index">
<p class="topplinker">
<a href="javascript:;">
<label>
<input type="radio" name="menuitem" id="toggle1" class="menuitem" />
<span class="control-me shjem">Hjem</span>
</label>
</a>
<a href="javascript:;">
<label>
<input type="radio" name="menuitem" id="toggle2" class="menuitem" />
<span class="control-me skontakt">Kontakt</span>
</label>
</a>
<a href="javascript:;">
<label>
<input type="radio" name="menuitem" id="toggle3" class="menuitem" />
<span class="control-me sbyggdrifter">Byggdrifter</span>
</label>
</a>
<a href="javascript:;">
<label>
<input type="radio" name="menuitem" id="toggle4" class="menuitem" />
<span class="control-me som">Om</span>
</label>
</a>
</p>
</body>
CSS only solution:
.topplinker {
display: flex;
justify-content: space-evenly;
}
a {
text-decoration: none;
color: black;
}
a:active, a:hover {
color: green;
font-weight: bold;
}
/* EDIT */
a:visited {
color: blue;
}
<p class="topplinker">
<span class="shjem">Hjem</span>
<span class="skontakt">Kontakt</span>
<span class="sbyggdrifter">Byggdrifter</span>
<span class="som">Om</span>
</p>
See for more selectors: https://www.w3schools.com/css/css_link.asp

CSS only menu, click to disappear

I have a menu structured like this:
<div class="nav">
<div class="drnav">
<ul class="ulMenu">
<li>
<div class="menuHeader">My Home</div>
<div class="menu-content">
<ul>
<li>item1</li>
<li>item3</li>
</ul>
</div>
</li>
<li>
<div class="menuHeader">My Stuff</div>
<div class="menu-content">
<ul>
<li>item4</li>
<li>item6</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
My css is setup so that when you hover over a menuHeader element the menu-content element is displayed (i.e. display: inline). This all works fine but what I want is that when you click one of the links in the list item elements within the menu-content that the menu (i.e. the parent menu-content element) disappears. Of course I want to do this without any JavaScript. I saw one example that used pointer-events but that restricts use to IE 11 and I'd like to support at least IE 10 if not 9 as well. Any suggestions on how to get this to work?
Technically it's possible, but it's much ado about nothing (hard to use it in practice):
.ulMenu .menu-content {
display: none;
}
.ulMenu > li:hover .menu-content {
display: inline-block;
}
.ulMenu > li .menu-content:target {
display: none;
}
<div class="nav">
<div class="drnav">
<ul class="ulMenu">
<li>
<div class="menuHeader">My Home</div>
<div class="menu-content" id="menuContent_1">
<ul>
<li>item1</li>
<li>item3</li>
</ul>
</div>
</li>
<li>
<div class="menuHeader">My Stuff</div>
<div class="menu-content" id="menuContent_2">
<ul>
<li>item4</li>
<li>item5</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
Besides, once you close a menu the only way to reopen it is by opening another and hovering the initial one.
Important note:
I would like to point out having a :hover based menu is a huge disadvantage compared to having a JavaScript based menu. Because more than half of today's traffic is coming from touch devices (and you don't hover much on a touch-device, do you?) while only less than 1% of traffic has JavaScript disabled.
So, could you perhaps explain why you ask for a pure CSS solution? The only practical use for pure CSS I had in past 8 years was for a payment gateway page, where JavaScript was strictly off. But, other than that?
I happen to know my way around CSS, but I was never keen on trying to transfer DOM manipulations to CSS, instead of leaving them for JavaScript. After all, that's what JavaScript is for. Use the right tool for the job. The job here is DOM manipulation. So use JavaScript.
Here's is the input/label solution I described in the comments. I realized they don't have to be checkboxes, I can use the :focus state to hide the menu contents. It's still buggy, in the sense that a click anywhere in the page is needed to make the :hover work again for the recently closed menu. But it's the closest you can get with CSS only or, at least, that's what I think.
.menuHeader input:focus + label,
.menuHeader label {
display: none;
}
.menuHeader:hover label
{
display: inline-block;
}
input.hidden {
position: absolute;
opacity: 0;
pointer-events: none;
}
<ul class="ulMenu">
<li>
<div class="menuHeader">
<div>My Home</div>
<input id="menuContent_1" class="hidden" type="text" />
<label class="menu-content" for="menuContent_1">
<ul>
<li>item1</li>
<li>item2</li>
</ul>
</label>
</div>
</li>
<li>
<div class="menuHeader">
<div>My Stuff</div>
<input id="menuContent_2" class="hidden" type="text" />
<label class="menu-content" for="menuContent_2">
<ul>
<li>item4</li>
<li>item5</li>
</ul>
</label>
</div>
</li>
</ul>
Changed the HTML tags not for semantics but for clarity. I find Nested lists easier to visualize if every other level is represented by <dl>, <dt>, <dd> list elements.
Used hidden radio inputs since it's the easiest way to maintain a 'state' (ex. 'on' and 'off') indefinitely using only CSS.
Targeting elements is done by pairing off groups of <label>s and <input type="radio">s.
Used the visibility property because it's ability to keep children elements of an element with visibility: hidden visible if said child had visibility: visible explicitly set.
I'm not sure what use it really is to remove the parent of a menu and wasn't sure if OP wanted the parent back or not. So the headings aren't really gone when the menu items are clicked, they are just invisible. If you want them back, just click the space above the lists.
SNIPPET
body {
background: #222;
}
li {
text-decoration: none;
display: inline-block;
cursor: pointer;
padding: 3px;
margin-bottom: 12px;
}
.rad {
display: none;
}
.rad + label,
dd {
visibility: hidden;
}
dl:hover dd,
.rad:checked + label {
cursor: pointer;
visibility: visible;
color: #fc2;
}
dd label:hover {
background: #930;
border: .5px solid cyan;
}
<nav class="mainNav">
<div class="drNav">
<ul class="mainMenu">
<li>
<dl class="menuContent">
<input id='rad0' class='rad' name='radA' type='radio' checked>
<label for='rad0'>
<dt class="menuHeader">HOME___</dt>
</label>
<dd>
<label for='rad1'>
<input id='rad1' class='rad' name='radA' type='radio'>Item1
</label>
</dd>
<dd>
<label for='rad2'>
<input id='rad2' class='rad' name='radA' type='radio'>Item2
</label>
</dd>
</dl>
</li>
<li>
<dl class="menuContent">
<input id='rad3' class='rad' name='radB' type='radio' checked>
<label for='rad3'>
<dt class="menuHeader">CONTENT</dt>
</label>
<dd>
<label for='rad4'>
<input id='rad4' class='rad' name='radB' type='radio'>Item3
</label>
</dd>
<dd>
<label for='rad5'>
<input id='rad5' class='rad' name='radB' type='radio'>Item4
</label>
</dd>
</dl>
</li>
</ul>
</div>
</nav>

Targeting CSS elements - Fontawesome and Glyphicons

I'm having some trouble getting Font Awesome and Glyphicons the same side in my side bar menu. I'm unsure how to target the elements to set a specific font size?
A snip of the side bar code.
<div class="col-md-3 md-margin-bottom-40">
<ul class="list-group sidebar-nav-v1" id="sidebar-nav">
<li class="list-group-item list-toggle active">
<a aria-expanded="true" class="" data-parent="#sidebar-nav" data-toggle="collapse" href="#collapse-group-6">My Account</a>
<ul aria-expanded="true" class="collapse in" id="collapse-group-6" style="">
<li class="my_account_links_inactive">
<a class="user_links" href="index.php?a=4&b=12">
<div class="my_account_icons">
<i class="fa fa-print"></i>
</div>print signs and flyers</a>
</li>
<li class="my_account_links_inactive">
<a class="user_links" href="store/The-Parts-Network.html">
<div class="my_account_icons">
<i class="glyphicons white shop"></i>
</div>my storefront</a>
</li>
</ul>
</li>
</ul>
</div>
I think I need something like this?
#sidebar-nav > li > ul > li > a > div > i [class*="fa-"],
#sidebar-nav > li > ul >li > a > div > i[class*="glyphicon-"] {
font-size: 16px;
}
This CSS should work to target the #sidebar-nav icons only:
#sidebar-nav i.glyphicons, #sidebar-nav i.fa {
font-size: 16px;
}

CSS Hover over an image to make a sub menu appear

I'm trying to make it so that I can use a menu like this
To be fair I barely understood this and I was informed that you can use a style with the hover function.
Here's what I tried with my code:
<center>
<ul>
<li>
<img src="images/audiorageGIFtype/audiorageBanner.gif" alt="Welcome to AudioRage!"/>
</li>
<li>
<img src="images/audiorageGIFtype/audiorageButtonHome.gif" alt="Home"/>
<img class="showHim" src="images/audiorageGIFtype/audiorageButtonStore.gif" alt="Store" />
<img src="images/audiorageGIFtype/audiorageButtonAbout.gif" alt="About"/>
<img src="images/audiorageGIFtype/audiorageButtonCart.gif" alt="Cart"/>
</li>
</ul>
</center>
<br />
<center>
<div class="showMe">I want this to show!!</div>
</center>
Here's the CSS
.showMe
{
display: none;
}
.showHim:hover .showMe
{
display: block;
}
Here's a pre-made link of the code to JSFIDDLE
This won't work because your display:block rule is looking for .showHim that is being hovers with the child of .showMe. You can do it a couple of ways.
Targeting a hidden child: jsFiddle
CSS
.hoverme:hover .showMe {
display: block;
}
HTML
<span class="hoverme">
<img class="showHim" src="images/audiorageGIFtype/audiorageButtonStore.gif" alt="Store" />
<span class="showMe">I want this to show!!</span>
</span>
Or targeting a sibling jsFiddle
CSS
.showHim:hover + .showMe {
display: block;
}
HTML
<img class="showHim" src="images/audiorageGIFtype/audiorageButtonStore.gif" alt="Store" />
<span class="showMe">I want this to show!!</span>
The typical menu is structured like this which makes it easy to target the hidden item. jsFiddle
HTML
<ul class="menu">
<li>
Some menu item
<ul class="submenu">
<li>Sub menu item</li>
</ul>
</li>
...
</ul>
CSS
.submenu {
display:none;
}
.menu > li:hover .submenu {
display:block;
}
You need to use some jQuery here to make if work:
$(".showHim").hover(
function () {
$('.showMe').css("display","block");
},
function () {
$('.showMe').css("display","none");
}
);
});
Here working example
for this you need to change your html structure
html code
<center>
<ul>
<li><img src="images/audiorageGIFtype/audiorageBanner.gif" alt="Welcome to AudioRage!"/></li>
<li><img src="images/audiorageGIFtype/audiorageButtonHome.gif" alt="Home"/></li>
<li class="showHim"><img src="images/audiorageGIFtype/audiorageButtonStore.gif" alt="Store" />
<div class="showMe">I want this to show!!</div></li>
<li><img src="images/audiorageGIFtype/audiorageButtonAbout.gif" alt="About"/></li>
<li><img src="images/audiorageGIFtype/audiorageButtonCart.gif" alt="Cart"/></li>
</ul>
</center>
CSS Code
.showMe{
display: none;
}
.showHim:hover .showMe{
display: block;
}
JsFiddle file

Bootstrap selector dropdown-menu.pullright not working?

The Bootstrap .dropdown-menu.pull-right selector doesn't seem to work (the code is in navbar.less, lines 330 to 341).
As you can see on this screenshot, the up caret is not aligned to the right.
My dropdown-menu class :
<ul class="dropdown-menu pull-right">
When I change line 286 to
right:10px;
it works fine.
Any idea on how to fix this ?
Here is my html code:
<div id="USE_dropdown_signin" class="btn-group pull-right">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-user"></i> Sign in
</a>
<ul class="dropdown-menu pull-right">
<form action="" method="post" accept-charset="UTF-8">{% csrf_token %}
<input id="USE_signin_username" type="text" name="signin[username]" size="30" placeholder="User Name"/>
<input id="USE_signin_password" type="password" name="signin[password]" size="30" placeholder="Password"/>
<input id="USE_signin_remember_me" type="checkbox" name="signin[remember_me]" value="1" />
<label id="USE_signin_remember_me_label" class="string optional" for="signin_remember_me"> Remember me</label>
<input id="USE_signin_submit" class="btn btn-primary" type="submit" name="commit" value="Sign in" />
</form>
</ul>
</div>
From your screenshot, I would assume that you have used .btn-group class to make your white log in button.
Assign "pull-right" class to the div element containing btn-group class instead of the ul element.
Correct Html markup
<div class="btn-group pull-right">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-user"></i> Username
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Profile</li>
<li class="divider"></li>
<li>Sign Out</li>
</ul>
</div>
Based on what I have analyzed on the twitter bootstrap, you will require the following css to work.
.navbar .pull-right .dropdown-menu, .navbar .dropdown-menu.pull-right {
right: 0;
left: auto;
}
.pull-right > .dropdown-menu {
right: 0;
left: auto;
}
.pull-right { float: right; }
Here is what I didn't understand:
1) The boostrtap selector in navbar.less line 329-330
.navbar .nav.pull-right .dropdown-menu,
.navbar .nav .dropdown-menu.pull-right {}
didn't match my dropdown-menu because I don't have the parent .nav class in my hierarchy.
2) My .dropdown-menu has the class .pull-right, and this doesn't match:
.navbar .dropdown-menu:before .pull-right {
left: auto;
right: 9px;
}
but this does:
.navbar .pull-right .dropdown-menu:before {
left: auto;
right: 9px;
}
3) I couldn't debug correctly until I removed the -yui-compress option of the less compiler ;-)
Thanks for everything!

Resources