Best way to create horizontal menu with fixed width - html

I wonder, what's the best way to create horizontal menu with fixed width and variable number of items?
To adjust menu to make items equally spaced on menu strip, seems the only way is to use table width=100% as menu wrapper and items as td. So they would be adjusted automatically.
Is there another solution (without td), considering that we don't know the number of items and, moreover, it can vary?

You could give the <ul>/<ol> a display: table and the <li> a display: table-cell:
HTML:
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
CSS:
ul {
width: 100%;
margin: 0;
padding: 0;
display: table;
}
li {
display: table-cell;
text-align: center;
}
http://jsfiddle.net/8RXUw/

Hmm, try this:
.wrapper {
width:100%; /*or fixed width*/
}
header {
text-align:center;
}
nav {
margin:0 auto
}
nav ul {
list-style: none outside none;
overflow: hidden;
height: 20px;
margin:0;
padding: 5px;
text-align: justify;
cursor: default;
}
nav ul li {
display: inline;
margin:0;
padding:0;
}
nav ul li a {
display: inline-block;
padding:5px 10px;
}
ul:after {
content: "1";
margin-left: 100%;
height: 1px;
overflow: hidden;
display: inline-block;
}
http://jsfiddle.net/inc1uder/VCy8L/13/

Unordered list with the list items set to display: inline:
CSS:
<style type="text/css">
ul li {
display: inline;
}
</style>
HTML:
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
<li>item 6</li>
</ul>
and if it needs to be a fixed width you can play about with the width on the ul tag.

This days we can use the HTML5 and Grid CSS benefits:
menu {
display: grid;
grid-template-columns: repeat(4, auto);
}
<menu>
<menuitem>Item 1</menuitem>
<menuitem>Item 2</menuitem>
<menuitem>Item 3</menuitem>
<menuitem>Item 4</menuitem>
</menu>
This method has the advantage that styling is only suitable for the parent element in structure and is very easily expandable.
Note 1: The equivalent form for this rule:
grid-template-columns: repeat(4, auto);
is also:
grid-template-columns: repeat(4, 1fr);
or
grid-template-columns: auto auto auto auto;
Note 2: Of course, we can use any other structure instead of <menu> and <menuitem> tags (eg <ul> with <li> or nested <div> tags, etc.). Also instead of <menuitem> tag we can use also <li> tag with <menu> tag, because currently the tag is not supported by the most of the web browsers.

Related

Horizontal align for dynamic content (special case)

I have a navigation menu (header menu) in my web page which is actually takes dynamic content. The dynamic menu items are loaded into ul>li>{dynamic_content}. I want this navigation bar (I mean the ul ) in the center of the section, not vertically but horizontally. I can have text-align:center because it has multiple children tags.
Also I can't do the following,
.class{
width:50%;
margin-left:auto;
margin-right:auto;
}
because I can't set a width since this is a DYNAMIC navigation.
You may think why I can't try
.class{
position:absolute;
left:50%;
transform:translateX(-50%);
}
This is also can not be done, because I can't set the position:absolute since it's going to be a fixed header on scroll. It make some position problems in responsive.
I'm looking for an alternative to solve this problem.
You can use justify-content: center from flexbox.
.container {
display: flex;
justify-content: center;
}
<div class="container">
<span>Title 1</span>
<span>Title 2</span>
<span>Title 3</span>
</div>
Using flexbox to center the list on the page will work.
nav {
width: 100%;
display: flex;
justify-content: center;
}
ul {
margin: 0;
display: flex;
list-style: none;
}
li:not(:last-child) {
margin-right: 1rem;
}
<nav>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</nav>
If you are looking for old browsers support, another option would be using display table as it is explained here
.something-semantic {
display: table;
width: 100%;
}
.something-else-semantic {
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="something-semantic">
<div class="something-else-semantic">
Unknown stuff to be centered.
</div>
</div>
I think I figured it out a new way to align contents Horizontally.This works nice to me and have no issue with browser compatibility.
Wrapped the navigation with a div and set text-align:center and added display:inline-block to the ul that I wanted to center.
div {
text-align: center;
}
ul {
display:inline-block;
list-style-type: none;
}
ul>li{
float:left;
}
<div>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</div>

css - how to position <li> in horizontal <ul>?

I have section, which contains horizontal ul, which contains four li's,
as following:
#header-strip ul {
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
height: 4vh;
width: 100%;
background-color: grey;
list-style-type: none;
}
#header-strip li {
display: inline;
}
<section id="header-strip">
<ul>
<li>meant to be left</li
><li>meant to be centered</li
><li>meant to be just before last</li
><li>meant to be right</li
>
</ul>
</section>
Requirements:
- first item at left with some small percentage offset of screen edge
- second item centered in the middle
- third item to be at right, with some percentage space between to fourth
- fourth item at right, with percentage offset from screen edge
I am able to position li's with basic justify-content: space-between;, but don't know how to position it the custom way.
It should look like this:
Simply change the margin of the second element and make it auto
As a side note: with flexbox you don't need to worry about whitespace and no need to define element as inline
#header-strip ul {
display: flex;
margin: 0;
padding: 0;
color:#ffff;
width: 100%;
background-color: grey;
list-style-type: none;
}
#header-strip li {
margin:0 2%;
}
#header-strip li:nth-child(2) {
margin: auto;
}
<section id="header-strip">
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
</section>
if you are using flexbox:
<section id="header-strip">
<ul>
<li>meant to be left</li
><li class="fill">meant to be centered</li
><li>meant to be just before last</li
><li>meant to be right</li
>
</ul>
</section>
//css
.fill {
flex-grow: 2;
}

Flexbox drop down menu position and movement

I'm trying to design a header with 3 items:
----------
LOGO
---------- hidden navigation bar
----------
BUTTON
----------
Which then grows to:
----------
LOGO
----------
li
li
li
li
----------
BUTTON
----------
I'd like the logo and the button to remain vertically centered and for all growth/movement to happen downwards from the top of the button/bottom of the logo-container. I've managed this without flexbox, but I'd like to use it so that I can get a better understanding of why this isn't working.
At the moment - unless I put a large height for the logo container - which feels a bit hacky - the menu grows upwards as well as down. I'd ideally like to use justify-content: space-around but obviously that attributes some space to the hidden menu.
I feel like flex-shrink might be a solution, but I'm very much a novice at all this, and can't get it to work. Here's what I've got so far:
https://codepen.io/nwoodward/pen/RMrRVZ
$('#button').click(function() {
$('.menu').toggleClass('menu--open', 700);
})
header {
background: #808080;
display: flex;
flex-direction: column;
min-height: 100px;
}
#logo-container {
display: block;
flex-grow: 2;
background: red;
width: 100%;
height: 50px;
text-align: center;
}
#logo-container img {
height: 40px;
}
.menu {
height: 0px;
overflow: hidden;
background-color: green;
}
.menu--open {
height: auto;
}
#button {
background: blue;
width: 100%;
height: 20px;
text-align: center;
cursor: pointer;
}
#rest {
height: 500px;
background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div id="logo-container">
<img src="https://trellis.co/wp-content/uploads/2015/09/hidden_meanings_facts_within_famous_logos_cover_image.jpg">
</div>
<div class="menu">
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
</ul>
</div>
<div id="button">Click Me</div>
</header>
<section id="rest">
I don't see any good reason to use flexbox in that case. But to answer your question, the key is the header {min-height: 100px;} setting.
With the default collapsed menu, the overall computed height of elements in the header is smaller than 100px. However it will be greater than 100px when the menu expands. Therefore, it creates the effects of pushing the menu to the top first and then to the bottom.

Flexbox element not scrolling overflow

I'm currently trying to make a sidebar layout work. I feel like I'm nearly there but the last bit is just not working.
html,body {
width: 100%;
height: 100%;
}
#wrapper {
display: flex;
width: 100%;
height: 100%;
}
.sidebar {
height: 100%;
position: fixed;
width: 200px;
background: red;
overflow: auto;
flex-direction: column;
display: flex;
}
ul {
padding: 0;
margin: 0;
}
.menu {
flex: 1;
background: rgb(150,0,0);
}
.users {
overflow: auto;
max-height: 240px;
min-height: 100px;
}
<div id="wrapper">
<div class="sidebar">
<ul class="menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
</ul>
<ul class="users">
<li>user 1</li>
<li>user 2</li>
<li>user 3</li>
</ul>
</div>
<div class="content">
</div>
</div>
Here is a fiddle of the layout which works better to see the issue than the above snippet:
https://jsfiddle.net/ybp4og8w/1/
All works great except when the height of the window gets really small, smaller than the menu items list. The content at the bottom disappears off screen. Ideally I'd like to have the user list sticky at the bottom (which is correct right now but I've also had this issue when messing with the code), not overlap the menu items when the height gets small but instead make the sidebar become scroll-able.
Any tips on how to achieve this?
I see your jsfiddle and apply this code in #sidebar
overflow-y:scroll;
May be this helpful.
Thanks!

CSS Distribute li over 2 rows, unlimited items

I want my li's to be distributed over 2 rows like this:
item 1 item 3 item 5 item 7 item 9 ....
item 2 item 4 item 6 item 8 ......
My CSS is really bad so I have no clue on how to achieve this and can't find anything on this... I tried some stuff with even and odd items, but I can't figure out how to force even items below odd items.
You can use flexbox to achieve this ordering. Support is pretty good (http://caniuse.com/#feat=flexbox) but you will need to provide fallbacks for older versions of IE.
ul {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 100px;
list-style-type: none;
margin: 0;
padding: 0;
width:200px;
}
li {
color: #000000;
height: 50px;
margin: 0;
padding: 0;
width: 100px;
}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
</ul>
You can use :nth-child selector to select odd elements in that list items.
Here is an example:
CSS
ul {
position: relative;
white-space: nowrap;
}
li {
display: inline-block;
list-style-type: none;
padding: 0px 5px;
}
li:nth-child(2n) {
top: 100%;
position: absolute;
margin-left: -36px; /* Changes as per the width of the first element */
}
Working Fiddle