How to vertically align website logo and header with navbar - html

I'm trying to vertically align the navbar with my website logo and header, but whenever I try to use display: inline-block; and vertical-align: middle; on the , , and tags (which are all in the same div on the top of my website), the navbar goes from horizontal to vertical which is not what I want. I'm also using Bootstrap 4.
Here's my html:
<div id="home">
<div class="page-header">
<div class="container">
<div class="row">
<div class="col-md-4">
<img src="images/test-logo.png">
<h1>Rigid Signs</h1>
</div>
<div class="col-md-8">
<ul class="nav">
<li>Home</li>
<li>Services</li>
<li>Portfolio</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
</div>
</div>
and here's my CSS:
#home {
background: url('../images/workshop-testphoto.jpg') no-repeat center / cover;
height: 844px;
}
.page-header {
background: rgba(0,0,0,.6);
postion: fixed;/*not working*/
padding: 11px 0;
}
.page-header img, .page-header h1 {
display: inline-block;
vertical-align: middle;
}
.page-header img {
width: 88px;
}
.page-header h1 {
font-size: 32px;
color: white;
margin-left: 11px;
}
.page-header .nav {
float: right;
}
.page-header .nav li a {
display: inline-block;
font-size: 22px;
text-decoration: none;
color: #fff;
}
I'm not very good with bootstrap 4, but if anyone knows how I could use bootstrap 4 to also achieve my goal of vertically aligning all elements in my navbar, that would be great aswell

If I'm not wrong, bootstrap grid have "float" styling in it. so col-md-4 and col-md-8 will have float left. so, use inline-block instead of float, and give vertical-align middle for both div. using the current html you're showing, it will be
.row {
font-size: 0; /*to remove blank space between two inline-block element*/
}
.col-md-4, .col-md-8 {
float: none;
display: inline-block;
vertical-align: middle
}

Related

How to vertically center all the elements inside a navigation bar?

Taking this example as starting point, I am trying to create a navigation bar with a left-aligned and a right-aligned section, ensuring vertical alignment into middle for all the elements inside it.
Unfortunately, the right part is not vertically centered, even if right-aligned and left-aligned classes have both the vertical-align: middle property set. What do I am missing? Here is the code bunch:
header img {
display: inline-block;
}
header nav {
display: inline-block;
font-size: 1em;
vertical-align: middle;
}
header nav ul {
padding: 0;
margin: 0;
}
header nav ul img {
vertical-align: middle;
}
header nav li {
display: inline-block;
}
header nav li a {
display: inline-block;
padding: .4em .8em;
font-size: 1em;
text-decoration: none;
color: black;
background: #eee;
line-height: 1;
}
header .container {
max-width: 800px;
margin: auto;
overflow: hidden;
}
.left-aligned {
float: left;
}
.right-aligned {
float: right;
}
<html>
<body>
<header role="banner">
<div class="container">
<div class="left-aligned">
<img class="left" src="http://placehold.it/200x200">
</div>
<div class="right-aligned">
<nav id="navigation" role="navigation">
<ul>
<li>
<img src="http://placehold.it/100x100">
About Us
</li>
<li>Biographies</li>
<li>Services</li>
<li>Careers</li>
<li>Contact</li>
</ul>
</nav>
</div>
</div>
</header>
</body>
</html>
This is a great use case for flexbox - by adding the following three lines to your container class, you can achieve a left and right aligned section:
display: flex;
justify-content: space-between;
align-items: center;
So your final code will look like this (I've separated HTML and CSS for legibility):
header img {
display: inline-block;
}
header nav {
display: inline-block;
font-size: 1em;
}
header nav ul {
padding: 0;
margin: 0;
}
header nav ul img {
vertical-align: middle;
}
header nav li {
display: inline-block;
}
header nav li a {
display: inline-block;
padding: .4em .8em;
font-size: 1em;
text-decoration: none;
color: black;
background: #eee;
line-height: 1;
}
header .container {
max-width: 800px;
margin: auto;
overflow: hidden;
display: flex;
justify-content: space-between;
align-items: center;
}
<header role="banner">
<div class="container">
<div class="left-aligned">
<img class="left" src="http://placehold.it/200x200">
</div>
<div class="right-aligned">
<nav id="navigation" role="navigation">
<ul>
<li>
<img src="http://placehold.it/100x100">
About Us
</li>
<li>Biographies</li>
<li>Services</li>
<li>Careers</li>
<li>Contact</li>
</ul>
</nav>
</div>
</div>
</header>
This justifies the direct children of the flexbox to horizontally align left and right with space in between. If more than two elements were to exist, they would be placed with equal spacing across the width of the container.
Align items will determine the vertical alignment of elements inside the flexbox.
This is true when flex-direction is not set (default value - row). When flex-direction is set to column, the "axis" affected by justify and align are reversed.

How can I align button vertically in nav bar? [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
How do I vertically align text in a div?
(34 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
I'm practicing creating a website, and am currently trying to replicate this site. At the moment I am working on the navigation bar, but I am unable to vertically align the button vertically in the nav.
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
line-height: 38px;
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<header class="header clearfix">
<h1>
<img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo">
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li>View icons</li>
<li>Buy now</li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>
My working image
I wanna align the top-right button of the nav bar vertically. I know this can be solved using display: flex;, but I wanna solve it another way. Is there a proper way to solve this issue?
Even though your solution using display: flexseems perfectly fine to me, you could fix it using vertical-align: middle; and display: inline-block on all of the items (the two links and the button)
.header {
background-color: red;
padding: 40px 20px;
}
.header h1 {
background-color: yellow;
float: left;
}
.header h1 img {
display: block;
}
.header__nav {
background-color: aqua;
float: right;
}
.header__nav li {
float: left;
height: 38px;
}
.header__nav li a {
padding: 0 20px;
display: inline-block;
<!-- Remove - line-height: 38px; -->
}
.contents {
background-color: green;
}
.footer {
background-color: blue;
}
<!-- new added class -->
.clearfix{
display: flex;
align-items: center;
}
<header class="header clearfix">
<h1>
<img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo">
</h1>
<nav class="header__nav">
<ul class="clearfix">
<li>View icons</li>
<li>Buy now</li>
<li><button class="menu">menu</button></li>
</ul>
</nav>
</header>
<section class="contents">
contents
</section>
<footer class="footer">
footer
</footer>

Centering footer text on bootstrap

Hey guys im having an issue here with my footer when resize the screen / on mobile. My footer text is placed to the left and my ul's are maligned. I attached my code bellow, im looking to keep the same layout on desktop but when the screen shrinks have the footer text align center with the layout but keep the same text styling as well so the uls and li's line up with text-align:left;
https://s32.postimg.org/glxnik7gl/footer.png
HTML:
<footer>
<div class="container" id="contact">
<div class="row">
<div class="col-sm-3">
<a class="navbar-brand" href="#"><span class="logo-text-footer">Company Logo</span></a>
</div>
<div class="col-sm-3">
<ul>Quick Links</ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
</div>
<div class="col-sm-3">
<ul>Address</ul>
<li>1234 Example Street </li>
<li>City</li>
<li>State</li>
<li>55555</li>
</div>
<div class="col-sm-3">
<li>Call: 555-555-5555</li>
<li>E-Mail: example#gmail.com</li>
<div class="social-icons">
<li>Facebook</li>
<li>YouTube</li>
<li>Instagram</li>
<li>Twitter</li>
</div>
</div>
</div>
<div class="row">
<div class="privacy">
<p>
<?php echo date("Y"); ?> Company Name | All Rights Reserved</p>
</div>
</div>
</div>
</footer>
CSS:
/* Footer */
footer {
height: auto;
background: #3a526a;
}
footer #contact {
height: auto;
min-height: 13em;
}
footer .contact-text {
position: relative;
top: 20em;
}
span.logo-text-footer {
color: #fff;
font-size: 30px;
vertical-align: top;
position: absolute;
bottom: 15px;
}
footer #contact ul {
font-size: 17px;
color: #fff;
text-transform: uppercase;
font-family: 'Myriad Pro';
padding: 0;
}
footer #contact li {
color: #fff;
list-style: none;
font-family: 'Myriad Pro';
padding-top: 5px;
}
footer .social-icons li {
display: inline-block;
}
footer .privacy {
height: 50px;
background: #455e76;
}
footer .privacy p {
color: #fff;
text-align: center;
display: block;
margin: auto;
line-height: 50px
}
Add text-center class in footer. Change col-sm-3 class to col-md-3 col-sm-12 in each div. As this is for mobile view, add following CSS:
#media (max-width: 768px) {
footer {
text-align: center;
}
.navbar-brand {float:none;}
}
Check jsFiddle here.
Alignment in bootstrap is very easy so just you know about some class name like text-center to make alignment in center position, text-right to make alignment in right position, text-left to make alignment in left position.
In your code you are using ul and li but for making responsive design to avoid ul and li . So you can use always work with only div.

Cannot align inline-block image and nav buttons

For some reason or another my navigation buttons are being pushed down by the image. I can solve the issue by floating the image but that does not interest me.
CSS:
.wrapped {
position: relative;
width: 70%;
margin: 0 auto;
}
.header {
width: 70%;
margin: 0 auto;
}
.headernav {
width: 70%;
margin: 0 auto;
height: 120px;
}
.logo, .logo img {
display: inline-block;
margin-right: 30px;
}
.nav {
padding-left: 30px;
display: inline-block;
height: 120px;
line-height: 120px;
vertical-align: middle;
}
.navbutton {
padding: 5px 30px;
display: inline-block;
}
#fitness, #wcontrol, #recipes, #supplementation {
text-align: center;
height: 36px;
line-height: 36px;
font-weight: bold;
}
HTML:
<div id="wrapperheader">
<div class="headernav">
<div class="logo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></div>
<div class="nav">
<div class="navbutton"><div id="recipes">Recipes</div></div>
<div class="navbutton"><div id="fitness">Fitness</div></div>
<div class="navbutton"><div id="wcontrol">Weight Control</div></div>
<div class="navbutton"><div id="supplementation">Supplementation</div></div>
</div>
</div>
</div>
If anyone knows why this is happening or of a simple solution, please let me know. Sorry if its an obvious answer.. I'm been staring at the code on and off for a day and it's just not clicking.
You could simplify your HTML & CSS to remove div soup, by moving the logo into the nav bar:
Usually the logo would be wrapped with an anchor tag, so that clicking on it will return the user to the home page. So it becomes a nav element.
HTML:
<div class="nav">
<div class="navlogo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></div>
<div class="navbutton"><span id="recipes">Recipes</span></div>
<div class="navbutton"><span id="fitness">Fitness</span></div>
<div class="navbutton"><span id="wcontrol">Weight Control</span></div>
<div class="navbutton"><span id="supplementation">Supplementation</span></div>
</div>
The HTML above uses spans, but you can also use <... class="navbutton" id="recipes">Recipes</...>, as shown below.
Optimized HTML: Generally people create UL LI tag stacks for nav bars.
<ul class="nav">
<li class="navlogo"><img src="http://protein.guru/images/PGlogo.png" alt="Protein.guru Logo" class="logo"></li>
<li class="navbutton" id="recipes">Recipes</li>
<li class="navbutton" id="fitness">Fitness</li>
<li class="navbutton" id="wcontrol">Weight Control</li>
<li class="navbutton" id="supplementation">Supplementation</li>
</ul>
Then cleaning up the CSS, will remove problems with applying duplicate right margins & paddings to both the .logo & .logo .img tags. In your example, there is also a left margin on the nav element, which was creating extra margin space between the logo & the 1st link.
In this CSS example below, I've also thrown in a vertical-align:middle; property, so that the nav links center vertically next to a larger image logo. See this jsfiddle.
CSS:
.nav {
height: 120px;
line-height: 120px;
margin: 0 auto;
vertical-align: middle;
width: 70%;
}
.nav li {
display: inline-block;
list-style-type: none; /* removes bullets */
}
.nav .logo,
.nav .navlogo,
.nav .navbutton {
display: inline-block;
}
.nav .logo {
vertical-align: middle;
}
.nav .navbutton {
font-weight: bold;
height: 36px;
line-height: 36px;
padding: 5px 0 0 30px;
text-align: center;
}
If you wants to keep width: 70% of your .wrapped container, you have to reduce padding of your nav menu and size of your image.
See this fiddle
(It works on large size of the window)
Otherelse, you can put a fixed width of the .wrapped container but still have to consider padding of your nav menu and width of the image to make it fits.
.navbutton {
padding: 5px 25px;
display: inline-block;
}

center text in top bar with logo on left

i've created a simple topbar with text centered. My question is then how can i place a text logo in the left side with the text still appearing in the center of the navigation bar and still being responsive?
css:
nav {
position: absolute;
top: 0;
width: 100%;
height: 40px;
background: #000;
opacity: 1.0;
text-align: center;
}
nav li {
display: inline-block;
padding: 10px 10px;
}
nav li a {
color: #757575;
text-transform: uppercase;
}
#media only screen and (max-width: 520px) {
nav li {
padding: 10px 4px;
}
nav li a {
font-size: 14px;
}
}
html:
<section id="screen1">
<p>Scroll down</p>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Team</li>
<li>Contact</li>
</ul>
</nav>
</section>
#user3646311, the CSS style that will accomplish having an image, text or any other element on the left while still centering the text is float: left;. When you float an element you are telling it to go to one side and not effect the other elements that it may be touching. It is also important that you set the parent element with position: relative; which will tell the floating child to not go outside the bounds of the parent. You can read a lot more about floats on css-tricks. I wrote a working example for you to play with at jsfiddle.net.
HTML
<nav>
<img src="http://doc.jsfiddle.net/_downloads/jsfiddle-logo.png" />
<span>Text Logo</span>
<ul>
<li>Home</li>
</ul>
</nav>
CSS
nav {
position: relative;
}
/* This is how to float an image */
nav img {
float: left;
max-height: 50px;
}
/* This is how to float text */
nav span {
color: #FFF;
float: left;
line-height: 3em; /* Use line-height to vertically position your text */
}