How to fully center a nav bar using flexbox - css

Still quite a newbie and would appreciate some guidance as this keeps happening on my projects.
I create a nav bar using display: flex - I think I have center aligned everything, but it's moving off to the right.
I was expecting the nav to be center aligned because of the following properties.
.navlist {
display: flex;
justify-content: center;
}
.navlist li {
padding: 20px;
list-style-type: none;
text-align: center;
}
But I get slightly off centre result
Please check my codepen
I've also tried margin: auto but still can't figure it out!
Thanks in advance

Add the following code in your CSS and it should work fine.
.navlist li:first-child {padding-left: 0;}
The reason your nav is a little off-center is that it is adding left padding of 20px based on your code so the nav shifts 20px towards the right.
Hope this answer is helpful!

Added a slight change to your code, see if it is what you're expecting
.navlist {
display: flex;
justify-content: center;
text-align: center;
padding-left: 0;
}

Related

Group of centered images, last row issue

I am centering a group of images using:
DIV Containter: text-align: center;
DIV Image: display: inline-block. (No Float)
It works perfectly, but the very last row which is 1 or 2 pixel shifted on the right, not matter how many rows there are (different resolution).
I can't make it work. See pages:
https://www.trampolineandparts.co.uk
https://www.trampolineandparts.co.uk/replacement-parts
Please help. Thank you!
As #Mary pointed out, it's the space between elements. You can just use display: flex on the parent instead, and that will maintain the layout and fix the spacing issue.
div.imgcnt {
/* text-align: center; */ /* no longer need this */
display: flex;
flex-wrap: wrap;
justify-content: center; /* if you want the `.img` elements centered on the page */
}
Try not leaving any space between your <div> and it should fix your issue.
Hi i took look at the website and noticed the 1 / 2 px displacement. It comes because of the display:inline-block. I tried fixing it. I would use float:left on the images rather than using inline-block. Try changing your css files to this.
div.imgcnt {
text-align: center;
padding-left: 6%;
}
div.img {
margin-left: 5px;
margin-right: 5px;
margin-bottom: 10px;
width: 160px;
border: rgba(0, 0, 0, 0.17);
border-style: solid;
border-width: 1px;
float: left;
}
You have whitespace between your inline-block elements for sure. Check if this will kill the unwanted space:
<div class="inline-block"></div><div class="inline-block"></div><div class="inline-block"></div>

Wordpress: CSS issues with centering a horizontal menu and mysterious paddings around menu

I am creating a website with Wordpress for my mother-in-law (that's for the girly design). Basically I am near completion, but I am way over my head with two issues in the CSS. These seem very basic issues even from my standpoint of view, but with hours and hours of tinkering I am fresh out of ideas and Google didn't help me this time. It's been a while since I have had to create or modify any CSS.
First problem:
I cannot get the horizontal menu to center. I have tried to remove the float:left, I have tried to replace it with float:none and I have changed the display: block and display:inline lines to inline-blocks but the menu stays in its position. Only difference I have managed to make is to move the whole menu to the top of the page and that's not desired. What could be the issue in this?
Second problem:
There seems to be padding (the white lines) at the top of the menu and at the bottom and top of the small menu (mobile). I have tracked down all the padding-lines in the CSS but none of them really make a difference, only one which removed the horizontal paddings and that's not desired.
I would be really glad if somebody spots where I have gone wrong!
The website is at http://janenlahwr.cluster020.hosting.ovh.net/
Thanks in advance!
Best regards,
Tero Korhonen
Lappeenranta, Finland
Hi Tero,
the first problem has really quick solution - CSS3 Flexbox. You can read about it on this w3schools site.
Remove unnecessary float: left and add display: flex and justify-content: center to .main-navigation ul. So it should looks like this:
.main-navigation ul {
margin: 0px 0 0 0;
padding: 0px 0;
padding-left: 0px;
list-style-type: none;
display: flex;
justify-content: center;
}
Second problem you can fix with setting div.site-logo max-height = height your logo (I see that is 200px). So in this case should looks like this:
.site-logo {
min-height: 70px;
padding: 0px;
float: left;
line-height: normal;
max-height: 200px;
}
Edit:
Sorry, I forgott check for lower resolutions. There is problem with overflow. You set in style.css:640 overflow: hidden and it works correctly since resolution is above 800px. If not then activates this rule:
#media only screen and (max-width: 800px) {
#main {
overflow: visible;
}
}
that overwrites previous correct rule for #main overflow: hidden. So you have two options: delete this rule for screens over 800px or change this property from visible to hidden.
I hope I helped you with your problems :) Good luck!
For the menu problem, change your CSS rules like this:
.main-navigation ul {
margin: 0px 0 0 0;
padding: 0px 0;
padding-left: 0px;
list-style-type: none;
/* remove float: left; */
text-align: center; /* added */
width: 100%; /* added */
}
.main-navigation ul li {
position: relative;
display: inline-block;
/* remove float: left; */
text-align: center; /* not necessary */
}

Center a CSS Menu/Drop Down Menu

I am using this template on my website: http://www.css3templates.co.uk/templates/CSS3_gallery_grey/index.html
jsfiddle: http://jsfiddle.net/uPw85/
What I need help with:
1) Center the menu on the page, as you can see on the link it's left aligned.
2) Still keep the text in the drop down menus aligned to the left (just as in the link above).
I've searched this page and on Google a lot before asking for help and none of the results have worked for me. I've tried just about every tip I've found but the menu still won't be centered for me, the only thing that happens is that the text in the drop down menu is centered but I want to keep it to the left.
I've tried with multiple variations of these in different places (nav, menu, li, ul) in the CSS but with no luck:
display: inline-block;
margin-left: auto;
margin-right: auto;,
margin: 0 auto;
text-align: center;
width: auto;
EDIT 2/10, 1 PM EST: I appreciate you guys trying to help but so far none of the answers you've given has helped.
Try this:
ul.sf-menu {
text-align: center;
}
ul.sf-menu li {
display: inline-block;
}
ul.sf-menu li a {
display: block;
}
ul.sf-menu ul {
text-align: left;
}
EDIT:
I've edited the JSfiddle: http://jsfiddle.net/uPw85/3/
EDIT 2:
Remove float: left in ul#nav, or just remove the id="nav" in the ul-tag in the html
In order to center the menu you need to set the width of the child div to be smaller than a set width of a parent. Additionally need to remove the float of the menu as floating an element effectively takes it out of the parent div.
Try setting the following:
ul.sf-menu {
float: none;
width: 760px;
margin: 0 auto;
}
Works when inspecting element. Another alternative would be to try the following code.
ul.sf-menu {
float: none;
display: table;
margin: 0 auto;
}
Just add:
ul#nav {
float:left;
padding-left: 80px;
}

Strange Disapearing elements in small browsers when hover

Hi There when I make my browser smaller than 420px (Using Chrome Stable) the Navigation elements in the first navigation line disapear, when I hover them. I cannot find the mistake.
Can anybody help? Website is: http://www.tokemedia.de
go to style.css and change display:inline to inline-block;
#navi ul li {
float: left;
margin-left: 10px;
list-style: none;
text-align: center;
display: inline-block;/* not inline*/
}

position:absolute one of display:table-cell elements causes WebKit to render gap

I'm trying to build a horizontal menu with the last item seperated and positioned right, so that a logo finds place between the last and the second last item.
Firefox, Opera (Presto) and even the dirty ones from Redmond (9.0+) render it like I would expect. But WebKit (Chrome and Safari both render it the same) takes some space after the second last item where the last item would stay without position: absolute.
<header>Logo</header>
<nav>
<ul>
<li>Home</li>
<li>Statistics</li>
<li>Data Management</li>
<li>Market Research</li>
<li>Web & Apps</li>
<li>Contact</li>
</ul>
</nav>
I display the list as table and the list items as table-cell because I want the left part of the menu (first to second last item, left to the logo) to have a fixed width while the items take the width they need for their contents. Text could change to anything. Till there, everything is alright. But if I give the last item a display: block; position: absolute, WebKit renders that gap (white in the example).
nav ul
{
display: table;
background: white; /* that's what you see in webkit */
}
nav ul li
{
display: table-cell;
}
nav ul li:last-child
{
display: block; /* "display: none;" works like I would expect */
position: absolute;
}
Here is a Fiddle.
I'm not sure if it is a bug in WebKit, because absolute positioning an element inside a table might not be default behavior. On the other hand, display: none works like I would expect. Shouldn't the space consumption be 0 in both cases?
Does anybody know of a bug in WebKit or has anyone an idea of how to prevent that gap?
Set the penultimate element "Web & Apps" to display block instead of table-cell:
nav ul li:nth-last-child(2)
{
display: block;
}
Display block fiddle
I've pushed this answer on other people, and often got the "flexbox isn't widely supported yet" response. However, here it goes again. The reason that Webkit is misbehaving is that within its implementation of display: table, the DOM is reserving space for the semantically declared cell. The easy way to implement this would be to simply break this element out into its own object, much like you'd done with the logo.
HOWEVER...
if you want to keep these semantically grouped - And why wouldn't you, they're all content, right? - you can always use the flexbox model to overcome this.
Here's a fiddle showing its use.
Here's what we change:
nav ul
{
display: box;
display: -webkit-flex;
flex-direction: row;
-webkit-justify-content: center;
-webkit-align-items: center;
-webkit-flex-direction: row;
background: white; /* that's what you see in webkit */
empty-cells: hide;
table-layout: auto;
}
nav ul li
{
flex: 1 1 auto;
-webkit-flex: 1 1 auto;
}
Now, your items properly cover the background, as it is no longer treating the last li as if it were a true table cell and still within the bounds of the table. Flexbox provides a flexible layout to fill available space. Often people see this as a solution for the "Holy Grail of Layout" problem, but its use expands way beyond just that.
So, if another "Flexbox isn't widely supported enough" response is incoming, I understand. But I'll keep proposing it as an answer on every one of these that I come across, because support is getting better every day.
A little CSS edited, take a look at please, if this what you want, Fiddle
body
{
position: relative;
background: #bbbbbb;
text-align: center;
color: white;
}
header
{
position: absolute;
top: 10px;
left: 630px;
width: 80px;
height: 60px;
line-height: 60px;
background: black;
text-transform: uppercase;
}
nav
{
position: absolute;
top: 20px;
left: 20px;
}
nav ul,
nav li
{
list-style-type: none;
margin: 0;
padding: 0;
}
nav ul
{
width: 200px;
}
nav ul li a
{
display: block;
padding: 1px;
background: #0099ff;
color: white;
text-decoration: none;
}
nav ul li a:hover
{
background: black;
}
nav li{
margin-bottom: 5px;
height: 25px;
line-height:25px;
}
While using absolute, every thing related/relating to that must be absolute in term of pixels.
I have updated fiddle, as your nav + logo .header + .nav:last-child were not totaling proper.
Fiddle link : http://jsfiddle.net/3TUk8/2/
in other case you will have to do that
nav ul
{
display: table;
background: #bbbbbb; /* that's what you see in webkit and same as bg color will hide it :) */
list-style: none;
}
I hope this solve your problem :)

Resources