reversing z-index based from page render order - z-index

Example Markup:
<div class="wrapper">
<h2>Trigger</h2>
<div>This is some content</div>
</div>
<div class="wrapper">
<h2>Trigger</h2>
<div>This is some content</div>
</div>
<div class="wrapper">
<h2>Trigger</h2>
<div>This is some content</div>
</div>
Example CSS:
.wrapper {z-index: 1}
.wrapper div {display: none; position: absolute;}
Via javascript (jQuery) I'm attaching a click event to each h2 that will then switch the content div to display: block.
The intent is that these are expandable blocks of content that will overlap anything else on the page.
The catch is that I'd like the first one to overlap the second, which would overlap the 3rd in the event that all of them are open.
However, since each one is being rendered AFTER the previous one, the actual stacking order is reversed (The last content div created end sup overlaying the previously created once).
Is there a clever way of reversing this behavior with CSS/HTML? Or is the solution to let the page render, then via javascript, grab all of the content divs in order and give them each a z-index in reverse order?
UPDATE:
Here's some more specific markup:
<div style="padding: 10px;">Hello World
<div style="position: relative; top: -5px;">
<div style="position: absolute; background: yellow;"><p>This</p><p>is</p><p>overlapping</p></div>
</div>
</div>
<div style="padding: 10px;">Hello World
<div style="position: relative; top: -5px;">
<div style="position: absolute; background: orange;"><p>This</p><p>is</p><p>overlapping</p></div>
</div>
</div>
<div style="padding: 10px;">Hello World
<div style="position: relative; top: -5px;">
<div style="position: absolute; background: red;"><p>This</p><p>is</p><p>overlapping</p></div>
</div>
</div>
The following markup will produce 3 divs, each with a colored div overlapping. Due to the render order, the last absolutely positioned DIV (red) will be on top of the one before it (orange).
I can't figure out what type of z-indexes I need to apply to get the FIRST colored overlapping div to be on top. The order from top-to-bottom in terms of z-index should mirror the markup (yellow on top, red on bottom).
This is, of course, reverse of the standard.
I'm willing to use javascript to fix this post-display but I'm still struggling for the exact CSS that I need to apply via javascript. Is what I'm after doable?

So still no answer here, i just did something similar, even though my workaround is 100% hack, if anyone else comes to this page, it did work!
#nav ul li:nth-child(1) {
z-index:10;
}
#nav ul li:nth-child(2) {
z-index:9;
}
#nav ul li:nth-child(3) {
z-index:8;
}
#nav ul li:nth-child(4) {
z-index:7;
}
#nav ul li:nth-child(5) {
z-index:6;
}
#nav ul li:nth-child(6) {
z-index:5;
}
I just had that and as long as i didn't get over 10 elements it seems to work...

Here is SASS function for the top answer:
#for $i from 1 through 10 {
&:nth-child(#{$i}) {
z-index: #{10 - $i};
}
}

Shiny new CSS flexbox technology makes this a bit easier,
but the downside is the actual content will be reversed.
This doesn't have to be a big problem, it's just semantically weird.
In short: wrap everything in a container with these styles:
display: flex;
flex-direction: column-reverse;
See the fiddles for a working example (hover over the wrapper elements to see the action).
It's useful if you don't want to rely on javascript to dynamically check z-indexes every time you update the content. If the <div class="wrapper"> elements are inserted on the fly (or any other reason specific styles cannot be set in advance) The CSS rules in the example should be enough to take care of the z-indexes IF you insert the <div>s in reverse order.
This is your current setup:
http://jsfiddle.net/dJc8N/2/
And this is the same HTML, with added CSS (notice the numbers in the <h2> tags):
http://jsfiddle.net/Mjp7S/1/
EDIT:
The CSS I posted is probably not ready to be copy-pasted yet. This, however, is:
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
-webkit-flex-direction: column-reverse;
-moz-flex-direction: column-reverse;
-ms-flex-direction: column-reverse;
-o-flex-direction: column-reverse;
flex-direction: column-reverse;

I had exactly this problem, but an indeterminate number of elements, and possibly too many to make the CSS hack posted by jamie-wilson feasible. Also, since my page is generated dynamically with PHP, I didn't want to fuss with reversing the order of everything in the DOM and using flexbox the way Sandy Gifford suggested. I found an extremely simple and elegant jQuery solution to use instead:
$(document).ready(function() {
var item_count = $('your-selector').length;
for( i = 0; i < item_count; i++ )
{
$('your selector').eq( i ).css( 'z-index', item_count - i );
}
});
I can't speak to how performant this is, but with ~35 items I didn't notice any delays.

Related

Getting rid of whitespace when using raw EL Expression in NetBeans [duplicate]

There will be a 4 pixel wide space between these span elements:
span {
display: inline-block;
width: 100px;
background-color: palevioletred;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
Fiddle Demo
I understand that I could get rid of that space by removing the white-space between the span elements in the HTML:
<p>
<span> Foo </span><span> Bar </span>
</p>
I'm Looking for a CSS solution that doesn't involve:
Altering the HTML.
JavaScript.
Alternatively, you should now use flexbox to achieve many of the layouts that you may previously have used inline-block for: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Since this answer has become rather popular, I'm rewriting it significantly.
Let's not forget the actual question that was asked:
How to remove the space between inline-block elements? I was hoping
for a CSS solution that doesn't require the HTML source code to be
tampered with. Can this issue be solved with CSS alone?
It is possible to solve this problem with CSS alone, but there are no completely robust CSS fixes.
The solution I had in my initial answer was to add font-size: 0 to the parent element, and then declare a sensible font-size on the children.
http://jsfiddle.net/thirtydot/dGHFV/1361/
This works in recent versions of all modern browsers. It works in IE8. It does not work in Safari 5, but it does work in Safari 6. Safari 5 is nearly a dead browser (0.33%, August 2015).
Most of the possible issues with relative font sizes are not complicated to fix.
However, while this is a reasonable solution if you specifically need a CSS only fix, it's not what I recommend if you're free to change your HTML (as most of us are).
This is what I, as a reasonably experienced web developer, actually do to solve this problem:
<p>
<span>Foo</span><span>Bar</span>
</p>
Yes, that's right. I remove the whitespace in the HTML between the inline-block elements.
It's easy. It's simple. It works everywhere. It's the pragmatic solution.
You do sometimes have to carefully consider where whitespace will come from. Will appending another element with JavaScript add whitespace? No, not if you do it properly.
Let's go on a magical journey of different ways to remove the whitespace, with some new HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
You can do this, as I usually do:
<ul>
<li>Item 1</li><li>Item 2</li><li>Item 3</li>
</ul>
http://jsfiddle.net/thirtydot/dGHFV/1362/
Or, this:
<ul>
<li>Item 1</li
><li>Item 2</li
><li>Item 3</li>
</ul>
Or, use comments:
<ul>
<li>Item 1</li><!--
--><li>Item 2</li><!--
--><li>Item 3</li>
</ul>
Or, if you are using using PHP or similar:
<ul>
<li>Item 1</li><?
?><li>Item 2</li><?
?><li>Item 3</li>
</ul>
Or, you can even skip certain closing tags entirely (all browsers are fine with this):
<ul>
<li>Item 1
<li>Item 2
<li>Item 3
</ul>
Now that I've gone and bored you to death with "one thousand different ways to remove whitespace, by thirtydot", hopefully you've forgotten all about font-size: 0.
For CSS3 conforming browsers there is white-space-collapsing:discard
see: http://www.w3.org/TR/2010/WD-css3-text-20101005/#white-space-collapsing
Today, we should just use Flexbox.
OLD ANSWER:
OK, although I've upvoted both the font-size: 0; and the not implemented CSS3 feature answers,
after trying I found out that none of them is a real solution.
Actually, there is not even one workaround without strong side effects.
Then I decided to remove the spaces (this answers is about this argument) between the inline-block divs from my HTML source (JSP),
turning this:
<div class="inlineBlock">
I'm an inline-block div
</div>
<div class="inlineBlock">
I'm an inline-block div
</div>
to this
<div class="inlineBlock">
I'm an inline-block div
</div><div class="inlineBlock">
I'm an inline-block div
</div>
that is ugly, but working.
But, wait a minute... what if I'm generating my divs inside Taglibs loops (Struts2, JSTL, etc...) ?
For example:
<s:iterator begin="0" end="6" status="ctrDay">
<br/>
<s:iterator begin="0" end="23" status="ctrHour">
<s:push value="%{days[#ctrDay.index].hours[#ctrHour.index]}">
<div class="inlineBlock">
I'm an inline-block div in a matrix
(Do something here with the pushed object...)
</div>
</s:push>
</s:iterator>
</s:iterator>
It is absolutely not thinkable to inline all that stuff, it would mean
<s:iterator begin="0" end="6" status="ctrDay">
<br/>
<s:iterator begin="0" end="23" status="ctrHour"><s:push value="%{days[#ctrDay.index].hours[#ctrHour.index]}"><div class="inlineBlock">
I'm an inline-block div in a matrix
(Do something here with the pushed object...)
</div></s:push></s:iterator>
</s:iterator>
That is not readable, hard to maintain and understand, etc.
The solution I found:
use HTML comments to connect the end of one div to the begin of the next one!
<s:iterator begin="0" end="6" status="ctrDay">
<br/>
<s:iterator begin="0" end="23" status="ctrHour"><!--
--><s:push value="%{days[#ctrDay.index].hours[#ctrHour.index]}"><!--
--><div class="inlineBlock">
I'm an inline-block div in a matrix
(Do something here with the pushed object...)
</div><!--
--></s:push><!--
--></s:iterator>
</s:iterator>
This way you will have a readable and correctly indented code.
And, as a positive side effect, the HTML source, although infested by empty comments,
will result correctly indented;
let's take the first example. In my humble opinion, this:
<div class="inlineBlock">
I'm an inline-block div
</div><!--
--><div class="inlineBlock">
I'm an inline-block div
</div>
is better than this:
<div class="inlineBlock">
I'm an inline-block div
</div><div class="inlineBlock">
I'm an inline-block div
</div>
Add display: flex; to the parent element. Here is the solution with a prefix:
Simplified version 👇
p {
display: flex;
}
span {
width: 100px;
background: tomato;
font-size: 30px;
color: white;
text-align: center;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
Fix with prefix 👇
p {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
span {
float: left;
display: inline-block;
width: 100px;
background: blue;
font-size: 30px;
color: white;
text-align: center;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
All the space elimination techniques for display:inline-block are nasty hacks...
Use Flexbox
It's awesome, solves all this inline-block layout bs, and as of 2017 has 98% browser support (more if you don't care about old IEs).
Are We Ready to Use Flexbox?
Using CSS flexible boxes - Web developer guide | MDN
A Complete Guide to Flexbox | CSS-Tricks
Flexy Boxes — CSS flexbox playground and code generation tool
Add comments between elements to NOT have a white space. For me it is easier than resetting font size to zero and then setting it back.
<div>
Element 1
</div><!--
--><div>
Element 2
</div>
This is the same answer I gave over on the related: Display: Inline block - What is that space?
There’s actually a really simple way to remove whitespace from inline-block that’s both easy and semantic. It’s called a custom font with zero-width spaces, which allows you to collapse the whitespace (added by the browser for inline elements when they're on separate lines) at the font level using a very tiny font. Once you declare the font, you just change the font-family on the container and back again on the children, and voila. Like this:
#font-face{
font-family: 'NoSpace';
src: url('../Fonts/zerowidthspaces.eot');
src: url('../Fonts/zerowidthspaces.eot?#iefix') format('embedded-opentype'),
url('../Fonts/zerowidthspaces.woff') format('woff'),
url('../Fonts/zerowidthspaces.ttf') format('truetype'),
url('../Fonts/zerowidthspaces.svg#NoSpace') format('svg');
}
body {
font-face: 'OpenSans', sans-serif;
}
.inline-container {
font-face: 'NoSpace';
}
.inline-container > * {
display: inline-block;
font-face: 'OpenSans', sans-serif;
}
Suit to taste. Here’s a download to the font I just cooked up in font-forge and converted with FontSquirrel webfont generator. Took me all of 5 minutes. The css #font-face declaration is included: zipped zero-width space font. It's in Google Drive so you'll need to click File > Download to save it to your computer. You'll probably need to change the font paths as well if you copy the declaration to your main css file.
2021 Solution
Unfortunately white-space-collapse is still not implemented.
In the meantime, give the parent element font-size: 0; and set the font-size on the children. This should do the trick
Two more options based on CSS Text Module Level 3 (instead of white-space-collapsing:discard which had been dropped from the spec draft):
word-spacing: -100%;
In theory, it should do exactly what is needed — shorten whitespaces
between 'words' by the 100% of the space character width, i.e. to
zero. But seems not to work anywhere, unfortunately, and this
feature is marked 'at risk' (it can be dropped from the specification, too).
word-spacing: -1ch;
It shortens the inter-word spaces by the width of the digit '0'. In a monospace font it should be exactly equal to the width of the space character (and any other character as well). This works in Firefox 10+, Chrome 27+, and almost works in Internet Explorer 9+.
Fiddle
Use flexbox and do a fallback (from suggestions above) for older browsers:
ul {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
Though, technically not an answer to the question:
"How do I remove the space between inline-block elements?"
You can try the flexbox solution and apply the code below and the space will be remove.
p {
display: flex;
flex-direction: row;
}
You can learn more about it on this link: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Simple:
item {
display: inline-block;
margin-right: -0.25em;
}
There is no need to touch the parent element.
Only condition here: the item's font-size must not be defined (must be equal to parent's font-size).
0.25em is the default word-spacing
W3Schools - word-spacing property
font-size:0; can be a bit trickier to manage...
I think the following couple lines is a lot better and more re-usable, and time saver than any other methods. I personally use this:
.inline-block-wrapper>.inline-block-wrapper,
.inline-block-wrapper{letter-spacing: -4px;}
.inline-block-wrapper>*{letter-spacing: 0;display: inline-block;}
/* OR better shorter name...*/
.items>.items,
.items{letter-spacing: -4px;}
.items>*{letter-spacing: 0;display: inline-block;}
Then you can use it as following...
<ul class="items">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
As far I as I know (I may be wrong) but all browsers support this method.
EXPLANATION:
This works (maybe -3px may be better) exactly as you would anticipate it to work.
you copy and paste the code (once)
then on your html just use class="items" on the parent of each inline-block.
You will NOT have the need to go back to the css, and add another css rule, for your new inline blocks.
Solving two issues at once.
Also note the > (greater than sign) this means that */all children should be inline-block.
http://jsfiddle.net/fD5u3/
NOTE: I have modified to accommodate to inherit letter-spacing when a wrapper has a child wrapper.
Generally we use elements like this in different lines, but in case of display:inline-block using tags in same line will remove the space, but in a different line will not.
An example with tags in a different line:
p span {
display: inline-block;
background: red;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
Example with tags in same line
p span {
display: inline-block;
background: red;
}
<p>
<span> Foo </span><span> Bar </span>
</p>
Another efficient method is a CSS job that is using font-size:0 to the parent element and give font-size to a child element as much as you want.
p {
font-size: 0;
}
p span {
display: inline-block;
background: red;
font-size: 14px;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
The above methods may not work somewhere depending on the whole application, but the last method is a foolproof solution for this situation and can be used anywhere.
I'm not pretty sure if you want to make two blue spans without a gap or want to handle other white-space, but if you want to remove the gap:
span {
display: inline-block;
width: 100px;
background: blue;
font-size: 30px;
color: white;
text-align: center;
float: left;
}
And done.
I had this problem right now and from font-size:0; I've found that in Internet Explorer 7 the problem remains because Internet Explorer thinks "Font Size 0?!?! WTF are you crazy man?" - So, in my case I've Eric Meyer's CSS reset and with font-size:0.01em; I have a difference of 1 pixel from Internet Explorer 7 to Firefox 9, so, I think this can be a solution.
p {
display: flex;
}
span {
float: left;
display: inline-block;
width: 100px;
background: red;
font-size: 30px;
color: white;
}
<p>
<span> hello </span>
<span> world </span>
</p>
I’ve been tackling this recently and instead of setting the parent font-size:0 then setting the child back to a reasonable value, I’ve been getting consistent results by setting the parent container letter-spacing:-.25em then the child back to letter-spacing:normal.
In an alternate thread I saw a commenter mention that font-size:0 isn’t always ideal because people can control minimum font sizes in their browsers, completely negating the possibility of setting the font-size to zero.
Using ems appears to work regardless of whether the font-size specified is 100%, 15pt or 36px.
http://cdpn.io/dKIjo
I think there is a very simple/old method for this which is supported by all browsers even IE 6/7. We could simply set letter-spacing to a large negative value in parent and then set it back to normal at child elements:
body { font-size: 24px }
span { border: 1px solid #b0b0c0; } /* show borders to see spacing */
.no-spacing { letter-spacing: -1em; } /* could be a large negative value */
.no-spacing > * { letter-spacing: normal; } /* => back to normal spacing */
<p style="color:red">Wrong (default spacing):</p>
<div class="">
<span>Item-1</span>
<span>Item-2</span>
<span>Item-3</span>
</div>
<hr/>
<p style="color:green">Correct (no-spacing):</p>
<div class="no-spacing">
<span>Item-1</span>
<span>Item-2</span>
<span>Item-3</span>
</div>
The simplest answer to this question is to add.
css
float: left;
codepen link: http://jsfiddle.net/dGHFV/3560/
With PHP brackets:
ul li {
display: inline-block;
}
<ul>
<li>
<div>first</div>
</li><?
?><li>
<div>first</div>
</li><?
?><li>
<div>first</div>
</li>
</ul>
I'm going to expand on user5609829's answer a little bit as I believe the other solutions here are too complicated/too much work. Applying a margin-right: -4px to the inline block elements will remove the spacing and is supported by all browsers. See the updated fiddle here. For those concerned with using negative margins, try giving this a read.
The CSS Text Module Level 4 specification defines a text-space-collapse property, which allow to control the how white space inside and around an element is processed.
So, regarding your example, you would just have to write this:
p {
text-space-collapse: discard;
}
Unfortunately, no browser is implementing this property yet (as of September 2016) as mentioned in the comments to the answer of HBP.
I found a pure CSS solution that worked for me very well in all browsers:
span {
display: table-cell;
}
Add white-space: nowrap to the container element:
CSS:
* {
box-sizing: border-box;
}
.row {
vertical-align: top;
white-space: nowrap;
}
.column{
float: left;
display: inline-block;
width: 50% // Or whatever in your case
}
HTML:
<div class="row">
<div class="column"> Some stuff</div>
<div class="column">Some other stuff</div>
</div>
Here is the Plunker.
There are lots of solutions like font-size:0,word-spacing,margin-left,letter-spacing and so on.
Normally I prefer using letter-spacing because
it seems ok when we assign a value which is bigger than the width of extra space(e.g. -1em).
However, it won't be okay with word-spacing and margin-left when we set bigger value like -1em.
Using font-size is not convenient when we try to using em as font-size unit.
So, letter-spacing seems to be the best choice.
However, I have to warn you
when you using letter-spacing you had better using -0.3em or -0.31em not others.
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: inherit;
cursor: auto;
}
.nav {
width: 260px;
height: 100px;
background-color: pink;
color: white;
font-size: 20px;
letter-spacing: -1em;
}
.nav__text {
width: 90px;
height: 40px;
box-sizing: border-box;
border: 1px solid black;
line-height: 40px;
background-color: yellowgreen;
text-align: center;
display: inline-block;
letter-spacing: normal;
}
<nav class="nav">
<span class="nav__text">nav1</span>
<span class="nav__text">nav2</span>
<span class="nav__text">nav3</span>
</nav>
If you are using Chrome(test version 66.0.3359.139) or Opera(test version 53.0.2907.99), what you see might be:
If you are using Firefox(60.0.2),IE10 or Edge, what you see might be:
That's interesting. So, I checked the mdn-letter-spacing and found this:
length
Specifies extra inter-character space in addition to the default space between characters. Values may be negative, but there may be implementation-specific limits. User agents may not further increase or decrease the inter-character space in order to justify text.
It seems that this is the reason.
Add letter-spacing:-4px; on parent p css and add letter-spacing:0px; to your span css.
span {
display:inline-block;
width:100px;
background-color:palevioletred;
vertical-align:bottom;
letter-spacing:0px;
}
p {
letter-spacing:-4px;
}
<p>
<span> Foo </span>
<span> Bar </span>
</p>
I thought I'd add something new to this question as although many of the answers currently provided are more than adequate & relevant, there are some new CSS properties which can achieve a very clean output, with full support across all browsers, and little to no 'hacks'. This does move away from inline-block but it gives you the same results as the question asked for.
These CSS properties are grid
CSS Grid is highly supported (CanIUse) apart from IE which only needs an -ms- prefix to allow for it to work.
CSS Grid is also highly flexible, and takes all the good parts from table, flex, and inline-block elements and brings them into one place.
When creating a grid you can specify the gaps between the rows and columns. The default gap is already set to 0px but you can change this value to whatever you like.
To cut it a bit short, heres a relevant working example:
body {
background: lightblue;
font-family: sans-serif;
}
.container {
display: grid;
grid-template-columns: 100px 100px;
grid-column-gap: 0; /* Not needed but useful for example */
grid-row-gap: 0; /* Not needed but useful for example */
}
.box {
background: red;
}
.box:nth-child(even) {
background: green;
}
<div class="container">
<div class="box">
Hello
</div>
<div class="box">
Test
</div>
</div>
Negative margin
You can scoot the elements back into place with negative 4px of margin (may need to be adjusted based on font size of parent). Apparently this is problematic in older IE (6 & 7), but if you don’t care about those browsers at least you can keep the code formatting clean.
span {
display: inline-block;
margin-right: -4px;
}
One another way I found is applying margin-left as negative values except the first element of the row.
span {
display:inline-block;
width:100px;
background:blue;
font-size:30px;
color:white;
text-align:center;
margin-left:-5px;
}
span:first-child{
margin:0px;
}

Vertical align a button in an absolute positioned div

Usually I vertically center a button, inside an absolute positioned div with top:50%, and margin-left:-(height/2), but today I realised it's not perfect, or I don't know how to use it correctly.
For example I did 2 examples. In the first example the <a> tag is an inline element, in the second example it's a block element. The positioning with block element is perfect, but unfortunately the width is 100%.
Please explain that why is the second example works well, with display:block;?
I'm really interested in your cross browser solution. How do you do this simple stuff?
Here is my css:
.container {
height:240px;
position:relative;
}
.box {
width:200px;
height:100%;
position:absolute;
left:0;
top:0;
background:yellow;
text-align:center;
padding:20px;
}
#example2 { left: 250px; }
.btn {
display:inline-block;
padding:5px 12px;
line-height:34px;
color:#fff;
background:red;
position:relative;
top:50%;
margin-top:-17px;
}
#example2 .btn { display:block; }
..and html
<div class="container">
<div id="example1" class="box">
button
</div>
<div id="example2" class="box">
button
</div>
</div>
The online version is available at http://jsfiddle.net/79hqgabq/2/
The alignment issue you see is being caused by the margin-top: -17px line as seen by this updated fiddle with that line removed on the .btn class: Updated Demo
On another note, vertical alignment is notoriously problematic with old CSS selectors, and it would be a good idea to transition over to Flexbox unless you needed to support older browsers.
Here's your problem with the new display: flex selector and corresponding sub-selectors: Demo w/ Flexbox. This removes the emphasis on having to pixel fudge to get proper vertical alignment.
New .box class
.box {
width:200px;
height:100%;
position:absolute;
left:0;
top:0;
background:yellow;
text-align:center;
padding:20px;
/* New lines for alignment */
display: flex;
align-items: center;
justify-content: center;
}
New .btn class (just removed the old tags)
.btn {
display:inline-block;
padding:5px 12px;
line-height:34px;
color:#fff;
background:red;
}
EDIT: After researching block elements have the property to expand to 100% of the parent container. This can be explained in more detail here. This is why display:block div expands until it reaches the padding on the .box class.
To answer why the display: inline-block element is slightly misaligned is because by default it is aligned on the baseline. See here for reference. Changing the vertical alignment of the div to be vertical-align: top will fix this.
Here is the new fiddle that uses all your previous syntax just with the added vertical-align: middle property.

How to correctly center website

im looking for center my website in the middle of the browser. This is my website:
http://marcosballester1.hol.es/test/test2.html
Left side is correctly centered, but right side no. Code:
<style>
#wrapper {
width: 50%;
margin: 0 auto;
}
</style>
<div id="wrapper">
<body>
HOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLAHOLA</body>
</div>
Both your original and the first answer are working as expected. See this JSFiddle
I added a background-color property to show you hide wide your div is. The div is centered on the page, but the text is overflowing outside of the box, because it is one continuous word and your browser doesn't want to break up a single continuous word. If you had multiple words in that box such as hello hello hello ... that wouldn't be an issue, because they would automatically wrap.
If you want to force your text to wrap you can use the word-wrap: break-word property, as shown in this example.
You have a error
you must put body before a div
.wrapper {
width: 900px;
margin: auto; //to center div
}
<div class="wrapper">
Hello
</div>

Flexslider Slide Width Issue

I have a strange issue using flexslider. The Slide LIs don't get the correct width so that all slides are shown. This only occurs on first page load. As soon as I switch to another tab and switch back everything looks fine. Maybe a JS Loading Problem?!
Screenshot: here
flexslider.css
.flexslider {margin: 0; padding: 0;}
.flexslider .slides > li {text-align: center; display: none; -webkit-backface-visibility: hidden;} /* Hide the slides before the JS is loaded. Avoids image jumping */
.flexslider .slides li.flex-active-slide img {text-align:center; width: auto; -webkit-border-radius: 6px; -moz-border-radius: 6px; -o-border-radius: 6px; border-radius: 6px; box-shadow: 0 1px 4px rgba(0,0,0,.2); -webkit-box-shadow: 0 1px 4px rgba(0,0,0,.2); -moz-box-shadow: 0 1px 4px rgba(0,0,0,.2); -o-box-shadow: 0 1px 4px rgba(0,0,0,.2); }
JS
$(window).load(function() {
$('.flexslider').flexslider({
controlNav: true,
directionNav: true
});
});
I've tried it with $(document).ready(function() too, but still the same problem.
Any ideas?
Well, Werner.
I ended up with your solution. Unfortunately.
This is otherwise another way to do it.
/* SLIDERS in content */
$('.flexslider').flexslider({
animation: "slide",
start: function(slider){
$('.flexslider').resize();
}
});
The reason I chose not to use the above code is that there was a delay and made a little jump when the slides updated to the correct width.
If anyone has a cleaner solution - please tell me. I am using the latest flexslider there is - and this issue only seems to occur sometimes. But when it does... arghhh.
I had the same problem.
At first load in a session, Flexslider get the width of the slider container which, if it is not explicit, has value 0px.
Then slides have width 0px and you can't see them.
To avoid this problem is sufficient to fix a width in slider container, in my case 604px:
<div class="flexslider" style="width: 604px">
<ul class="slides">
<li>
<img id="slide1" />
</li>
<li>
<img id="slide2" />
</li>
</ul>
</div>
I faced the same issue with flexi slider.
Tried above mentioned solutions, but it didn't work for me.
I tried to updated css with following code as turn-around for this issue.
.flexslider .slides {
zoom: 1.1 !important;
}
It fixed the problem. You can try this code and let me know if it worked for you or not.
This code/order below may help; well at least I have had success with it.
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/jquery.easing.js"></script>
<script defer src="js/jquery.flexslider-min.js"></script>
<script>
$(document).ready(function(){
$('.flexslider').flexslider({
controlNav: true,
directionNav: true,
});
});
</script>
I resolved this issue by removing minItems in my flexslider call. Seems to work with maxItems though.
I found out why it was doing this, and it is pretty simple.
It is because of CSS!!! on the ul in the slider.
take off the paddings and the margins on the ul.slides
.slides{
padding:0;
margin:0;
}
after i did THIS the resizing worked perfect every time on all browsers and mobile.
For me, the problem was the markup. I had this:
<div class='carousel-slides'>
<div class='carousel-slide'></div>
<div class='carousel-slide'></div>
</div>
$('.carousel-slides', context).flexslider({
selector: '.carousel-slide'
});
... but it seemed that it needed to be applied to an inner div on each slide, which is ridiculous.
<div class='carousel-slides'>
<div class='carousel-slide'>
<div class='carousel-slide-inner'></div>
</div>
<div class='carousel-slide'>
<div class='carousel-slide-inner'></div>
</div>
</div>
$('.carousel-slides', context).flexslider({
selector: '.carousel-slide > .carousel-slide-inner'
});
Maybe it's a bit late for another answer, but after trying all the answers here and in other forums, I ended up with my own solution, similar to others, but at least I find it pretty coherent and clean.
I had the problem in mobile environment, where the slider is inside a tab that is opened as soon one clicks on the title. The content was in display:none, and this causes the problem to flexslider (v. 2.5), that is not able to determine the correct width of the contained slides.
So i corrected my css and put the container hidden by playng with visibility and opacity.
Html sampl
<dd class="tab-container with-slider">
<div class="flexslider">
<ul class="slides">
<li>whatever </li>
</ul>
</div>
</dd>
Css:
/*all other containers behave well with display none/block so I leave them with normal behavior*/
.parent dd.tab-container {
display: none; position: relative;
}
/*container with flexslider inside uses visibility and opacity to be hidden*/
.parent dd.tab-container.with-slider{
display: block;
height: 0px;
visibility: hidden;
opacity: 0;
}
.parent.accordion-open dd.tab-container.with-slider{
height: auto;
visibility: visible;
opacity: 1;
}
Js is straighforward from the basic example, nothing more.
$j('.myTabOpener').click(function () {
var mySlider = $j(this).find('.flexslider');
mySlider.flexslider({
animation: "slide",
animationLoop: false,
itemWidth: 210,
itemMargin: 5,
minItems: getGridSize(),
maxItems: getGridSize()
});
});

Divs in line - align to bottom

I have a problem with div positioning in my form. My page contains a sheet. With div layout as below.
In divs on the left side, there are description of the fields. (they share the same style class)
In divs on the right side, there are the fields. (they share the same style class)
After validation my page look like this:
But I want it to look like this:
Honestly, I thought how do deal with it, for quite a white, and I simlpy have no idea what to do it. My page is almost ready so I'd like to fix that with possible at low cost.
[edit1]:
My current css look simple, something like this:
div_on_left{
clear: both;
float: left;
width: 440px;
padding-top: 5px;
padding-bottom: 8px;
}
div_on_right{
float: left;
width: 500px;
padding-top: 3px;
padding-bottom: 6px;
}
[edit2]:
I have just found one solution (posted below), but I don't like it. It will crash if context of divs on the left is too big. That's due to
position:absolute;
So I'd like to avoid this property.
<html>
<head>
<style type="text/css">
.row
{
position:relative;
}
.left
{
font-size:100%;
position:absolute;
left:0px;
bottom:0px;
}
.right
{
font-size:200%;
position:relative;
left:150px;
bottom:0px;
}
</style>
</head>
<body>
<div class="row">
<div class="left">Left_1</div>
<div class="right">Right_1</div>
</div>
<div class="row">
<div class="left">Left_2</div>
<div class="right">Right_2</div>
</div>
<div class="row">
<div class="left">Left_3</div>
<div class="right">Right_3</div>
</div>
</html>
It have to be a common problem. How do you deal width forms with validation that apear over the field boxes?
There's a solution for your problem but it involves a table-cell layout. The layout must have a row and two inner cells aligned to the bottom.
Here is a JSFiddle Example: http://jsfiddle.net/cvbLC/
I'm not aware of which browser support you are needing, but here is more information about this matter: http://www.quirksmode.org/css/display.html

Resources