This question is in reference to this documentation https://en.bem.info/methodology/css/#external-geometry-and-positioning
The parent class is "header" and the button is "button header__button". Typically, I would use (and see elsewhere in tutorials) "button button--header".
My site uses dropdown menus with the class "dropdown-menu" and I am also using "dropdown-menu--nav" but is "--nav" an appropriate modifier? By the logic in the documentation, I should use "dropdown-menu navbar__dropdown-menu". I'm lost because I can see both navbar and dropdown-menu being their own blocks, but when they interact with each other, I'm not so sure. If navbar is the block and dropdown-menu is the element, should I use "dropdown-link navbar__dropdown-link" instead of "dropdown-menu__link dropdown-menu__link--nav" for links?
I could see the "element block__element" approach working better because the block specific styling to that element would be near the block styling, instead of with the element styling as a modifier.
Example of how I use BEM for button example and dropdown menu: https://codepen.io/SROwl/pen/eYmVzBE
Explanation:
I would write dropdown-menu--nav as dropdown-menu__nav as it is a component of the dropdown menu. Also, I tend to rebase inner components seen in the nav example below.
The button example is used to show multiple classes being used to create the button look you want. However, if the .button class doesn't have any of the same styles as .header__button then this would not be necessary. You would simply use .header__button.
I would use a modifier if I wanted to change the button color, ex: .header__button--green.
I stray away from the BEM documentation a little where they want you to use something like: < div class="header__button header__button--green">. I use SCSS extends to include the properties of header__button to header__button--green so the markup ends up being: < div class="header__button--green"> rather than including both classes. This is a personal preference, some people do not like extends as they find it difficult to manage or don't like the way it compiles the css.
BEM
<!-- How BEM states it should be done -->
<div class="button">Shop Now</div>
<div class="button header__button">Shop Now</div>
<div class="button header__button header__button--green">Shop Now</div>
BEM EXTEND
<!-- How I prefer to do it -->
<div class="button2">Shop Now</div>
<div class="header__button2">Shop Now</div>
<div class="header__button2--green">Shop Now</div>
BEM NAV
<div class="dropdown-menu">
<div class="dropdown-menu__header">
Menu
</div>
<ul class="dropdown-menu__nav">
<li class="nav__link">Link 1</li>
<li class="nav__link--active">Link 2</li>
<li class="nav__link">Link 3</li>
</ul>
</div>
body {
font-family: sans-serif;
}
// BEM WAY
.button {
background: #000;
color: #fff;
border-radius: 5px;
font-size: 16px;
text-align: center;
max-width: 200px;
padding: 5px 15px;
text-transform: uppercase;
margin-bottom: 10px;
}
.header__button {
font-size: 32px;
color: #ccc;
&--green {
background-color: green;
}
}
// BEM-ish way
.button2 {
background: #000;
color: #fff;
border-radius: 5px;
font-size: 16px;
text-align: center;
max-width: 200px;
padding: 5px 15px;
text-transform: uppercase;
margin-bottom: 10px;
}
.header__button2 {
#extend .button2;
font-size: 32px;
color: #ccc;
&--green {
#extend .header__button2;
background-color: green;
}
}
// BEM-ish menu
.dropdown-menu {
background: gray;
width: 300px;
padding: 15px;
&__header {
background-color: #ccc;
padding: 5px;
}
&__nav {
background: darken(#ccc, 20%);
padding: 10px;
list-style-type: none;
.nav {
&__link {
text-transform: uppercase;
color: purple;
padding: 5px;
&--active {
#extend .nav__link;
color: pink;
background: gray;
}
}
}
}
}
Related
I am adding a responsive navigation bar to a website, closely based on a w3 school tutorial (https://www.w3schools.com/howto/howto_js_topnav_responsive.asp).
All is working fine, except that I am trying to center the menu links on the page (for the desktop version), instead of having it on the left side like the tutorial explains.
I tried modifying the value of each property, including float, but without success so far. It must be extremely simple, but what am I missing?
html:
<div class="nav" id="nav">
Home
About
Menu
Drinks
Values
Gallery
Booking
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
css:
.nav {
background-color: gold;
overflow: hidden;
}
.nav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.nav a:hover {
background-color: pink;
color: green;
}
.nav .icon {
display: none;
}
I saw that this question has already been asked with bootstrap, but as a beginner, I am only using plain css here.
you can use flex with center layout, give it a try
.topnav {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
background-color: #333;
}
Another approach if you wanted to avoid using flexbox for whatever reason would be to set the menu items to display: inline-block instead of using floating. Then a simple text-align: center on the nav wrapper will be sufficient.
On a side note, it would make more sense semantically to use the <nav> element as a wrapper and put the menu items in a <ul> list as <li> elements.
.nav {
background-color: gold;
overflow: hidden;
text-align: center;
}
.nav a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.nav a:hover {
background-color: pink;
color: green;
}
.nav .icon {
display: none;
}
<div class="nav" id="nav">
Home
About
Menu
Drinks
Values
Gallery
Booking
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
So, I just started learning html from w3school. And I have started my little project which I am going to create simple website. And now I am stuck with creating drop down menu from my navigation bar. I can't seem to put it in the navigation bar. ps.I have edited some code I don't think it's relevant.
.topnav {
overflow: hidden;
padding-top: 6px;
padding-bottom: 6px;
margin: 0px;
}
.topnav h1 {
text-align: center;
font-size: 22px;
color: #FFFFFF;
margin: 0px;
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
float: left;
}
.show {
display:block;
}
</style>
</head>
<body>
<div class="topnav">
<h1>Home</h1>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Menu</button>
<div id="myDropdown" class="dropdown-content">
Content
</div>
</div>
</div>
Add below css and jQuery
css::
#myDropdown{
display: none;
}
jQuery::
$('.dropbtn').click(function(){
$(this).next('#myDropdown').stop(true,false).slideToggle();
})
You have to use script for hide/show your dropdown menu, Here you can see script
http://jsfiddle.net/renukaSingh/njhwr4z2/
I have a pretty specific question: I'm building something like a simple flat table (I don't use table itself because of rounded borders issue).
I'm using unordered list here and the problem is that I can't figure out how to align items in the second column, taking into account that the content should be dynamic (e.g. changing numbers).
Here's the markup for one row:
<section class="ktbl_head">
<ul>
<li>VALUE</li>
<li>VALIDITY</li>
</ul>
</section>
<section class="ktbl_mid_wht">
<ul>
<li>500 units</li>
<li>15 days</li>
<button class="btn btn-sm getdramz pull-right">GET</button>
</ul>
</section>
And CSS:
.ktbl_head {
padding: 15px 0 0 0;
height: 100%;
background-color: #ebe7e7;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.ktbl_head ul li {
display: inline;
padding-right: 135px;
font-family: "Open Sans", sans-serif;
font-size: 14px;
font-weight: 300;
color: #888888;
}
.ktbl_mid_wht {
background-color: #ffffff;
height: 100%;
padding: 20px 0 0 0;
}
.ktbl_mid_wht ul li {
display: inline;
text-align: left;
padding-right: 90px;
font-family: "Open Sans", sans-serif;
font-size: 18px;
font-weight: 400;
color: #888888;
}
Thanks for your attention!
here is my implementation on aligning the table without the table tag:
HTML
<div class="container">
<section class="ktbl_head">
<ul>
<li>VALUE</li>
<li>VALIDITY</li>
</ul>
</section>
<section class="ktbl_mid_wht">
<ul>
<li>500 units</li>
<li>15 days</li>
<button class="btn btn-sm getdramz pull-right">GET</button>
</ul>
</section>
CSS
.container {
border-radius: 10px;
border: 1px solid rgb(200,200,200);
overflow: hidden;
}
section {
font-family: "Open Sans", sans-serif;
width: 100%;
}
section:nth-child(2n+1) {
background-color: #ebe7e7;
}
section ul {
padding: 0;
margin: 0;
height: 65px;
}
section ul li {
width: 45%;
line-height: 65px;
display: inline-block;
}
section ul li:first-child {
padding-left: 35px;
}
Result
Explanation
You see, in the HTML, I added a new div as a container to create the curved corner with border-radius (the overflow: hidden needs to be used so that the content is encapsulated by the container).
For the CSS, section maintains general property such as font-family. Furthermore, section:nth-child(2n+1) is used to create background-color every other element starting with 1st,3rd,5th,... element. The selectors section ul, section ul li, and section ul li:first-child are used to make the CSS selectors more semantic (it makes clean code and easy to maintain in the future). Please see the code below for the demo. Happy coding!
PLAYGROUND
Give all the li's a width in which all of the content-length will fit..
I have 2 items in a in HTML5 and I am trying to centrality(horizontal) align them. However the margin: 0 auto; isnt working nor can i think of anything else?
HTML5 Code
<section class="fourth">
<div id="Top"> Top </div>
<ul class="topUL" >
<li class="topthings"> List 1 </li>
<li class="topthings"> List 2 </li>
</ul>
CSS
Because the methods i tried didnt work so i deleted them from the code this is really a simple CSS code.
.fourth {
height: 35em;
width: 100%;
/** STYLE **/
background-color: #0099FF;
}
#Top {
height:auto;
width:inherit;
text-align:center;
/** FONT STYLE **/
font-family: 'Wire One', Gadget, sans-serif;
color: White;
font-size: 150px;
}
.topthings {
display: inline-block;
margin: auto;
/**FONT STYLE**/
font-family: 'Tulpen One', Gadget, sans-serif;
color: White;
font-size: 75px;
}
Link to how it looks right now.
https://drive.google.com/file/d/0B27I0PqG2ru9eTByOTJsbTd2c1U/edit?usp=sharing
I want 'List 1' and 'List 2' to be equally aligned under the word "Top"
Don't have enough rep to comment, but have you tried simply adding text-align: center to .fourth class?
.fourth {
height: 35em;
width: 100%;
text-align: center;
http://jsfiddle.net/W92Q5/
Add to your stylesheet:
.topUl { text-align: center; }
add one more css rule for UL tag
.topUL{display:block;text-align:center;}
see demo http://jsfiddle.net/2w34K/1/
I have this HTML Code
<a href="test.html">
<div class=" menubox mcolor1">
<h3>go to test page</h3>
</div>
</a>
and this is the css
.menubox {
height: 150px;
width: 100%;
font-size: 14px;
color: #777;
margin: 0 0 0 0;
padding: 0;
-moz-border-radius: 10px;
border-radius: 10px;
position: relative;}
.mcolor1 { background: #3A89BF url(../images/prod2.png) no-repeat center center; }
on mouse hover this div, the text shows the hyperlink line, how can I hide it?
As others have suggested, it's easy to remove the underline from links. However, if you need to target just this specific link, try giving it a class. Example:
.no-underline:hover {
text-decoration: none;
}
<a href="test.html" class="no-underline">
<div class=" menubox mcolor1">
<h3>go to test page</h3>
</div>
</a>
If you want to remove the underline on hover, use this CSS:
a:hover {
text-decoration: none;
}
Note :
Unless your page uses the HTML5 doctype (<!doctype html>), your HTML structure is invalid. Divs can't be nested inside a element before HMTL5.
With the HTML as it stands, you can’t hide the link underline just for this link.
The following CSS will remove the underline for all links:
a:hover {
text-decoration: none;
}
To remove it for just this link, you could move the link inside the <div>:
.menubox > a {
display: block;
height: 100%;
}
.menubox > a:hover {
text-decoration: none;
}
<div class="menubox mcolor1">
<a href="test.html">
<h3>go to test page</h3>
</a>
</div>