I floated the search_and_avatar div right, and used display and vertical-align properties to align it in middle of parent container. But it sites on the bottom. Why is that?
Here is the complete code in fiddle
HTML:
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
</header>
CSS:
header {
background-color:#151515;
overflow:hidden;
height: 100px;
}
header h1, img, #search_form {
float:left;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
header img {
width:35px;
display: table-cell;
vertical-align: middle;
}
#search_and_avatar {
display: flex;
justify-content: flex-end;
flex: 1;
display: table;
display: table-row;
float:right;
}
header form {
text-align:right;
display: table-cell;
vertical-align: middle;
}
Added flex property into header
body {
margin:0;
font-family: 'Source Sans Pro', sans-serif;
}
ul {
padding:0;
}
header {
background-color:#151515;
overflow:hidden;
height: 100px;
display:flex;
flex:1 1 0;
align-items: center;
justify-content: center;
}
h1 {
color:#FFF;
font-family: "Arial Rounded MT", "Helvetica Rounded", Arial, sans-serif;
font-size: 40px;
font-weight: normal;
line-height:25px;
padding: 10px 20px 0px;
flex: 1;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
#main_nav {
display: table;
text-align:center;
margin:0px auto;
}
#main_nav ul {
display: table-row;
}
#main_nav ul li {
padding: 15px;
display: table-cell;
vertical-align: middle;
top: 50%;
display:inline;
}
header img {
width:35px;
display: inline-block;
vertical-align: middle;
}
#search_and_avatar {
/*display: flex;
justify-content: flex-end;
flex: 1;
display: table;
display: table-row;
float:right;*/
}
header form {
text-align:right;
display:inline-block;
vertical-align: middle;
}
<div id="wrapper">
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
<!--
<ul id="account_nav">
<li>Log In</li>
<li>Sign Up</li>
</ul>
-->
</header>
</div>
Here is fiddle Demo
the normal DOM rendering is that it adds element to next line. This can be easily fixed by wrapping both search_and_avatar and nav_main inside new main_nav_div Div.
Remove the padding from main_nav and have that in main_nav_id
Here is the complete code in Fiddle https://jsfiddle.net/sd2eqkuc/2/
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
#main_nav {
display: table;
text-align:center;
margin:0px auto;
float:left;
}
#main_nav_div
{
padding-top: 35px;
}
Related
I'm trying to say "my website bby" right next to the image logo and for some reason it displays under the logo. Help me please. Here is what it is right now, check the image
Here is my html:
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
.branding {
float: left;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float:right;
}
#logo {
margin-top:5px;
width:40%;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://picsum.photos/200/300" style="width:100px;" />
</div>
<span><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</header>
To have a better understanding of what I'm talking about, check the picture above. I just started out coding, really need to understand what's wrong.
You can also use a combination of display: inline-block and vertical-align: middle.
For example:
body {
font-size: 20px;
font-family: Optima, sans-serif;
line-height: 1.5;
padding: 0;
margin: 0;
}
.container {
width: 80%;
overflow: hidden;
}
header {
font-family: Monaco, sans-serif;
color: white;
padding-top: 100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background: #6BD326;
}
#logo {
float: left;
display: inline-block;
vertical-align: top;
}
.i-b {
display: inline-block;
vertical-align: top;
width: calc(100% - 40%);
}
.branding {
float: left;
}
nav li {
float: left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float: right;
}
#logo {
margin-top: 5px;
width: 40%;
}
<div class="branding">
<div id="logo">
<img src="https://i.picsum.photos/id/237/536/354.jpg?hmac=i0yVXW1ORpyCZpQ-CknuyV-jbtU7_x9EBQVhvT5aRr0" style="width:100px;" />
</div>
<span class="i-b"><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
Jsfiddle code
check this out i removed all the floats and worked with flex
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
margin: 0;
padding: 10px;
background:#6BD326;
display: flex;
justify-content:space-between;
flex-wrap: wrap;
}
#logo {
margin-top: 5px;
display: flex;
align-items: center;
}
#logo span{
margin-left: 10px;
}
nav ul {
display: flex;
flex-wrap: wrap;
}
nav li {
padding: 0px 20px;
list-style-type: none;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://cdn.mos.cms.futurecdn.net/BVb3Wzn9orDR8mwVnhrSyd-1200-80.jpg" style="width:100px;" />
<span><u>My</u> Website bby</span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
</header>
Here is your code which is work
body like that :
<body>
<div class="branding">
<div id="logo">
<img src="./img/logo.png" style="width:100px;" />
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
<span><h1 style="padding: 30px; margin-left:130px;"><u>My</u> Website bby</h1></span>
</div>
</div>
</body>
your css should be like that :
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.body img{
display: inline;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
your mistake was here
.branding {
float: left;
}
inside the <div class="branding"> you can add another div with a style:
display: flex;
justify-content: space-between
Please run your code through the W3C Validator.
You will see that you have an error in the HTML:
Error: Element h1 not allowed as child of element span in this context. (Suppressing further errors from this subtree.)
You can find out more about the distinction between block and inline elements at Can <span> tags have any type of tags inside them? which in turn references the formal spec at https://www.w3.org/TR/html401/struct/global.html#h-7.5.3
For your immediate problem you could make the span a div (which can have h1 elements as children) and make both it and the image inline-blocks.
try with flex and delete float:left
.branding {
display:flex;
align-items:center;
}
I am recreating Google's page as practice, I have looked and looked, but how can I make two li, gmail and images, appear on the right side of the page? Also what can I improve on in my code?
body {
font-family: Arial;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
img {
width: auto;
height: 100px;
display: block;
margin-left: auto;
margin-right: auto;
padding-top: 15%;
}
.searchbar {
text-align: center;
}
.nav_bar {
word-spacing: 10px;
}
<nav class="nav_bar">
<ul>
<li>About</li>
<li>Store</li>
<li>Gmail</li>
<li>Images</li>
</ul>
</nav>
<div class="google">
<img src="http://pngimg.com/uploads/google/google_PNG19644.png" alt="google">
</div>
<div class="searchbar">
<input type="text" placeholder="Search..." id="search">
</div>
You need to float the element to the right, for instance with:
.nav_bar ul {
float: right;
}
As a more general comment, you could organize the HTML and CSS in different files, to better diferentiate between the content and the styles. Try to follow the W3 basic tutorial to catch the first concepts behind markup for the web.
Ideal way is to have separate menu ul for the left and for right side of the screen.
So then you can use float property to move the menu for right to the right side in a clean way.
The code will look like this:
body {
font-family: Arial;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
img {
width: auto;
height: 100px;
display: block;
margin-left: auto;
margin-right: auto;
padding-top: 15%;
}
.searchbar {
text-align: center;
}
.nav_bar {
word-spacing: 10px;
}
.menu_left{
float: left;
}
.menu_right{
float: right;
}
<nav class="nav_bar">
<ul class="menu_left">
<li>About</li>
<li>Store</li>
</ul>
<ul class="menu_right">
<li>Gmail</li>
<li>Images</li>
</ul>
</nav>
<div class="google">
<img src="http://pngimg.com/uploads/google/google_PNG19644.png" alt="google">
</div>
<div class="searchbar">
<input type="text" placeholder="Search..." id="search">
</div>
I hope it helps.
I have understand that you want to put only "images" and "gmail" on the right, so you can do this (the code is on the comment)
Good luck
<nav class="nav_bar">
<ul>
<li>About</li>
<li>Store</li>
<li>Images</li>
<li>Gmail</li>
</ul>
</nav>
<div class="google">
<img src="http://pngimg.com/uploads/google/google_PNG19644.png" alt="google">
</div>
<div class="searchbar">
<input type="text" placeholder="Search..." id="search">
</div>
CSS
body{
font-family: Arial;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li{
display: inline;
float:left;
padding: 0 5px;
}
img {
width: auto;
height: 100px;
display: block;
margin-left: auto;
margin-right: auto;
padding-top: 15%;
}
.searchbar {
text-align:center;
}
.nav_bar {
word-spacing: 10px;
}
nav.nav_bar li:nth-child(3), nav.nav_bar li:nth-child(4) {
float: right;
}
I am displaying the box with text in the center of the screen which is working. Now I am adding the footer at the bottom but that is displaying at the top because my div is absolute. I have to display at the bottom with the left and right text.
.bg_cyan{background-color: #5AFCEB;}
.center_screen{
position: absolute;
left: 50%;
transform: translate(-50%,-50%);
}
.thankyou_set{top: 50%;}
.thankyou_set .thanku_popup3{
width: 100%;
margin: auto;
box-sizing: border-box;
padding: 50px;
background-color: red;
}
.thankyou_content h2{font-size: 30px;}
.form_terms{
margin: 0;padding: 0;list-style: none;
float: right;
}
.form_terms li{display: inline-block;margin: 10px;}
.form_terms li a{color: #5e5e5e;font-size: 18px;}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<div class="clearfix">
<div class="thankyou_set center_screen">
<div class="thanku_popup3">
<div class="thankyou_content">
<h2>Thank you for registering!</h2>
</div>
</div>
</div>
</div>
<div class="corp_footer">
<div class="form_copy_right pull-left">
<p>© All right reserved</p>
</div>
<ul class="form_terms">
<li>Website</li>
<li>About</li>
<li>Privacy Policy</li>
</ul>
</div>
</div>
I am getting the output
No need to absolute position when we have the power of Flexbox these days. Here's a CodePen you can play around with to see how other elements will behave, but vertical alignment is easy with Flexbox by setting the parent container to display:flex; then give the child element (what you want centered) a margin of margin: auto auto; to perfectly center it to the center of its parent both vertically and horizontally.
.container {
display: flex;
background: red;
width: 100%;
height: 100vh;
}
.thankyou_content {
margin: auto auto;
}
.thankyou_set .thanku_popup3 {
display: flex;
padding: 50px;
width: 100%;
box-sizing: border-box;
background-color: red;
}
.thankyou_content h2 {
font-size: 30px;
}
.corp_footer {
display: flex;
justify-content: space-between;
}
.form_terms {
margin: 0;
padding: 0;
list-style: none;
float: right;
}
.form_terms li {
display: inline-block;
margin: 10px;
}
.form_terms li a {
color: #5e5e5e;
font-size: 18px;
}
<div class="container">
<div class="thankyou_content">
<h2>Thank you for registering!</h2>
</div>
</div>
<div class="corp_footer">
<div class="form_copy_right pull-left">
<p>© All right reserved</p>
</div>
<ul class="form_terms">
<li>Website</li>
<li>About</li>
<li>Privacy Policy</li>
</ul>
</div>
</div>
You do this very easily with write some flex properties. All you have to do now just replace the below code and you get the result.
.main-container{
height:100vh;
}
.content{
display:flex;
background:red;
flex-direction:column;
justify-content:center;
width:100%;
height:100%;
background:#fff;
}
.corp_footer{
display:flex;
align-items:flex-end;
justify-content:space-between;
}
.thankyou_content h2{
font-size:30px;
}
.thankyou_content{
width: 100%;
margin: auto;
box-sizing: border-box;
padding: 50px;
background-color: red;
text-align:center;
}
.center_screen{
flex:3;
align-items:center;
display:flex;
justify-content:center;
}
.form_terms{
display:flex;
list-style:none;
flex-wrap:wrap;
}
.form_terms li a{
margin: 10px;
text-decoration:none;
}
.form_terms li a{
color: #5e5e5e;font-size: 18px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container main-container">
<div class="content">
<div class="thankyou_set center_screen">
<div class="thanku_popup3">
<div class="thankyou_content">
<h2>Thank you for registering!</h2>
</div>
</div>
</div>
<div class="corp_footer">
<div class="form_copy_right pull-left">
<p>© All right reserved</p>
</div>
<ul class="form_terms">
<li>Website</li>
<li>About</li>
<li>Privacy Policy</li>
</ul>
</div>
</div>
</div>
Please have a look at this fiddle
HTML:
<nav id="main_nav">
<ul>
<li>Blog</li>
</ul>
</nav>
CSS:
#main_nav {
text-align:center;
width:50%;
padding-top: 35px;
}
Why is the nav not aligning in the center of the header
Don't give unwanted margin of padding to adjust your html
Here is a working Demo
removed padding-top margin etc and added:
header{
display: flex;
align-items: center;
overflow: scroll;
justify-content: center;
}
header h1{
min-width: 250px;
margin: 0;
}
#search_and_avatar{
flex: 1;
display: flex;
justify-content: flex-end;
}
body {
margin:0;
font-family: 'Source Sans Pro', sans-serif;
}
header {
background-color:#151515;
overflow:hidden;
height: 100px;
}
h1 {
color:#FFF;
font-family: "Arial Rounded MT", "Helvetica Rounded", Arial, sans-serif;
font-size: 40px;
font-weight: normal;
line-height:25px;
padding: 0px 20px;
}
header h1, img, #search_form {
float:left;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
#main_nav {
display: table;
text-align:center;
width:50%;
}
#main_nav ul {
display: table-row;
}
#main_nav ul li {
padding: 15px;
display: table-cell;
vertical-align: middle;
top: 50%;
display:inline;
}
header img {
width:35px;
}
header{
display: flex;
align-items: center;
overflow: scroll;
justify-content: center;
}
header h1{
flex: 1;
min-width: 250px;
margin: 0;
}
header form{
text-align: right;
}
#search_and_avatar{
flex: 1;
display: flex;
justify-content: flex-end;
}
<div id="wrapper">
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
<!--
<ul id="account_nav">
<li>Log In</li>
<li>Sign Up</li>
</ul>
-->
</header>
</div>
#main_nav {
text-align:center;
width:50%;
padding-top: 35px;
margin:0px auto;
}
Remove display: table; and add margin: 0 auto; to your nav
#main_nav {
text-align: center;
width: 50%;
padding-top: 35px;
margin: 0 auto;
}
Check this Example fiddle
here is the html, that for one reason or another, the li's and input's will not go on the same line
<div class="nav">
<form class="searchbox">
<input name="search" type="text">
<input type="submit">
</form>
<ul>
<li>Home</li>
<li>Galleries</li>
<li>Contact</li>
<li>Story</li>
</ul>
</div>
this is the css to go with
.searchbox {
display:inline;
padding-right:2em;
padding-left:55em;
}
.nav {
position:absolute;
padding:.5em 0em .5em 2em;
font-size:1.25em;
margin:-3.5em 0em 98em 0em;
background-color:black;
}
.nav li {
display:inline-block;
float:left;
color:#7C6C5A;
}
this is going on a semi professional site, on four pages, so i need something that works.
The "one reason or another" is that your <ul> is a block-level element. Apparently you were going in the right direction, since you put display: inline; on the <form> and display: inline-block; on the <li> elements.
Since the <ul> is the sibling of the <form>, these should both have display: inline-block;. And I might recommend some vertical-align: middle; action.
So I changed your CSS to:
.searchbox {
display: inline-block;
padding-right: 2em;
}
ul {
display: inline-block;
vertical-align: middle;
}
.nav {
position:absolute;
padding:.5em 0em .5em 2em;
font-size:1.25em;
margin: 0 0 98em 0em;
background-color:black;
}
.nav li {
display:inline-block;
float:left;
color:#7C6C5A;
}
And here is a JSFiddle. Cheers!
Try this...
.content-box {
margin: 0 auto 0;
width: 1000px;
}
.nav {
margin: 0;
padding: 0;
width: 700px;
}
.nav li {
float: left;
margin: 0;
padding: 0 6px;
overflow: hidden;
}
.nav li a {
color: #444;
margin: 0;
padding: 0;
text-decoration: none;
}
.searchbox {
width: 250px;
float: right;
}
<div class="content-box">
<div class="nav">
<ul>
<li>Home</li>
<li>Galleries</li>
<li>Contact</li>
<li>Story</li>
<form class="searchbox">
<input name="search" type="text">
<input type="submit">
</form>
</ul>
</div>
</div>