I want to create a simple navigation menu with a special feature:
On a desktop browser it shows as a bar like "item1 | item2 | item3"
On a mobile browser it shows a button. When tapping on it, it shows the menu as stack
I'm searching a solution without Javascript. I know about media queries in CSS but I don't know how to add a menu with this requirement.
This is the menu for example:
<nav>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
</nav>
CSS:
/* default for all browsers */
nav>ul {
padding: 0;
margin: 0;
list-style: none;
background-color: #3d3d3d;
}
nav>ul>li>a {
display: block;
color: #ffffff;
}
#media only screen and (max-width: 360px) {
/* mobile */
/* WHAT NOW? */
}
#media only screen and (min-width: 361px) {
nav>ul>li {
/* bar layout */
display: inline-block;
}
}
fiddle: https://jsfiddle.net/1fg4qkx5/
i have edited your jsfiddle and found a rather "Simple" solution for you,
jsfiddle
I have not (yet) found a way to do this completely without javascript to catch the button click.
to post out the results here:
HTML
<nav>
<button class='hide-lg right'>
☰
</button>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
css
/* default for all browsers */
*{
margin: 0;
padding: 0;
}
body{
min-width: 100%;
min-height: 100%;
}
button{
border:1px solid gray;
background-color: transparent;
display: inline-block;
}
.show{
display: inline-block;
}
.right{
float:right;
}
nav{
background-color: #3d3d3d !important;
color: #ffffff;
height:auto;
width: 100%;
display: block;
}
nav>ul {
padding: 0;
margin: 0;
list-style: none;
background-color: #3d3d3d;
}
nav>ul>li>a {
display: block;
color: #ffffff;
}
#media only screen and (max-width: 360px) {
.hide-lg{
display:inline-block;
}
ul{
display: none;
}
/* mobile */
/* WHAT NOW? */
}
#media only screen and (min-width: 361px) {
.hide-lg{
display: none !important;
}
nav>ul{
display: block;
}
nav>ul>li {
/* bar layout */
display: inline-block;
}
nav>ul>li>a{
display: block;
width:100%;
height: 100%;
}
}
Javascript/jQuery
$(document).ready(function(){
$('button').click(function(){
$('ul').toggleClass('show');
});
});
I hope you find my answer helpfull,
Giovanni
Related
I'm writing a tag cloud. When displayed on a phone, it displays the full width and looks fine. But on a desktop, it also displays the full width and doesn't look as I want. I would like to limit the width of the div on a desktop to some part of the total width, say 60%. But as you can see in my jsfiddle, when the display is widened, the text becomes one long-line. I've tried applying various suggestions I've found here, like max-width, but none seem to make a difference. Can someone please point out how to do this? Here's my code:
<style>
#container {
width:100%;
text-align:center;
}
.cloud {
display:inline;
list-style-type:none;
max-width:50%;
width:100%;
}
.cloud li {
list-style: none;
display: inline;
}
#tagcloud .smallest { font-size:.8em; font-weight:400; }
#tagcloud .small { font-size:.9em; font-weight:500; }
#tagcloud .medium { font-size:1em; font-weight:600; }
#tagcloud .large { font-size:1.3em; font-weight:700; }
#tagcloud .largest { font-size:1.6em; font-weight:800; }
</style>
<div id="container">
<ul class="cloud" id="tagcloud">
<li class="small">performance testing</li>
<li class="largest">stress testing</li>
<li class="large">conformance testing</li>
<li class="medium">acceptane testing</li>
<li class="small">smoke testing</li>
<li class="smallest">smoke testing</li>
<li class="small">performance testing</li>
<li class="largest">stress testing</li>
<li class="large">conformance testing</li>
<li class="medium">acceptane testing</li>
<li class="small">smoke testing</li>
<li class="smallest">smoke testing</li>
</ul>
</div>
You have to use media queries to control the width in certain screen sizes. Note that I used 4 media queries, which are the common breakpoints for various screen sizes (commonly used by Bootstrap 4); for the smallest screen size (<576px width), the CSS style is set outside the media queries.
Also note that I have changed the display of container from inline to inline-block, to give width attribute to the element.
#container {
width: 100%;
text-align: center;
}
.cloud {
display: inline-block;
list-style-type: none;
width: 90%;
}
.cloud li {
list-style: none;
display: inline;
}
#tagcloud .smallest {
font-size: .8em;
font-weight: 400;
}
#tagcloud .small {
font-size: .9em;
font-weight: 500;
}
#tagcloud .medium {
font-size: 1em;
font-weight: 600;
}
#tagcloud .large {
font-size: 1.3em;
font-weight: 700;
}
#tagcloud .largest {
font-size: 1.6em;
font-weight: 800;
}
/* ---------------------------------------------
Media Queries
--------------------------------------------- */
/* SM Small devices */
#media(min-width: 576px) {
.cloud {
width: 80%;
margin: auto;
}
}
/* MD Tablets */
#media (min-width: 768px) {
.cloud {
width: 70%;
margin: auto;
}
}
/* LG Desktop */
#media (min-width: 992px) {
.cloud {
width: 60%;
margin: auto;
}
}
/* XL Modern desktop */
#media (min-width: 1200px) {
.cloud {
width: 50%;
margin: auto;
}
}
Demo: JSFiddle
What would be the best way to center the navigation on this wordpress page:
tried this:
#media (min-width: 768px) {
.navbar-nav {
float: none;
padding: auto;
}
.nav {
padding-left: 10%;
}
}
from here: https://colorlib.com/wp/forums/topic/how-do-i-center-my-sites-navigation-bar/
but has absolute no affect
Thanks in advance.
Add this snippet in your style :
#media only screen and (min-width: 60.001em){
.main-navigation-menu>li {
float: none;
display: inline-block;
}
#menu-oberes-menue {
text-align: center;
}
.main-navigation-menu ul li{
text-align:left;
}
}
<div class="menu-wrap"> // need to add this div before ul
<ul id="menu-oberes-menue" class="main-navigation-menu">
{your menu list}
</ul>
</div>
Add your css
.menu-wrap {
display: table;
margin-left: auto;
margin-right: auto;
}
On responsive website, I would like to display a vertical menu on smaller screens and a horizontal menu on larger screens.
Currently, the following HTML and CSS code does NOT display a vertical menu on smaller screens. Can any one please revise/improve this code? Thanks in advance.
#menu {
width: 100%;
min-height: 40px;
position: relative;
background-color: rgb(52, 85, 154);
}
#menu a {
display: inline-block;
padding: 10px 4% 0px 4%;
font: 400 16px/32px 'Courier', sans-serif;
min-height: 40px;
color:white;
text-decoration: none;
font-weight:bold;
transition: .5s;
}
#menu a:hover {
color: red;
background-color: blue;
}
#media screen and (max-width:640px) {
#menu {
max-width: 100%;
}
}
#media screen and (max-width:775px) {
#menu a {
max-width: 100%;
padding: 0px 5px 0px 5px;
float: none;
text-align: center;
}
}
#media screen and (max-width:980px) {
#menu {
max-width: 100%;
}
}
<body>
<nav id="menu"> Home About Services Blog Links FAQ Contact
</nav>
</body>
When creating a responsive site, the main navigation is usually the trickiest because of the often requirement to display the items vertically (and within a hamburger dropdown/flyout) on a mobile screen and then horizontally on a desktop screen
The first step is to develop it using a mobile first approach. This means:
Style everything so that it looks good on a small screen
Use media queries to progressively style larger screen sizes
Here is a basic snippet of how to style a menu so that it shows vertically in a small screen and horizontally in a large screen.
/* Mobile style first */
.menu {
text-align: center;
}
.menu a {
display: block;
padding: 10px;
}
.menu a:hover {
background-color: #eee;
}
/* Desktop style after */
#media (min-width: 600px) {
.menu a {
display: inline-block;
}
}
See this jsFiddle for an example
add a display: block; declaration here:
#media screen and (max-width:775px) {
#menu a {
max-width: 100%;
padding: 0px 5px 0px 5px;
float: none;
text-align: center;
display: block;
}
}
You can try the following code snippets
HTML
just add a span tag right above the nav tag
<span id='trigger'> Menu </span>
CSS
In the initial menu a tag definition change the display to block from inline-block and set float to left.
And then put the following:
#trigger {
display: none;
}
#media screen and (max-width: 560px /* just as an example*/) {
#trigger {
display: block;
}
#menu {
display: none;
}
div.expand {
display: block;
}
#menu a {
float: none;
border-bottom: 1px solid;
}
}
Javascript
jQuery("#trigger").click(function() {
jQuery("#menu").slideToggle(500, function() {
jQuery(this).toggleClass("expand").css('display','500');
});
});
I hope this helps
I have seen that other persons have asked the same question, but the solution does not apply to this problem. The solution should preferably work on different devices, such as iPhone, iPad, etcetera, so a generic solution is preferable - not something that just works on one device.
I have tried to set text-align:center and also tried to set margin-left:auto and margin-right:auto , but it doesn't work.
Html (only the relevant code is included):
<div class="container">
<div class="buttonyear"> <img class="buttonyear" src="./navi/yearen.png" /><span>2014</span></div>
<div class="buttonyear"> <img class="buttonyear" src="./navi/yearen.png" /><span>2013</span></div>
<div class="buttonyear clearfix"> <img class="buttonyear" src="./navi/yearen.png" /><span>2012</span></div>
<br/>
<br/>
</div> <!-- end of navi -->
Css:
.container {
max-width: 48rem;
width: 90%;
display: table-cell;
text-align: center;
vertical-align: middle;
}
body{
background-image: url(../images/gradient.jpg);
}
// todo improve css..ask on stackoverflow..
.navi {
width:100%;
text-align:center;
text-align: center;
margin-left: 42%;
margin-right: 42%;
}
.buttonyear
{
float: left;
}
.buttonyear a
{
text-decoration: none;
}
.logocontainer {
text-align:center;
}
.logo {
width:180px;
height:60px;
}
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
/* Small screens (default) */
html { font-size: 100%; }
/* Medium screens (640px) */
#media (min-width: 40rem) {
html { font-size: 112%; }
}
/* Large screens (1024px) */
#media (min-width: 64rem) {
html { font-size: 120%; }
}
ul {
list-style-type:none;
}
.buttonyear {
position: relative;
width: 42px;
height: 20px;
}
.buttonyear span {
left: 0;
position:absolute;
width: 100%;
color: white;
font: 12px Gill Sans;
font-weight:600;
text-decoration:none;
text-align:center;
width:42px;
height:20px;
padding-top:2px;
position:absolute;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
Fiddle: http://jsfiddle.net/wzhwtvmt/
What you want to do is put a container around your buttonyear divs that will center everything appropriately. I've created a fiddle for you with what I think you want. You'll have to modify it to your needs, but it centers all of your buttons within the container. Use your media queries to break them up at the right sizes.
http://jsfiddle.net/vtgw5zfg/
I had some time on my hands, and messed with your code a bit. Here's an updated version that centers things horizontally and vertically based on size. It also uses a bit of JavaScript and jQuery to center your span's. It's not perfect, but should help get you started.
http://jsfiddle.net/vtgw5zfg/1/
Is there any way to make a variable width navbar, I don't know how to make variable the clickable area... http://jsfiddle.net/tirengarfio/a9ssC/
As you can see, now I have set a 50px padding to the anchor tag, but I would like it to be variable actually..
This is the jsfiddle code:
ul {
list-style: none;
padding: 0;
margin: 0;
width: 100%;
height:20px;
background-color:red;
border:2px solid;
}
li {
float: left;
width: 50%;
text-align:center;
}
a {
padding: 0 50px;
}
Try this one:
HTML
<div class="nav">
<ul>
<li>1jkflasd</li>
<li>2jkflasd</li>
<li>3jkflasd</li>
<li>4jkflasd</li>
<li>5jkflasd jffjadj faljdf aljf adjf ladjf lkdjf alsjdf ldsjf ldsjf </li>
<li>6jkflasd</li>
<li>7jkflasd</li>
</ul>
</div>
CSS
.nav {
display: table;
width: 100%;
}
ul {
list-style: none;
display: table-row;
padding: 0;
margin: 0;
width: 100%;
height:20px;
background-color:red;
border:2px solid;
}
li {
display: table-cell;
padding: 5px;
text-align: center;
}
The trick here is using the table as values of display in CSS.
Demo
http://jsfiddle.net/jlratwil/4srb6/1/
You can handle it with table, hacking css by javascript. Or you can use CSS3 also.
If you work with CSS3 your code should like this:
<style>
ul{
display:-moz-box; /* Firefox */
display:-webkit-box; /* Safari and Chrome */
display:-ms-flexbox; /* Internet Explorer 10 */
display:box;
width:300px;
border:1px solid black;
}
li {
-moz-box-flex:1.0; /* Firefox */
-webkit-box-flex:1.0; /* Safari and Chrome */
-ms-flex:1.0; /* Internet Explorer 10 */
box-flex:1.0;
border:1px solid red;
}
</style>
<ul>
<li>jkflasd</li>
<li>jkflasd</li>
<li>jkflasd</li>
</ul>
You can read more about flexbox at here: http://www.w3schools.com/cssref/default.asp#flexbox