floating fluid and fixed width element - html

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.

Related

Position third horizontal div to the bottom of other divs?

EDIT: The problem is solved, so thanks to everyone who helped!
Original post:
So I am trying to put three divs next to each other (until thus far this part has been successful) with the third and last div to like go to attach to the bottom of the divs, which I have no clue how to do this.
How can I put the third div to attach to the bottom of the middle div and stay within the container?
To show you, I made a quick example. Something like this:
The black colour in the image is the 'body'.
The grey is a container div I put the three other divs in.
Each other box represents a div with what I want them to do and how approx. I want them to be positioned of one another.
I hope this can be done only using html and css. I would appreciate any help.
So far I have this as html for the divs:
#nav,
#textarea,
#contactallpages {
vertical-align: top;
display: inline-block;
*display: inline;
}
#containerpage {
position: relative;
margin: auto;
padding-top: 5%;
padding-bottom: 5%;
background-color: black;
height: 100%;
width: 70%;
}
#centercontainer {
background-color: lightblue;
width: 75%;
margin: 0 auto;
padding: 2%;
}
#nav {
float: left;
background: #aaaaaa;
height: 50%;
width: 15%;
padding: 1%;
}
#textarea {
display: inline-block;
background: #cccccc;
height: 70%;
width: 64%;
padding: 1%;
}
#contactallpages {
background: #bbbbbb;
position: absolute;
width: 15%;
padding: 1%;
bottom: 0;
}
<div id="containerpage">
<div id="centercontainer">
<div id="nav">
<ul>1
</ul>
<ul>2
</ul>
<ul>3
</ul>
</div>
<div id="textarea">
<header>
<h1>Welcome</h1>
</header>
<p>
Text text more text.
</p>
<p>
And more text.
</p>
</div>
<div id="contactallpages">
Random small textbox
<br>More small text.
</div>
</div>
</div>
The way you should lay this out is one container div and 3 children div's set to display: inline-block;
Using display: inline-block; will position all the div's next to each other and allows you to use the vertical-align property.
Now all you would need to do is set the proper vertical-alignment for each of the child div's. You can also set the height to the container div (#myPage) and that is the height that vertical-align will use to determine the positioning.
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
#myPage div {
display: inline-block;
width: 100px;
}
#centerFold {
height: 200px;
vertical-align: middle;
background-color: yellow;
}
#navBar, #contact{
height: 100px;
}
#navBar {
vertical-align: top;
background-color: red;
}
#contact {
vertical-align: bottom;
background-color: blue;
}
<div id="myPage">
<div id="navBar">
</div>
<div id="centerFold">
</div>
<div id="contact">
</div>
</div>
Try out flexbox if you do not have too much to worry about backward compatibility. My time at the moment doesn't allow to elaborate, but the essential part would be
#centercontainer {display: flex}
#contactallpages {align-self: flex-end}
Be aware though that some prefixing will be necessary for older browsers and this is only the standards-compliant solution. It does everything you want and you can forget about floating. Adding a
#textarea {flex-grow: 1}
would even allow the center to grow not only in height but in width also.

How to justify and center my vertical navigation links

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.

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

How to make a navbar like WikiHow?

I would like a navbar like WikiHow with a icon on top and text beneath. I have been taking a look at their code but it seems pretty messy and I think there is easier ways to do it.
CSS
nav ul li{
border-left: 1px solid red;
height: 80px;
position: relative;
display: inline-block;
width: 70px;
}
.nav_icon{
margin-top: 15px;
background: url('inc/icon.png');
}
HTML
<nav>
<ul>
<li><div class="nav_icon"></div>HOME</li>
<li>PICTURES</li>
<li>ABOUT</li>
</ul>
</nav>
Then I created a <div> that I inserted before "HOME" in the first <li> element. I put some padding-top: 15px; on the div to make it go down a bit, but affects the whole <li> elements. I just want the icon to get some margin from the top...
http://jsfiddle.net/JmZbG/1/
By default inline blocks will align based on their text baseline.
Just add vertical-align: top; to the CSS for nav ul li to have them align by their top edge instead.
Here's my version: http://jsfiddle.net/JmZbG/2/
And here's an explanation of the changes:
nav ul li {
border-left: 1px solid red;
height: 80px;
line-height: 80px; /* Center the labels vertically */
float: left; /* Another way of lining up the <li>s horizontally */
display: inline-block;
}
.nav_icon {
display: inline-block; /* Needs to be an inline-block to be inline with the text */
vertical-align: middle; /* This centers the image vertically in it's <li> */
width: 46px; /* Need to define a size for an empty <div>... */
height: 41px; /* ...in order to see the background image */
background-image: url("http://i.imgur.com/mDXvZOZ.jpg");
}

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)