How do I distribute space evenly among inline links within a div? - html

I'm looking to distribute the space between the links evenly, so each take up 1/3 of the space of the containing div. The whole reason they are within this div is because I want to line it up with the banner, and I'm unsure how to it otherwise.
Here is a fiddle:
https://jsfiddle.net/yuy84gmq/13/
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
.navLinks li {
border: 1px solid black;
display: inline;
font-size: 25px;
}
#navBar {
border: 1px solid black;
width: 553px;
margin: 0 auto;
}
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav id="navBar">
<ul class="navLinks">
<li><a href='#'>About</a>
</li>
<li><a href='#'>Hours</a>
</li>
<li><a href='#'>Contact</a>
</li>
</ul>
</nav>
<!--#navBar-->

Use flexbox and set justify-content to space-between or space-around:
space-between
Flex items are evenly distributed in the line. [...]
space-around
Flex items are evenly distributed in the line, with half-size spaces on either end. [...]
.navLinks {
display: flex;
justify-content: space-around;
}
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
.navLinks li {
border: 1px solid black;
display: inline;
font-size: 25px;
}
#navBar {
border: 1px solid black;
width: 553px;
margin: 0 auto;
}
.navLinks {
display: flex;
padding: 0;
justify-content: space-around;
}
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav id="navBar">
<ul class="navLinks">
<li><a href='#'>About</a></li>
<li><a href='#'>Hours</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</nav>

I see a flexbox solution has been posted, so I'll post the table/table-cell solution. It's a simple but effective, and you don't have to worry about browser discrepancies.
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
#navBar {
border: 1px solid black;
width: 553px;
margin: 0 auto;
}
/* set the container to act like a table */
.navLinks {
display: table;
table-layout: fixed; /* evenly space all elements */
/* remove default styling */
width: 100%;
margin: 0;
padding: 0;
}
.navLinks li {
display: table-cell;/* set to a table-cell */
text-align: center;
font-size: 25px;
padding: 10px;
}
.navLinks a {
border: 1px solid black;
}
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav id="navBar">
<ul class="navLinks">
<li><a href='#'>About</a>
</li>
<li><a href='#'>Hours</a>
</li>
<li><a href='#'>Contact</a>
</li>
</ul>
</nav>
<!--#navBar-->

Try This code
is working for me
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
.navLinks{
padding: 0px;
}
.navLinks li {
border: 1px solid black;
display: inline-block;
font-size: 25px;
width:32%;
}
#navBar {
border: 1px solid black;
width: 553px;
margin: 0 auto;
}
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav id="navBar">
<ul class="navLinks">
<li><a href='#'>About</a>
</li>
<li><a href='#'>Hours</a>
</li>
<li><a href='#'>Contact</a>
</li>
</ul>
</nav>

hi xD
your css:
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto; /*After setting a width this will make object sit centrally within parent container. Auto sets left and right margins equally. 0 denotes no top or bottom margin */
}
.li1 {
border: 1px solid black;
display:inline-block;
margin-right: 15%;
float: left;
}
.li2 {
border: 1px solid black;
display:inline-block;
margin-right: 17%;
margin-left: 20%;
}
.li3 {
border: 1px solid black;
display:inline-block;
float: right;
margin-right: 8%;
}
/* Adjust left/right margin as appropriate */
#navBar {
border: 1px solid black;
width:553px;
margin: 0 auto;
}
your html:
<body>
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav id="navBar">
<ul class="navLinks">
<li class="li1"><a href='#'>About</a></li>
<li class="li2"><a href='#'>Hours</a></li>
<li class="li3"><a href='#'>Contact</a></li>
</ul>
</nav> <!--#navBar-->
</body>

Related

Change menu icon when hovering over its container

I am trying to build a nav bar. So here is the thing. What I want to do is to change both the color of menu icon and the word "menu" when hovering over their container.
I use background color to build this icon, so I cannot simply change background color to change the icon. Otherwise, the background of the whole container would be blue.
.nav-item{
display: inline-block;
vertical-align: middle;
height: 60px;
margin: 5px;
}
#primary-logo{
height: 50px;
}
.navbar{
border-top: 1px solid gray;
border-bottom: 1px solid gray;
}
.nav-list{
list-style-type: none;
padding-left: 0px;
display: flex;
align-items: center;
padding-bottom: 5px;
gap: 24px;
}
.menu-line{
width: 34px;
height:5px;
background-color: black;
margin: 6px 0px;
}
.menu-item{
display: inline-block;
vertical-align: middle;
}
.menu-sec:hover{
color: rgb(39, 42, 202);
cursor: pointer;
}
.menu-line:hover{
width: 34px;
height:5px;
background-color: rgb(39, 42, 202);
margin: 6px 0px;
}
<nav>
<div class="nav-item">
<img
id="primary-logo"
src="800px-The_Economist_Logo.svg.png"
alt="Economist Logo"
/>
</div>
<div class="navbar nav-item">
<ul class="nav-list">
<li class="menu-sec">
<div class="menu-item"> <!-- menu icon -->
<div class="menu-line"></div>
<div class="menu-line"></div>
<div class="menu-line"></div>
</div>
<div class="menu-item">Menu</div>
</li>
<li>Weekly edition</li>
<li>Search</li>
</ul>
</div>
</nav>
You can do it as below. I added this line .menu-sec:hover .menu-line {background-color: rgb(39, 42, 202)}. I also removed some duplicated lines.
.nav-item {
display: inline-block;
vertical-align: middle;
height: 60px;
margin: 5px;
}
#primary-logo {
height: 50px;
}
.navbar {
border-top: 1px solid gray;
border-bottom: 1px solid gray;
}
.nav-list {
list-style-type: none;
padding-left: 0px;
display: flex;
align-items: center;
padding-bottom: 5px;
gap: 24px;
}
.menu-line {
width: 34px;
height: 5px;
background-color: black;
margin: 6px 0px;
}
.menu-item {
display: inline-block;
vertical-align: middle;
}
.menu-sec:hover {
color: rgb(39, 42, 202);
cursor: pointer;
}
.menu-sec:hover .menu-line {
background-color: rgb(39, 42, 202);
}
<nav>
<div class="nav-item">
<img id="primary-logo" src="800px-The_Economist_Logo.svg.png" alt="Economist Logo" />
</div>
<div class="navbar nav-item">
<ul class="nav-list">
<li class="menu-sec">
<div class="menu-item">
<!-- menu icon -->
<div class="menu-line"></div>
<div class="menu-line"></div>
<div class="menu-line"></div>
</div>
<div class="menu-item">Menu</div>
</li>
<li>Weekly edition</li>
<li>Search</li>
</ul>
</div>
</nav>

Moving list items issue [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 4 years ago.
The problem is, if I put any margin on any element which is inside the product div or the slider div the whole list-items are moving down.
.product-slider {
margin-top: 16px;
text-align: center;
}
.slide-item {
list-style-type: none;
display: inline;
width: 80%;
overflow: hidden;
margin: auto;
}
.product, .slider {
height: 200px;
border: 1px solid black;
display: inline-block;
}
.product {
width: 25%;
}
.slider {
width: 5%;
}
<ul class="product-slider">
<li class="slide-item"><div class="slider"><i class="fas fa-angle-left"></i></div></li>
<li class="slide-item"><div class="product">Test</div></li>
<li class="slide-item"><div class="product">Test</div></li>
<li class="slide-item"><div class="product">Test</div></li>
<li class="slide-item"><div class="slider"><i class="fas fa-angle-right"></i></div></li>
</ul>
Also try this:
.product-slider{
margin-top: 16px;
text-align: center;
}
.slide-item{
list-style-type: none;
display: inline;
width: 80%;
overflow: hidden;
margin: auto;
}
.product, .slider{
height: 200px;
border: 1px solid black;
display: inline-block;
vertical-align:text-top;
}
.product{
width: 25%;
}
.slider{
width: 5%;
}
<ul class="product-slider">
<li class="slide-item">
<div class="slider"><i class="fas fa-angle-left"></i></div>
</li>
<li class="slide-item">
<div class="product">Test</div>
</li>
<li class="slide-item">
<div class="product">Test</div>
</li>
<li class="slide-item">
<div class="product">Test</div>
</li>
<li class="slide-item">
<div class="slider"><i class="fas fa-angle-right"></i></div>
</li>
</ul>
Try this:
.product-slider{
margin-top: 16px;
text-align: center;
}
.slide-item{
list-style-type: none;
display: inline;
width: 80%;
overflow: hidden;
margin: auto;
}
.product, .slider{
height: 200px;
border: 1px solid black;
display: inline-block;
}
.product{
width: 25%;
}
.slider{
width: 5%;
}
<ul class="product-slider">
<li class="slide-item slider"><div class="slider-left"><i class="fas fa-angle-left"></i></div></li>
<li class="slide-item product"><div class="product-item">Test</div></li>
<li class="slide-item product"><div class="product-item">Test</div></li>
<li class="slide-item product"><div class="product-item">Test</div></li>
<li class="slide-item slider"><div class="slider-right"><i class="fas fa-angle-right"></i></div></li>
</ul>

Vertical centering of a shrinked menu

I am building a responsive menu using only css and html. The only problem I have right now is that I don't know how to verticaly center the links in menu.
Here is my code:
#media only screen and (max-width: 768px) {
nav {
display: none;
width: 100%;
height: auto;
background: black;
float: none;
text-align: center;
border-top: 1px solid white;
}
nav ul,
li,
a {
width: 100%;
display: inline-block;
margin: 0 auto;
}
nav ul {
padding-bottom: 5px;
margin: 0 auto;
}
nav li {
border-top: 1px solid white;
line-height: 30px;
}
}
<nav>
<a href="index.html">
<div class="logo">
<img src="img/logo.png">
</div>
</a>
<ul>
<li>Kontakt
</li>
<li>Reference
</li>
<li>Moje služby
</li>
<li>Kdo jsem
</li>
</ul>
</nav>

Last <li> not in place with others

This is my jsfiddle example. My ul.nav is 910px and i have 7 li.nav inside it. I set their width to 130px each but the last one is out of place. Thanks.
Try increasing the min/max width of ul.nav
ul.nav {
width: 90%;
min-width: 1000px;
max-width: 1000px;
margin: 0 auto;
}
nav.nav {
width: 100%;
min-width: 980px;
max-width: 1024px;
height: 40px;
padding: 0;
border: 1px solid black;
}
ul.nav {
width: 90%;
min-width: 1000px;
max-width: 1000px;
margin: 0 auto;
}
li.nav,
a.nav {
width: 130px;
display: inline-block;
}
<nav class="nav">
<ul class="nav">
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
<a href="#" class="nav">
<li class="nav">Home</li>
</a>
</ul>
</nav>
set an exact width on your last nav and it works:
<a href="#" class="nav" style="width:40px">
<li class="nav">Home</li>
</a>
try to reduce nav length
li.nav, a.nav {
width: 110px;
display: inline-block;
}
I reduced 130 to 110
Try adding the below css
nav.nav {
width: 100%;
min-width: 980px;
max-width: 1024px;
height: 40px;
padding: 0;
border: 1px solid black;
}
ul.nav {
width: 90%;
min-width: 910px;
max-width: 910px;
margin: 0 auto;
}
li.nav, a.nav {
width: 130px;
float: left;
}
ul.nav:after {
content:"";
clear:both;
display:block;
}
li.nav {
list-style-type:none;
padding: 12px 0;
box-sizing:border-box;
}
instead <li class="nav">Home</li>
use - <li class="nav">Home</li>
maybe use display: flex; justify-content: space-between; for ul.nav?
nav.nav {
width: 100%;
min-width: 980px;
max-width: 1024px;
height: 40px;
padding: 0;
border: 1px solid black;
}
ul.nav {
width: 100%;
min-width: 980px;
max-width: 1024px;
margin: 0 auto;
text-align: center;
padding-left: 0;
list-style: none;
display: flex;
justify-content: space-between;
border: 1px solid #ccc;
}
ul.nav > li {
width: 130px;
}
ul.nav > li a{
display: block;
}
ul.nav > li a:hover{
background: #ccc;
}
<nav class="nav">
<ul class="nav">
<li class="nav">Home</li>
<li class="nav">Home</li>
<li class="nav">Home</li>
<li class="nav">Home</li>
<li class="nav">Home</li>
<li class="nav">Home</li>
<li class="nav">Home</li>
</ul>
</nav>

Making a mini vertical divider

I am trying to make a miniature vertical bar like in this site, where they have the navigation and the vertical bars in between each link. I have tried the solution to a previous question, but when I tried to use 'margin-left' to move the text, the bar wouldn't stay between each link, it'd to this.
HTML
<div id="nav-clearfix">
<div id="nav">
<ul class="nav-pages">
<li>HOME</li>
<li><div class="mini-divider"></div></li>
<li>ROSTER</li>
<li><div class="mini-divider"></div></li>
<li>GALLERY</li>
<li><div class="mini-divider"></div></li>
<li>ABOUT US</li>
<li><div class="mini-divider"></div></li>
<li>SPONSORS</li>
</ul>
</div>
</div>
CSS
#nav-clearfix {
width: 100%;
height: 100px;
background: #000;
}
#nav {
margin-left: 10%;
width: 100%;
}
.nav-pages {
padding-top: 10px;
}
.mini-divider {
position: absolute;
top: 26%;
bottom: 71%;
border-left: 1px solid white;
}
.nav-pages li, .mini-divider {
float: left;
margin-left: 50px;
}
CSS
.nav-pages li:not(:last-child) a:after{
content: "";
/* width: 0px; */
background: white;
margin-left: 20px;
position: absolute;
height: inherit;
color: white;
border: 1px solid white;
height: 15px;
}
Remove The Border Related HTML & CSS
<li><div class="mini-divider"></div></li>
DEMO
You can also use + css selector to give border to the next element no need to add extra element for border
added
*{
margin: 0;
padding: 0;
}
for removing default styles given by browsers
* {
margin: 0;
padding: 0;
}
#nav-clearfix {
width: 100%;
height: 100px;
background: #000;
}
#nav {
width: 100%;
}
.nav-pages {
padding-top: 10px;
text-align:center;
}
.nav-pages li {
display: inline-block;
padding: 0 10px;
}
.nav-pages li + li {
border-left: 1px solid white;
}
<div id="nav-clearfix">
<div id="nav">
<ul class="nav-pages">
<li>HOME</li>
<li>ROSTER</li>
<li>GALLERY</li>
<li>ABOUT US</li>
<li>SPONSORS</li>
</ul>
</div>
</div>
Use this .separator class for vertical separator.
CSS
ul > li{
float: left;
display: block;
position: relative;
}
ul > li > a{
padding: 4px 6px;
display: block;
}
.separator {
background: none repeat scroll 0 0 #222;
border-left: 1px solid #333;
float: left;
height: 30px;
width: 1px;
}
HTML
<ul>
<li >
<a href="#" >Home</a>
</li> <span class="separator"></span>
<li> Link 1 </li> <span class="separator"></span>
<li > Link 2 </li> <span class="separator"></span>
<li> Link3 </li> <span class="separator"></span>
<li > Contact </li>
</ul>
jsfiddle: demo