Center aligning the top menu categories via CSS - css

I needed to center align my menu categories, such that the menu categoreis should sit for a width of 1200px and there should be even gaps (i.e. margin-right) between the category items in the menu and last item should sit to the right more corner in the screen (i.e. margin-right:0). Please find below the sample code for the same.
https://jsfiddle.net/es0o324t/1/
HTML:
<div class="navigationbarcollectioncomponent">
<div id="nav_main">
<ul>
<li class="La parent">
Clothing
</li>
<li class="La parent">
Denim
</li>
<li class="La parent">
Accessories
<li class="La parent">
Online Exclusives
</li>
<li class="La parent">
Sale
</li>
<li class="La parent">
Hot offers
</li>
</ul>
</div>
</div>
CSS:
body {
margin:0;
padding:0;
}
ul{
margin-left:0;
}
ul,li{
list-style: none;
padding:0;
}
.navigationbarcollectioncomponent {
width: 1200px;
margin: 0 auto;
}
#nav_main li.La{
float: left;
}
#nav_main li.La > a {
color: #373737;
display: inline-block;
font-family: "Lato-Regular";
font-size: 16px;
line-height: 36px;
margin: 0;
padding: 0px 8px;
text-transform: uppercase;
}
I had achieved this via JavaScript and by reading the width of each of the categories in the menu and then calculating the gap b/w the categories based on the category count.
But the issue is i need to do this only via CSS not using JavaScript. bcoz the calculation via JS is taking some time.
Also the count of number of categories is dynamic. Hence i cannot hardcode the margin-right value for each of the categories (i.e. li tag).
Please let me know if there is a solution for the same.
PFA for the snapshot on expected result.
First category should be left most aligned in 1200px width and last category should be aligned to the right and the categories that are b/w first and last categories should be center aligned with even spacing

As you tagged this question with CSS3, why not use flex?
Make your container ul set to display flex with justify-content set to space-between.
ul {
width: 100%; padding: 8px;
display: flex;
justify-content: space-between;
}
Your Fiddle: https://jsfiddle.net/abhitalks/es0o324t/2/

To center you nav menu, please add this css too.
#nav_main{
margin: 0 auto;
display: table;
}

check this fiddle : jsfiddle.net/shiyaspathiyassery/6jqy6702/1/
You have added float: left; Please remove that.

Here's something close enough. Run the code snippet to see the result:
although the leftmost and rightmost menu are not sticking to the sides, respectively, since the <li> text-align is set to center in order to achieve even space between the menus.
body {
margin:0;
padding:0;
}
ul{
display: -webkit-box;
display: -moz-box;
display: box;
width: 100%;
}
ul,li{
list-style: none;
padding:0;
}
.navigationbarcollectioncomponent {
display: inline-block;
width: 1200px;
margin: 0 auto;
}
#nav-main{
display: inline-block;
width: 100%;
}
#nav_main li.La{
text-align: center;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
#nav_main li.La > a {
color: #373737;
display: inline-block;
font-family: "Lato-Regular";
font-size: 16px;
line-height: 36px;
margin: 0;
padding: 0px 8px;
text-transform: uppercase;
}
<div class="navigationbarcollectioncomponent">
<div id="nav_main">
<ul>
<li class="La parent">
Clothing
</li>
<li class="La parent">
Denim
</li>
<li class="La parent">
Accessories
<li class="La parent">
Online Exclusives
</li>
<li class="La parent">
Sale
</li>
<li class="La parent">
Hot offers
</li>
</ul>
</div>
</div>

table-layout: fixed can be used to achieve what you are looking for.
In table-layout: fixed the horizontal layout only depends on the table's width and the width of the columns irrespective of the contents of the cells.
So your table width will be equally divided for each columns.
Add/modify these css properties for ul and #nav_main li.La
ul{
margin-left:0;
display: table;
table-layout: fixed;
width: 100%;
text-align: center;
}
#nav_main li.La{
display:table-cell;
}
https://jsfiddle.net/f03mqxd7/

Related

I am trying to center align the menu

I am a newbie in CSS. I am learning by playing with the code of a WordPress theme.
Last time I posted this issue but I contracted covid and was hospitalized for more than two weeks. I have deleted the old question and broken the initial question into two parts. This is the first part.
To remove the Site-Name on the left side. I used display: none.
<div id="header-left">
<div id="hgroup" class="logo-disable">
<h1 id="site-title">
Catch Kathmandu
</h1>
<h2 id="site-description"> Catch Kathmandu Theme is a fully responsive WordPress theme that looks elegant on any devices.</h2>
</div><!-- #hgroup -->
</div>
#header-left {
-webkit-text-size-adjust: 100%;
color: #404040;
line-height: 1.8;
text-rendering: optimizeLegibility;
word-wrap: break-word;
border: 0;
font-family: inherit;
font-size: 100%;
font-style: inherit;
font-weight: inherit;
margin: 0;
outline: 0;
padding: 0;
vertical-align: baseline;
float: left;
max-width: 100%;
display: none;
}
To center-align the menu on the right side.
<div id="header-right" class="header-sidebar widget-area">
<aside class="widget widget_nav_menu">
<div id="primary-menu-wrapper" class="menu-wrapper">
<div class="menu-toggle-wrapper">
<button id="menu-toggle" class="menu-toggle" aria-controls="main-menu" aria-expanded="false"><span class="menu-label">Menu</span></button>
</div><!-- .menu-toggle-wrapper -->
<div class="menu-inside-wrapper">
<nav id="site-navigation" class="main-navigation" role="navigation" aria-label="Primary Menu" aria-expanded="false">
<ul id="menu-primary-items" class="menu nav-menu"><li class="current-menu-item">Home</li><li class="page_item page-item-2">About</li><li class="page_item page-item-182">Home</li><li class="page_item page-item-46 page_item_has_children" aria-haspopup="true">Parent Page<button class="dropdown-toggle" aria-expanded="false"><span class="screen-reader-text">expand child menu</span></button><ul class="children"><li class="page_item page-item-49">Sub-page</li></ul></li></ul> </nav><!-- .main-navigation -->
</div>
#header-right {
-webkit-text-size-adjust: 100%;
color: #404040;
line-height: 1.8;
text-rendering: optimizeLegibility;
word-wrap: break-word;
border: 0;
font-family: inherit;
font-size: 100%;
font-style: inherit;
font-weight: inherit;
margin: 0;
outline: 0;
padding: 0;
vertical-align: baseline;
padding-top: 50px;
float: right;
margin-right: 33%;
}
I reached my center alignment by reducing the margin-right from 50% then to 33%. Though the sweet spot is between 32% to 33%.
I also tried center-align the menu with pixels(margin-right :400px).
The difference between both methods is visible when I reduce the screen size. 33% mostly remains center-align but the px changes the position of the menu, as the screen changes.
Is there any other way I could center the menu?
There are many ways to center items depending on their block type.
You can use display flex on the parent and then add a justify-content: center to center the children items on the page horizontally, see flex axis - in basic concepts of flex for more info.
Then you can use flex property on the children and can add a number to that property to tell CSS how much of the parents width that child will take up.
For example if I set both to child elements to flex: 1 they will take up 50%, if there were three child elements all with a flex: 1, they would then take up 33% of their parents width. I can also place a percentage on the flex property as well => .header set flex: 60% and .nav-parent set flex: 40%, they will then take up their set percentages of the parents width.
Furthermore I can add display: flex properties to the child elements as well to further center their contents, as I have done on the .menu class which is a child of the .nav-parent. These can then be further align and centered using the axis centering properties for flex, justify-content and align-items. The good thing is that these properties are dynamic in that they scale to fit browser sizes as well.
.nav-section {
display: flex;
justify-content: center;
font-family: sans-serif;
align-items: center;
padding: 1rem;
}
.header h1 {
line-height: 1px;
}
.header {
flex: 60%;
}
.nav-parent {
flex: 40%;
}
.header span {
font-size: .8rem;
font-weight: 100;
font-style: italic;
}
ul li {
list-style: none;
}
ul li ~ li {
margin-left: .5rem;
}
.btn {
flex: 1;
padding: .5rem 0;
text-align: center;
}
.menu {
display: flex;
justify-content: center;
color: #555;
font-size: .8rem;
}
.active {
background: skyblue;
}
<div class="nav-section">
<div class="header">
<h1>Catch Kathmandu</h1>
<span>Catch Kathmandu Theme is a fully responsive WordPress theme that looks elegant on any device.</span>
</div>
<div class="nav-parent">
<ul class="menu">
<li class="home active btn">Home</li>
<li class="about btn">About</li>
<li class="contact btn">Contact</li>
<li class="parent btn">Page</li>
</ul>
</div>
</div>
Is there any other way I could center the menu?
There are a good handful of ways you can center the menu.
If your menu is a block-level element with an explicitly declared width, one of the most basic techniques is applying the keyword auto to margin-right and margin-left:
.my-menu {
margin-top: 12px; // <= Just an example, you can use any value
margin-right: auto;
margin-bottom: 12px; // <= Just an example, you can use any value
margin-left: auto;
}
the CSS shorthand for these four declarations is:
.my-menu {
margin: 12px auto 12px auto;
}
or, since the vertical and horizontal values are the same, you can declare:
.my-menu {
margin: 12px auto;
}
Working Example:
.my-menu {
display: block;
margin: 12px auto;
}
.my-menu.vertical {
width: 100px;
}
.my-menu.horizontal {
width: 396px;
padding: 0;
}
.my-menu.horizontal li {
display: inline-block;
padding: 0 6px;
}
.my-menu.horizontal li::before {
content: '•';
display: inline-block;
padding-right: 6px;
font-size: 20px;
}
<ul class="my-menu vertical">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
</ul>
<ul class="my-menu horizontal">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
</ul>

How to avoid expanding the parent flex element on hover

I have simple nav bar list and i need change item font weight on hover. But when I hover on item its width changes so other nav items position changes too. How to fix this issue?
Exampe:
.container {
margin: 0 auto;
max-width: 500px;
}
.nav {
display: flex;
justify-content: space-between;
list-style: none;
text-transform: uppercase;
}
.nav__item {
font-weight: 100;
cursor: pointer;
}
.nav__item:hover {
font-weight: bold;
}
<html>
<div class="container">
<ul class="nav">
<li class="nav__item">Home</li>
<li class="nav__item">About</li>
<li class="nav__item">Contacts</li>
<li class="nav__item">Donate</li>
<li class="nav__item">FAQ</li>
</ul>
</div>
</html>
An easy way is to set a vertical or horizontal text-shadow to mimic bold letters, transform with skew() or scale(), ... could also help making effects witouth having every thing jump .
example with an horizontal text-shadow
.container {
margin: 0 auto;
max-width: 500px;
}
.nav {
display: flex;
justify-content: space-between;
align-items:center;
list-style: none;
text-transform: uppercase;
}
.nav__item {
font-weight: 100;
cursor: pointer;
}
.nav__item:hover {
text-shadow: 1px 0;
}
<html>
<div class="container">
<ul class="nav">
<li class="nav__item">Home</li>
<li class="nav__item">About</li>
<li class="nav__item">Contacts</li>
<li class="nav__item">Donate</li>
<li class="nav__item">FAQ</li>
</ul>
</div>
</html>
This should help you out:
Inline elements shifting when made bold on hover
Although flex isnt used in this example, the top voted solution should prove to be a working work around as the li elements's width won't react to the change in width caused by the font weight change.

CSS - problem with show a sub item under a <li>

I try to make a list menu with a submenu using JSX(React) and css Pure (Not jquery!). My intention is show "box-blue" under the <li> "Technologies" but I don't know how to make the div "box-blue" stay visible when I move out from the <li> "Techonologies" element. (obviously always inside the <div> "box-blue")
<ul className="display-menu">
<li className="display-menu-item"><NavLink className="link-categories" exact to="/">Technologies</NavLink>
<div className="box-blue"></div>
</li>
<li className="display-menu-item"><NavLink className="link-categories" exact to="/">Furniture</NavLink></li>
<li className="display-menu-item"><NavLink className="link-categories" to="/">Entertainment</NavLink></li>
</ul>
CSS code
.display-menu-item{
margin-left: 1rem;
position:relative;
}
.link-categories{
text-decoration: none;
color:white;
font-size: 0.85rem;
width: 40%;
text-align: center;
white-space: nowrap;
width:10rem;
}
.box-blue{
height:25rem;
width: 80rem;
background-color: blue;
position: absolute;
bottom: -25.5rem; /* the box appears under the ul */
z-index: 10;
display: none;
right: -45.5rem; /* the box appears at center */
}
.link-categories:hover ~ .box-blue{
display:block;
}

Center-align horizontal inline-block list of page numbers

I am trying to center-align a block of page numbers at the bottom of this page. The HMTL and CSS looks like this:
.pt-cv-pagination-wrapper {
position: relative;
display: block;
text-align: center;
margin: 20px 0;
padding: 0;
}
.pt-cv-pagination {
position: static;
display: inline-block;
text-align: center;
margin: 20px 0;
padding: 0;
}
li {
position: relative;
display: inline;
text-align: center;
margin: 0;
padding: 0;
}
.pt-cv-pagination a {
position: relative;
display: block;
float: left;
padding: 6px 12px;
margin: 1em;
}
<div class="pt-cv-pagination-wrapper">
<ul class="pt-cv-pagination pt-cv-ajax pagination" data-totalpages="3" data-currentpage="1" data-sid="98f4b5c3fg" data-unid="">
<li class="cv-pageitem-prev">
<a title="Go to previous page">‹</a>
</li>
<li class="cv-pageitem-number">
<a title="Go to page 1">1</a>
</li>
<li class="cv-pageitem-number">
<a title="Go to page 2">2</a>
</li>
<li class="cv-pageitem-number active">
<a title="Current page is 3">3</a>
</li>
<li class="cv-pageitem-next active">
<a title="Go to next page">›</a>
</li>
</ul>
<div class="clear pt-cv-clear-pagination"></div>
</div>
If you look on a narrow screen (on the live site), it will be easier to see that the page numbers are very slightly off centre. I've read several articles on here that all make sense but seem to have no further effect on my outcome. It's driving me crazy trying figure out why it's not working. Any help appreciated!
Ah, the art of centering elements in CSS. Good thing we have flexbox to help us all out.
This should do the trick:
.pt-cv-pagination-wrapper .pt-cv-pagination.pagination {
display: flex;
justify-content: center;
}
The centering looks fine. If you want to have a border on the <li> then change the inline of the li in an inline-block.
.pt-cv-pagination li {
position: relative;
display: inline-block;
text-align: center;
margin: 0;
padding: 0;
}
Remove the float from the <a> to have the block on this anchor working the correct way.
.pt-cv-pagination a {
position: relative;
display: block;
padding: 6px 12px;
margin: 1em;
}

How to left align all LI links, but right adjust pictures within the same LI link?

I've got the following code that is being used in a Drop-down menu:
.menu-label {
font-size: 0;
}
.menu-label.coming-soon:before {
background: url(lb-coming-soon1.png);
width: 40px;
height: 20px;
margin-top: -10px;
}
<ul>
<li>Soap Company<span class="menu-label coming-soon"></span></li>
<li>Apparel <span class="menu-label coming-soon"></span></li>
</ul>
What I'm trying to do is have the Menu display the text link left align, and the related "Coming Soon" picture right aligned.
right now, it looks a disjointed. I'm not sure how/what to change in the css to make the alignment work as desire.
You can use flexbox and justify-content to separate the elements.
li { display: flex; justify-content: space-between; }
And looks like you have an HTML discrepancy between the list items. Change the 2nd one to
<li>Apparel <span class="menu-label coming-soon"></span></li>
there is a few method according to which structure and which browsers you want to support.
examples :
/* reset and basic makup */
ul {
padding:2em;
}
li {
background:lightgray;
display: block;
overflow: hidden;
margin: 3px;
}
a {
display:inline-block;
text-decoration:none;
text-align:left;
color:green
}
.menu-label.coming-soon:before {/* include image*/
display: inline-block;
content: url(http://dummyimage.com/40x20/abc/f00&text=soon);
width: 40px;
height: 20px;
vertical-align: middle;
}
/* float first element and align right text */
li.float {
text-align: right;
}
a.float {
float: left;
}
/* end float */
/*=== */
/* flex & justify-content */
.flex {
display: flex;
justify-content: space-between;
}
/* end flex */
/*=== */
/* text-align */
.justify, .justifyli{
display: block;
text-align: justify;
line-height:0em;
}
[class^="justify"]:after {
content: '';
display: inline-block;
vertical-align: top;
width: 99%;
}
/* end text-align */
<ul>
<li class="float">
float a
<span class="menu-label coming-soon"></span>
</li>
<li class="flex">
<a href="#" >flex li</a>
<span class="menu-label coming-soon"></span>
</li>
<li>
<a href="#" class="flex">flex a
<span class="menu-label coming-soon"></span>
</a>
</li>
<li>
<a href="#" title="buggy, text needs to be wrapped" class="justify">justify a
<span class="menu-label coming-soon"></span>
</a>
</li>
<li class="justifyli">
justify li
<span class="menu-label coming-soon"></span>
</li>
</ul>

Resources