I have an navbar with centered li items. Two of this li items should have an dropdown while hovering over them. I tried this but the dropdown-content appears in the navbar container.
I tried this with the .dropdown and while hovering over it, the .dropdwon-content should be visible under the red border of the navbar.
My Code:
function resNavBar() {
var x = document.getElementById("idTopNav");
var y = document.getElementById("idHomeBtn");
var z = document.getElementById("idIconClose");
var zz = document.getElementById("idIconMenu");
if (x.className === "topnav" & y.className === "homebtn") {
x.className += " responsive";
y.className += " responsive";
z.className = "icon-close";
zz.className += " invisible";
} else {
x.className = "topnav";
y.className = "homebtn";
z.className += " invisible";
zz.className = "icon-menu";
}
}
* {
padding: 0;
margin: 0;
font-family: 'Roboto', sans-serif;
}
body {
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
height: 100%;
}
html {
height: 100%;
}
body * {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
header .topnav {
background-color: #201D1D;
overflow: hidden;
border-bottom: 4px solid #A90101;
}
header .topnav li {
list-style: none;
list-style-type: none;
}
header .topnav li a {
color: #fff;
display: block;
text-align: center;
float: left;
font-family: 'Roboto Condensed', sans-serif;
font-size: 25px;
text-decoration: none;
padding: 0.5em 1.2em;
}
header .topnav li a:hover {
background-color: #A90101;
color: #201D1D;
}
header .topnav .icon-home {
display: none;
font-size: 1.5rem;
padding: 0.5em 1em;
color: #fff;
}
header .topnav .topnav a.icon-menu,
header .topnav .topnav a.icon-close {
display: none;
}
header .topnav .dropdown {
position: relative;
display: inline-block;
}
header .topnav .dropdown .dropdown-content {
display: none;
position: absolute;
background-color: #f8f9fa;
min-width: 10vw;
z-index: 1;
padding: 12px 16px;
}
header .topnav .dropdown:hover .dropdown-content {
display: block;
}
// Responsive:
#media screen and (max-width: 1000px) {
.topnav ul li a {
display: none;
}
.topnav a.icon-home {
float: left;
display: block;
}
.topnav a.icon-menu {
float: right;
display: block;
font-size: 1.5rem;
padding: 0.5em 1em;
color: #fff;
}
.topnav a.icon-close {
float: right;
display: block;
font-size: 1.6rem;
padding: 0.5em 1em;
color: #fff;
}
}
#media screen and (max-width: 1000px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive a.icon-home {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a.icon-menu {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a.icon-close {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.homebtn.responsive {
display: none;
}
.topnav a.icon-close.invisible {
display: none;
}
.topnav a.icon-menu.invisible {
display: none;
}
}
#media screen and (min-width: 1000px) {
.topnav-mid {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
text-align: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.topnav a.icon-close {
display: none;
}
.topnav a.icon-menu {
display: none;
}
}
<!---HTML:--->
<header>
<nav class="topnav">
<div class="homebtn" id="idHomeBtn">
<a href="index.html" class="icon-home" onclick="none">
<i class="fas fa-home"></i>
</a>
</div>
<ul class="topnav-mid">
<li>link</li>
<li>link</li>
<li>link</li>
<li class="dropdown">link
<!--! DROPDOWN -->
<div class="dropdown-content dropdown-content-about">
link
<li>link</li>
</ul>
<a href="javascript:void(0);" class="icon-close invisible" id="idIconClose" onclick="resNavBar()">
<i class="fas fa-times"></i>
</a>
<a href="javascript:void(0);" class="icon-menu" id="idIconMenu" onclick="resNavBar()">
<i class="fas fa-bars"></i>
</a>
</nav>
</header>
<script src="js/responsive.js"></script>
Has anyone an idea why the dropdown is in the navbar?
The dropdown is in the navbar because you don't give it a position. Just add a top attribute to the .dropdown-content.
header .topnav .dropdown .dropdown-content {
display: none;
position: absolute;
background-color: #f8f9fa;
min-width: 10vw;
z-index: 1;
padding: 12px 16px;
top: 100%;
}
And you need to get rid of the overflow on your .topnav element.
Related
body {
margin: 0;
font - family: Arial,
Helvetica,
sans - serif;
}
.topnav {
overflow: hidden;
background - color: #333;
}
.topnav a {
float: left;
display: block;
color: # f2f2f2;
text - align: center;
padding: 14 px 16 px;
text - decoration: none;
font - size: 17 px;
}
.topnav a: hover {
background - color: #ddd;
color: black;
}
.topnav a.active {
background - color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
#media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: # 5 c6664;
background - image: none;
flex: 1;
padding: 0 0.5e m;
color: #fff;
cursor: pointer;
font - size: 1e m;
font - family: "Open Sans", sans - serif;
}
select::-ms - expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20e m;
height: 3e m;
line - height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "\25BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: # 2 b2e2e;
cursor: pointer;
pointer - events: none;
transition: 0.25 s all ease;
}
.select: hover::after {
color: #23b499;
}
<div class="topnav" id="myTopnav">
Red FMTuning
Choose a tune from ur favourite
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div>
</a>
About
<a href="javascript:void(0);" class="icon" #click="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
As in the above image, u can see the , all the elements like "tuning", "Choose a tune from ur favourite" , "Choose a book format", ""About". All the contents are not in proper shape. and mainly the dropdown("Choose a book format") it is in not aligned according to other content. and when click on dropdown also. it is overlapping on content.
I need to align all the elements until it reaches to some 1200px like below.
Code:- https://jsfiddle.net/se8pa7m3/2/
I have wrapped "Choose a book format", "About" <div class="topnav-inner-wrap"> and applied flexbox css on .topnav & .topnav-inner-wrap
Working Demo: https://jsfiddle.net/k2beLnrv/
function myFunction(){
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
display: flex;
align-items: center;
justify-content: space-between;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04aa6d;
color: white;
}
.topnav .icon {
display: none;
}
#media screen and (max-width: 600px) {
.topnav > a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block;
text-align: left;
}
}
#media screen and (max-width: 1200px) {
.topnav > a:not(:first-child), .topnav .topnav-inner-wrap {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 1200px) {
.topnav.responsive {
position: relative;
display: block;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a,
.topnav.responsive .topnav-inner-wrap {
float: none;
display: block !important;
text-align: left;
}
}
/* select dropdown css code */
select {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
outline: 0;
box-shadow: none;
border: 0 !important;
background: #5c6664;
background-image: none;
flex: 1;
padding: 0 0.5em;
color: #fff;
cursor: pointer;
font-size: 1em;
font-family: "Open Sans", sans-serif;
}
select::-ms-expand {
display: none;
}
.select {
position: relative;
display: flex;
width: 20em;
height: 3em;
line-height: 3;
background: #5c6664;
overflow: hidden;
border-radius: 0.25em;
}
.select::after {
content: "\25BC";
position: absolute;
top: 0;
right: 0;
padding: 0 1em;
background: #2b2e2e;
cursor: pointer;
pointer-events: none;
transition: 0.25s all ease;
}
.select:hover::after {
color: #23b499;
}
.topnav-inner-wrap {
display: flex;
align-items: center;
}
<div class="topnav" id="myTopnav">
Red FMTuning
Choose a tune from ur favourite
<div class="topnav-inner-wrap">
<a href="#contact">
<div class="select">
<select name="format" id="format">
<option selected>Choose a book format</option>
<option value="pdf">PDF</option>
<option value="txt">txt</option>
<option value="epub">ePub</option>
<option value="fb2">fb2</option>
<option value="mobi">mobi</option>
</select>
</div></a
>
About
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
Check this fiddle. You can use flexbox for this scenario. Add these properties to your topnav:
display: flex;
align-items: center;
justify-content: space-evenly;
align-items is used to align the items vertically, and justify-content is used to space them equally. You can only use both properties if you've set the display to flex.
More on flexbox here.
Use justify-content:space-between; in the parent class
this is a problem only in mobile. here's the image https://i.stack.imgur.com/LvS77.png
this is the navbar html:
<div class="topnav" id="myTopnav">
LOGO
example
<a href="javascript:void(0);" class="icon" onclick="mob()">
<i class="bars">
<div></div>
<div></div>
<div></div>
</i>
</a>
</div>
and this is the css:
.topnav {
background-color: #333;
overflow: hidden;
}
.topnav a {
float: right;
display: block;
color: #dfdfdf;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.bars div {
width: 35px;
height: 5px;
background-color: black;
margin: 6px 0;
}
.topnav .icon {
display: none;
}
.topnav .logo {
float:left;
}
#media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
p.s. the responsive class is added with javascript when the three bars in mobile is clicked
Thanks
I added this css
.topnav .logo {
flex: 1 1 auto;
text-align: left;
}
.topnav {
display: flex;
}
Is this something you want?
Don't use float in your css. We have amazing css properties flexbox for layouting. Float destroys layouting.
.topnav {
background-color: #333;
overflow: hidden;
display: flex;
}
.topnav a {
display: block;
color: #dfdfdf;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.bars div {
width: 35px;
height: 5px;
background-color: black;
margin: 6px 0;
}
.topnav .icon {
display: none;
}
.topnav .logo {
flex: 1 1 auto;
}
.topnav .logo a {
display: inline-block;
text-align: left;
}
#media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
<div class="topnav" id="myTopnav">
<div class="logo">
LOGO
</div>
example
<a href="javascript:void(0);" class="icon" onclick="mob()">
<i class="bars">
<div></div>
<div></div>
<div></div>
</i>
</a>
</div>
Can someone help me to align the search bar beside my icon? This is how it currently looks like.
Before expanding :
After expanding :
I want to have my search bar beside my image after expanding, can someone help me out here? Thanks!
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.topnav {
background-color: transparent;
}
.topnav a {
display: inline-block;
color: black;
padding: 14px 16px;
text-decoration: none;
font-size: 16px;
}
.topnav .icon {
display: none;
}
.dropdown {
float: right;
padding-top: 5px;
}
.hehe {
float: right;
padding-top: 5px;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover,
.dropdown:hover .dropbtn {
background-color: transparent;
color: skyblue;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 900px) {
.topnav a:not(:nth-child(-n+2)),
.dropdown .dropbtn {
display: none;
}
.topnav a:not(:nth-child(-n+2)),
.hehe {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 900px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {
float: none;
}
.topnav.responsive .hehe {
float: none;
}
.topnav.responsive .dropdown-content {
position: relative;
}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
.topnav.responsive .hehe {
display: block;
width: 100%;
text-align: left;
}
}
<div class="topnav" id="myTopnav" ng-controller="searchController">
<img ng-src="" />LOGO
<input type="text" class="searchFeature" ng-model="selected" ng-keyup="$event.keyCode == 13 && searchFunction()" uib-typeahead="value for value in themename | filter:$viewValue | limitTo:7" placeholder="Start your search here...">
<div class="hehe">
Developer
Data Enquiry
</div>
<div class="dropdown">
<button class="dropbtn">Categories <i class="fa fa-caret-down"></i></button>
<div class="dropdown-content" >
<a ng-click="pickCate(x)" ng-repeat="x in selectCate">{{ x }}</a>
</div>
</div>
☰
</div>
Try adding, position: relative; to the nav, and an absolute for .searchFeature
.topnav{
position: relative;
}
.searchFeature{
position: absolute;
top: 15px;
left: 65px;
}
Here you have a codepen!
Alright, what I am trying to do is, I want the button beside my text box to be aligned beside the text box whenever my page resize, which would cause my text box to shrink or expand accordingly ( as you can see below in the following snippet) Can someone help me out here? Thanks in advance for the help!
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.topnav {
background-color: transparent;
}
.topnav a {
display: inline-block;
color: black;
padding: 14px 16px;
text-decoration: none;
font-size: 16px;
}
.topnav .icon {
display: none;
}
.dropdown {
float: right;
padding-top: 5px;
}
.hehe {
float: right;
padding-top: 5px;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav a:hover,
.dropdown:hover .dropbtn {
background-color: transparent;
color: skyblue;
}
.dropdown-content a:hover {
background-color: #ddd;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
#media screen and (max-width: 900px) {
.topnav a:not(:nth-child(-n+2)),
.dropdown .dropbtn {
display: none;
}
.topnav a:not(:nth-child(-n+2)),
.hehe {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 900px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {
float: none;
}
.topnav.responsive .hehe {
float: none;
}
.topnav.responsive .dropdown-content {
position: relative;
}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
.topnav.responsive .hehe {
display: block;
width: 100%;
text-align: left;
}
}
.searchFeature {
border: 1px solid #999;
padding: 3px 18px 3px 4px; /* Use the same right padding (18) in jQ! */
border-radius: 3px;
/* transition: background 0.4s; */
width:40%;
position: absolute;
top: 18px;
left: 75px;
}
.test {
position: absolute;
top: 20px;
left: 350px;
}
<div class="topnav" id="myTopnav" ng-controller="searchController">
<img ng-src="" />LOGO
<input type="text" class="searchFeature" ng-model="selected" ng-keyup="$event.keyCode == 13 && searchFunction()" uib-typeahead="value for value in themename | filter:$viewValue | limitTo:7" placeholder="Start your search here..."><button class="test">Test</button>
<div class="hehe">
Developer
Data Enquiry
</div>
<div class="dropdown">
<button class="dropbtn">Categories <i class="fa fa-caret-down"></i></button>
<div class="dropdown-content" >
<a ng-click="pickCate(x)" ng-repeat="x in selectCate">{{ x }}</a>
</div>
</div>
☰
</div>
Remove absolute positioning from both .test and .searchFeature, then wrap them in a flexbox. I also recommend adding a min-width to the input.
HTML:
<div class="search-wrapper">
<input type="text" class="searchFeature" ng-model="selected" ng-keyup="$event.keyCode == 13 && searchFunction()" uib-typeahead="value for value in themename | filter:$viewValue | limitTo:7" placeholder="Start your search here...">
<button class="test">Test</button>
</div>
CSS:
.search-wrapper {
display: flex;
flex-direction: row;
}
.test {}
.searchFeature {
border: 1px solid #999;
padding: 3px 18px 3px 4px; /* Use the same right padding (18) in jQ! */
border-radius: 3px;
/* transition: background 0.4s; */
width:40%;
}
* {
box-sizing: border-box;
font-family: sans-serif;
margin: 0;
padding: 0;
}
html,
body {
font-size: 14px;
}
header {
width: 100vw;
height: 50px;
background-color: #222;
margin: 0 auto;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
}
/* Container for the Left and Right nav sections*/
header>div {
display: flex;
justify-content: space-between;
align-items: center;
max-width: 1140px;
width: 100%;
padding: 0 20px;
}
/* adjust width to set size */
nav {
width: 70%;
display: inline-flex;
justify-content: space-between;
}
nav ul {
list-style-type: none;
display: flex;
padding: 0 20px;
}
nav a {
padding: .8rem 1rem;
}
header>div>a {
font-size: 18px;
border-bottom:
}
.a-tag-header {
text-decoration: none;
color: #999;
display: block;
}
nav a:hover {
color: #fff;
}
/* -- Menu Toggle -- */
.MenuToggle {
display: none;
}
#media screen and (max-width: 890px) {
/* Header */
header {
height: auto;
}
header>div {
flex-direction: column;
padding: 0;
}
nav {
display: flex;
width: 100%;
flex-direction: column;
}
nav ul {
flex-direction: column;
text-align: center;
align-items: center;
padding: 0;
}
nav ul li {
height: 50px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
nav ul li:hover {
background-color: aqua;
}
header>div>a {
padding: .8rem 0;
}
header>div>a:hover {
background-image: #eee;
}
/* -- Menu Toggle -- */
.MenuToggle {
display: inline-block;
color: #F0F0F0;
font-size: 22px;
cursor: pointer;
position: absolute;
}
}
<header>
<div>
<a class="a-tag-header">Chemical Finger Print Analysis</a>
<div class="MenuToggle">
<span id="MenuToggleButton" onclick="NavToggle()">☰</span>
</div>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Data</li>
<li>Reports</li>
</ul>
<ul>
<li>Register</li>
<li>Login</li>
</ul>
</nav>
<div class="MenuToggle">
<span id="MenuToggleButton" onclick="NavToggle()">☰</span>
</div>
</div>
</header>
Hi i'm creating a flexbox navigation bar (Desktop First), this contains a title, and a nav with two separate tags. Iv'e gotten the nav and ul tags to act in a responsive manner however I cannot seem to figure out how to get the title and the menu toggle to share the top container responsively when a screen is smaller than 850px, I've tried a number of fixes but i can't seem to figure it out. Please Help
Try this
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
#media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="topnav" id="myTopnav">
Home
News
Contact
About
☰
</div>
</body>
</html>