Bootstrap 3 dropdown menu center align not working - html

I am not able to align the dropdown to the center of the parent on which the dropdown is opened with hover. I have tried to text-align: center for the entire .nav .navbar li elements, and it doesn't work. I have tried to set margin and left: 50% for the entire ul.dropdown-menu that is the dropdown and it doesn't work. Here is the html code:
<ul class="nav navbar-nav navbar-right" id="headerDropdowns">
<li><div class="btn-toolbar">
<div class="btn-group">
<button class="btnCustom" data-toggle="dropdown" data-hover="dropdown" data-delay="1000">Our Difference</button>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Made in the USA</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Human Grade</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Ingredients Fresh</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">USDA Meats</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Our Story</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Campare</a></li>
</ul>
</div>
</li>
</ul>
In which class dropdown-menu, or nav navbar-nav, should I do the aligment, and what should I set?

You can use transform to avoid having to use fixed widths:
.dropdown-menu {
left: 50%;
right: auto;
text-align: center;
transform: translate(-50%, 0);
}
Answer influenced by http://css-tricks.com/centering-percentage-widthheight-elements/

You can do this as long as you're willing to give the dropdown ul a fixed width. See code below:
.dropdown{text-align:center;}
.button, .dropdown-menu{margin:10px auto}
.dropdown-menu{width:200px; left:50%; margin-left:-100px;}
First 2 declarations are for visualization purposes, but the important part is the 3rd line. You'll need to define a width (in this case 200px) and then a margin-left equal to half the width. This will work with any width, no matter if the button is at the right, left or between elements (but of course you'll probably need some space for the button element)
You can see a fiddle here

The only, ugly way is to set the position depending on your needs like:
.dropdown-menu{
right: -25px !important;
}
Fiddle
But thas not the way it should be used. Keep in mind that you have to adjust the setting for all media devices.

This is what did the trick for me:
.dropdown-menu{
left: -25% !important;
}
You might need to adjust the percentage value depending on your dropdown-menu's padding etc.

Related

How to center bootstrap navbar, but also pull 1 element right?

I was able to center my navbar with the CSS below. Now I want to put another element but pull that to the right. However, when I add the icon, it drops down to the next line. How do I center my navbar and add the icon, while keeping everything inline?
This is a piece of my navbar with the css.
<div class="collapse navbar-collapse" id="myNavbar" style="text-align: center;">
<ul class="nav navbar-nav">
<li >Home</li>
<li >About</li>
<li >Get Help</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-search pull-right" aria-hidden="true" style="font-size:18px; display: inline-block;"></span></li>
</ul>
</div>
CSS:
#media (min-width: 708px){
.navbar-nav{
float:none;
margin: 0 auto;
display: table;
table-layout: fixed;
}
}
One option is to add the "top" property on the element then give it a negative value to correctly position your search icon.
.search {
top: -50px;
font-size: 18px;
}
If necessary, adding additional navigation links wont't break style .
See my plunker
Also, you may take out the styles in your HTML tags.

Submenu/Drop-down items taking up space among main navbar items - why?

I'm using a template, HTML5UP - Miniport, for my web design class - I'm just beginning to learn to code. In order to meet the specification for my class I needed to add submenus/drop-down navbar. This works fine in desktop mode, but when I decrease the size of the windows, I get some problems. The submenus stay inside the fixed navbar, pushing their way between other menus items. Here's a link to what it currently looks like:
https://jsfiddle.net/OrangeJones/9u0seLxu/
The CSS is in the fiddle link, but here's my HTML.
<div class="nav">
<ul class="container">
<li><a class="jumper home" href="#top">Home</a></li>
<li><a class="jumper about" href="#about">About</a>
<ul>
<li>Resume</li>
</ul>
</li>
<li><a class="jumper portfolio" href="#portfolio">Portfolio</a></li>
<li><a class="jumper blog" href="#blog">Blog</a>
<ul>
<li>Best of the Twin Cities</li>
</ul>
</li>
<li>
<a class="jumper contact" href="#contact">Contact</a>
</li>
</ul>
</div>
How can I get it to where the submenus drop down when hovered over or clicked on in small screens instead of taking up main navbar space.
Thank you!
Your CSS for the submenu to show and hide is inside the media query and so the dropdown elements were showing when on a screen that was smaller instead of being hidden, you also had the deceleration of the background for the submenu elements in the media query.
.nav li ul
{
position: absolute;
display: none;
width: inherit;
}
.nav li:hover ul
{
display: block;
}
.nav li ul li
{
display: block;
background-color: #282828;
}
Updated fiddle

Forcing <ul> levels to display in separate panels

I've got a 3-level menu generated by CMS (Joomla to be exact), which looks something like this:
<ul class="nav menu">
<li class="item-108">1-level-1</li>
<li class="item-124">1-level-2</li>
<li class="item-125 active deeper parent">1-level-3
<ul class="nav-child unstyled small">
<li class="item-164">2-level-1</li>
<li class="item-165">2-level-2</li>
<li class="item-166 current active deeper parent">
2-level-3
<ul class="nav-child unstyled small">
<li class="item-212">3-level-1</li>
<li class="item-213">3-level-2</li>
</ul>
</li>
<li class="item-210">2-level-3</li>
<li class="item-211">2-level-4</li>
</ul>
</li>
<li class="item-126">1-level-4</li>
</ul>
What I need is to force it to behave like a menu for example from this site: http://wachtel.de/backoefen/etagenoefen.html - I mean display every nested level in a separate box under it's parent.
I'm having trouble achieving it in pure CSS, so before I loose my mind, I'd like to ask you: Can it even be done, or do I need to use JS?
EDIT:
So far I've tried to make the prime UL relative and sub-ULs absolute with "left: 0" and "width: 100%", but it doesn't seem to work.
EDIT2:
Problem was caused by bootstrap.css property .nav > li {position: relative}, which was ruining the layout.
All you need in your case is some positioning. Position the .nav relatively, then you can absolutely position the .nav-childs accordingly. I inserted a minimal example for you - I'm sure you can figure out the hide/show stuff on hover by yourself.
.nav.menu{
position:relative; /* positioning the base element*/
}
li{
float:left; /* aligning the list node */
/* the rest is presentational stuff: */
list-style:none;
background-color:#ddd;
border:1px solid #aaa;
}
li a{
text-decoration:none;
}
.nav-child{
/* the important part:*/
position:absolute; /* absolute positioning*/
left:0; /* left according the the root element*/
/* the top positioning stays untouched! */
}
<ul class="nav menu">
<li class="item-108">1-level-1</li>
<li class="item-124">1-level-2</li>
<li class="item-125 active deeper parent">1-level-3
<ul class="nav-child unstyled small">
<li class="item-164">2-level-1</li>
<li class="item-165">2-level-2</li>
<li class="item-166 current active deeper parent">
2-level-3
<ul class="nav-child unstyled small">
<li class="item-212">3-level-1</li>
<li class="item-213">3-level-2</li>
</ul>
</li>
<li class="item-210">2-level-3</li>
<li class="item-211">2-level-4</li>
</ul>
</li>
<li class="item-126">1-level-4</li>
</ul>

Write slightly different CSS for Parent Menu Item

everybody!
Got into a bit of a trouble here.
So, basically, I badly want to make my Parent Menu Items in Joomla! main menu slightly different then other Menu Items. What I want to achieve is that the Parent Item would have ... let's say, a little triangle made with bg image on the right to show visitors there are some submenus included.
I've been trying to get my CSS working, but somehow nothing happenes and with inspecting the generated code and elements CSS hasn't applied anything to the Parent Items.
Here's the code:
<li class="item-101 current active"><a href="/" >Domov</a></li>
<li class="item-107"><a href="/index.php/o-meni" >O meni</a></li>
<li class="item-108 deeper parent"><a href="/index.php/psihoterapija" >Psihoterapija</a>
<ul class="nav-child unstyled small">
<li class="item-113"><a href="/index.php/psihoterapija/podmeni-1" >Podmeni 1</a></li>
</ul>
</li>
<li class="item-109"><a href="/index.php/zdravstvena-psihoterapija" >Zdravstvena psihologija</a></li>
<li class="item-114 deeper parent"><a href="/index.php/ponudba" >Ponudba</a>
<ul class="nav-child unstyled small">
<li class="item-117"><a href="/index.php/ponudba/podmeni-2" >Podmeni 2</a></li>
<li class="item-118"><a href="/index.php/ponudba/podmeni-3" >Podmeni 3</a></li>
<li class="item-119"><a href="/index.php/ponudba/podmeni-4" >Podmeni 4</a></li>
</ul>
</li>
<li class="item-139 deeper parent"><span class="nav-header">Ostalo</span>
<ul class="nav-child unstyled small">
<li class="item-138"><a href="/index.php/ostalo/aktualno" >Arhiv novic</a></li>
<li class="item-116"><a href="/index.php/ostalo/povezave" >Povezave</a></li>
<li class="item-115"><a href="/index.php/ostalo/kontakt" >Kontakt</a></li>
</ul>
</li>
So, the items I want to change are those with .deeper.parent class.
The CSS code I wanted to apply but doesn't work:
#navigation .parent {
padding:37px 22px 37px 8px !important;
background-image: url(../images/more.png) !important;
background-position: right !important;
background-repeat: no-repeat !important;
}
I also tried changing #navigation .parent to #navigation .deeper.parent and to #navigation li.item-108.deeper.parent as well. Nothing really works. Any ideas? Thanks!
I'm assuming some coding here for display purposes, but what you want here is this part:
#navigation li.parent { padding:37px 22px 37px 8px !important; background:url(http://upload.wikimedia.org/wikipedia/commons/f/f7/Arrow-down-navmenu.png) no-repeat right center}
You can see fiddle and the adjust at will (REMEMBER: I'm assuming your code so you'll have to adjust it to your real code!)
Firstly, ensure you actually have the #navigation ID assigned to your <ul> like so:
<ul id="navigation">
Secondly, make sure you do not target menu items based on their item number, such as .item-108. These are assigned by Joomla and may change in the future.
Ok, so as you mentioned, you want to target menu items with the deeper and parent classes, for this, you can use the following:
.deeper.parent a {
background: url(../images/more.png) no-repeat;
background-position: right center;
height: 12px;
}
Note that I have used right center to define the X and Y axis, and also defined the height of the image, which may be different for you.
Hope this helps

Fixed position div with 'top' relative to parent div

The basic bootstrap template here has a fixed bar at the top. I want to place a div directly underneath it.
Here's the HTML of that bar (copied straight from the page's source so there are CSS class references):
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar collapsed" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
Project name
<div class="nav-collapse collapse" style="height: 0px;">
<ul class="nav">
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li class="nav-header">Nav header</li>
<li>Separated link</li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form pull-right">
<input type="text" placeholder="Email" class="span2">
<input type="password" placeholder="Password" class="span2">
<button class="btn" type="submit">Sign in</button>
</form>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
Here's my attempt at a div to go underneath it:
<div style="background-color:#CF4342;color:#fff;top:40px;margin:0 auto;position:fixed;z-index:5000; width:50px;">Hello</div>
It almost works but the problem with this is the 'top:40px;'. When you resize the screen, the fixed bar at the top changes height.
How do I make it so that it always sits directly underneath the bar regardless of the bar's height? Bonus points for how to center it horizontally without using <center>
Edit: for the horizontal centering thing, i tried wrapping my div in a div with 100% width and then adding 'margin:0 auto' to it, but that doesn't work with fixed position
Edit2: here is the jsfiddle. line 38 of the html is my attempt, everything above that is the nav bar div.
If you're already using jQuery you could use something like this:
Working Example
Full screen example
x = (function() {
var t = $('.navbar').height();
var c = $(window).width() / 2 - $('.new').width() / 2;
$('.new').css({
top: t,
left: c
});
});
$(document).ready(x);
$(window).resize(x);
Note: updated examples with media queries to better show the effect of the script.
Of course you can do this with CSS alone, but you will need to use a media query like so:
CSS only working example
.new {
position:fixed;
margin: 0 auto;
top: 51px;
left:0;
right:0;
background-color:#CF4342;
color:#fff;
z-index:5000;
width:50px;
}
#media (max-width: 979px) {
.new {
top: 61px;
}
}
Notice that the css only solution will "break" if you narrow the screen too far. The jQuery solution won't have that problem.
Try to add this line in your css or edit the class in the bootstrap css file. Make sure your css file is under the bootstrap css file incase you choose to add it in your own. Does it make sense?
.navbar-fixed-top {
margin-bottom: 0 !important;
}
Bootstrap says the following in it's documentation:
Add .navbar-fixed-top and remember to account for the hidden area
underneath it by adding at least 40px padding to the <body>. Be sure
to add this after the core Bootstrap CSS and before the optional
responsive CSS.
So that is what i would try...
I updated your fiddle to demonstrate http://jsfiddle.net/Pevara/MgcDU/5403/
I did the following:
- Moved your 'hello inside the container with the hero unit
- Removed the positioning on the 'hello'
- Removed the 10px margin you set on the .container
- Added the following to your css, as suggested by Bootstrap
body {
padding-top: 40px;
}