My navbar won't align with the center of the page. In fact any code i added to change the margins between each individual link is also not working, idk why that is. I made the position absolute because I couldn't find any other way to get the navbar to go in front of the background/ the slideshow in my side (not shown here). I have included a fiddle. Also I tried making a centered navbar that would be transparent, have my next element be behind it but I couldn't.
https://jsfiddle.net/ep93zz08/
HTML:
<div id="navbar" class="li flex-container nav row">
<a class="nav-link flex-item " href="index.html">PHOTOGRAPHER</a>
<a class="nav-link flex-item" href="#">PORTFOLIO</a>
<a class="hplogo-a flex-item " href="idex.html"><img id="logo" src="http://www.dynamicaudiovideo.com/_Media/samsung_logo_large.jpeg" alt=""></a>
<a class="nav-link flex-item" href="Investment.html">INVESTMENT + FAQ</a>
<a class="nav-link flex-item" href="#">BLOG</a>
</div>
CSS:`
body{
margin:0;
overflow-x: hidden;
font-family: Calibri;
font-style: normal;
font-variant: normal;
background:black;
}
.nav{
position: absolute;
list-style-type: none;
display: inline;
z-index: 2;
text-align: center;
}
.resize-anchor{
display: inline-block;
height: auto;
width: 300px;
}
.hplogo-a{
display: inline-block;
height: auto;
width: 200px;
min-width: 200px;
}
a:hover{
color:#D1946F;
text-decoration: none;
}
a:link{
color:#D1946F;
text-decoration: none;
}
.navlink, .hplogo-a{
text-align: center;
display:inline-block;
margin-left: 50px;
margin-right: 50px;
padding:0;
}
.nav-link{
color:white;
text-decoration: none;
white-space: nowrap;
text-align: center;
font-family: Calibri;
font-size: 18px;
font-style: normal;
font-variant: normal;
line-height: 26.4px;
margin-top: 20px;
}
.sticky {
position: fixed;
top: 0;
width: 100%
z-index: 3;
}
.li{
text-align: center;
}
.flex-container {
display: flex;
display: -webkit-flex;
-webkit-align-items: center;
align-items: center;
}
.flex-item {
margin: 5px;
}
img{
width: 100%;
}
`
It would be better to add CSS rules for #navbar selector. Because rules for .nav will affect to all HTML elements with .nav class. Using the #navbar selector, the rules will only affect the specific div with that id. Try to remove the rules for .nav and add this:
#navbar {
position: relative;
list-style-type: none;
z-index: 2;
justify-content: center;
}
to adjust the margins between links:
#navbar a {
margin-right: 20px;
}
https://jsfiddle.net/ep93zz08/8/
One easy way that I used when trying to center the navbar is under .li or ul.li use the following:
margin-left: auto;
margin-right: auto;
Or you can use the following and add your own flare to it:
#nav {
width:750px;
margin:0 auto;
list-style:none;
}
#nav li {
float:left;
}
#nav a {
display:block;
text-align:center;
width:150px; /* fixed width */
text-decoration:none;
}
With the following html to follow up:
<ul id="nav">
<li>HOME</li>
<li>CAPABILITIES</li>
<li>ABOUT US</li>
<li>RFQ</li>
<li>CONTACT US</li>
</ul>
You can add this styling to the navbar ID.
left:50%; transform:translateX(-50%);
You should look at some of your CSS because you are using position absolute on the navbar ID and then display flex on the flex-container class of that same div. It looks like you are trying to center the div using two different methods.
Related
I'm having a very basic problem with the CSS in my React website, and I was looking for some help.
I have a very barebones header section set up
HTML:
<div className="nav">
<div className="menu">
Menu
</div>
<div className="logo">
<a className="title" href="/">Brand Name</a>
<a className="subtitle" href="/">Slogan</a>
</div>
<div className="lang">
<ul>
<li>FB</li>
<li>IG</li>
<li>TW</li>
</ul>
</div>
</div>
CSS:
body {
margin: 0;
padding: 0;
}
.nav{
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
color: white;
text-decoration: none;
font-family: Visual;
letter-spacing: 1px;
width: 100%;
.menu{
a{
text-decoration: none;
color: white;
letter-spacing: 1.3px;
font-size: 1.2rem;
}
}
.logo{
.title{
font-size: 2rem;
}
.subtitle{
text-align: center;
margin-top: 5px;
}
a{
text-decoration: none;
color: white;
display: block;
}
}
.lang{
font-size: 1.2rem;
ul{
li{
display: inline;
margin-left: 10px
}
}
}
}
The flex seems to be working, but when I resize the window horizontally, the center element gets pushed off center and eventually out of the screen on one side.
Is this related to media queries?
add padding: 0; property to ul in your css
ul {
padding: 0;
li {
display: inline;
margin-left: 10px;
}
}
You can use border styling something like
border: 1px solid black;
when have problems with css. In this way, you can see which elements have unexpected properties like paddings or margins. When you use border styling for your ul, you will see it has padding. Make padding zero and your items will be centralized.
I'm a newbie to frontend development and have been experimenting with making custom templates for practice. I created a responsive navbar containing a right-aligned 'Contact' button along with an icon from FontAwesome. The problem arises when the navbar collapses -- the 'Contact' icon seems to shift above the Contact text. Could anyone help me out? I'm attaching some images and code here.
<nav>
<ul class='main-nav'>
<li id="logo">Brand</li>
<li>About</li>
<li>Projects</li>
<ul class='right-nav'>
<li id="contact"><i class="fas fa-envelope-square"></i> Contact</li>
</ul>
</ul>
</nav>
nav{
background-color: rgba(0, 0, 0, 0.75);
font-size:1.33rem;
position: relative;
}
#logo{
margin-right: 3rem;
}
ul{
display: inline-flex;
margin-bottom:0;
margin-top:0;
padding:0;
list-style-type: none;
flex-flow:row;
justify-content: flex-start;
align-items: center;
}
.right-nav{
position: absolute;
right: 0;
}
a{
display: inline-block;
color: antiquewhite;
text-decoration: none;
padding:0.75rem 2.0rem;
}
a:hover{
text-decoration: none;
background-color: rgba(243,134,48,0.5);
color: rgb(255, 255, 255);
font-weight: 500;
}
li{
cursor: pointer;
}
#media all and (max-width:600px){
.main-nav{
flex-direction: column;
width: 100%;
}
a{
padding: 12px 600px;
}
nav{
display: flex;
flex-flow: row wrap;
justify-content: center;
}
#logo{
margin-right: inherit;
}
.right-nav{
position: unset;
}
}
You could use the white-space: nowrap css property to prevent the text wrapped to the next line :
#media all and (max-width:600px){
.main-nav li a{
white-space: nowrap;
}
...
You haven't defined contact in the style sheet , so just define it and keep the position relative
Set the link width of contact to 100%
#media all and (max-width:600px){
#contact a {
width: 100%;
}
}
I tried several times, float: right or display: inline-block, but it didn't work on my side. Can anyone guys help me to achieve it? I want to display my navigation next to each other just like below.
I tried to run several codes just what I mention above. But seems it need your help to work it properly.
.menu {
list-style-type: none;
display: inline-block;
padding: 0 11px;
font-size: 1.5rem;
margin-right: 6rem;
font-family: "Montserrat", sans-serif;
}
ul li .link {
position: relative;
display: inline-block;
}
ul li a {
text-decoration: none;
text-transform: uppercase;
}
.head {
position: relative;
text-align: right;
width: 100%;
padding: 22px 48px 25px;
border-bottom: 1px solid #f5f5f5;
}
div .logopix {
height: 5rem;
display: inline-block;
font-size: 0;
top: 1.1rem;
left: 37px;
position: absolute;
}
<div class="head">
<img class="logopix" src="./images/logo.png" alt="HUB Motivator Logo" >
<ul class="menu">
<li class="link"> About </li>
<li class="link"> Contact Us </li>
</ul>
</div>
add css in
.menu {
display: flex;
justify-content: flex-end;
}
Following same technique as you used with inline-block, to display li on same line add:
ul li {
display: inline-block;
}
Here is the code: Codepen
You can achieve the same requirement by writing very minimal semantic code and styling. Please check the code below.
Semantic HTML code
<header class="header">
<img src="http://via.placeholder.com/150x60"/>
<nav class="navbar">
About
Services
Contact
</nav>
</header>
CSS Style
.header {
display: flex;
justify-content: space-between;
align-items: center;
}
.navbar a {
padding-left: 10px;
font-size: 20px;
}
View Demo : jsfiddle
I'm starting ton code and I have some struggles with this website. First, I made the header with a nabber in it but I can't align it to the right, if I try it with float:right it switches the links. And underneath all that I want a big picture but auto height and width doesn't work if I make the page bigger.
<!DOCTYPE html>
<style>
header{ background-color: white;
margin-left: 20px;
margin-top: 30px;
}
img.logo{ width: 200px;
height: 30px;
}
li{ display: inline;
}
ul{ position: fixed;
top: 20px;
}
a{ font-family: 'Open Sans', sans-serif;
font-size: 20px;
font-weight: 300;
letter-spacing: 0.5px;
}
img.picvp{margin-top: 20px;
width: auto;
height: auto;
}
</style>
<header>
<img class="logo" src="#">
<ul>
<li style="float:right;">Projecten</li>
<li style="float:right;">over</li>
<li style="float:right;"><a href='#'>Contact</a></li>
</ul>
</header>
<sec>
<img class="picvp" src="https://lh4.googleusercontent.com/G3M2vxtCatAm1yxWxUA0VVLZjtePu32ziMPd6TLL3wQhk53s4mokl5v_7Rx0crGBp_2Q6iZJnRU-lzQ=w1262-h905">
</sec>
Thank you!
Using display:flex will help with this.
.header{
background-color: white;
align-items: center;
display: flex;
margin-left: 20px;
margin-top: 30px;
}
.logo{
background-color: #0f0;
width: 200px;
height: 30px;
}
.nav {
font-family: 'Open Sans', sans-serif;
font-size: 20px;
font-weight: 300;
letter-spacing: 0.5px;
margin-left: auto;
}
.nav li{ display: inline-block; }
.nav a {
border-left: solid 1px;
display: block;
padding: 1em;
}
img.picvp{
margin-top: 20px;
width: auto;
height: auto;
}
<header class="header">
<img class="logo" src="#">
<ul class="nav">
<li>Projecten</li>
<li>over</li>
<li><a href='#'>Contact</a></li>
</ul>
</header>
<sec>
<img class="picvp" src="https://lh4.googleusercontent.com/G3M2vxtCatAm1yxWxUA0VVLZjtePu32ziMPd6TLL3wQhk53s4mokl5v_7Rx0crGBp_2Q6iZJnRU-lzQ=w1262-h905">
</sec>
Here is a link to the solution: Fiddle N1
You just needed to add: width: 100%; to the class img.picvp.
Another thing is that you should've set your ul elements like that:
ul{
position: relative;
display: inline;
text-align:right;
}
text-align:right automatically pulls the elements to the right side of the parent div. Also use relative positioning rather than fixed one. I have made some changes to the width of the background image as well. Compare the changes to the code so you can see what exactly has been changed.
hii i modify some of code but impornant i use parent and child an example in your i use as parent and as child see example as follow
(dont forgot to change img src)
header{ background-color: white;
margin-left: 20px;
margin-top: 30px;
}
img.logo{
width: 1100px;
height: 130px;
}
ul,li{ display: inline;
text-decoration:none;
}
a,li{
display: inline;
}
a{ font-family: 'Open Sans', sans-serif;
text-decoration:none;
display: inline;
}
img.picvp{margin-top: 20px;
width: auto;
height: auto;
}
<img class="logo" src="nows.jpg">
<ul>
<li style="float:right;">Projecten</li>
<li style="float:right;">over</li>
<li style="float:right;"><a href='#'>Contact</a></li>
</ul>
I have some navigation links that I would like to align to the right and bottom of my header. The contents are
<ul class="nav navbar-nav navbar-right">
<li>My Subscriptions</li>
<li>Help</li>
<li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
</ul>
and the style I’m applying is
header ul {
float: right;
list-style: none;
margin-right: 15px;
vertical-align: text-bottom;
}
Although these line up to the right ok, they do not align to the bottom of the div — https://jsfiddle.net/cujz8tye/ . What am I missing?
Use CSS Flexbox. Updated Fiddle.
Wrap everything inside the container under .content-holder like:
<div class="content-holder">
<a id="logo" href="#">sample app</a>
<nav>
<ul class="nav navbar-nav navbar-right">
<li>My Subscriptions</li>
<li>Help</li>
<li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
</ul>
</nav>
</div>
Then, apply the CSS:
.content-holder {
display: flex;
height: 60px;
align-items: center;
}
nav {
flex: 1;
align-self: flex-end;
}
nav ul {
margin-top: 0;
margin-bottom: 0;
}
Have a look at the working snippet below:
header {
box-shadow: none;
background: #44505d;
border-bottom: none;
height: 73px;
}
#branding {
height: 60px;
position: fixed;
width: 100%;
top: 0;
z-index: 100001;
}
.content-holder {
display: flex;
height: 60px;
align-items: center;
}
#logo {
font-size: 1.7em;
color: #fff;
text-transform: uppercase;
letter-spacing: -1px;
font-weight: bold;
}
nav {
flex: 1;
align-self: flex-end;
}
#logo:hover {
color: #fff;
text-decoration: none;
}
header ul {
float: right;
list-style: none;
margin: 0 15px 0 0;
}
header ul li {
float: left;
margin-left: 15px;
}
body {
background: #eee;
padding-top: 73px;
line-height: 1.4;
font-family: "Karla", "Helvetica Neue", Arial, Helvetica, sans-serif;
color: #333;
font-size: 14px;
margin: 0;
}
a, .clickable {
color: #53a3c2;
text-decoration: none;
}
<header id="branding" class="clearFix">
<div class="container clearFix">
<div class="content-holder">
<a id="logo" href="#">sample app</a>
<nav>
<ul class="nav navbar-nav navbar-right">
<li>My Subscriptions</li>
<li>Help</li>
<li><a rel="nofollow" data-method="delete" href="/logout">Log Out</a></li>
</ul>
</nav>
</div>
</div>
</header>
Hope this helps!
You can use Flexbox to align vertically items in a simpler way.
Here's an updated Fiddle
Relevant modifications: list isn't floating anymore and:
.container {
display: flex;
justify-content: space-between; /* The 2 flex items a#logo and nav will be aligned leftmost and rightmost */
height: 100%; /* flex container will occupy the full height of its parent (73px) */
}
.container > nav {
align-self: flex-end; /* this flex item (nav children of .container, not ul directly) will align itself vertically at the bottom. */
/* Because the flex container is by default flex-direction: row thus align-self is for the other axis, vertical */
}
edit: use Autoprefixer (if it isn't already the case) to be compatible with IE10+. It'll take care of adding for you the required prefixes for all 3 versions of the flexbox syntax ;)
Try using this code:
header {
height:40px;
line-height: 40px;
}
Replace 40px with the height of the header. This forces vertical-align to work in some cases.