How to define correct CSS selector? - html

I have a question concerning CSS selectors. How to choose a correct one?
For example I have this piece of HTML:
<header>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<p style="font-size:1.2em" class="navbar-text">Project name</p>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="navbar-nav nav navbar-right">
<li><a style="font-size:1.2em" href="#AboutMe">Text1</a></li>
<li><a style="font-size:1.2em" href="#Social">Text2</a></li>
<li><a style="font-size:1.2em" href="#Projects">Text3</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
</header>
To make my list elements li in unordered list ul to change appearance I saw next CSS selector:
.navbar-inverse .navbar-nav > li > a:hover {
background-color: white;
color: black;
}
And how I understand this selector says: chose all a elements children of li element who in his turn a child of an element with .navbar-nav class (?) and all of this inside and element with .navbar-inverse class (?).
And this work well. But. If change this selector to another one:
li > a:hover {
background-color: white;
color: black;
}
This does not work. But what it's say: chose all a elements that's a direct children of li element.
Or maybe I do not understand very well concept of CSS selectors.
Thanks.
Edit:
That's all my internal CSS who could potentially influence nav element.
Thanks.
/*
Solution from "http://stackoverflow.com/questions/11124777/twitter-bootstrap-navbar-fixed-top-overlapping-site"
*/
body { padding-top: 40px; }
#media screen and (max-width: 768px) {
body { padding-top: 0px; }
}
.anchor {
padding-top:40px;
}
.navbar-inverse .navbar-nav > li > a:hover {
background-color: white;
color: black;
}
.navbar {
background-color: #444;
border: 0px;
letter-spacing: 2px;
padding-right: 50px;
padding-left: 50px;
}

Well, your problem is definitely one of specificity since the selector with more specificity is providing the correct results. You can look at MDN to explain more or look here at CSS tricks to go into more detail. But essentially, selectors each have a number associated with them of priority, elements have the lowest number, inline styles and !important have the highest. The more selectors in your CSS, the higher the number. Higher numbers will override lower numbers when rendering int he browser.
It is hard to tell without seeing more of your CSS, but if you have styles for your a tag or l tag that have classes on them this would cause your issue. For example:
.navbar-nav .nav-right a {
background: blue;
}
Would prevent your hover from taking effect with just "li > a:hover" because the browser takes the background color of the higher specificity. So perhaps look at other styles above this in your sheet. When in doubt, try things out in codepen.

Related

Why does the exact same CSS apply differently when inserted via external stylesheet compared to internal stylesheet?

So I'm revamping my personal website and starting from scratch. I'm starting off with a navbar that's fixed to the bottom. However, when I insert the CSS rules with an internal stylesheet, it works the way I want it to, but when I use those exact same rules with an external stylesheet, the margin does not work properly.
Specifically, this is the problem rule:
.nav li {
margin: -5px;
}
With an internal stylesheet, it acts as intended, covering up unwanted whitespace in between the list items of my navbar (which is a stacked navbar fixed on the bottom).
However, when I put this exact same rule in the external sheet, it does not cover up that whitespace. Some other weird behavior is that if I comment out "margin: -5px;" it changes nothing, but if I comment out all of the rule ".nav li { margin: -5px; } it moves the content of each list item to the left (which is supposed to be centered using "text-align: center" in another rule.
I'm really confused by this odd behavior. Can anyone help me? Thanks.
EDIT: The weird thing is that other than linking bootstrap, jquery, etc. the html and css is pretty much nonexistent. I just put in a navbar and that's about it.
Here's the html in the body:
<div class="container-fluid">
<nav class="navbar navbar-fixed-bottom">
<ul class="nav nav-pills nav-stacked">
<li><a class="about" href="about.html"><div class="navTileContent"><i class="glyphicon glyphicon-user" aria-hidden="true"></i><p>about</p></div></a></li>
<li><a class="portfolio" href="portfolio.html"><div class="navTileContent"><i class="glyphicon glyphicon-briefcase" aria-hidden="true"></i><p>portfolio</p></div></a></li>
<li><a class="art" href="art.html"><div class="navTileContent"><i class="glyphicon glyphicon-edit" aria-hidden="true"></i><p>art</p></div></a></li>
<li><a class="contact" href="contact.html"><div class="navTileContent"><i class="glyphicon glyphicon-envelope" aria- hidden="true"></i><p>contact</p></div></a></li>
</ul>
</nav>
</div>
And here's the only CSS I have:
<style>
.nav li {
margin: -5px;
}
.nav li .navTileContent {
color: #e1e8f4;
text-align: center;
padding: 1%;
}
.nav li .navTileContent p {
padding-left: 20px;
display: inline;
}
.about {
background-color: #6ad8d1;
}
.portfolio {
background-color: #12d132;
}
.art {
background-color: #12466b;
}
.contact {
background-color: #0d336d;
}
</style>
This is meant to be a very simple sort of use case example, so while the styles won't match yours, I hope you see what I am trying to say.
<style>
li {
color: orange;
}
li.active {
color: blue;
}
li.muted {
color: red;
}
</style>
<style>
li {
color: blue;
}
li.active {
color: orange;
}
</style>
<ul>
<li>Notice this is not orange</li>
<li class="active">Notice this is orange</li>
<li class="muted">notice this is red, because nothing has overwritten it's style definition</li>
</ul>

How to Change the Background Color and Text Color of Bootstrap Navbar Dropdown-menu List Items After Menu Collapses

New to Bootstrap and creating a basic navigation bar. I am hoping that what I need makes sense. Put simply, I need the background color and text color of list items in the dropdown-menu to match both before and after they collapse. I cannot seem to find the right CSS attributes to edit, and the many combinations that I have tried do not solve my problem. A similar question did not solve my problem, but perhaps it is worth looking at for others.
Below is what the menu looks like before it collapses. Each <li> has a link, or <a>, within it (see basic HTML below). The text color is black, and the background color is white. When the item is hovered over, the background of <li> turns tan. I want the same colors after the menu collapses:
After collapse it appears like this:
The colors seem to change back to Bootstrap's defaults after collapsing. I would like the text for Sub 1 and Sub 3 to be black by default with a white background, while keeping the color behind the header, Stuff 2, the same. The colors seem to work fine when clicked or hovered over (tan background with black font).
Below is some of the relevant HTML and CSS. I know that some of it is messy (and that the use of !important; is often considered poor form), but I hope someone out there can help me identify the block of code so I can get it working and then focus on cleaning it up.
Basic HTML:
<nav class="navbar navbar-inverse">
<div id="navbarbg" class="container-fluid">
<div id="rbgnavbar" class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a id="logo" class="pull-left" href="http://www.redbuttegarden.org"><img src="images/logo.png" alt="Red Butte Garden"></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="rbgwhitelink">STUFF 1 <span class="caret"></span></span></a>
<ul class="dropdown-menu">
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="rbgwhitelink">STUFF 2 <span class="caret"></span></span></a>
<ul class="dropdown-menu">
<li>Sub 1</li>
<li>Sub 2</li>
<li>Sub 3</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="rbgwhitelink">STUFF 3 <span class="caret"></span></span></a>
<ul class="dropdown-menu">
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
Basic CSS:
#rbgnavbar a:hover, a, li, .dropdown-menu li:hover {
color: #000000;
}
.dropdown-menu li:hover {
background-color: #F2EDE4;
color: #000000;
}
/* Main dropdown menu items change cover when selected/clicked */
.dropdown-toggle:active, .open .dropdown-toggle, .dropdown:hover {
background-color: #196143 !important;
}
.dropdown-menu li, .dropdown-menu a {
text-align: center;
color: #000000;
}
.dropdown {
font-size: 10px;
padding: 0;
font-weight: bold;
}
#rbgnavbar {
max-width: 1400px;
margin-left:auto;
margin-right:auto;
background-color:#00502F;
}
.rbgwhitelink {
color: #FFFFFF;
}
.dropdown ul {
padding-top: 0px;
padding-bottom: 0px;
font-size: 10px;
font-weight: bold;
}
/* Border thickness and color between dropdown menu li, padding */
.dropdown-menu li {
border-top: 2px double #D3D3D3;
padding-top: 5px;
padding-bottom: 5px;
}
/* Background color of li in navbar when hovered over */
.dropdown-menu li > a:hover, .dropdown-menu li > a:focus, .dropdown-submenu:hover > a, .dropdown-menu li:hover {
background-color: #F2EDE4;
}
/* Makes dropdown-menu li links bold*/
.dropdown-menu li > a, .dropdown-menu li > a:visited {
font-weight: bold;
}
I suggest to use media queries and add the classes you want to tweak.
For example:
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
.dropdown-menu>li>a {
background-color: #fff;
}
}
For Bootstrap 3 default media queries you may found it here
After some experimenting, this did it:
.dropdown-menu li {
background-color: #FFFFFF;
}
.dropdown-menu li:hover {
background-color: #F2EDE4;
}
.dropdown-menu li > a:hover {
text-decoration: none;
}
/* Changes the color of link text <a> after navbar has collapsed for small screen or mobile mode */
.navbar-inverse .navbar-nav .open .dropdown-menu > li > a, .navbar-inverse .navbar-nav .open .dropdown-menu > li > a:visited {
color: #000000;
}
I think you have to change this color:
.dropdown-toggle:active, .open .dropdown-toggle, .dropdown:hover {
background-color: #196143 !important;
}

Bootstrap Dropdown Hover Text Color

I've done a crap ton of research into how I can accomplish this, but I can't find anything solid or anything that I can properly understand.
I want to override the Bootstrap Dropdown hover color. Not the Dropdown menu, the actual Dropdown button itself. By default, the text color is white, and when you hover over it, it's some sort of blue-green color. I want to make the blue-green color to white as well.
Does anyone have any idea how I can go about doing this?
Here's my current code incase you need it:
CSS:
#media {
ul.nav li.dropdown:hover > ul.dropdown-menu {
display: block;
}
.navbar-custom {
color: #262626;
background-color: #262626;
}
.navbar-default {
color: #262626;
background-color: #262626;
border-top: 4px solid red;
}
.navbar-default .navbar-brand:hover {
color: white;
}
.dropdown:hover {
color: white;
}
}
body {
background-color: black;
}
#navbar {
width: 100%;
}
HTML:
<div class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav" id="dropdowns">
<a class="navbar-brand" id="brand">Traders: </a>
<li class="dropdown">
General <span class="caret"></span>
<ul class="dropdown-menu pull-left" role="menu">
<li>Medical</li>
<li>Utility</li>
</ul>
</li>
</ul>
</div>
</div>
Sorry if I left something out or don't make much sense, currently on some powerful pain meds. This helps me get my mind off the pain.
You can try including the !important to the style:
color: white !important;
this is done to override the default styles bootstrap establishes for the elements.

Cannot remove default Bootstrap padding in navigation bar

I have a Bootstrap navigation bar that is more or less customized. The things is, Bootstrap applies padding-left and padding-right to the <a> elements in the navbar. I tried to override the padding-left on the first <a> element in the navbar (the one that says "Collection") but no success. It actually has to look like the first <a> element is starting at the very beginning of the navbar. I added a photo for easier understanding.
Here is the fiddle: http://jsfiddle.net/jxgkrtsd/8/
Code:
<ul class="nav nav-tabs nav-justified" role="tablist">
<div class="bottom-line"></div>
<li>Kolekcija 24<sup> 7</sup></li>
<li>Izdelki</li>
<li>Zgodba</li>
<li>Mediji</li>
<li>Materiali</li>
</ul>
<div class="bottom-line"></div>
CSS:
#import url("http://fonts.googleapis.com/css?family=Exo:400,500&subset=latin,latin-ext");
body {
font-family: 'Exo', sans-serif;
font-weight: 500;
}
.nav > li > a:hover,
.nav > li > a:focus {
background-color: transparent;
border-color: transparent;
}
.nav > li > a {
padding: 10px 5px !important;
margin-right: 0 !important;
color: black;
}
.nav-tabs.nav-justified > li > a {
border-bottom: none;
}
.nav-tabs > li > a:hover {
border-color: none !important;
}
a:hover,
a:focus {
color: black !important;
}
.bottom-line {
height: 1px;
width: 1430px;
background: black;
position: absolute;
}
The problem is not the padding. The problem is that the text is aligned to the center.
Add a text-align: left to your element and things should adjust how you would like them.
This is the code in Bootstrap that is causing the issue
.nav-tabs.nav-justified > li > a {
}
If you alter your jsFiddle you can add it to the following section of CSS:
.nav > li > a {
padding: 10px 5px !important;
margin-right: 0 !important;
color: black;
text-align:left !important;
}
Please note the !important after it. This will override the main call and give the desired effect.
Here is the updated jsFiddle example.
I've needed to do this before. After using <nav class="...."> put the rest of the contents in a new div with class="container-fluid". It should look something like this:
<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand visible-xs" href="{{ HOME_PATH }}/">{{ site.title }}</a>
</div>
...
Then you can use this to get rid of padding for the links in the navbar or for the brand.
nav .container-fluid{
padding-left:0px;
padding-right:0px;
}
To get rid of padding in the links you can use something like this:
.navbar-nav>li>a{
padding-left:0px;
}
I wanted to change the default navbar bootstrap padding. My intention was to change the bottom padding to 4.5rem. Below code worked for me
.navbar{
padding-bottom: 4.5rem !important;
}
The issue is that the a tags in the navigation are displayed as blocks, instead of the inline. You can change it to display as a flexible block by setting the display to flex, then retain the full block area as the link by setting the width to 100%.
Simply add these to your custom CSS for .nav > li > a to look like:
.nav > li > a {
padding: 10px 5px !important;
margin-right: 0 !important;
color: black;
display: flex;
width: 100%;
}
This will retain the top and bottom padding/margins for blocks, the full width of the block as the link, and remove the left/right padding/margin.
Here's a JSFiddle showing it: http://jsfiddle.net/9kzfaypp/
I agree with a few other answers here like #dippas, but to get the text flush to the left, as your picture shows you need to override the anchor padding as well:
.nav-tabs.nav-justified > li > a {
text-align:left;
padding-left:0px !important;
padding-right:0px !important;
}
http://jsfiddle.net/jxgkrtsd/10/

Decreasing the height bootstrap navbar size without using less?

I am trying to decrease the height of the Bootstrap 3.1.1's navbar without using the less version of it. I have tried several ways of doing this by reading around on different sites, and none of them are decreasing the CSS's .navbar height.
I have managed to fix the padding on the .navbar-nav > li > a.
My HTML:
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">QuadCMS</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Help</li>
</ul>
</div>
</div>
</div>
My CSS which overrides the Bootstrap to try to fix the navbar.
.navbar {
height: 25px !important;
}
.navbar-nav > li > a {
padding-top: 5px !important;
padding-bottom: 5px !important;
}
My main issue is with the actual height of the navbar, not the padding now. The padding of the navigation links are fixed and working at the small size. I have also tried changing the .navbar to .navbar-nav, and .navbar-fixed-top respectfully and nothing has worked yet.
Do I need to call a different CSS Class then the ones I have tried, or do I need to call two of the ones I have already tried to get the effect needed?
Here is the default .navbar styling:
.navbar {
position: relative;
min-height: 50px;
margin-bottom: 20px;
border: 1px solid transparent;
}
You need to overwrite the min-height property. You can use the value inherit.
Example Here
.navbar {
height: 30px;
min-height: inherit;
}
.navbar-nav > li > a {
padding-top: 5px;
padding-bottom: 5px;
}
.navbar-brand {
padding:5px 0 0 0;
}