vertical-align and pseudo element - css

To vertically center <a> tag i found this solution in this answer
you may use a pseudo element displayed as an inline-box using full
height of li and vertical-aligned to midlle. DEMO
body, html {
height:100%;/ needed for demo */
}
nav {
height: 50%;/* increased for demo */
width: 100%;
}
nav ul {
height: 100%;
margin: 0px;
}
nav ul li {
height: 33%;
box-shadow:inset 0 0 0 1px;/* show me li , for demo */
}
nav ul li:before {
content:'';
height:100%;
display:inline-block;
vertical-align: middle;
}
My question why we have to use a pseudo element ?
Why it's not working when I delete the pseudo element and I put just :
a {
height:100%;
display:inline-block;
vertical-align: middle;
}
Is the vertical-align have to apply to a first element (:before) and the other (<a>) will follow the vertical alignment ?

Why pseudo works?
With the before and after pseudo-elements, we can insert virtual elements before or after an element's content. They both work the same way, except that one inserts the content before the element and the other inserts the content after.
Pseudo elements forces some contents to be there in the html page and in the css stylesheet you apply a empty content with the height of 100% ie. with your tag height and now the content is occupying the space before or after (as you use pseudo) and to that align vertically with options top, middle, or bottom:
try this fiddle : just set height: 100px; and then increase manually like 200px, 300px then you will understand the exact reason
Without vertical alignment it's going to bottom as it is 100% heighty and if you use vertical-align:middle then you'll notice how the pseudo is working.
You may understand by seeing this picture:

UPDATE:
using pseudo element you are getting vertically center the text because of known height as you used height:100%; on pseudo element. As soon you will removed the height the layout will be broken.
As a tag is inline element so you made it block level element. but vertical-align: middle; only work with display:table-cell. but wait you have to use display:table in li element also.
Check the DEMO. so this piece of code will work..
a{
height:100%;
display: table-cell; /*replaced with display:inline-block*/
vertical-align: middle;
}
nav ul li {
height: 33%;
box-shadow:inset 0 0 0 1px;
display:table; /*add this line*/
width:100%; /*add width as well*/
}

Related

padding-top not working but padding-right is

this is my css:
body {
margin: 0px;
background-color: white;
}
#navbar {
background-color: red;
margin: 0 auto;
width: 900px;
height: 200px;
}
#navbar a {
padding: 20px;
color: grey;
text-decoration: none;
font-weight: bold;
font-size: 20px;
}
navbar is a div inside body and i have a couple of tags inside navbar.
this is the output:
there is a difference between the 'Block level' elements and 'Inline Elements'. The Margins & Paddings of 'Inline Elements' effect only in Horizontal Direction, but not in Vertical Direction, as per the basic concept.
Your Div was a block level element, but anchor tag is an inline element. To give vertical space make it a block element, as you've already found out OR put the anchor in a div, which has vertical space in form of 'padding' or 'margin'!
div a {display:block;padding:20px;} OR div a{display:inline-block;padding:20px;}
In later two cases, padding will now effect in vertical direction also, as has now converted to block-level element, from inline form. Hope, that helps!
I figured it out, I just needed to use: display:inline-block;
you can try like this: Demo
#navbar a {
display:block;
float:left;
}

Vertical Align to bottom with single Element

I have a holder div that has specific dimensions, and has a single child element of varying height, and I am trying to align it to the baseline. Currently, I have a second element that is the same fixed height as the container which makes it aligned to the bottom, but if it is on its own, it sticks to the top, regardless of what rules are applied.
So, how can I vertically align an element to the bottom of a container if it is the only child element?
EDIT
While in the process of putting up the code that I am using, I came up with a solution which I have posted. The initial problem is similar to that, but without the position rules, and display:inline-block on the child elements. That is pretty much the long and short of it...
Damn it, thought of a solution after posting the question which works nicely:
Parent Element:
.parent {
height:200px;
position:relative;
width:200px;
}
Child Element:
.parent > * {
bottom:0px;
left:0px;
position:absolute;
right:0px;
}
The height of the Child element is then defined by block elements within it, but it sticks to the bottom
One way using table-cell
Assuming the bare bones markup:
<div class="wrap">
<div>Some content...</div>
</div>
the following CSS will do it:
.wrap {
border: 1px solid red;
height: 200px;
width: 200px;
display: table-cell;
vertical-align: bottom;
}
Major advantage: works with both inline and block level elements.
Disadvantage: Older browsers don't recognize display: table-cell
Demo at: http://jsfiddle.net/audetwebdesign/eXKbt/
Alternate way using inline-block
You can also do it this way by applying the following CSS:
.wrap2 {
border: 1px solid red;
height: 200px;
width: 200px;
}
.wrap2:before {
content: "";
width: 0;
height: 190px;
display: inline-block;
}
.wrap2 div {
display: inline-block;
width: 190px;
border: 1px dotted red;
vertical-align: bottom;
}
However, this approach involved using a pseudo-element to define a fictitious inline block to set a baseline nearly the full height of the box and then using vertical-align on the child element. There were some issues related to the width but it can be made to work.
See earlier fiddle for demo.
Not much detail to go off of, but maybe something like this
.container {
position: relative;
}
.child-element {
position: absolute;
bottom: 0;
}

making span tag cover entire width of div

I have the following structure for html:-
<div>
<span>
Wherever
</span>
</div>
If I want the span to cover the entire width of the div, how can I do that in chrome?
I tried in css using
span{
background:#FFF;
width:100%;
}
It works in firefox and ie but not in chrome.
span elements are inline. Inline elements adjust their width automatically and ignore your width: expressions.
Make it block-level:
span {
display: block;
}
But then it's basically just a <div>.
There are two ways to resolve your issue.
As span tags adjust their width according to the contents in it. So to assign width to this tag we will use float left with width.
span{
background:#FFF;
width:100%;
float: left;
}
secod way is to use display block with the code
span{
background:#FFF;
width:100%;
display: block;
}
Change span's CSS to:
span {
display: block;
}
Try this:
div{
padding:0px;
}
span{
background:#FFF;
width:100%;
}

Can't center an ul inside a div. Tried many things

The navigation bar at top contains width: 100% height: 50px and is fixed positioned to the top.
However, the UL tag inside is always keeps positioning itself to the left side. It should be centered.
I tried so many things, like making a secondary div and giving it margin: 0 auto, giving
UL margin: 0 auto, making left %30 and right %30, float: center etc.
Am I missing something? What's the reason I can't position it to the center?
On '#navlist' change text-align: left to be text-align: center
On '#navlist ul' add the property display: inline-block;
You will need a div that wraps the ul and set its margin to "0px auto" just like you tried with the ul.
<div style="width:600px; margin:0px auto;">
<ul>...</ul>
</div>
I have had good luck centering a UL with these styles:
<style>
#container {text-align:center;}
#container ul {display:inline;}
#container ul li {display:inline;list-style-type:none;}
</style>
Generally, I have to steer clear of display:inline-block as we still develop for older IE browsers that don't acknowledge 'inline-block'.
you need to set and width for the Navigation bar, so it can be centered.
#menu-main-nav {
width: 990px;
}
make that div with some fix width and float. left or right.
then inside it.. make UL and with property of margin:0 auto;
div {
width:200px;
height:auto;
float:left;
}
ul {
margin:0 auto;
}

CSS floated elements not sitting next to each other

Please take a look at: http://jsfiddle.net/yCrA8/
The blue sidebar should float next to red middle box but instead it's clearing it and sitting below...
How do I fix this? I can't set a width for the .Middle div because it has content that needs to flow outside of the browser view and be scrollable.
Cheers
See: http://jsfiddle.net/thirtydot/yCrA8/4/
One way is to use display: inline-block and white-space: nowrap.
Remove float: left from .Sidebar and .Middle, then add this:
.MainContent {
white-space: nowrap
}
.Sidebar, .Middle {
white-space: normal;
vertical-align: top;
display: inline-block;
/* if you need ie6/7 support */
*display: inline;
zoom: 1
}
#Camernon; there is a reason why your middle div is not wrap because you did not define width to your middle div for this you can define width your middle & it's parent div
CSS:
.Middle
{
background:red;
width:9125px;
float:left;
}
.MainContent
{
margin: 20px;
width: 9330px;
}
check this fiddle http://jsfiddle.net/sandeep/yCrA8/11/

Resources