Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
This was probably asked in a different way before, but I couldn't find any that answer this specifically.
I want to have a navbar that on a normal page is ABOVE the page content and on one with a hero image looks like it's WITHIN the page content.
I typically use Jinja2 templates (for Django) so manually putting the navbar into the page content doesn't make a lot of sense.
I currently use negative margins to do this, but it feels a bit hacky (checkout code below)
EDIT: The second navbar (where the Navbar background is BEHIND the hero background, but the Navbar text is IN FRONT of the hero background) is how I want it to be.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial;
}
:root {
--primary: #21abde;
--navbar-height: 50px;
}
.navbar {
height: var(--navbar-height);
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background: #333;
border: 1px solid grey;
color: white;
}
.navbar ul {
list-style: none;
display: flex;
}
.navbar ul a {
padding: 10px;
color: white;
text-decoration: none;
transition: .2s ease;
}
.navbar ul a:hover {
color: var(--primary);
}
.content,
.hero {
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: darkcyan;
height: calc(100vh - var(--navbar-height));
}
.hero {
background: darkorange;
margin-top: calc(-1*var(--navbar-height));
height: 100vh;
}
<nav class="navbar">
<div class="logo">Navbar 1</div>
<div class="nav-menu">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</nav>
<div class="content">
Content
</div>
<nav class="navbar">
<div class="logo">Navbar 2</div>
<div class="nav-menu">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</nav>
<div class="hero">
Hero
</div>
Is there a better way to do this or is this the only way that doesn't involve manually including the navbar into the page's content (As I feel like this doesn't make a lot of sense for anything other than a static navbar)?
You're right. There is a better way. Just use position: fixed.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial;
}
:root {
--primary: #21abde;
}
.navbar {
height: 50px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background: #333;
border: 1px solid grey;
color: white;
position: fixed;
width: 100%;
}
.navbar ul {
list-style: none;
display: flex;
}
.navbar ul a {
padding: 10px;
color: white;
text-decoration: none;
transition: .2s ease;
}
.navbar ul a:hover {
color: var(--primary);
}
.content,
.hero {
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: darkcyan;
height: 100vh;
}
.hero {
background: darkorange;
height: 100vh;
}
<nav class="navbar">
<div class="logo">Navbar 1</div>
<div class="nav-menu">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</nav>
<div class="content">
Content
</div>
<nav class="navbar">
<div class="logo">Navbar 2</div>
<div class="nav-menu">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</nav>
<div class="hero">
Hero
</div>
Related
I've been trying to write a menubar that has two groupings in the same row across the top of a webpage: on the left is the site name and in the center should be the menu options (a ul/li). So far, following similar issues, I've written the following, which appears on first glance to do exactly what I'm seeking.
HTML:
<div class="menubar">
SITE NAME
<ul class="ul">
<li>MENU 0</li>
<li>MENU 1</li>
<li>MENU 2</li>
<li>MENU 3</li>
</ul>
</div>
CSS:
.menubar {
width: 100%;
height: 50px;
line-height: 50px;
vertical-align: middle;
background-color: #fff;
border-bottom: 1px solid #f5f5f5;
position: fixed;
top: 0;
display: flex;
flex-flow: row nowrap;
}
.logo {
width: 33.33%;
float: left;
margin-left: 20px;
font-size: 24px;
}
.ul {
font-size: 18px;
list-style: none;
padding: 0;
margin: 0;
}
.ul li {
margin-left: 15px;
margin-right: 15px;
display: inline-block;
}
However, if you look carefully in the JSFiddle (more apparent when widening browser windows or shrinking the window down just before the items begin wrapping), the 'centered' ul/li is not actually centered—it's closer to the left side of the browser window than the right. How do I fix this so that the ul/li remains truly centered in the menubar (as if the site name doesn't exist) with the left-aligned site name, regardless of what the browser window's width is? (I'm assuming within non-wrapping reason, since I plan to adjust sizes and behavior for smaller devices.)
JSFiddle
You're using a lot of margins, width and stuff. Check out flex here and you can get the same thing, properly aligned using flex and directions.
<!-- NEW CODE -->
<nav>
<div class="logo">
<span>Your Company</span>
</div>
<ul class="nav-items">
<li class="nav-item"> Menu 1 </li>
<li class="nav-item"> Menu 2 </li>
<li class="nav-item"> Menu 3 </li>
<li class="nav-item"> Menu 4 </li>
</ul>
</nav>
<!-- OLD CODE -->
<nav>
<div class="logo">
<img src="https://placehold.it/200x200" alt="logo">
</div>
<div class="menu-items">
<div class="menu-item"> Menu 0 </div>
<div class="menu-item"> Menu 1 </div>
<div class="menu-item"> Menu 2 </div>
<div class="menu-item"> Menu 3 </div>
</div>
</nav>
and the css
// MORE PROPERTIES
nav {
align-items: center;
}
nav div.logo {
position: absolute;
}
// OLD-NEW CSS
nav {
display: flex;
border: 1px solid pink;
}
nav div.logo {
border: 1px solid green;
display: flex;
align-items: center;
}
nav div.logo span {
padding: 0 0.5rem;
}
ul.nav-items {
border: 1px solid red;
display: flex;
flex-direction: row;
list-style-type: none;
margin: 0 auto;
padding: 0;
}
ul.nav-items li {
margin: 0 0.25rem;
padding: 0.5rem;
border: 1px solid blue;
}
// OLD CSS
nav {
display: flex;
}
nav div.menu-items {
display: flex;
flex-direction: row;
margin: 0 auto;
}
nav div.menu-items div.menu-item {
margin-left: 0.25rem;
margin-right: 0.25rem;
}
Fiddle:
NEW: https://jsfiddle.net/vzgn0Lju/1/
OLD: https://jsfiddle.net/kp9nsmah/1/
I added some margins between menu options and you can tweak a little bit more but flex is way easier than using lists and lots of things. You could use spans instead of div.menu items, can remove the container for items and such. But the general idea is there.
I currently have a section that is a flex box. I am trying to get a drop down menu to appear when I mouse over the Vs div. Unfortunately, this div is inside a section tag and the drop down menu won't appear.
The way the code is currently set up causes the drop down menu to appear when I mouse over any part of the section. I want the drop down menu to only appear when I mouse over the Vs. div.
Here is an image of the section.
.line {
height: 4px;
flex: 1;
background: red;
margin: 0 10px;
}
section {
display: flex;
align-items: center;
}
.home {
margin-left: 50px;
}
.m {
margin-right: 30px;
}
.logout {
margin-right: 50px;
}
ul {
list-style-type: none;
margin-top: 20px;
}
.dropDown:hover ~ .dropDownContent {
display: block;
}
.dropDownContent {
display: none;
}
<section class="dropDown">
<div class="home">Home</div>
<div class="line"></div>
<div class="m">Reports</div>
<div class="m dropDownVs">Vs.</div>
<div class="logout"> Log Out
</div>
</section>
<div class="dropDownContent">
<ul>
<li>name 1</li>
<li>name 2</li>
<li>name 3</li>
</ul>
</div>
Bonus: Any suggestions on aligning the drop down directly below the Vs. div and suggestions for selecting the drop down items when the mouse is off the Vs. div would be appreciated. Currently when I mouse off the section the dropdown disappears and a user is unable to select anything.
In your code you have the drop-down menu positioned as a sibling to the main menu. It's better to nest the drop-down within the related main menu item. Try this:
.dropDownVs:hover .dropDownContent {
display: block;
position: absolute;
}
.dropDownContent {
display: none;
}
.line {
height: 4px;
flex: 1;
background: red;
margin: 0 10px;
}
section {
display: flex;
align-items: center;
position: relative;
}
.home {
margin-left: 50px;
}
.m {
margin-right: 30px;
}
.logout {
margin-right: 50px;
}
ul {
padding: 0;
list-style-type: none;
}
<section class="dropDown">
<div class="home">Home</div>
<div class="line"></div>
<div class="m">Reports</div>
<div class="m dropDownVs">Vs.
<ul class="dropDownContent">
<li>name 1</li>
<li>name 2</li>
<li>name 3</li>
</ul>
</div>
<div class="logout"> Log Out
</div>
</section>
Sorry, my bad, was really tired of trying to figure out the issue. So lemme rephrase the question - "How do i make drop-down menu appear below specific item of my centered horizontal menu". ( I've changed the code a bit)
HTML
<div class="menu">
<ul id="nav">
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4đŸ”½
<ul id="dropdown">
<li>sublink1</li>
<li>sublink2</li>
</ul>
</li>
</ul>
</div>
CSS of centered .menu
#nav {
list-style: none;
margin: 0;
padding: 0;
text-align: center;
height: 30px;
position: relative;
}
#nav li {
display: inline;
}
#nav a {
display: inline-block;
padding: 10px;
margin-top: 40px;
font-family: "oswald", sans-serif;
color: black;
text-decoration: none;
}
#nav a:hover {
background-color: rgba(107, 163, 252, 0.28);
}
just add
ul#dropdown
{
padding: 0px;
}
see jsfiddle here : https://jsfiddle.net/yxLzbkL3/
edit fyi : if the padding is not specified the user-agent styling from your browser will auto indent multiple lists using padding.
I am trying to use a flexbox nav menu with dropdowns for some menus. For some reason, on hover, the dropdown lists are showing up to the right of the container I am hovering on. I would like for them to show below, like a normal nav menu.
Here is a codepen:
http://codepen.io/anon/pen/XbmaBY
HTML:
<nav id="nav">
<ul class="nav-flex">
<li class="nav-brand-flex">
<img src="img.png" \>
</li>
<li class="nav-link-flex nav-flex-dropdown">
Dropdown 1
<div>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
<li>Link 6</li>
</ul>
</div>
</li>
<li class="nav-link-flex nav-flex-dropdown">
Dropdown 2
<div>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
</li>
<li class="nav-link-flex">
Regular 1
</li>
<li class="nav-link-flex">
Regular 2
</li>
<li class="nav-link-flex">
Regular 3
</li>
</ul>
</nav>
Sass:
.nav-flex {
display: flex;
list-style-type: none;
padding: 0;
background-color: #58575f;
li {
justify-content: center;
a {
align-self: center;
color: white;
font-weight: 300;
font-family: 'Open Sans', sans-serif;
letter-spacing: .4px;
}
}
#media (max-width: 760px) {
padding-top: 0;
flex-wrap: nowrap;
-webkit-flex-wrap: nowrap;
flex-direction: column;
-webkit-flex-direction: column;
background-color: #f6f6f6;
}
}
.nav-link-flex {
display: flex;
padding: 0 12.5px;
position: relative;
#media (max-width: 760px) {
width: 90%;
background-color: #494949;
border-radius: 20px;
margin-bottom: 10px;
margin-left: auto;
margin-right: auto;
font-size: 22px;
padding: 10px;
}
&:hover {
background-color: white;
a {
color: black;
text-decoration: none;
&+ div {
display: block;
}
}
}
a {
&+ div {
border-radius: 0 0 2px 2px;
box-shadow: 0 3px 1px rgba(0,0,0,.05);
display: none;
font-size: 1rem;
position: absolute;
width: 195px;
}
}
}
.nav-brand-flex {
margin-right: auto;
display: flex;
padding: 5px 0;
a {
display: flex;
img {
height: 35px;
align-self: center;
}
}
#media (max-width: 760px) {
margin: 0;
background-color: #494949;
width: 100%;
border-radius: 0;
font-size: 36px;
padding: 10px 0;
margin-bottom: 10px;
}
}
Any help would be greatly appreciated, because I cannot figure out why they are going to the right instead of below.
This happens because the default value of flex-direction is flex-direction: row. You want to override this, by adding flex-direction: column; to .nav-link-flex. So, it should be:
.nav-link-flex {
display: flex;
padding: 0 12.5px;
position: relative;
flex-direction: column; // <-- add this line in your scss
...
By way of example I've added this in to a fork of your pen.
I have pretty simple navigation.
Three divs inside a header (logo, navigation and phone).
I want to make them responsive and stretchable whenever user zooms out.
Example
http://www.zendrive.com/
Can someone give me a simple CSS example on how to achieve this?
.header {
display: table;
position: relative;
width: 100%;
height: 60px;
}
.header > * {
display: table-cell;
position: relative;
height: 100%;
}
.header .navigation ul {
display: inline-block;
position: relative;
list-style: none;
margin: 0;
padding: 0;
}
.header .navigation li {
display: inline-block;
}
<header class="header">
<div class="logo">
<img src="http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png">
</div>
<nav class="navigation">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</nav>
<div class="phone">555.555.5555</div>
</header>