navigation using BEM [closed] - html

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to get my head around BEM, and I'm having troubles even with the most basic things. Such as a menu.
Consider this code
<ul class="menu">
<li class="menu__item">
What
</li>
<li class="menu__item">
Why
</li>
<li class="menu__item">
How
</li>
</ul>
ul is the block, li is the element, but what do I do with that anchor? Since I need both li and a styled, li has to be at very least styled to be inline, a has to be block and stuff. I could make the a a .menu_item, but how would I style that li then, since I'm supposed to not to use element selectors in css and since menu block should be appliable to any html element, something like .menu li {} would be, had I decided to use say div and a combo, senseless..
So how do I do this in the "right" bem way?

You have two options here (or you can decide to use both of them):
Use different elements for li and a:
<ul class="menu">
<li class="menu__item">
<a class="menu__link" href="/what">What</a>
</li>
<li class="menu__item">
<a class="menu__link" href="/why">Why</a>
</li>
<li class="menu__item">
<a class="menu__link" href="/how">How</a>
</li>
</ul>
Important notice here is that you shouldn't use nested elements like menu__item__link.
Use separate block for links:
<ul class="menu">
<li class="menu__item">
<a class="link" href="/what">What</a>
</li>
<li class="menu__item">
<a class="link" href="/why">Why</a>
</li>
<li class="menu__item">
<a class="link" href="/how">How</a>
</li>
</ul>
So you can apply rules with a little bit of cascade: .menu .link {}
Or you can use mixes which is the best way I think:
<ul class="menu">
<li class="menu__item">
<a class="link menu__link" href="/what">What</a>
</li>
<li class="menu__item">
<a class="link menu__link" href="/why">Why</a>
</li>
<li class="menu__item">
<a class="link menu__link" href="/how">How</a>
</li>
</ul>
This time you can avoid using cascade but preserve common styles for links on your project.

Related

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>

Links don't work in Materialize tabs

I'm relatively new to web development, and I'm using Materialize for the project I'm working on right now. My navbar code is attached, for some reason I can click on and follow the links in the sidebar, but not in the nav-content tabs. When I take out the tabs tabs-transparent classes in the ul, it works, but it looks ugly. I am loading JQuery- that was the only resolution I saw to this problem when I googled.
<nav class="blue-grey lighten-1 nav-extended" role="navigation">
<div class="nav-wrapper">
<a id="logo-container" href="#" class="brand-logo center"><img src="/application/static/application/colorondarknotext.png" height=60px/></a>
<ul class="right hide-on-med-and-down">
<li><a class="dropdown-button" href="#!" data-activates="dropdown1">Welcome, (USERNAME)!<i class="material-icons right">arrow_drop_down</i></a></li>
</ul>
<ul id="nav-mobile" class="side-nav">
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header">Welcome, (USERNAME)!<i class="material-icons right">arrow_drop_down</i></a>
<div class="collapsible-body">
<ul>
<li>Account Info</li>
<li>Log Out</li>
</ul>
</div>
</li>
</ul>
</li>
<li>
<div class="divider"></div>
</li>
<li>Application</li>
<li>Decisions</li>
<li>Travel Information</li>
</ul>
<i class="material-icons">menu</i>
</div>
<div class="nav-content hide-on-med-and-down">
<ul class="tabs tabs-transparent">
<li class="tab">Application</li>
<li class="tab">Decisions</li>
<li class="tab">Travel Information</li>
</ul>
</div>
</nav>
I've run into a series of issues like this.
Generally you need to look deeper in the docs. In this case the navbar component docs don't address the issue in their example code. You need to look under javascript>tabs section of the docs to uncover more details about how the tabs work.
http://materializecss.com/tabs.html#external
By default, Materialize tabs will ignore their default anchor behaviour. To force a tab to behave as a regular hyperlink, just specify the target property of that link!
The normal anchor (<a>) functionality is disabled/ignored. In order to make your links work like "normal" you need to add a target attribute to your links.
Assuming you want to open the links in the same window, you would add target="_self" to each <a> tag.
<li class="tab">Application</li>
<li class="tab">Decisions</li>
<li class="tab">Travel Information</li>

purecss drop down menu not working

I am using trying to create vertical drop-down menu using purecss library but no success
purecss.io/menus/
My css code looks like:
<div class="pure-menu custom-restricted-width">
<ul class="pure-menu-list">
<li class="pure-menu-item pure-menu-has-children">
T-Shirts
<ul class="pure-menu-children">
<li class="pure-menu-item">
<a class="pure-menu-link" href="t-shirts-rundhals-tx-2-1,-1-2-.html">Rundhals</a>
</li>
<li class="pure-menu-item">
<a class="pure-menu-link" href="t-shirts-v-neck-tx-3-1,-1-3-.html">V-Neck</a>
</li>
<li class="pure-menu-item">
<a class="pure-menu-link" href="t-shirts-longsleeve-tx-4-1,-1-4-.html">Longsleeve</a>
</li>
<li class="pure-menu-item">
<a class="pure-menu-link" href="t-shirts-tanktop-tx-124-1,-1-124-.html">Tanktop</a>
</li>
</ul>
</li>
</ul>
</div>
Even I copy paste the sample code from their documentation and its not working too
jsBin
It's working fine just add css for custom-restricted-width because your pure-menu div content 100% width of the screen, so that you can not show the dropdown menu.
.custom-restricted-width{
display:inline-block;
}

Bootstrap 3: Navbar Tabs Equal to Page Width

I'm trying to create spaced tabs. I want about 6-8 of them running across the width of the screen, each with equal distance apart from each other. I also need them to have dropdown ability on hover.
Initially I went with Bootstrap 3 for quick and fast tabs. However, I can't seem to get them to space equally across the width of the page.
Alternatively, I found this awesome snippet of code which is pure css. However, I'm far from a css wizard and making a pure dropdown like the tabs would take me a long time.
Anyone know a solution to the bootstrap 3 problem of spacing or know where I can get a pure css dropdown solution? Bootstrap 3 code below, pure css in jsfiddle link
Thanks!
Code:
<ul class='nav nav-tabs'>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 1</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 1</a></li>
</ul>
</li>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 2</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 2</a></li>
</ul>
</li>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 3</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 3</a></li>
</ul>
</li>
</ul>
You can enclose your code inside a div with container-fluid class. It will help you to utilize entire width of apge.
ul.nav-tabs > li {
width: 33%;
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<ul class='nav nav-tabs'>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 1</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 1</a>
</li>
</ul>
</li>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 2</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 2</a>
</li>
</ul>
</li>
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'>Test 3</a>
<ul class='dropdown-menu'>
<li><a href='#'>test 3</a>
</li>
</ul>
</li>
</ul>
</div>

Is there anything "wrong" with this markup?

Here is the markup I'm using:
<ul class="menu">
<li id="planning_menuItem" class="menuItem">Estate Planning</li>
<ul class="subMenu">
<li id="will_menuItem" class="subMenuItem">Last Will & Testament</li>
<li id="poa_menuItem" class="subMenuItem">Power Of Attorney</li>
<li id="livingWill_menuItem" class="subMenuItem">Living Will</li>
<li id="trusts_menuItem" class="subMenuItem">Trusts</li>
</ul>
<li id="probate_menuItem" class="menuItem">
Estate Probate Administration
</li>
<li id="realEstate_menuItem" class="menuItem">Real Estate</li>
<li id="medicaid_MenuItem" class="menuItem">Medicaid</li>
<li id="guardianships_menuItem" class="menuItem">Guardianships</li>
</ul>
Is there anything stylistically wrong about having that "ul" (class=subMenu) in the middle of a list without it having it's own li tag? This particular mark-up solves my problem, but I want at least be aware of whether I'm breaking some standard or convention.
The alternative is as follows, but it doesn't work for my particular task.
<ul class="menu">
<li id="planning_menuItem" class="menuItem">Estate Planning
<ul class="subMenu">
<li id="will_menuItem" class="subMenuItem">Last Will & Testament</li>
<li id="poa_menuItem" class="subMenuItem">Power Of Attorney</li>
<li id="livingWill_menuItem" class="subMenuItem">Living Will</li>
<li id="trusts_menuItem" class="subMenuItem">Trusts</li>
</ul>
</li>
<li id="probate_menuItem" class="menuItem">
Estate Probate Administration
</li>
<li id="realEstate_menuItem" class="menuItem">Real Estate</li>
<li id="medicaid_MenuItem" class="menuItem">Medicaid</li>
<li id="guardianships_menuItem" class="menuItem">Guardianships</li>
</ul>
Thanks!
Yes, there is something wrong, <ul>'s can be inside of <li>'s, but not other <ul>'s.
This is correct...
<ul>
<li>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</li>
<li></li>
<li></li>
<li></li>
</ul>
According to the HTML DTD, you cannot have a <ul> directly under another <ul>. You should nest it inside a <li>.
If you're worried about the additional padding/margins, you can remove them with a rule that only applies to <ul> elements as direct children of <li> elements:
li > ul { margin: 0; padding: 0 }
(Or experiment with the best setting for your style)
BELOW is proper way
<ul class="menu">
<li id="planning_menuItem" class="menuItem">
Estate Planning
</li>
<li>
<ul class="subMenu">
<li id="will_menuItem" class="subMenuItem">
Last Will & Testament
</li>
<li id="poa_menuItem" class="subMenuItem">
Power Of Attorney
</li>
<li id="livingWill_menuItem" class="subMenuItem">
Living Will
</li>
<li id="trusts_menuItem" class="subMenuItem">
Trusts
</li>
</ul>
</li>
<li id="probate_menuItem" class="menuItem">
Estate Probate Administration
</li>
<li id="realEstate_menuItem" class="menuItem">
Real Estate
</li>
<li id="medicaid_MenuItem" class="menuItem">
Medicaid
</li>
<li id="guardianships_menuItem" class="menuItem">
Guardianships
</li>
</ul>
as others have said the second way is valid
here's a forked jsfiddle showing some new CSS which should help
summary is that you want to keep the ul's and their li's full width no side padding or margins, so reset all ul's; then make the a's display block so they fill their parent li's - then use text-indent to create the indented look of the li's
then however you choose to apply an "active or selected" class - apply it to the li OR a and any hover changes can be made too.