Position floated elements directly under each other [duplicate] - css

This question already has answers here:
CSS Floating Divs with different height are aligned with space between them
(3 answers)
Closed 9 years ago.
I would like to display a row of tow divs next to each other while in the next row, the next div sits directly under the last one. Like this:
Because the layout has to be built into an CMS, I can't put Box 1,3 and 2,4 in a separat div. Is there a way to achieve this kind of behavior without extra wrapping elements? (Normal float behavior doesn't work, display inline/inline-block also doesn't do the trick.) Or is some JavaScript required to build a layout like this?

Because of the different heights, this looks like the problem where I still haven't found a general purpose pure CSS technique to handle it properly, despite trying really hard.
I've posted this answer before: css alignment question
I've given up and used a jQuery plugin
to do this in the past for something
similar:
jQuery Masonry
A picture is worth a thousand words:

you can use the nth-child(odd) to clear the float:left;.
css
.box {height:100px;width:100px;float:left;}
.box:nth-child(odd) {clear:left;}
.green {background:green;}
.red{background:red;}
.blue {background:blue;}
.yellow {background:yellow;}
html
<div class="box green">BOX 1</div>
<div class="box red">BOX 2</div>
<div class="box blue">BOX 3</div>
<div class="box yellow">BOX 4</div>
Demo: http://jsfiddle.net/YSP2S/
Keep in mind that this will not work for internet explorer. You can use conditional comment and javascript to achieve the same for internet explorer also.

Related

when to use offset-3 in bootstrap css

i read a article from here http://victorshi.com/blog/post/How-to-make-a-div-center-in-Bootstrap3 that how to center a div when using bootstrap framework.
their code
<div class="row">
<div class="col-lg-6 col-lg-offset-3 text-center">center</div>
</div>
i just like to know why they use col-lg-offset-3 ? why they did not use col-lg-offset-2 or col-lg-offset-4 etc.
please guide me about the above code sample. thanks
col-lg-6 is a div with width 6 columns (out of 12). The col-lg-offset-3 pushes the div 3 columns to the right. In this case, the row displays a div of half width (6/12 columns) in the center of the div (3/12 pushes 6/12 to the middle).
All of this is documented very well here. I would encourage you to read the documentation and experiment before posting questions to stack overflow in the future.
Its like using margins in this case it adds a dynamic margin-left based on column width.

css align vertically specific divs

I currently have the following html:
<div>objA</div>
<div class="tower">objB</div>
<div class="tower">objC</div>
<div class="tower">objD</div>
<div>objE</div>
<div>objF</div>
<div>objG</div>
<div>objH</div>
<div>objI</div>
<div>objJ</div>
I'd like to tower(align vertically) the divs in class "tower" so that the following image can be reproduced:
Instead, what I currently have is the following:
http://jsfiddle.net/kAMB5/
Are there any ways in which I could go about achieving my wanted result purely through css? (preferably without changing the html content)
UPDATE: You can assume these are fixed width divs.
You need to put objC and objD divs inside objB div like this: DEMO to achieve desired result:
HTML:
<div>objA</div>
<div>
objB
<div class="tower">objC</div>
<div class="tower">objD</div>
</div>
<div>objE</div>
<div>objF</div>
<div>objG</div>
<div>objH</div>
<div>objI</div>
<div>objJ</div>
You need an extra div wrapping your .tower as in: http://jsfiddle.net/kAMB5/4/
Apart from that, CSS Grid Layout (IE10) or on your .tower CSS3 flexbox (still need a container I guess) could achieve the same result but with lesser compatibility

how to control div size when other divs are changing [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
CSS - Equal Height Columns?
I'm not the greatest with css, so my page layout looks like this:
<div style="width:80%;border:1px solid #000;">
<div style="float:left; width:33.3%; background:#aaa;">one</div>
<div style="float:left; width:33.3%; background:#bbb;">two</div>
<div style="float:left; width:33.4%; background:#ccc;">three</div>
<div style="clear:both;"></div>
</div>
div one has the side links and image
div two holds the main content
div tree has some ads div
now when the main content increases in size the other two stay the same height, so how do I get the height for all 3 divs to increase??
I used this recently for one of my websites.
http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
This worked great for me. you may have to reverse engineer the code a little to get your columns to the right widths for you.
I researched that topic some time ago, and as I remember, there no clear CSS way to do it...at least in CSS 2.0. I found some workarounds like background image, or playing with overflow. For myself, I choose to use java script to make same height all divs that I need.

Float:right divs appear on next line in IE only

Ok, so I'm working on a prototype of my UI before I start coding the webapp. I got the design mostly done while working in Firefox and (of course) when I tested it in IE, there were a lot of rendering issues. One of those issues is that if I have a div that contains some text and another div that's set to float:right, that nested div shows up on the next line, below its parent div. This is the problem markup in its simplest form...
<div style="background-color:red;">
Text
<div style="background-color:yellow; float:right;">Right</div>
</div>
I scoured the internet for solutions and the only working relevant solution I found that makes this work in IE is to place the floating div at the beginning of its parent like this...
<div style="background-color:red;">
<div style="background-color:yellow; float:right;">Right</div>
Text
</div>
In reality, the nested div has a class and my CSS is floating that class. But what happens if I eventually make another stylesheet to target mobile devices and I no longer want that inner div to be floated? Then the content itself would be out of order in HTML, just for the sake of accommodating a CSS issue in IE. Is there a better way to solve this?
A colleague of mine recently had a very similar problem. I recommended simply using positioning rather than floating. I believe you could do the same here:
<div style="background-color:red; position:relative;">
Text
<div style="background-color:yellow; position:absolute; right:0; top:0;">Right</div>
</div>
I don't know if you have a requirement to use floats or not. Using the positioning method will cause the positioned element to not take up space in normal flow, but otherwise keep the correct source order and visually accomplish what I think you want to do.
Set a width value on your inner div and make it display: inline-block. Div's are block elements that take 100% width of the parent, that's why IE puts it on the next line.
I am not sure if it is a possibility for you, but putting the text within the outer div in a div of its own seems to solve the problem
<div style="background-color:red;">
<div style="float: left;">Text</div>
<div style="background-color:yellow; float:right;">Right</div>
</div>
I just hit this problem in IE7 - in my case, the item that was going to clear the float was going to be full width anyway. I just set that to "float: none;clear: left" and it seems to work.

CSS Noob Could really use some Skilled help with layout and positioning [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Stacking Divs with CSS
Im kinda new to CSS have been using tables for my html for years, Im trying to figure out how I can nest divs or stack them inside the content section of a 3 column layout. with tables I'd just do a new TR but if I float another div into the content line line it will appear parallel or vertically to content, instead of under it. is there another way to do this or am I missing the point of Divs here?
<div id="header">Header</div>
<div id="leftcolumn">Left Column</div>
<div id="content">Content</div>
<div id="footer">Footer</div>
If you nest something inside #content and apply clear:both to it then it will appear beneath the content in #content.
See the following articles from the Opera Web Standards Curriculum:
Floats and clearing
CSS Static and relative positioning
CSS Absolute and fixed positioning
To have a floated element appear under another floated element, set clear: both; on that element.
You might be better asking this on http://doctype.com/.
You need to clear your floats by using the {clear:left} rule or simply remove the floats. Read up on the the difference between inline and block elements and how you can manipulate them using the display property.

Resources