I'm trying to align my h2 to the right side of the header while keeping the same vertical orientation as it is right now, but float right does not seem to work. Any ideas?
<div class="header-bg">
<h1>
Heading
</h1>
<h2>
this is where you write more things
</h2>
</div>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact Me</li>
<li>About</li>
</ul>
Here is the CSS:
body {
margin: 0;
}
.header-bg {
background: lightblue;
}
h1, h2 {
display: inline-block;
margin: 10px 10px;
/* border: solid black 1px; */
}
}
h2 {
/* border: solid black 1px; */
float: right;
}
ul {
clear: both;
list-style: none;
margin: 0;
padding: 0;
background: #333;
overflow: hidden;
}
Here is the link to jsfiddle: https://jsfiddle.net/n8xjk4ax/3/
First, remove the extraneous }, like the comments say.
Now with the h2 floating, it will no longer be on the same baseline as the h1. If you do want them to be, there are several possibilities.
Don't float it, but justify-align the items in the container
body {
margin: 0;
}
.header-bg {
background: lightblue;
padding: 10px;
text-align: justify;
-moz-text-align-last: justify;
text-align-last: justify;
}
h1, h2 {
display: inline-block;
margin: 0;
/* border: solid black 1px; */
}
ul {
clear: both;
list-style: none;
margin: 0;
padding: 0;
background: #333;
overflow: hidden;
}
li a {
display: block;
color: white;
text-decoration: none;
text-align: center;
padding: 14px 16px;
}
li {
float: left;
}
li:last-child {
float: right;
}
li a:hover {
background-color: #111;
color: white;
}
<div class="header-bg">
<h1>
Heading
</h1>
<h2>
this is where you write more things
</h2>
</div>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact Me</li>
<li>About</li>
</ul>
or if you do want to float, the container will also need overflow:hidden to account for narrow screens. And you should give the h1 and the h2 the same metrics. Using line-height is most straightforward.
body {
margin: 0;
}
.header-bg {
background: lightblue;
overflow:hidden;
}
h1, h2 {
display: inline-block;
margin: 0 10px;
line-height: 4rem;
/* border: solid black 1px; */
}
h2 {
/* border: solid black 1px; */
float: right;
}
ul {
clear: both;
list-style: none;
margin: 0;
padding: 0;
background: #333;
overflow: hidden;
}
li a {
display: block;
color: white;
text-decoration: none;
text-align: center;
padding: 14px 16px;
}
li {
float: left;
}
li:last-child {
float: right;
}
li a:hover {
background-color: #111;
color: white;
}
<div class="header-bg">
<h1>
Heading
</h1>
<h2>
this is where you write more things
</h2>
</div>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact Me</li>
<li>About</li>
</ul>
Related
I am trying to place an unordered list underneath two headers. I currently have the two headers aligned right and floated right, and they seem to be in the right position. However, when I try to align and float the unordered list to the right or use margins, it goes to very strange positions on the page.
body {
background-color: forestgreen;
color: yellow;
}
ul {
list-style-type: none;
align: right;
float: right;
padding: 0px;
width: 200px;
background-color: greenyellow;
text-align: center;
border: 2px solid yellow;
}
li a {
display: block;
margin: 0;
padding: 8px 16px;
color: darkcyan;
text-decoration: none;
font-family: Helvetica;
}
li a.active {
background-color: yellow;
color: red;
}
li a:hover:not(.active) {
background-color: skyblue;
color: forestgreen;
text-decoration: underline wavy brown;
}
h1 {
float: right;
align: right;
font-family: Helvetica;
font-size: 50px;
padding-right: 10px;
}
h2 {
float: right;
align: right;
font-family: Helvetica;
font-size: 30px;
margin-left: 1200px;
padding-right: 30px;
}
<h1>Welcome to ENlightenment!</h1>
<h2>Can you feel the love?</h2>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Programming</li>
<li>Japanese</li>
<li>Russian</li>
<li>Video Games</li>
<li>Music</li>
</ul>
And here is an image explaining what I am trying to do:
Webpage plan
The best way to do this would be Flex Box.
I edited some parts of your code to see. with the arrival of CSS3, people won't use float / position to do this kind of stuff.
<!DOCTYPE html>
<html>
<head>
<title>ENlightenment</title>
</head>
<body>
<style>
body {
background-color: forestgreen;
color: yellow;
}
ul {
list-style-type: none;
padding: 0px;
width: 200px;
background-color: greenyellow;
text-align: center;
border: 2px solid yellow;
}
li a {
display: block;
margin: 0;
padding: 8px 16px;
color: darkcyan;
text-decoration: none;
font-family: Helvetica;
}
li a.active {
background-color: yellow;
color: red;
}
li a:hover:not(.active) {
background-color: skyblue;
color: forestgreen;
text-decoration: underline wavy brown;
}
h1 {
/* float: right; */
font-family: Helvetica;
font-size: 50px;
padding-right: 10px;
}
h2 {
/* float: right; */
font-family: Helvetica;
font-size: 30px;
/* margin-left: 1200px; */
padding-right: 30px;
}
.navBar {
display: flex;
justify-content: space-between;
}
</style>
<h1>Welcome to ENlightenment!</h1>
<div class="navBar">
<h2>Can you feel the love?</h2>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Programming</li>
<li>Japanese</li>
<li>Russian</li>
<li>Video Games</li>
<li>Music</li>
</ul>
</div>
</body>
</html>
I'm trying to create a navbar whith white bars on top of the options, but the position: absolute is not responding as I expect, even if I place it after a position: relative, the white bars are wider than the width of the options:As you can see here
This is the code I'm following from the tutorial, I would appreciate your help.
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding: 40px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
}
nav a:hover {
color: black;
}
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
width: 100%;
}
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
Another way to achieve the same:
Instead of using the pseudo element, you can use the property border-top along with padding-top to achieve the same, if what you need is the border width to be equal with the length of the option.
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding: 40px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
border-top:5px solid #fff;
padding-top:10px;
}
nav a:hover {
color: black;
}
/*
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
width: 100%;
}*/
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
Here is another way, in-case you need the width of all borders be same. You must give a fixed width to the pseudo element, instead of 100%.
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding: 40px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
}
nav a:hover {
color: black;
}
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
/*width: 100%;*/
width: 100px;
}
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
Your line item element is wider than your link element which means that your white bar will copy the relatively positioned line item's width. If you look in inspector you can see this clearly.
The width
Use the border-top property instead on your links.
nav a{
color: white;
text-decoration: none;
border-top: solid 3px #fff;
padding-top: 35px
}
nav a:hover {
color: black;
}
nav a::before {
}
On nav a:before :
If you want to create it the same width as your text - substract the padding:
width: calc(100% - 80px);
or you want to place it the same size as your li item use:
left: 0;
You have to account for the 40px * 2 = 80 px of padding you have added to the li element.
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding: 40px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
}
nav a:hover {
color: black;
}
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
width: calc(100% - 80px);
}
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
/*delete padding */
margin-left: 70px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
/*add this */
padding: 40px 0;
display: inline-block;
border-top: 5px solid transparent;
}
nav a:hover {
color: black;
/*and this*/
border-top: 5px solid white;
}
/*
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
width: 100px;
}
*/
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
The nav a::before pseudo-element is a child of a::before. Pseudo-elements like ::before or ::hover are always children of the elements whose selectors preface them. You need to put the position:relative property on the rule for nav a. Currently, you have the position:relative property on the li element, which is wider than the a element.
You'll also need to add some other property to raise the line. I've added padding-top to solve that problem.
body {
margin: 0;
background: #222;
font-family: sans-serif;
font-weight: 400;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #151515;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding: 40px;
position: relative;
}
nav a {
position: relative;
padding-top: 20px;
color: white;
text-decoration: none;
}
nav a:hover {
color: black;
}
nav a::before {
content: '';
display: block;
height: 5px;
background-color: white;
position: absolute;
top: 0;
width: 100%;
}
<header>
<div class="container">
<img src="img1.png" style="max-width: 80px; margin-top: 0px;" alt="logo" class="logo">
<nav>
<ul>
<li>CONTACTO</li>
<li>REGISTRATE</li>
<li>INGRESAR</li>
</ul>
</nav>
</div>
</header>
How am I able to center the photo with the text, without minimizing the photo itself?
I've tried; max-height: xx, but that wasn't it.
The photo is centered in height, but not the text. How's this possible?
http://jsfiddle.net/gLpqoamn/
.hoyre{
background-color:#f19f00;
}
.hoyre:hover{
background-color:#d98500;
}
header{
background-color: white;
}
.logo{
width: 57px;
height: 34px;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
}
li {
float: left;
}
li a {
display: block;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
}
li a:hover {
color: #f19f00;
}
body{
margin:0;
font-family: 'Roboto', sans-serif;
background-color: #333;
}
.container{
max-width:1300px;
margin-left:auto;
margin-right:auto;
}
.active {
position: relative;
}
</style>
<body>
<header>
<div class="container">
<ul>
<li><img src="http://i.imgur.com/QAEzQxp.png" class="logo"></li>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li class="hoyre" style="float:right;">Donate</li>
</ul>
</div>
</header>
</body>
I would use flex on the ul, then you can use align-items to vertically center or position the children. A margin-left: auto on the last link will push it to the right, then you can move the background color to the link to keep it from stretching the height of the parent
.hoyre {
margin-left: auto;
background-color: #f19f00;
min-height: 100%;
align-self: stretch;
display: flex;
align-items: center;
}
.hoyre:hover {
background-color: #d98500;
}
header {
background-color: white;
}
.logo {
width: 57px;
height: 34px;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
display: flex;
align-items: center;
}
li {}
li a {
display: block;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
}
li a:hover {
color: #f19f00;
}
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background-color: #333;
}
.container {
max-width: 1300px;
margin-left: auto;
margin-right: auto;
}
.active {
position: relative;
}
</style>
<body>
<header>
<div class="container">
<ul>
<li>
<img src="http://i.imgur.com/QAEzQxp.png" class="logo">
</li>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li class="hoyre">Donate</li>
</ul>
</div>
</header>
</body>
For using height:100% in your css you should define parent element height first. then you change li element to behave like table and fill 100% height.
Update your styles like this(new styles have comment in front of them):
li {
float: left;
height: 100%; /*fill height */
display: table; /* behave as table*/
}
li a {
display: table-cell; /* behave as table-cell then you can use vertical alignment*/
vertical-align: middle;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
height: 100% ; /*fill height */
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
height: 80px; /*define height for using height:100% for child elements */
}
just doing a simple header with a nav element, and I am getting a strange flickering effect on the font when I mouse over the header. Here is the code and and a gif. The links have a :hover rule that changes the color when you mouse over them but that is not the issue as I have tried removing that rule and the flickering still happens. Any help much appreciated. Thank you for your time
.cf:before,
.cf:after {
content: "";
display: table;
}
.cf:after {
clear: both;
}
html,
body {
margin: 0px;
padding: 0px;
height: 100vh;
}
#header {
position: absolute;
width: 100vw;
height: 10vh;
background-color: #666;
opacity: 1;
z-index: 6;
}
#menu {
line-height: 10vh;
float: right;
margin-right: 5vw;
}
#title {
margin-left: 10vw;
line-height: 10vh;
float: left;
}
li,
ul {
padding: 0;
margin: 0;
list-style: none;
float: right;
}
li a {
display: block;
line-height: 10vh;
padding: 0 1em;
font-size: 1.3em;
color: rgb(255, 255, 255);
text-decoration: none;
}
ul {
margin-right: 2em;
}
nav {}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
color: #FFBB3F
}
<div id="header">
<div id="title">
<h1>Title <span id="subtitle">subtitle</span></h1>
</div>
<nav class="cf" id="menu">
<ul>
<li>about</li>
<li>gallery</li>
<li>contact</li>
<li>shop</li>
</ul>
</nav>
</div>
Why isn't the vertical-align: middle; not working on my h1? The ul isn't aligning with the h1 and it should.
#logo {
color: white;
display: inline;
vertical-align: middle;
}
ul {
padding: 20px;
background: rgba(0,0,0,.5);
}
#title {
position: absolute;
text-align: center;
color: white;
font-size: 50px;
top: 100px;
}
#navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: url('../img/bg.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
height: 600px;
}
li {
float: right;
display: inline;
}
li a {
display: inline;
padding: 10px 10px 10px 20px;
text-decoration: none;
color: white;
}
<div id="navbar">
<ul>
<h1 id="logo">Jordan Baron</h1>
<li>About Me</li>
<li>Contact</li>
<li>Projects</li>
</ul>
<h1 id="title">Freelance Web Developer</h1>
</div>
Because vertical-align only applies to inline and table-cell elements, not block-level elements.
In order to have your links line up with your header, you need to assign a line-height equal to the height of the header element (35.33px):
li {
line-height: 35.33px;
}
#logo {
color: white;
display: inline;
vertical-align: middle;
}
ul {
padding: 20px;
background: rgba(0, 0, 0, .5);
}
#title {
position: absolute;
text-align: center;
color: white;
font-size: 50px;
top: 100px;
}
#navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: url('../img/bg.jpg');
background-repeat: no-repeat;
background-size: 100% 100%;
height: 600px;
}
li {
float: right;
display: inline;
line-height: 35.33px;
}
li a {
display: inline;
padding: 10px 10px 10px 20px;
text-decoration: none;
color: white;
}
<div id="navbar">
<ul>
<h1 id="logo">Jordan Baron</h1>
<li>About Me</li>
<li>Contact</li>
<li>Projects</li>
</ul>
<h1 id="title">Freelance Web Developer</h1>
</div>
It's also worth noting that having a <h1> element as a child of <ul> is invalid syntax. Only <li> elements shouold be a child of <ul>. What you should do is bring the title out of the <ul>, and float the entire <ul> element to the right.
Hope this helps! :)
Ready for a bit of a mind flip?
Here comes FLEXBOX! This could be done "better" but I didn't want to change your html structure: https://jsfiddle.net/ohbffjjm/
#navbar {
background-color: pink;
}
#logo {
margin-right: 150px;
align-self: flex-start;
}
ul {
display: flex;
align-items: center;
list-style-type: none;
}
li a {
padding: 10px 10px 10px 20px;
text-decoration: none;
color: white;
}
<div id="navbar">
<ul>
<h1 id="logo">Jordan Baron</h1>
<li>About Me</li>
<li>Contact</li>
<li>Projects</li>
</ul>
</div>
And a flexbox solution with a few html improvements and improvisations.
body {
margin: 0;
}
header {
color: white;
background: rgba( 0, 0, 0, 0.5 );
}
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
header,
ul {
display: flex;
align-items: center;
justify-content: space-between;
}
h1,
h2 {
margin: 0.5rem 0;
}
h1 {
padding: 0 20px;
}
h2 {
text-align: center;
}
ul {
padding: 0 10px;
}
li {
padding: 10px;
}
li a {
color: white;
text-decoration: none;
transition: color 350ms ease-in-out;
}
li a:hover {
color: gold;
}
<header>
<h1 id="logo">Jordan Baron</h1>
<ul>
<li>About Me</li>
<li>Contact</li>
<li>Projects</li>
</ul>
</header>
<main>
<h2>Freelance Web Developer</h2>
</main>