How to force menu to open upwards in a searchable dropdown? - html

I have an html code below
Dropdown-->
About
Base
Blog
Contact
Custom
Support
Tools
Its basically taken from this website https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_filter
This is how it looks like
It does a search on the items displayed in the menu. Now I want this menu to open upwards and not downwards
On looking online, I found I have to provide bottom: 100%; Drop-down menu that opens up/upward with pure css. Now in my case, there are no <ul> or <li> tags. So this is what I did
.dropdown {
position: relative;
display: inline-block;
bottom: 100%; //added this
}
But it doesn't do anything. How can I force the menu to show upwards?

You could do this using Flexbox:
.dropdown-content.show {
display: flex;
flex-direction: column-reverse;
bottom: 45px; /* hardcoded height of the button */
}
Using w3cschool's existing code:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
var dropdown, button;
dropdown = document.getElementById("myDropdown");
button = document.getElementById("myButton");
dropdown.classList.toggle("show");
dropdown.style.bottom = button.offsetHeight + "px";
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover,
.dropbtn:focus {
background-color: #3e8e41;
}
#myInput {
box-sizing: border-box;
background-image: url('searchicon.png');
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: none;
border-bottom: 1px solid #ddd;
}
#myInput:focus {
outline: 3px solid #ddd;
}
.dropdown {
position: relative;
display: inline-block;
padding-top: 250px; /* just for demo purposes */
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f6f6f6;
min-width: 230px;
overflow: auto;
border: 1px solid #ddd;
z-index: 1;
flex-direction: column-reverse; /* show children in a reversed colum (bottom to top) */
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {
background-color: #ddd;
}
.show {
display: flex;
}
<h2>Search/Filter Dropdown</h2>
<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>
<div class="dropdown">
<button id="myButton" onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
About
Base
Blog
Contact
Custom
Support
Tools
</div>
</div>
EDIT:
I just removed the hardcoded button height from the CSS and instead calculated the height using Javascript.
See the modified function myFunction() and the added id="myButton" in the markup.

Related

Breadcrumbs and cart display under navbar

I would like to add two elements under my navbar. On the right side the cart of which you can see the content by clicking on it and on the other side, the breadcrumbs both aligned on a same row. cf. pic related
The whole thing would be dynamic also. I spent the whole day trying to find solutions without success. What would be the best way to do it? Flex display?
Thank you in advance for your help. Here is my code:
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.breadcrumbs ul{
list-style-type: none;
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
.flex-container {
display: flex;
flex-direction: row;
font-size: 30px;
text-align: center;
}
.flex-item-left {
background-color: #f1f1f1;
padding: 10px;
flex: 50%;
}
.flex-item-right {
background-color: dodgerblue;
padding: 10px;
flex: 50%;
}
/* Responsive layout - makes a one column-layout instead of two-column layout */
#media (max-width: 800px) {
.flex-container {
flex-direction: column;
}
}
<div class="flex-container">
<div class="flex-item-left">
<div class="breadcrumbs">
<ul>
<li>
Home
</li>
<li>
<a></a>
</li>
</ul>
</div>
</div>
<div class="flex-item-right">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Cart</button>
<div id="myDropdown" class="dropdown-content">
<span>Cart item</span>
</div>
</div>
</div>
</div>
Two solutions come to mind.
Use margin: left on the button to push it to the further point available.
Use flex-grow: 1 or flex: 1 on the breadcrumb bar so that it uses all the available space except for that used by your button.
Working Codepen example here
Well, you are already doing it correctly, you just have to replace the <span>Cart Item</span> with your actual items, as of the formatting you don't really need to use flex since every item will be block scoped div here's what you will have to do to generate the dynamic content:
<div class="flex-item-right">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Cart</button>
<div id="myDropdown" class="dropdown-content">
<?php
foreach($items as $item) {
?>
<div>
<?php $item['name'].' '.$item['quantity'].'x'; ?>
</div>
<?php
}
?>
</div>
</div>
</div>
and similarly you can add the total amount and go to cart button after the loop ends.
UPDATE
I think I got the question wrong, you want your cart button to go extreme right and let the right side take whole space, here's what you should do:
<div class="flex-container">
<div class="flex-item-left flex-grow-1">
...
</div>
<div class="flex-item-right">
...
</div>
</div>
Add ``flex-grow``` property to your stylesheet as:
.flex-grow-1 {
flex-grow:1;
}
and change the existing styles:
.flex-item-left {
background-color: #f1f1f1;
padding: 10px;
}
.flex-item-right {
background-color: dodgerblue;
padding: 10px;
}
UPDATE <800px
Simply make your .flex-container block scoped and it will center automatically:
#media (max-width: 800px) {
.flex-container {
display:block;
}
Working Code Snippet
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.breadcrumbs ul{
list-style-type: none;
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
.flex-container {
display: flex;
flex-direction: row;
font-size: 30px;
text-align: center;
}
.flex-item-left {
background-color: #f1f1f1;
padding: 10px;
}
.flex-item-right {
background-color: dodgerblue;
padding: 10px;
}
.flex-grow-1{
flex-grow:1;
}
/* Responsive layout - makes a one column-layout instead of two-column layout */
#media (max-width: 800px) {
.flex-container {
display:block;
}
}
<div class="flex-container">
<div class="flex-item-left flex-grow-1">
<div class="breadcrumbs">
<ul>
<li>
Home
</li>
<li>
<a></a>
</li>
</ul>
</div>
</div>
<div class="flex-item-right">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Cart</button>
<div id="myDropdown" class="dropdown-content">
<span>Cart item</span>
</div>
</div>
</div>
</div>

how to create a drop down menu in sidebar in html5 CSS3

sending in enter image description here the screenshot of the code
You need to add a bit of javascript to do that.. look at this example its pretty straightforward.
<head>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
<style>
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.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;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
</style>
</head>
<body>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
CLoud
Data
Digitale
Commerece
Crm/Erp
About Us
</div>
</div>
</body>

how to add a clickable dropdown menu in blogger?

So, I found this link on clickable dropdown menus but when I implement that, it doesn't work for some reason. I tried adding the html into the gadget but I don't know where to put the JavaScript in.
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.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;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
This is the same code used in the example you sent, you can get the javascript and insert at the end of the html between two script tags like this:
<script type="text/javascript">
<!-- javascript GOES HERE -->
<script>
You can add javascript code inside html putting it inside <script></script> tag
Create Drop down menu in blogger using add custom Gadget. In this you can add your own codes. This requires a little bit of CSS and HTML knowledge. Try my code to create drop down menu in blogger. referenced from this blogger post
.dropdown-section1{
border-radius:3px;
width:80%;margin:0 auto;
background:#607d8b;
height:38px;
-webkit-box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
-moz-box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
}
.dropdown-section1 .menu li a {
color:#fff;
font-weight:bold;
text-decoration:none;
}
.dropdown-section1 .menu li {
list-style: none;
float: left;
padding: 10px;
background:#607d8b;
}
.dropdown-section1 .menu li:last-child{
border-right:none !important;
}
.dropdown-section1 .menu .sub-menu li{
float: none;
margin-bottom:0px;
padding: 10px;
}
.dropdown-section1 .sub-menu {
display: none;
position: absolute;
z-index: 100;
width:100px;
margin-left: -50px !important;
margin-top: 10px !important;
}
.dropdown-section1 .menu li:hover > .sub-menu {
display: block;
}
.dropdown-section1 .menu li:hover {
background:#94c018;
}
.dropdown-section1 .menu > li {
border-right:1px solid #ccc;
}
.dropdown-section1 .place-right{
left: 150px;
top: 30px;
}
<div class="dropdown-section1">
<ul class="menu">
<li>Home</li>
<li>Coose Niche
</li><li>Create Blog
</li><li>Moniter Blog
</li><li>DropDown
<ul class="sub-menu">
<li>String</li>
<li>Array
<ul class="sub-menu place-right">
<li>String</li>
<li>Array</li>
<li>Match</li>
</ul>
</li>
<li>Match</li>
</ul>
</li>
<li>About Me</li>
<li>Reach Me</li>
</ul>
</div>

How to move drop-down menu in html/css?

How do I move the drop down menu onto a certain area ?
HTML CODE:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Mirror Page
Link 2
Link 3
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
CSS CODE:
/* Dropdown Button */
.dropbtn {
background-color: #262829;
color: white;
padding: 16px;
font-size: 16px;
right:80px;
border: none;
cursor: pointer;
}
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
IMAGE OF WHAT I WANT DONE:
want to move the dropdown onto the restoration page
You have to add a top: x px; for your dropdown content in order to place it below your header bar. Like this:
.dropdown-content {
display: none;
position: absolute;
top: 30px;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
Any button is set to display: block by default. To align it next to the dropdown buttons, use either float: left or display: inline-block.

Header title paragraphs and misplaces itself

Previously, when my header only had 3 titles, i had no wrapping issues, however after adding another title, that specific title misplaces itself on top of the actual header.
Diagram of desired result: https://i.gyazo.com/3a71cc861daf2ec897cceed30d4bb576.png
Codepen: https://codepen.io/valik140795/pen/qadXOo
I believe the issue is coming from a dropdown button positioning(if not css), yet I am unable to specifically locate the issue.
Code of the button of the title that relocates itself:
<body>
<div class="body">
<div class="block_header">
<div class="lang">RU | ENG</div>
<img src="pictures/logo.png" class="logo" width="220px;" />
<ul>
<style>
.dropbtn {
background-color: #282828;
color: #AA9568;
padding: 0px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #282828;
}
.dropdown {
position: relative;
z-index: 999;
display: inline-block;
}
.dropdown-content {
display: none;
position: fixed;
z-index: 999;
background-color: #282828;
min-width: 180px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: #AA9568;
padding: 3px 5px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #282828}
.show {display:block;}
</style>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn"><a href=#fab>ФАБРИКИ</a></button>
<div id="myDropdown" class="dropdown-content">
Nobilis
Color de Seda
Eugenio Colombo
Libra
</div>
</div>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
</script>
Thanks in advance.
The dropdown element that positions on top is out of the ul element that holds the other three, you should put it in the same parent container as them.