How to make html menu text span 100% from end to end - html

I am trying to create a navigation menu with a total of 5 link to other pages.
I cannot figure out how to make the text span from one end to the other, so it takes the whole width of the page, and in the same time is flexible.
The structure is very simple:
ul {
display: flex;
justify-content: center;
width: 100%;
}
ul li {
display: inline-block;
text-align: center;
flex: 1;
justify-content: space-between;
}
ul li a {
display: inline-block;
width: 100%;
font-size: 16px;
font-weight: 300;
color: #070707;
}
<ul>
<li>Contact us</li>
<li>Delivery</li>
<li>About us</li>
<li>Terms & Conditions</li>
<li>Returns</li>
</ul>
This works, but as the text is centered inside every li element, there is some space on the left and on the right. I am trying to make the text "touch" the edges of the ul element (which takes 100% width of the parent element). So if the ul element has a width of 1240px, I am trying to make the text take up 1240px, from end to end.
This is what it looks like when I did the page mockup in photoshop:
the blue lines are the edges (with one indicating the middle).
When I use the flexbox code I wrote, it appears like this:
Is there a way to make this appear like I originally wanted it?

Remove flex: 1 from the li. You do not want the elements to grow as this will prevent the text from reaching the extremities.
Add justify-content: space-between; to the ul. You are currently centering the elements which will cause them to bunch together.
You also need to remove the default ul padding, but presumabaly you are doing this already.
body {
outline: 1px dashed lightBlue;
}
ul {
display: flex;
justify-content: space-between;
width: 100%;
padding: 0;
}
ul li {
display: inline-block;
outline: 1px solid orange;
}
ul li a {
display: inline-block;
width: 100%;
font-size: 16px;
font-weight: 300;
color: #070707;
}
<ul>
<li>Contact us</li>
<li>Delivery</li>
<li>About us</li>
<li>Terms & Conditions</li>
<li>Returns</li>
</ul>

Keep your flexbox code and use justify-content: space-between;, so your items will be like this:
Just don't forget to make sure your flexbox direction is set to "row".
Just an extra tip: Since you are doing a navigation bar, don't forget to use the <nav> tag for better html semantics and accessibility.
http://html5doctor.com/nav-element/

The CSS in this codesandbox returns what you seem to want (it uses React, but for the CSS : https://codesandbox.io/s/wandering-sound-1cme5
The key points I see :
setting body's margin to 0. As a general rule, to ensure consistent styling across browsers, you might want to use a normaliser tool like this: https://necolas.github.io/normalize.css/, or a complete reset like this : https://meyerweb.com/eric/tools/css/reset/ to take care of that kind of baseline styles.
removing the text-align: center. They are not needed if you use justify-content: space-between
only use justify-content: space-between on the flex wrapper (ul)
reset the default padding of ul by setting padding-left: 0
The styles on the li are mostly unnecessary, afaik, and only list-style: none; should be needed for your purpose.

List elements have indentation to make room for markers.
ul {
display: flex;
}
ul li {
text-align: center;
flex: 1;
}
ul li a {
font-size: 16px;
font-weight: 300;
color: #070707;
}
<ul>
<li>Contact us</li>
<li>Delivery</li>
<li>About us</li>
<li>Terms & Conditions</li>
<li>Returns</li>
</ul>
Remove the default space on the list elements. (Also, a lot of your code isn't necessary.)
ul {
display: flex;
/* new */
list-style: none;
margin: 0;
padding: 0;
}
ul li {
flex: 1;
/* for demo */
text-align: center;
display: flex;
justify-content: center;
align-items: center;
border: 1px dashed black;
}
ul li a {
font-size: 16px;
font-weight: 300;
color: #070707;
}
<ul>
<li>Contact us</li>
<li>Delivery</li>
<li>About us</li>
<li>Terms & Conditions</li>
<li>Returns</li>
</ul>
Even better, clean up the HTML, as well. Get rid of the list elements and use the simpler and semantically valuable nav element.
nav {
display: flex;
}
a {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
font-size: 16px;
color: #070707;
border: 1px dashed black;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
}
<nav>
Contact us
Delivery
About us
Terms & Conditions
Returns
</nav>

Related

the clickable area of an anchor become bigger after adding margins on it

In order to maker distance between title and list items, I set an <a> with margin: 8px 0;. The weird thing is that the clickable area of the anchor expands just like I set paddings on the element however I did not. What is the situation?
ul.footer-nav {
margin: 0 12px;
}
ul.footer-nav > a {
display: block;
font-size: 15px;
font-weight: 600;
color: #000;
margin: 8px 0;
}
<ul class="footer-nav">
HELP & INFORMATION
<li>Help</li>
<li>Track Order</li>
<li>Delivery& Returns</li>
</ul>
EDITED----------
If I set both margin and padding on the element, the clickable area and the distance work correctly.
Your a element is set to display:block which expands its clickable are, so it expands to the whole width. I'm guessing you did that because a is display:inline by default, and vertical margin doesn't work for inline elements.
You can use display: inline-block instead, which will still display the margin setting. Here's an example. I've removed the ul entirely, and gave the a element background color, so we ca see what's happening:
a {
background-color:#fe5;
display: inline-block;
margin: 8px 0;
}
HELP & INFORMATION
Compare to the original with display:block;
a {
background-color:#fe5;
display: block;
margin: 8px 0;
}
HELP & INFORMATION
Note that as the comments warn, you should not have an a element directly in a ul list - that isn't valid and may produce unwanted results.
One way, though it might be easiest to just move the link out of the ul.
ul.footer-nav {
margin: 0 12px;
}
ul.footer-nav > a {
display: block;
font-size: 15px;
font-weight: 600;
color: #000;
margin: 8px 0;
}
ul.footer-nav li:first-child {
list-style: none;
}
ul.footer-nav li:first-child a {
text-decoration: none !important;
}
<ul class="footer-nav">
<li>HELP & INFORMATION</li>
<li>Help</li>
<li>Track Order</li>
<li>Delivery& Returns</li>
</ul>
Ideally ul should only have the child elements as li.
If one wish to make headings, then there are other tags like h1, h2, etc.
So, you should modify your code as below:
ul.footer-nav {
margin: 0 12px;
}
h3 {
text-align: center;
}
h3 > a {
color: black;
}
<h3>
HELP & INFORMATION
</h3>
<ul class="footer-nav">
<li>Help</li>
<li>Track Order</li>
<li>Delivery& Returns</li>
</ul>

Creating a CSS border around custom li bullet

I'm trying to figure out if it's possible to incorporate a css border around an image I imported as a custom bullet for my li's:
ul {
list-style: none;
margin-right: 0;
padding-left: 0;
list-style-position: inside;
}
ul > li {
align-content: center;
display: flex;
margin: 5px 0;
padding-left: 1em;
text-indent: -1em;
}
ul li:before {
/* I'm a different image but found a similar
sized one online for demonstration
purposes seen below */
content: url("https://www.dicentral.com/css/assets/imgs/Flag_Nation_france.png");
border: 1px solid grey;
}
<ul>
<li>Get to know the business</li>
<li>Get to know people (stakeholders, key players, cross-functional partners, etc.)</li>
<li>Learn how the team's priorities impact our mission</li>
<li>Get to know your projects, the team's projects, who's involved, and your onboarding goals</li>
</ul>
The results in the embedded code editor mirror that of the image I'm using.
This is the desired output:
Any ideas? I'm thinking unfortunately I might have to import the icon with border, but am seeing if I can manage without.
Thanks!
Yes, it is quite easy to do, please take a look at the example below. You just mess up things a bit.
You have align-content instead of align-items that makes line positioning incorrect. text-indent results into incorrect offset. I've removed these small issues.
About image itself - I've used em as example because of emoji, but for image it will be better to use px and re-calculate values that are currently defined as em.
ul {
margin-right: 0;
padding-left: 0;
list-style: none;
}
ul > li {
align-items: center;
display: flex;
margin: 5px 0;
}
ul li:before {
/* I'm using the url method to fetch an icon, but
inserted a emoji for demonstration
purposes seen below */
/*content: url("path/to/icon");*/
content: '🚩';
border: 1px solid #808080;
border-radius: 100%;
width: 1em;
height: 1em;
display: inline-block;
padding: 0.25em;
line-height: 1.0;
margin-right: 0.5em;
}
<ul>
<li>Get to know the business</li>
<li>Get to know people (stakeholders, key players, cross-functional partners, etc.)</li>
<li>Learn how the team's priorities impact our mission</li>
<li>Get to know your projects, the team's projects, who's involved, and your onboarding goals</li>
</ul>
there are many ways to achieve result.
1) use image with rounded border as background on "li". background should be no-repeat left center and some padding-left on li.
2) give height, width, inline-block and border-radius to li:before.
You should remove "display:flex" from ul > li
ul {
list-style: none;
margin-right: 0;
padding-left: 0;
}
ul > li {
align-items: center;
display: flex;
margin: 5px 0;
}
ul li:before {
/* I'm using the url method to fetch an icon, but
inserted a emoji for demonstration
purposes seen below */
content: '🚩';
border: 1px solid #808080;
border-radius: 100%;
width: 1em;
height: 1em;
display: inline-block;
padding: 0.25em;
line-height: 1.0;
margin-right: 0.5em;
}

Equal padding around horizontal list of flex items

How to set equal padding between elements set with display: flex and justified-content?
ul {
background-color: #ddd;
list-style-type: none;
padding: 0;
display: flex;
justify-content: space-around;
}
li.active a {
background-color: #111;
color: #fff;
}
<ul>
<li>Apples</li>
<li class="active">Bananas</li>
<li>Coconut</li>
<li>Apples</li>
<li>Kale</li>
<li>Coconut</li>
<li>Kale</li>
<li>Kale</li>
<li>Coconut</li>
<li>Kale</li>
</ul>
Bootply example
It is about background-color of active link. I would like to have something like in this image:
STEP 1
Allow for an equal distribution of free space among all list items and center the text (as in the image).
Add this to your CSS:
li { flex: 1; text-align: center; }
STEP 2
Enable the anchor element (a) to extend the full width of its container (so the entire li is clickable).
Add this to your CSS:
li a { display: block; }
Revised Demo

Inline Block Elements Overflowing Parent Container

I have a list of 4 menu items sitting side by side using display:inline-block;. Each item is 120px, therefore I should be able to set the parent container to be 480px wide, however this sends the last item into the next row, why is this ??
Here is a jsfiddle :http://jsfiddle.net/htdgdhxn/
My html:
<section id="nav">
<div id="nav-wrapper">
<ul id="nav-list">
<li id="nav-home">Home
</li>
<li id="nav-clothes"><a class="category All">Clothes</a>
</li>
<li id="nav-about">About Us
</li>
<li id="nav-contact">Contact Us
</li>
</ul>
</div>
</section>
CSS:
* { margin: 0px; padding: 0px; }
#nav { background-color: #fff; }
#nav-wrapper { text-align: center; height: 74px; }
#nav-list { height: 100%; width: 480px; }
#nav-list li { display: inline-block; vertical-align: top; width: 120px; height: 100%; }
#nav-list li a { text-decoration: none; color: #000; font-size: 1.6em; display: block; height: 100%; line-height: 74px; }
#nav-list li a:hover { background-color: #F0ECE1; cursor: pointer; }
I have tested and this happens in Chrome, IE and Firefox.
Remove the whitespace between each <li>
<li></li> <...space here...> <li></li>
Inline block elements create a gap between li elements.
<ul id="nav-list">
<li id="nav-home">Home
</li><li id="nav-clothes"><a class="category All">Clothes</a>
</li><li id="nav-about">About Us
</li><li id="nav-contact">Contact Us
</li>
</ul>
See fiddle
The inline-block value is incredibly useful when wanting to control margin and padding on
"inline" elements without the need to block and float them.One problem that arrises
when you use inline-block is that whitespace in HTML becomes visual space on screen.
Gross.There are a few ways to remove that space; some of them are just as gross, one is
reasonably nicer.
Solution 0: No Space Between Elements:
The only 100% solution to this issue is to not put whitespace between those elements in the HTML source code:
<ul><li>Item content</li><li>Item content</li><li>Item content</li></ul>
Solution 1: font-size: 0 on Parent
The best white-space solution is to set a font-size of 0 on the parent to the inline block
elements.
.inline-block-list { /* ul or ol with this class */
font-size: 0;
}
.inline-block-list li {
font-size: 14px; /* put the font-size back */
}
Solution 2: HTML Comments
This solution is a bit gangsta but also works. Using HTML comments as spacers between the elements works just as placing no space between elements would:
<ul>
<li>Item content</li><!--
--><li>Item content</li><!--
--><li>Item content</li>
</ul>
It might help you.
Just increase the width of your container to 500px
#nav-list { height: 100%; width: 500px; }
or remove the white spaces between consecutive li tags
or
apply display:initial in #nav-list { height: 100%; width: 480px;}
i.e #nav-list { height: 100%; width: 480px; display: initial;}
Reason:-
1. The font size of the text in the li element might be causing the problem.
You can modify it by reducing the font-size.
#nav-list li a { text-decoration: none; color: #000; font-size: 1.2em; display: block; height: 100%;line-height: 74px; }
Instead of using this
#nav-list li { display: inline-block; }
You can do like this:-
#nav-list li { display: inline; font-weight:bold;}
Please let me know if this helps.

CSS Navbar changes size on different sized screens

I am doing a website for school, and it's been going well. The only problem I am stuck on is this: The navbar changes size on different sized screens. It's most readily apparent when zooming out, but it is slightly off on different screens.
Here is a screenshot of what I mean:
https://docs.google.com/a/g.ccsd.net/file/d/0B_Sda_-LouAKbnVKVHhMSW5yeXc/edit?usp=sharing
Please note that the left side, which is the problem, moves around a lot depending on the scale. The above image is only one example.
This is my CSS:
ul {
font-family: 'Open Sans', Times;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
position: relative;
float: right;
z-index: 150;
/* min-width: 739px;
max-width: 739px; */
}
ul li {
display: block;
position: relative;
float: right;
}
li ul { display: none; }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 7px solid #CC4D4D;
padding: 25px 26.45px 30px 26.45px; /*top right bottom left*/
background: #333333;
margin-left: 0px;
white-space: nowrap;
}
ul li a:hover { background: #757575; }
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #757575; }
li:hover li a:hover { background: #757575; }
This is my HTML:
<ul id="menu">
<li>Showcase</li>
<li>Contact</li>
<li>FAQ</li>
<li>Faculty
<ul>
<li>Mrs. Rosarita Olvina</li>
<li>Mrs. Christine Pavesich</li>
<li>Mr. Francisco Virella</li>
<li>Mrs. Susan Williams</li>
</ul>
</li>
<li>Program Areas
<ul>
<li>Graphic Design</li>
<li>Photography</li>
<li>Video Production</li>
<li>Animation</li>
<li>Art</li>
</ul>
</li>
<li>About
<ul>
<li>What We Do</li>
<li>Where We Go</li>
</ul>
</li>
<li>Home</li>
</ul>
Thank you for any help.
***I tried first answer, it didn't work, but I may be doing it very wrong.
The problem is not necessarily within the menu. Rather the whole menu (or its container) needs to be placed properly.
As far as I can tell from the code you've given, the floats may also play a part in fixing this. The whole menu and nested ULs are floating. My suggestion would be to avoid these floats and work with "display: inline-block" instead to get the horizontal arrangement.
After streamlining the menu entries like that, you can properly position your whole menu container in the surrounding HTML.
In case you don't already use it: Firebug or Chromebug plugins are really handy for identifying errors like this.