I have a div here which contains 3 dropdowns:
<div class="text-center container">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" id="make" type="button" data-toggle="dropdown"> Subject
<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="makein">
<li>bmw</li>
<li>mercedes</li>
<li>mazda</li>
<li>ford</li>
<li>lada</li>
<li>audi</li>
<li>skoda</li>
<li>fiat</li>
</ul>
</div>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" id="year" type="button" data-toggle="dropdown"> Year
<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="yearin">
<li>2016</li>
<li>2015</li>
<li>2014</li>
<li>2013</li>
<li>2012</li>
<li>2011</li>
<li>2010</li>
<li>2009</li>
</ul>
</div>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" id="level" type="button" data-toggle="dropdown"> Level
<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="levelin">
<li>luxury</li>
<li>ordinary</li>
</ul>
</div>
</div>
I use the following to center the div in the middle of the screen centered:
.container{
width: 300px;
height: 300px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.dropdown{
display: inline-block;
}
It also aligns my drop downs horizontally.
I locate my div in the middle of the screen using this custom css. I am wondering if there is a way to avoid using the custom css and do this same thing just purely using Bootstrap.
My aim is:
1: Locate the container in the center in the middle of the page.
2: Horizontally align the dropdowns.
Bootstrap shipped with container and text-center classes then why you are defining your own container class? Just include bootstrap files properly in your code and no need for this custom CSS.
text-center class will align your drop down in middle of screen and container class has fixed width with auto margin. Check jsFiddle
To achieve expected result , use below CSS to container
.container{
width: 300px;
height: 60%;
position: absolute;
top:20%;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
http://codepen.io/nagasai/pen/LkoLGw
There are no way in bootstrap to locate div in the middle of the screen.
you may check below custom css code:
.container {
position: absolute;
width: 100%;
top: 45%;
}
.dropdown {
display: inline-block;
}
Related
I have a problem with the positioning (?) of my div boxes. I have a div box for my navigation bar.
I want a div box right under the navigation bar in the center, which can be filled with some text.
I tried:
.navigation {
background-color: black;
position: fixed;
width: 1920px;
left: 0px;
top: 0px;
}
.inhalt {
position: realative;
margin: 0px auto;
width: 600px;
top: 600px;
background-color: white;
}
<div class="navigation">
<table>
<tr>
<th><img src="logo.jpeg"></th>
<th></th>
<th></th>
<th>Startseite</th>
<th>Beats</th>
<th>Preise</th>
<th>Kontakt</th>
</tr>
</table>
</div>
Thanks for any answer!!
1- first, .inhalt class position must be "relative" not "position:realative;" as you have written above so the "top" property could work.
2- give height to your second box so you can see it.
3- When elements are positioned, they can overlap other elements. you can solve this by either give your navigation bar "sticky" position or if you want to use fixed position, use "top" property to solve the problem. I add a snippet for this solution.
4- you can use first give your navigation bar relative position and then on scrolling give it a fixed position. see the example on codepen.
.navigation{
background-color: black;
position: fixed;
width: 100%;
left: 0px;
top: 0px;
z-index: 99999;
}
.inhalt{
position:relative;
width: 600px;
height: 500px;
top: 30px;
margin: 0px auto;
background-color: lightblue;
}
<div class="navigation">
<table>
<tr>
<th><img src="logo.jpeg"></th>
<th></th>
<th></th>
<th>Startseite</th>
<th>Beats</th>
<th>Preise</th>
<th>Kontakt</th>
</tr>
</table>
</table>
</div>
<div class="inhalt"></div>
Your question is vague, but this should give you some idea to work with:
.navigation {
background-color: black;
float: left;
position: fixed;
width: 1920px;
left: 0px;
top: 0px;
float: left
}
.inhalt {
float: left;
text-align: center;
top: 15%;
left: 10%;
margin: 0px auto;
width: 100%;
background-color: white;
font-size: 20px;
}
<div id="wrapper">
<div class="navigation">
<table>
<tr>
<th><img src="logo.jpeg"></th>
<th></th>
<th></th>
<th>Startseite</th>
<th>Beats</th>
<th>Preise</th>
<th>Kontakt</th>
</tr>
</table>
</div>
<div class="inhalt">
<p> Some random Text </p>
</div>
</div>
Also you should prefer using lists <li> for your navigation, since tables can be a nightmare to work with in that particular usecase.
Ok so in addition to what explained Phanti, your HTML has an issue as there are two times the closing tab
That would be my CSS I removed some of the instructions that were not making sense to me like the fix width:
.navigation {
background-color: black;
float: left;
position: fixed;
width: 100%;
height:40px;
left: 0px;
top: 0px;
float: left
}
.inhalt {
float: left;
text-align: center;
margin-top: 60px;
margin: 0px auto;
width: 100%;
background-color: white;
font-size: 20px;
}
That would be my HTML :
<div id="wrapper">
<div class="navigation">
<table>
<tr>
<th><img src="logo.jpeg"></th>
<th></th>
<th></th>
<th>Startseite</th>
<th>Beats</th>
<th>Preise</th>
<th>Kontakt</th>
</tr>
</table>
</div>
<div class="inhalt">
<p> Some random Text </p>
</div>
</div>
Overall, since you seem to be starting on html and CSS, I would storngly advise you to start with a CSS Framwork like bootstrap, you will be able to easily build from scratch responsive website by relying on predefined classes and module (especially for menu where moving from desktop to tablet to mobile is very painful).
If you were to use Bootstrap 4 here would be a code working on all support :
Insert this between your :
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
Then add this in your code for the navigation and your content
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<p>Some content</p>
</div>
</div>
</div>
<!-- HTML -->
div class="inhalt">
<p> some text </p>
</div>
/*-- CSS-- */
.inhalt{
margin: 10px auto 0;
width: 50%;
}
This is the perfect way of centring the div element.
i have a problem with my navbar notification button,
the button resize the navbar.
example
here is the code
.badge-notify{
background:red;
position:relative;
top: -20px;
left: -35px;
}
<ul class="nav navbar-nav navbar-right">
<li><a href="#">
<button class="btn btn-lg btn-link">
<span class="glyphicon glyphicon-envelope"></span>
</button>
<span class="badge badge-notify">3</span>
</a></li>
i dont know if a css problem or what, the button creates a blank space on navbar.
posibily the problem is
Firstly, buttons inside links are invalid HTML so either use just a link or just a button...not both.
You need to put the second span inside the button (or link) and the position it absolutely.
.btn-link {
position: relative;
}
.badge-notify {
background: red;
position: absolute !important;
top: -15px; /* adjust as required*/
right: 5px; /* adjust as required*/
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav navbar-nav navbar-center">
<li>
<button class="btn btn-lg btn-link">
<span class="glyphicon glyphicon-envelope"></span>
<span class="badge badge-notify">3</span>
</button>
</li>
</ul>
I'm trying to create a portfolio in which when the user scrolls, a small bar icon is visible and fixed. On clicking, it should display the tools in navigation bar like "about me, contact me".
I tried using position: fixed; but it isn't working.
and I can't seem to get this bar icon to the left side of the page. Tried giving it different margin and padding and the drop down isn't working either.
.fa {
transform: scale(0.5,0.5);
}
.dropdown{
position: fixed;
}
<div class="container">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle pull-left" type="button" id="menu1" data-toggle="dropdown"><i class="fa fa-bars" aria-hidden="true"></i></button>
<span class="caret"></span></button>
<nav role="navigation">
<ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Home</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">About</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Contact</a></li>
</ul>
</nav>
</div>
</div>
.dropdown not must be child another elem, except body
body {
position: relative;
}
.dropdown{
position: fixed;
}
Example:
body {
position: relative;
height: 200vh;
background: linear-gradient(orange 0, red 50%, pink 100%);
}
.dropdown{
position: fixed;
width: 140px;
height: 180px;
background-color: blue;
}
<div class="dropdown">
</div>
I have a navigation menu built using bootstrap. The menu is filled with buttons that introduce drop down menus once clicked.
Here is the navigation menu:
How can I change the following code to align the dropdown menus to the absolute right (floating right) of the selected button. Despite being contained by the parent div.
Here is my code:
<div class="f_profile_add col-xs-12 col-sm-4 col-md-3 np">
<nav>
<ul class="list-unstyled nm">
<li>
<div class="dropdown">
<button id="btn-add-trybe" class="btn btn-block btn-default btn-lg rmr pbpad dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="glyphicon glyphicon-th"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenu1">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li>Separated link</li>
</ul>
</div>
</li>
</ul>
</nav>
</div>
And here is what to happen when a button is clicked:
Can you offer me a solution?
Thanks
Update
The code that got me the result is as follows:
.absolute-right-dropdown-menu {
position: absolute;
top: 0;
left: 74px;
/* Note opposite values */
right: -20rem;
width: 20rem;
}
I added this to the dropdown-menu. Resulting in the following:
You can achieve this with CSS absolute-positioning on your .dropdown-menu, as in the code below.
See this JSFiddle for demo.
.dropdown ul.dropdown-menu {
position: absolute;
top: 0;
/* Note opposite values */
right: -20rem;
width: 20rem;
}
Notes
In Bootstrap 3, class .dropdown already has position:relative;, and .dropdown-menu is normally a descendant of .dropdown, so you can absolute-position the .dropdown-menu based on its .dropdown.
When you specify the width of .dropdown-menu, be sure that your specified width is not less than the min-width that will be determined by the menu's content (e.g. in this example, 10rem is too thin).
The width and right styles of .dropdown-menu should be equal and opposite values.
I have a drop down menu that opens a row of icons underneath it (see picture 1, names and pictures were removed to maintain privacy)
I am trying to make this menu to open to the right of choose child, and not underneath it (see picture 2, of what I am trying to get. the Images will open to the right of the drop down and not under it)
My current code is:
<div class="span4">
<div class="btn-group">
<a class="btn btn-link dropdown-toggle" data-toggle="dropdown" href="#">
<h3>Choose Child</h3>
<img id="mainIcons" src="boyStudent.png" alt="boyStudent">
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>
<div class="btn-group">
<a href="parent_homepage_2.html" style="text-align:center;" class="btn btn-link operationsButtons">
<h5>A</h5>
<img id="mainIcons" src="boyStudent.png" alt="boyStudent"></a>
<a href="parent_homepage_2.html" style="text-align:center;" class="btn btn-link operationsButtons">
<h5>J</h5>
<img id="mainIcons" src="girlStudent.png" alt="girlStudent"></a>
</div>
</li>
</ul>
I tried to use the following example, that helps push the drop down to the left
how to make twitter bootstrap submenu to open on the left side?
but I could not figure out a way to use this to what I am looking to do
Thanks
How does this look:-
Demo
Demo2 Submenu dropping down.
<div class="span4">
<div class="btn-group"> <a class="btn btn-link dropdown-toggle" data-toggle="dropdown" href="#">
<h3>Choose Child</h3>
<img id="mainIcons" src="boyStudent.png" alt="boyStudent">
<span class="right-caret right"></span>
</a>
<ul class="dropdown-menu rightMenu">
<li>
<div class="btn-group"> <a href="parent_homepage_2.html" style="text-align:center;" class="btn btn-link operationsButtons">
<h5>A</h5>
<img id="mainIcons" src="boyStudent.png" alt="boyStudent"></a>
<a href="parent_homepage_2.html" style="text-align:center;" class="btn btn-link operationsButtons">
<h5>J</h5>
<img id="mainIcons" src="girlStudent.png" alt="girlStudent"></a>
</div>
</li>
</ul>
</div>
css
.rightMenu {
position:relative;
float:right;
}
.right-caret {
border-bottom: 4px solid transparent;
border-top: 4px solid transparent;
border-left: 4px solid #000000;
display: inline-block;
height: 0;
opacity: 1;
vertical-align: top;
width: 0;
}
.right
{
float:right;
}
Just add the class "pull-right" into the <ul class="dropdown-menu"> this way...
<li class="dropdown">
<a class="dropdown-toggle" href="#">Link</a>
<ul class="dropdown-menu pull-right">
<li>...</li>
</ul>
</li>
I just add this class to the dropdown-menu:
.rightMenu {
position:absolute;
float:right;
top: 0;
left: 160px;
}
And it aligns perfectly.
To those who may find this page in the future, the now official way of doing this is with adding the .dropdown-menu-right class to your dropdown list.