So I want to center a h1 without an image at the center of my navigation which has 2 more elements and it has to be responsive. I tried almost everything I can find but...works mostly with background images, and with inline-block I can't center it perfectly.
Below I have the best I could do, but hover doesn't work, probably cause of h1 width which 100%;
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: white;
text-shadow: 1px 1px 1px black;
}
li {
list-style: none;
}
header {
margin: 0 auto;
text-align: center;
}
ul li {
float: left;
display: block;
background-color: #232323;
width: 50%;
padding: 10px 0 10px 0;
}
ul li:hover {
background-color: black;
}
.logo {
position: relative;
top: -38px;
}
<header>
<nav>
<ul>
<li><a href='#blog'>Blog</a>
</li>
<li><a href='#portofolio'>Portofolio</a>
</li>
</ul>
</nav>
<a href='#/' class='logo'><h1>Tao SandBox</h1></a>
</header>
It has to be a better way!!!
http://codepen.io/taosx/pen/vNWojo?editors=110
http://codepen.io/anon/pen/OyOKrN
Make the h1 use the .logo class. Then set absolute position and center it.
.logo {
position:absolute;
top:0;
left:0;
right:0;
text-align:center;
}
I'd also recommend adding the h1 markup prior to the nav, to maintain a better semantic flow.
<header>
<nav>
<h1 class='logo'><a href='#/' >Tao SandBox</a></h1>
<ul>
<li><a href='#blog'>Blog</a></li>
<li><a href='#portofolio'>Portofolio</a></li>
</ul>
</nav>
</header>
The simplest method is just to make the logo` part of the menu like so:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: white;
text-shadow: 1px 1px 1px black;
}
ul {
overflow: hidden;
}
li {
list-style: none;
}
header {
margin: 0 auto;
text-align: center;
background-color: #232323;
}
ul li {
float: left;
width: 33%;
vertical-align: middle;
}
ul li:hover {
background-color: black;
}
<header>
<nav>
<ul>
<li><a href='#blog'>Blog</a>
</li>
<li><a href='#/' class='logo'><h1>Tao SandBox</h1></a>
</li>
<li><a href='#portofolio'>Portofolio</a>
</li>
</ul>
</nav>
</header>
Related
This is my HTML:
<nav class="navbar">
<ul>
<li><a class="active home_button" href="#home">Home</a></li>
<li>Profile</li>
<li>Results</li>
<li>About Us</li>
</ul>
</nav>
This is the CSS:
.navbar {
height: 73px;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
height: 70px;
padding: 0;
overflow: hidden;
background-color: #362890;
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
color: #fff;
text-align: center;
padding: 22px 20px;
text-decoration: none;
font-size: 26px;
I want to do two things. Firstly I want to make the color of Home orange. Secondly, I want to move About Us on the right side while keeping others on the left side. How can I do that?
Use float:right for the About Us item, and color:orange for the Home item.
.navbar {
height: 73px;
width: 100%;
}
.navbar ul {
list-style-type: none;
margin: 0;
height: 70px;
padding: 0;
overflow: hidden;
background-color: #362890;
}
.navbar li {
float: left;
}
.navbar li a {
display: block;
color: #fff;
text-align: center;
padding: 22px 20px;
text-decoration: none;
font-size: 26px;
}
.home_button{
color:orange !important;
}
#about-us{
float:right;
}
<nav class="navbar">
<ul>
<li><a class="active home_button" href="#home">Home</a></li>
<li>Profile</li>
<li>Results</li>
<li id="about-us">About Us</li>
</ul>
</nav>
I think adding a
!important to .home_button should do the trick, give it a try...
Example
.home_button {
color: orange !important;
}
The !important property in CSS is used to provide more weight (importance) than normal property. In CSS, the !important means that “this is important”, ignore all the subsequent rules, and apply !important rule
For more info on how !important works check here
give home and about a class, then style it
I'm trying to do a sort of nav bar without using any pre existing library.
The problem is that I want the nav links to have a padding but the header is not covering their height.
Here's what I've done so far
* {
margin: 0;
}
.header {
color: #fefbed;
box-sizing: border-box;
width: 100%;
position: sticky;
top: 0;
padding-right: 4.7%;
text-align: right;
background-color: #115695;
z-index: 1;
}
ul {
box-sizing: content-box;
}
li {
font-family: Montserratextrabold;
font-size: 16px;
display: inline;
margin-left: 2%;
}
li a {
padding: 5%;
border: 2px solid yellow;
color: #fefbed;
text-decoration: none;
}
<div class="header">
<ul>
<li>HOME</li>
<li>IL FILM</li>
<li>GALLERY</li>
<li><a class="selected" href="about.html">ABOUT</a></li>
</ul>
</div>
The result is this one. Why?
This is because a tags are inline elements. Inline elements treat margin and padding differently than block elements. You can add left and right properties, but not top or bottom.
You can change the display to inline-block which will respect the top and bottom padding you set.
* {
margin: 0;
}
.header {
color: #fefbed;
box-sizing: border-box;
width: 100%;
position: sticky;
top: 0;
padding-right: 4.7%;
text-align: right;
background-color: #115695;
z-index: 1;
}
ul {
box-sizing: content-box;
}
li {
font-family: Montserratextrabold;
font-size: 16px;
display: inline;
margin-left: 2%;
}
li a {
padding: 5%;
border: 2px solid yellow;
color: #fefbed;
text-decoration: none;
display: inline-block;
}
<div class="header">
<ul>
<li>HOME</li>
<li>IL FILM</li>
<li>GALLERY</li>
<li><a class="selected" href="about.html">ABOUT</a></li>
</ul>
</div>
EDIT
I did mark this as a duplicate, but wanted to give an answer, because OP asked why the header wasn't expanding - and the duplicated doesn't answer the question explicitly.
I've added a transparent mask on my background image. The 1st layer will be my navigation bars, contents. Second layer will be the transparent mask, and last layer will be the background image. I manage to make my navigation bar to stay in front of the transparent mask, but now I'm unable to click on the navigation.
HTML:
<body>
<div id="top">
<div id="mask"></div>
<ul>
<li>RACHEL LIM</li>
<li style="float:right">CONTACT</li>
<li style="float:right">GALLERY</li>
<li style="float:right">ABOUT ME</li>
<li style="float:right"><a class="active" href="index.html">HOME</a></li>
</ul>
<h1>I'M A CAT LOVER.</h1>
</div>
</body>
CSS:
body {
font-family:Calibri;
font-size:18px;
margin:0px;
}
#top{
background-image:url(https://static1.squarespace.com/static/5376a044e4b0cf50765bdde1/t/5377554ae4b0aefc671d7dcc/1400329549490/typewriter.jpg);
background-size: 100%;
position:relative;
height:100vh;
z-index:-10;
}
#mask{
position:absolute;
background-color:rgba(0,0,0,0.4);
height:100%;
width:100%;
z-index:-5;
}
ul {
list-style-type: none;
margin:0;
padding:0;
overflow:hidden;
}
li {
float:left;
padding:20px;
}
li a {
display:block;
color:#EDECEA;
text-align:center;
padding:14px 16px;
text-decoration: none;
}
li a:hover {
color: white;
}
.active{
color: white;
}
h1{
font-size:100px;
color:white;
text-align:center;
}
I suggest to not use negative z-index in this scenario, as it will make things a lot more complicated. Instead remove all the z-indexes and set the position of your list and headline to relative.
body {
font-family: Calibri;
font-size: 18px;
margin: 0px;
}
#top {
background-image: url(https://static1.squarespace.com/static/5376a044e4b0cf50765bdde1/t/5377554ae4b0aefc671d7dcc/1400329549490/typewriter.jpg);
background-size: 100%;
position: relative;
height: 100vh;
/*z-index: -10;*/
}
#top > * {
position: relative;
}
#mask {
position: absolute;
background-color: rgba(0, 0, 0, 0.4);
height: 100%;
width: 100%;
/*z-index: -5;*/
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
padding: 20px;
}
li a {
display: block;
color: #EDECEA;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
color: white;
}
.active {
color: white;
}
h1 {
font-size: 100px;
color: white;
text-align: center;
}
<div id="top">
<div id="mask"></div>
<ul>
<li>RACHEL LIM
</li>
<li style="float:right">CONTACT
</li>
<li style="float:right">GALLERY
</li>
<li style="float:right">ABOUT ME
</li>
<li style="float:right"><a class="active" href="index.html">HOME</a>
</li>
</ul>
<h1>I'M A CAT LOVER.</h1>
</div>
I've been trying to center the items within the navigation but no such luck. Every single solution turns my navigation bar from this: horizontal navigation bar to this: vertical navigation bar.
Any help would be greatly appreciated!
HTML with CSS code:
ul {
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align: center;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 10px 10px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<html>
<head>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</body>
</html>
Just remove the float from the li and make the inline-block
li {
/* float: left; */
display:inline-block;
}
The first rule of centering is..."Don't use floats"
ul {
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align: center;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 10px 10px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>News
</li>
<li>Contact
</li>
<li>About
</li>
</ul>
I removed your css on li tag with float and changed it to display and width:
li {
display: -webkit-inline-box;
width: 60px;
}
https://i.stack.imgur.com/LbYd4.png`
I hope this helped you #C. Lagos
Ok, so this is what my site currently looks like. (I can't post an actual pic here apparently) http://imgur.com/Cqb2rf2
Is there a way to align that picture with the Home | About Me | Contact nav bar?
Also, as you can see, the borders to the right of Home and About Me are too close to the text. Can I center that between them somehow? I'm slowly building my first site as I teach myself, so i really appreciate your help!
Here's my code:
#firstpic {
margin: 0px;
padding: 0px;
}
#propic {
width: 15%;
}
#navigation {
border-bottom: 2px dotted #000000;
}
.bh {
border-right: 2px solid black;
}
.navbar {
font-size: 50px;
text-align: center;
font-family: Courier;
}
.navbar li {
display: inline;
}
<html>
<head>
<title>NAV test</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<div id="navigation">
<div id="firstpic"><img src="D:\Testnavbar\Images\Profile Pic.png" id="propic"/></div>
<ul class="navbar">
<li class="bh">Home</li>
<li class="bh">About Me</li>
<li>Contact</li>
</ul>
</div>
</body>
</html>
You can set the image div and the ul to display inline...
See this demo
#firstpic {
margin: 0px;
padding: 0px;
display: inline;
}
.navbar {
font-size: 50px;
text-align: center;
font-family: Courier;
display: inline;
}
Or this demo for inline-block
Set the divs to be inline-block and vertical-align:middle
#firstpic {
margin: 0px;
padding: 0px;
display: inline-block;
width: 15%;
vertical-align: middle;
background: red;
height: 50px;
}
#navigation {
border-bottom: 2px dotted #000000;
}
ul {
display: inline-block;
vertical-align: middle;
}
li {
border-right: 2px solid black;
padding-right: 1rem;
}
li:last-child {
border-right: none;
}
.navbar {
font-size: 20px; /* for demo purposes only */
text-align: center;
font-family: Courier;
}
.navbar li {
display: inline-block;
}
<div id="navigation">
<div id="firstpic">
<img src="D:\Testnavbar\Images\Profile Pic.png" id="propic" />
</div>
<ul class="navbar">
<li>Home</li>
<li>About Me</li>
<li>Contact</li>
</ul>
</div>
Add padding-right to push the list text away from the border.
To move the separator over, try adding "padding-right: 20px;" to .navbar li:
.navbar li {
display: inline;
padding-right: 20px;
}
https://jsfiddle.net/lemoncurry/2xat53n8/
Make the picture one of the list item elements so it displays in-line with the other navigation links.
Give .navbar a fixed height.
Set it to vertical-align middle.
Give it a line-height equal to the .navbar height.
#firstpic {
margin: 0px;
padding: 0px;
height: 150px;
}
#navigation {
border-bottom: 2px dotted #000000;
text-align: center;
height: 100px;
vertical-align: middle;
line-height: 100px;
}
.bh {
border-right: 2px solid black;
}
ul {
margin: 0;
padding: 0;
}
.navbar {
font-size: 50px;
font-family: Courier;
}
.navbar li {
display: inline;
}
<body>
<div id="navigation">
<ul class="navbar">
<li id="firstpic"><img src="D:\Testnavbar\Images\Profile Pic.png" id="propic"/></li>
<li class="bh">Home</li>
<li class="bh">About Me</li>
<li>Contact</li>
</ul>
</div></body>