Trying to get my UL to be horizontal, while also using CSS Grid. I basically have two columns than span the whole page and in the second one (on the right) I want to have my UL appear horizontally. I have tried both display: block and inline and neither seem to be having the desired effect.
Any ideas?
I have eliminated all my testing in the css, so currently there is no reference to the ul
HTML
#nav-bar {
display: grid;
grid-template-columns: 50% 50%;
background-color: #3e2723;
padding: 10px;
border-radius: 10px;
}
#header-img {
object-fit: cover;
width: 70%;
max-height: 100%;
float: left;
}
<header>
<nav id="nav-bar">
<div class="image">
<img id="header-img" src="https://s3.amazonaws.com/freecodecamp/original_trombones.png" alt="logo"><!--U.S.2-->
</div>
<div class="links">
<ul>
<li><a class="nav-link" href="#features">Features</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#video">Video</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#pricing">Pricing</a></li><!--U.S.4, 5-->
</ul>
</div>
</nav>
</header>
You should add the code the the list elements (li). Adding inline display to list (ul) will not change much.
Your problem has basically nothing do do with CSS Grid, since you're not using grid directly on the list.
Also there are many ways to do it, using display: inline, display: inline-block, using flexbox or even grid.
Solution using display: inline or display: inline-block
#nav-bar {
display: grid;
grid-template-columns: 50% 50%;
background-color: #eee;
padding: 10px;
border-radius: 10px;
}
#header-img {
object-fit: cover;
width: 70%;
max-height: 100%;
float: left;
}
ul li {
display: inline;
}
<header>
<nav id="nav-bar">
<div class="image">
<img id="header-img" src="https://s3.amazonaws.com/freecodecamp/original_trombones.png" alt="logo"><!--U.S.2-->
</div>
<div class="links">
<ul>
<li><a class="nav-link" href="#features">Features</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#video">Video</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#pricing">Pricing</a></li><!--U.S.4, 5-->
</ul>
</div>
</nav>
</header>
Solution using display: flex
#nav-bar {
display: grid;
grid-template-columns: 50% 50%;
background-color: #eee;
padding: 10px;
border-radius: 10px;
}
#header-img {
object-fit: cover;
width: 70%;
max-height: 100%;
float: left;
}
ul {
display: flex;
}
<header>
<nav id="nav-bar">
<div class="image">
<img id="header-img" src="https://s3.amazonaws.com/freecodecamp/original_trombones.png" alt="logo"><!--U.S.2-->
</div>
<div class="links">
<ul>
<li><a class="nav-link" href="#features">Features</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#video">Video</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#pricing">Pricing</a></li><!--U.S.4, 5-->
</ul>
</div>
</nav>
</header>
Solution using display: grid
(One way of doing it)
#nav-bar {
display: grid;
grid-template-columns: 50% 50%;
background-color: #eee;
padding: 10px;
border-radius: 10px;
}
#header-img {
object-fit: cover;
width: 70%;
max-height: 100%;
float: left;
}
ul {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
<header>
<nav id="nav-bar">
<div class="image">
<img id="header-img" src="https://s3.amazonaws.com/freecodecamp/original_trombones.png" alt="logo"><!--U.S.2-->
</div>
<div class="links">
<ul>
<li><a class="nav-link" href="#features">Features</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#video">Video</a></li><!--U.S.4, 5-->
<li><a class="nav-link" href="#pricing">Pricing</a></li><!--U.S.4, 5-->
</ul>
</div>
</nav>
</header>
to get the <li> elements in the <ul> appear horizontally, you can simply add the following to the CSS, making <li> elements to display:inline-block or display:inline:
li{
display: inline-block;
}
Related
I am trying to make a navbar but my text is flowing off of the screen even though I have overflow: hidden on my ul
Here is my snippet
.header {
background-image: url('img/bg.jpg');
background-size: 100%;
background-repeat: no-repeat;
color: white;
width: 100vw;
height: 100vh;
}
.heading {
display: inline-block;
font-size: 3em;
}
.nav-items {
overflow: hidden;
}
<div class="header">
<div class="nav">
<!-- <img src="img/logo.png" alt="logo" class="logo"> -->
<ul class="nav-items">
<li class="nav-item">Home</li>
<li class="nav-item">About</li>
<li class="nav-item">Portfolio</li>
<li class="nav-item">Contact</li>
</ul>
</div>
<h1 class="heading" id="heading"></h1>
</div>
does anyone know how to achieve this in bootstrap for the nav? Logo on the left and nav on right.
My guess is you use col's with some css?
I managed to create the section i wanted the code i am using is:
CSS:
.menu {
background-color: lightblue;
overflow: hidden;
background-color:white;
}
.menu:after {
content: "";
border-bottom: 850px solid transparent;
border-right: 400px solid #0055b7;
position: absolute;
left: 80%;
top: 0;
}
.nav-menu ul {
list-style: none;
display: inline-flex;
}
.nav-menu ul li {
padding: 40px;
}
.blue-bg {
background-color: #0055b7;
}
HTML:
<div class="container-fluid">
<div class="row">
<div class="menu col-md-5">
<img class="paddingHeader" src="assets/images/headerlogo.png" />
</div>
<div class="content col-md-7 blue-bg">
<div class="nav-menu">
<ul>
<li><a class="text-white" href="">Home</a></li>
<li><a class="text-white" href="">About Us</a></li>
<li><a class="text-white" href="">Properties To Let</a></li>
<li><a class="text-white" href="">We Buy Houses</a></li>
<li><a class="text-white" href="">Contact Us</a></li>
</ul>
</div>
</div>
</div>
</div>
I'm recreating an article I found on The Economist and I'm having trouble creating the header. Please keep in mind I'm doing this without recreating their code verbatim. I'm trying to create my own implementation.
header {
background-color: #364043;
}
.header__content {
width: 70%;
margin: auto;
}
.header__left-content {
display: inline;
width: 50%;
}
.header__nav ul {
display: inline;
}
.header__nav li {
line-height: 0px;
display: inline-block;
vertical-align: middle;
}
.header__logo {
padding-right: 25px;
}
.header__nav-link {
padding-right: 15px;
}
<html>
<body>
<header>
<div class="header__content">
<div class="header__left-content">
<div class="header__nav">
<ul>
<li class="header__logo">
<img src="http://jobs.printweek.com/getasset/2eef9541-354f-4fec-8ce2-87b008f0323d/">
</li>
<li class="header__nav-link">
Topics</li>
<li class="header__nav-link">
Print Edition
</li>
<li class="header__nav-link">
More
</li>
</ul>
</div>
</div>
<!-- <div class="header__separator"></div> -->
<div class="header__site-functions">
<p>right</p>
</div>
</div>
</header>
<div class="container"></div>
</body>
</html>
I'm having trouble getting the paragraph element, and ultimately its container to sit to the right as shown in the article.
Thoughts on this?
You can use display: flex; justify-content: space-between; on the element that wraps the the left/right portions of the header to put those in a row separated by the available space left over. And you can use align-items to align that content vertically.
header {
background-color: #364043;
}
.header__content {
width: 70%;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.header__left-content {
display: inline;
width: 50%;
}
.header__nav ul {
display: inline;
}
.header__nav li {
line-height: 0px;
display: inline-block;
vertical-align: middle;
}
.header__logo {
padding-right: 25px;
}
.header__nav-link {
padding-right: 15px;
}
<header>
<div class="header__content">
<div class="header__left-content">
<div class="header__nav">
<ul>
<li class="header__logo">
<img src="http://jobs.printweek.com/getasset/2eef9541-354f-4fec-8ce2-87b008f0323d/">
</li>
<li class="header__nav-link">
Topics</li>
<li class="header__nav-link">
Print Edition
</li>
<li class="header__nav-link">
More
</li>
</ul>
</div>
</div>
<!-- <div class="header__separator"></div> -->
<div class="header__site-functions">
<p>right</p>
</div>
</div>
</header>
<div class="container"></div>
This is happening due to your class="header__site-functions" is ablock element and is taking the 100% of the width, so it doesn't fit in a line. You can use a floating element to fix it:
header {
background-color: #364043;
}
.header__content {
width: 70%;
margin: auto;
}
.header__left-content {
display: inline-block;
width: 50%;
}
.header__nav ul {
display: inline;
}
.header__nav li {
line-height: 0px;
display: inline-block;
vertical-align: middle;
}
.header__logo {
padding-right: 25px;
}
.header__nav-link {
padding-right: 15px;
}
.header__site-functions{
float:right;
}
<html>
<body>
<header>
<div class="header__content">
<div class="header__left-content">
<div class="header__nav">
<ul>
<li class="header__logo">
<img src="http://jobs.printweek.com/getasset/2eef9541-354f-4fec-8ce2-87b008f0323d/">
</li>
<li class="header__nav-link">
Topics</li>
<li class="header__nav-link">
Print Edition
</li>
<li class="header__nav-link">
More
</li>
</ul>
</div>
</div>
<!-- <div class="header__separator"></div> -->
<div class="header__site-functions">
<p>right</p>
</div>
</div>
</header>
<div class="container"></div>
</body>
</html>
I have a section of code for the header portion of my website:
.nav {
position:relative;
display:inline-block;
}
#header-image {
display:inline-block;
position:relative;
height:50%;
}
.header-container {
height:6vw;
position:relative;
vertical-align: bottom;
}
<div class="header-container">
<a href='index.html'>
<img src='img/logo.png' alt="Logo" id='header-image'/>
</a>
<nav class="nav">
<ul>
<li><a href='aboutus.html'>About Us</a></li>
<li><a href='activities.html'>Activities</a></li>
<li><a href='google.ca'>Media</a></li>
<li><a href='google.ca'>Contact Us</a></li>
</ul>
</nav>
</div>
I wanted the bottom of the image to line up with the bottom of the navbar. But what's happening is that the image is lower than the navbar. Any ideas why?
Set your list items to display: inline-block;
.nav {
position: relative;
display: inline-block;
}
#header-image {
position: relative;
}
.header-container {
position: relative;
vertical-align: bottom;
}
li {
display: inline-block;
}
<div class="header-container">
<a href="index.html">
<img src='http://placehold.it/50x50' id='header-image' />
</a>
<nav class="nav">
<ul>
<li><a href='aboutus.html'>About Us</a>
</li>
<li><a href='activities.html'>Activities</a>
</li>
<li><a href='google.ca'>Media</a>
</li>
<li><a href='google.ca'>Contact Us</a>
</li>
</ul>
</nav>
</div>
.nav {
position:relative;
display:inline-block;
}
#header-image {
display: inline-block;
position: absolute;
bottom: 15px;
}
.header-container {
position:relative;
}
Use this styling settings may fill your requirements
^^ The above images show =900px, >900px, <900px...I just want to center and shorten padding as window shrinks.(at 15px)
^^Using 1.666666666666666%
Right now im trying to make my webpage navbar work with changes in widths. When the window is exactly 900px The navbar fits perfectly. I have 30px spacing around each block (15px left and right; 15px start and end of list). I took 900 and divided by 60 to get 15px and that is my percentage (%/60). When i use the formula calc(1/60*100%) the navbar has the wrong spacing. Am i misunderstanding something.
I dont think this is google chrome issue assuming something is wrong with the above logic. I can post the html file if anyone needs it. Don't know if its neccessary.
body {
margin:0px;
font-family:avenir, sans-serif;
}
.nav {
margin: 0px 0px 0px 0px;
overflow:hidden;
width:100%;
<!--box-shadow: 0px 0px 10px 0px #000000;-->
}
.link-header {
background-color:rgb(242,242,235);
}
.school-header {
background-color:rgb(40,110,123);
}
.nav-left {
display: inline-block;
float:left;
}
.nav-right {
display: inline-block;
float:right;
}<!--
.nav-center {
text-align: center;
}-->
a {
text-decoration: none;
}
.school-header a {
color:white;
}
.link-header a {
color:rgb(40,40,40);
}
.nav-li-outer {
padding-left:calc(1/60*100%);
padding-right:calc(1/60*100%);
display: inline-block;
margin-top: 0px;
vertical-align: top;
}
.school-header li {
line-height:80px;
}
.link-header li {
line-height:30px;
}
.link-header li:hover {
box-shadow:inset 0 -3px 0 rgb(40, 40, 40);
}
ul {
margin: 0;
list-style-type: none;
padding-left: calc(1/60*100%);
padding-right:calc(1/60*100%);
}
html:
<html>
<head>
<link rel="stylesheet" href="kamsc.css">
</head>
<body>
<div class="school-header">
<div class="nav">
<div class="nav-left">
<ul>
<a href="#">
<div class="nav-li-outer">
<li>
<img src="Logo-Test.png" width=600px style="vertical-align:middle">
</li>
</div>
</a>
</ul>
</div>
<div class="nav-right">
<ul>
<a href="#">
<div class="nav-li-outer">
<li>
Login
</li>
</div>
</a>
</ul>
</div>
</div>
</div>
<div class="link-header">
<div class="nav">
<div class="nav-left">
<ul>
<a href="#">
<div class="nav-li-outer">
<li>
Home
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
KAMSC
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
Staff
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
Admissions
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
Curriculum
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
Sizzling Summer
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
KAMSC Connection
</li>
</div>
</a>
<a href="#">
<div class="nav-li-outer">
<li>
Alumni
</li>
</div>
</a>
</ul>
</div>
</div>
</div>
</body>
</html>
You can significantly simply your HTML, particularly for the nav section. Here's a JSFiddle: http://jsfiddle.net/vanu2ynx/
I didn't completely recreate what you're trying to do, but you can probably fill in the details.
Here's the HTML I used:
<header>
<h1>Title</h1>
<div class="login">Login</div>
</header>
<nav>
<ul>
<li>Home</li>
<li>KAMSC</li>
<li>Staff</li>
<li>Adminssions</li>
<li>Curriculum</li>
<li>Sizzling Summer</li>
<li>KAMSC Connection</li>
<li>Alumni</li>
</ul>
</nav>
And the CSS:
header {
background: teal;
overflow: hidden;
padding: 2% 2%;
}
header h1 {
float: left;
margin: 0;
padding: 0;
}
header .login {
float: right;
}
nav {
background: lightgray;
padding: 2% 2%;
}
nav ul {
font-size: 0;
margin: 0;
padding: 0;
text-align: justify;
}
nav ul:after {
content: '';
display: inline-block;
width: 100%;
}
nav li {
display: inline-block;
font-size: 16px;
}
The trick here is the text-align: justify; on nav ul and then the :after creates a full width child element for the justify to work.
You'll need to add media queries to have this break properly, but that should be pretty straight-forward.
Read more here: How to stretch a fixed number of horizontal navigation items evenly and fully across a specified container