How to justify and center my vertical navigation links - html

If I understood how Justify works, it will fill or remove the needed spacing to my words to make it the same width like the others. My problem is I want to make my links centered, and justified at the same time, I want to add some spacing so that the other links are aligned with PORTFOLIO.
I've tried giving the div a text-align: center, and created a class for each of my a tags and gave it a text-align: justify. Sadly, it didn't work and I'm lost.
#side-nav-menu {
width: 40%;
position: fixed;
text-align: center;
top: 50%;
bottom: auto;
left: auto;
right: auto;
transform: translate(0, -50%);
display: none;
}
#side-nav-menu ul li a {
text-align:justify;
color: white;
}
EDIT:
Something like this

This was a huge pain in the ass to get to work, but here it is:
Example fiddle
Here's the relevant code:
JavaScript
$('#side-nav-menu li a').each(function() {
var t = $(this),
letters = $(this).text().split(''),
width = $(this).width(),
output = "";
for (var i=0,l=letters.length;i<l;i++){
output += "<span style='width:" + ((i===(l-1)) ? 0 : width/(l-1)) +"px;'>" + letters[i] + "</span>";
}
t.html(output);
});
CSS
body {
background-color: #999;
font-family: sans-serif;
}
#side-nav-menu {
width: 200px;
position: fixed;
text-align: center;
top: 50%;
bottom: auto;
left: auto;
right: auto;
transform: translate(0, -50%);
list-style: none;
padding: 0;
margin: 0;
}
#side-nav-menu li {
height: 30px;
}
#side-nav-menu li a {
display: block;
color: white;
text-align: justify;
}
#side-nav-menu li a span {
display: inline-block;
}
HTML
<ul id="side-nav-menu">
<li>
<a>HOME</a>
</li>
<li>
<a>BLOG</a>
</li>
<li>
<a>ABOUT</a>
</li>
<li>
<a>RESUME</a>
</li>
<li>
<a>PORTFOLIO</a>
</li>
<li>
<a>PROFILES</a>
</li>
<li>
<a>CONTACT</a>
</li>
</ul>
Update - A cooler way to do it
If you're up to using a monospace font, you can do something like this:
Fiddle with monospace
Here's what's different from the first:
CSS
body {
background-color: #999;
font-family: monospace;
}
JavaScript
for (var i=0,l=letters.length;i<l;i++){
output += "<span style='font-size:" + width/l +"px;'>" + letters[i] + "</span>";
}
Sample output:

I think you can fiddle some with flexbox:
.container {
width: 200px;
height: 300px;
padding: 5px;
background: #555;
color: white;
font-size: 24px;
}
.item {
display: flex;
justify-content: space-between;
}
<div class="container">
<div class="item">
<i>F</i><i>i</i><i>r</i><i>s</i><i>t</i>
</div>
<div class="item">
<i>S</i><i>e</i><i>c</i><i>o</i><i>n</i><i>d</i>
</div>
<div class="item">
<i>L</i><i>a</i><i>s</i><i>t</i>
</div>
</div>
Flexbox to justify text

The best way I can think of doing this would be like this (using inline styles for brevity).
inline-block elements use the minimum space necessary, which can come in handy.
li {
display: block;
}
<div style="text-align: center; width: 100%;">
<div style="text-align: left; display: inline-block;">
<ul>
<li>
Home
</li>
<li>
Blog
</li>
<li>Supercalafragilistic</li>
</ul>
</div>
</div>

Essentially what you need is the UL (container) to be centered, while the LI (content) to be left aligned, but the way I know requires a width to be set on the container.
ul {
display: block;
margin: auto;
width: 40%;}
li {
display: block;
text-align: left;}
To get it centered as much as possible, the width needs to be a tad wider than the longest word. You can play with the value until it's perfectly centered.

The 'text-align: justify' styling causes the render engine to alter the width of whitespace spans so that the lines are all the same width (with the exception of the trailing orphan line). Since your anchor texts have no whitespace spans, the render engine can't do anything.
What you need to be adjusting is letter-spacing style. Here's a different StackOverflow answer with a bit of jQuery showing how it is done:
Stretch text to fit width of div
I caution you however: the difference in lengths between "HOME" and "PORTFOLIO" are likely to look very odd when you make the words be the same length.

Related

HTML Images extend farther and block links

Images I place are blocking me from clicking links, and I think it is because the image is possibly larger than I thought (though I think I cropped it and I am not sure if it is something else.
Here is a picture (I moved he image as far over as I could in order to avoid this issue, but I would like to move the image closer if this problem can be fixed and I feel it will be helpful to know in the future):
Note: If I move it right it does not push the content or anything, it just makes the links in the nav bar unclickable (if that was not clear).
Here is the HTML:
<nav>
<ul>
<li class="current">Home</li>
<li>Jehovah's Witness</li>
<li>Wood Block Print</li>
<li>Jazz</li>
<li>Being Ethical and Socially Responsible </li>
</ul>
</nav>
<div class="container">
<header>
<h1>
<img src="images/banner.png" alt="banner">
Designer Websites
</h1>
</header>
and my CSS:
nav ul
{
list-style-type: none;
text-align: center;
}
nav ul li
{
padding: 5px;
display: inline-block;
border: solid 1px black;
color: black;
background-color: tan;
}
.current
{
background-color: yellow;
}
body
{
background-color: tan;
font-family: Arial, "Times New Roman", "Sans Serif", Georgia;
}
.container
{
width: 80%;
max-width: 960px;
margin:0px auto;
}
h1 img
{
height: 40%;
position:absolute;
left:-15px;
top: -30px;
}
New picture with the absolute position removed:
I think your "absolute" positioned banner image was floating over navbar. That's the reason it was not clickable.
Remove this code (You may keep the "height" to set fixed height for your image)
h1 img {
height: 40%;
position: absolute;
left: -15px;
top: -30px;
}
Now image and heading will be left aligned. You can simply align them by adding text-align:center in .container. Or you can add this code:
header {
text-align: center;
}
Try adding this in order to put anchor "above" the img
h1 img {
z-index: 1;
}
h1 a {
position: relative;
z-index: 2;
}

css vertical align img and text (multiline) in <li>

I did take a look at loot of similar questions here, but no one helped me solve my problem. I have a problem with vertically align the img on the left side in the li cell (this is working), but i can't align the text next to img. The line-height from ul li div is messing my things.
Here is a Jsfiddle.
What i wan't to achive is this:
Vertically and horizontally align img in 1/3 of the li cell on the left side.
Vertically and horizontally align text in 2/3 of the li cell, text align should be left. Text can be multiline and with bolded heading in first line.
You can also edit html code, if it is necessary.
HTML
<div class="product_banner_right">
<div class="product_banner_right title">
<h3>LOOK DOWN</h3>
</div>
<ul>
<li>
<img src="http://dummyimage.com/26x23/000/fff.png" alt="" />
<p><span>HEADING1</span>first line text
<br>second line text</p>
</li>
<li>
<img src="http://dummyimage.com/48x9/000/fff.png" alt="" />
<p><span>HEADING2</span>first line text</p>
</li>
<li>
<img src="http://dummyimage.com/40x24/000/fff.png" alt="" />
<p><span>HEADING3</span>first line</p>
</li>
<li>
<img src="http://dummyimage.com/46x17/000/fff.png" alt="" />
<p><span>HEADING4</span>first line text
<br>second line text
<br>third line text</p>
</li>
</ul>
</div>
CSS
.product_banner_right {
font-size: 1.2em;
position: relative;
width: 250px;
}
.product_banner_right .title {
height: 40px;
background: #1b3a6f;
}
.product_banner_right .title h3 {
text-align: center;
line-height: 40px;
}
.product_banner_right ul {
margin: 0;
padding: 0;
}
.product_banner_right ul li {
display: block;
height: 70px;
border: 1px solid black;
}
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 10px;
max-width: 50px;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
}
.product_banner_right ul li p span {
font-weight: bold;
}
change the following styles to :
.product_banner_right {
font-size: 100%;
position: relative;
width: 250px;
}
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 4%;
width: 11%;
max-width: 50px;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
width: 80%;
}
the result:
I have rewrote your html to accomodate the changes.
I have applied two options:
variable height list items.
fixed height list items with overflow.
Fixed height list items
CLICK FOR DEMO
This option is fully browser compatible but would require manually adjustment of the top margin for each list item.
Alternatively this option could still be used with the box flex model described below.
Fix height of list item and add scroll on overflow:
height:70px;
overflow:auto;
Variable height list items
CLICK FOR DEMO
This option relies on css3 flex box model:
display:flex;
display:-webkit-flex;
align-items:center;
-webkit-align-items:center;
justify-content:center;
-webkit-justify-content:center;
Please note flex box requires browser support. It is now highly compatible with modern browsers however old versions of the useless outdated browser ie will not support it.
Users of these browsers will still have a nice viewing experience however the images will be aligned at the top of each list box and not the center.
i don't know if this is the best approach but it looks allready a bit better than yours.
i just changed the following:
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 10px;
/* CHANGED*/
width: 33%;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
/* CHANGED*/
width: 66%;
position:absolute;
}
but you still have to get the text to fit into the table.
hope it helped, cheers!
You can make the texts in separate classes and then arrange the as you wish using margin

floating fluid and fixed width element

I want to have a 4 columns layout Panel Bar with the following conditions:
#c1, #c2 = with specific width
#c3 autofill with remaining width
#c4 auto width (e.g. increase / decrease width if more list added) and will correspond to #c3
I'm looking for a solution that could:
have #c4 floated to the right instead of position absolute
not having a specific margin right on #c3 and it will correspond spaces dynamically disregards how many list added to #c4
have a variable width on .smenu rather than having a specific width to get the list item flow horizontally.
work responsively cross platform and devices (minimum browser support IE8)
display smenu list horizontally without using a specific width for the container
Additional Issue:
When i hover to the a tag with class name .show-sub the .smenu shows
/ display but when i move my mouse over trying to hover over on one of the sub menu list it goes hidden. What was the way to work around to keep it open when i hover?
Different Attempt:
I've also tried with display:table-cell but couldn't get it working correctly. Click here for demo
HTML
<div id="sticky-bar" class="cf">
<div id="c1" class="left">col 1</div>
<div id="c2" class="left">col 2</div>
<div id="c3">
<span class="incredibly-long-txt">So many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many text so many tex</span>
</div>
<div id="c4">
<ul class="mmenu">
<li>
m1
<ul class="smenu">
<li>
a1
</li><li>
a2
</li><li>
a3
</li><li>
a4
</li>
</ul>
</li>
<li>
m2
<ul class="smenu">
<li>
b1
</li><li>
b2
</li><li>
b3
</li><li>
b4
</li>
</ul>
</li>
<li>
m3
<ul class="smenu">
<li>
c1
</li><li>
c2
</li><li>
c3
</li><li>
c4
</li>
</ul>
</li>
</ul>
</div>
</div>
CSS
*, *:before, *:after {
box-sizing: border-box;
}
ul, li {
margin: 0;
padding:0;
list-style:none;
}
a {
color: #fff;
}
.left {
float: left;
}
.right {
float: right;
}
.cf:before, .cf:after {
content:'';
display: table;
}
.cf:after {
clear:both;
}
.cf {
*zoom: 1;
}
#sticky-bar {
color: #fff;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
display: block;
width: 100%;
height: 30px;
background: #582A72;
position: relative;
}
#c1 {
width: 100px;
height: 100%;
background: #9775AA;
padding: 6px;
}
#c2 {
width: 150px;
height: 100%;
background: #764B8E;
padding: 6px;
}
#c3 {
height: 100%;
background: #3D1255;
padding: 6px;
margin: 0 90px 0 250px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#c4 {
background: #260339;
position: absolute;
right:0;
top:0;
}
.mmenu {
display:block;
}
.mmenu li {
float:left;
width: 30px;
height: 30px;
text-align: center;
border-left: 1px solid #fff;
background: #887CAF;
}
.mmenu li a {
display: block;
width: 100%;
height: 100%;
padding: 6px 0;
position: relative;
}
.smenu {
display: none;
background: #403075;
position: absolute;
top: 100%;
right: 0;
width: 120px;
}
.smenu li {
background: #882D61;
}
.show-sub:hover + .smenu {
display: block;
}
Have you considered placing #c1 and #c2 inside #c3. It would allow you to set their specific width, float them left or right, thus giving the illusion that #c3 is filling the empty space. I prefer to use tables in the case I need empty space filled. It's my experience that it requires drastically more markup to achieve liquid width with divs than tables.

How do I align a banner above buttons

I need to align my banner with the buttons, banner size is w:967 h:106, tried to directly add the image but it pushes all the buttons to the right.
This is what my code looks like so far:
HTML
<nav>
<img src="png"/>
<ul class="fancyNav">
<li id="home">Home</li>
<li id="s">Social</li>
<li id="p">Political</li>
</ul>
</nav>
CSS
.fancyNav{
overflow: hidden;
display: inline-block;
position: absolute;
text-align: center;
top: 16%;
margin-left: 170px;
}
Site link: http://mops.pcriot.com/main.html
This is caused by display: inline-block; which behaves like this:
Displays an element as an inline-level block container.
The inside of this block is formatted as block-level box,
and the element itself is formatted as an inline-level box.
Using display: block will solve this, but then you may need to do more to align the navigation. Anything with inline in it, will cause what you're seeing.
Here are your other options for the display: property:
http://www.w3schools.com/cssref/pr_class_display.asp
.fancNav {
top: 110px;
}
adjust 110 to whatever looks best
make some changes in your class
.fancyNav {
overflow: hidden;
display: inline-block;
text-align: center;
top: 16%;
margin-left: 170px;
width: 100%;
}
Left Aligned - Try removing:
.fancyNav {
margin-left: 170px;
}
Then add:
.fancyNav {
padding: 0;
}
Center Aligned - Do as above then add:
nav {
margin-left: 170px;
}
or instead (probably better):
nav {
width: 967px;
margin: 0 auto;
}

Right align some text within a list item

I have a nested unordered list which has main content on the left, and I would like to add options which are floated right, such that they are aligned on the right regardless of the level of indentation.
<ul>
<li> Item 1 <span class='options'> link </span> </li>
<li> Item 2 <span class='options'> link </span>
<ul>
<li>Item 3 <span class='options'> link </span> </li>
</ul>
</li>
</ul>
I have the following css:
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
padding-left: 15px;
width: 400px;
}
.options {
float: right;
width: 50px;
}
When this is used however the options span is aligned to the right, but 1 line below the expected line.
How can I get the options span to line up with the list item?
TIA,
Adam
Instead of floating, you may want to try absolute positioning.
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
padding-left: 15px;
width: 400px;
position: relative;
}
.options {
width: 50px;
position: absolute;
right: 0px;
}
Using this CSS:
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
padding-left: 15px;
width:400px;
}
.options {
float: right;
width: 50px;
}
li li { width:385px}
This unfortunately requires your to define a width minus the padding. depending on your flexibility this will work. Tested in Chrome 3.0.
If modifying the HTML code is OK, you could enclose "Item 1" in a first span and:
float it to left (still floating .options to the right)
use display: inline-block on both span and text-align: right on .options, instead of floats (no compatible with Fx2 though, and only working in IE6/7 because span is an inline elements by default. Would not work with div)