Is it possible to do this using css (and how)?
------------------------------------------
| ---------- ---------- ---------- |
| | Child 1 | | Child 3 | | Child 5 | |
| ---------- ---------- ---------- |
| ---------- ---------- ---------- |
| | Child 2 | | Child 4 | | Child 6 | |
| ---------- ---------- ---------- |
| ---------- |
| | Child 7 | |
| ---------- |
| ---------- |
| | Child 8 | |
| ---------- |
------------------------------------------
using the following :
<div class="parent">
<div class="child">Child1</div>
<div class="child">Child2</div>
<div class="child">Child3</div>
<div class="child">Child4</div>
<div class="child">Child5</div>
<div class="child">Child6</div>
<div class="child">Child7</div>
<div class="child">Child8</div>
</div>
.parent {width:100%}
.parent div {width:100px; margin:2px;}
Edit:
Maybe I didn't explain clearly what i want...so
There can be more than 8 children.....but always an even number (10,12,14 etc)
A div with an even number must be always under his preceding odd div (2 under 1, 4 under 3....8 under 7, 10 under 9)
When parent's width is not enough to hold pairs of childer: it expands its height (like starts a new line)
Edit2:
The correct result is here:
http://jsfiddle.net/97ZpN/3/
but in this solution i had to put every pair of divs into a sub-container. Is it posiible to do it with the original html?
hope it will help you
.parent {width:100%}
.parent div {float:left; margin:2px;width:30%}
.parent div.child:nth-child(7),.parent div.child:nth-child(8){
float:none;
clear:both;
}
demo: http://jsfiddle.net/97ZpN/
Explanation:
.parent div {float:left; margin:2px;width:30%}
This line in the CSS will make all the child elements to float towards the left. So the elements will automatically stack one after other.
.parent div.child:nth-child(7),.parent div.child:nth-child(8){
float:none;
clear:both;
}
The clear:both does the trick here. clear:both means there cannot be any elements on the left or right side of the referenced element.
Related
Let's as is that I have a bootstrap modal, in the body of the modal. I have two <div> the <div> from the left contains an image while the <div> from the right contains the caption of the image.
Target Output!
Let's assume that this is a bootstrap modal body:
-------------- ----------------
| | | |
| | |Name: |
| <image> | |Description: |
| | |Price: |
| | | |
-------------- ----------------
Nevermind of the space in between the two divs, let's just assume that it is just a small space. So the question is how can I put two <div> in a same alignment where the first <div> is on the left and the other one is on the right?
As of now, this is what I have done.
---------------
| |
| |
| <image> |
| |
| |
---------------
---------------
| |
| |
|Name: |
|Description: |
|Price: |
| |
---------------
The other <div> goes down instead to the right.
Here is my code for the above wrong output.
CSS
#divforimg{
position: left;
}
#divforinfo {
position: right;
}
HTML
<div class="modal-body">
<div id = 'divforimg'>
<img style="height:50%; width:50%;" id = 'appimage'>
</div>
<div id = "divforinfo">
<i><p id = "appdesc"></p></i>
<strong>Price: </strong><label id = "appprice"></label><br>
<strong>Brand: </strong><label id = "appbrand"></label><br>
<strong>Color: </strong><label id = "appcolor"></label><br>
<strong>Model: </strong><label id = "appmodel"></label><br>
<strong>Available Quantity: </strong><label id = "appqty"></label><br>
<strong>Date Posted: </strong><label id = "appposted"></label><br>
</div>
</div>
Nevermind of the image source above, and other fields included. Thank you!
Should be a simple matter of giving them both a width and floating them:
CSS
#divforimg{
width:200px;
float:left;
}
#divforinfo {
width:200px;
float:left;
}
To get it more like your example, change the second to float:right; and it'll stick to the right side of its container.
I use bootstrap to design my website. And I try to do like below motion with layouts. How can I do this?
when size is big;
-------------------------
| A(md-8) | B(4) |
| |--------|
| | C(4) |
| |--------|
| |
----------------
when size is small;
----------------
| B |
----------------
| A |
----------------
| C |
----------------
In HTML put the divs in order B,A,C and give a class say pull-right-lg to B and C for floating it right. Target this class only for large devices using media queries.
HTML
<div class="container">
<div class="row">
<div class="col-md-4 pull-right-lg">B</div>
<div class="col-md-8">A</div>
<div class="col-md-4 pull-right-lg">C</div>
</div>
</div>
CSS
#media (min-width: 992px) {
.pull-right-lg {
float: right;
}
}
See Fiddle
I have a page in which I have a structure of divs in an inline block. Now each of the block divs have a minimum size and when the window size reduces to a size smaller than the sum of all the inner divs minimum width, I move some of the elements to the next line. I would now like a padding to be added to the div which moves to the next line alone and I am not looking to use js to achieve this. How can I do this in just CSS?
<div width="100%">
<div style="display:inline-block; width=33%; max-width=300px; min-width=135px">
<div style="display:inline-block; width=33%; max-width=300px; min-width=135px">
<div style="display:inline-block; width=33%; max-width=300px; min-width=135px">
</div>
Rendering
------------------- ------------------- -------------------
| | | || |
| | | || |
| div1 | | div 2 || div 3 |
| | | || |
------------------- ------------------- -------------------
constrained space
------------------- -------------------
| | | |
| | | |
| div1 | | div 2 |
| | | |
------------------- -------------------
<^margin 5px inserted^>
-------------------
| |
| |
| div 3 |
| |
-------------------
You can use media queries to apply CSS rules based on the width of the viewport.
#media all and (max-width: _width_value_) {
/* When the screen size is less than or equal to _width_value_ the css rules here will apply */
}
Here's a jsfiddle offering a solution to your original question.
margin-bottom will do the trick, however, it'll be there even when all of the divs are on one line. There's no other way without the JavaScript imho:
.innderDiv {
display:inline-block;
width: 33%;
max-width: 300px;
min-width: 135px;
border: 1px dashed;
margin-bottom: 5px;
}
http://jsfiddle.net/ruslans/KNDFE/
I have a list that looks like this
--------------------
| | |
| 1 | 2 |
| | |
| 3 | 4 |
| | |
| 5 | 6 |
| | |
--------------------
(it's a simple <ul> with <li>'s)
the container of this list, let's call it div.wrap has a fixed width like 400 pixels, and the list items are floated to left with 50% width.
How can I add a 10 pixel spacing between the left and right list items, without screwing up the layout?
Note that I have no control over the HTML from within the list, so I can't add any classes to these list items :(
I tried with margin-right: 10px on the <li>'s and margin-right: -10px on the <ul> but that doesn't work :)
An example with margin-right.
edit
If you want to hide second margin, you can make ul a little bit bigger than its wrap and hide overflow:
http://jsfiddle.net/YBy2K/3/
Not terribly elegant, but simple enough.
Unfortunately I have to support IE7 (and preferably IE6)
In IE8, Safari, Firefox, Chrome, I get a perfectly good layout ujsing an outer div to enlose two boxes.
------------------------------------
| |
| -------------- ----------- |
| | | | | |
| | A | | B | |
| | | | | |
| -------------- ----------- |
| |
------------------------------------
I'm using inline-block to style A and B. A is floated left, B right. Both boxes have internal divs and other elements.
However, when I use IE6 and IE7 I get this monstrosity.
------------------------------------
| |
| -------------- |
| | | |
| | A | |
| | | |
| -------------- |
| ----------- |
| | | |
| | B | |
| | | |
| ----------- |
| |
------------------------------------
Any definitive answers to what is going on and how to solve it?
Firstly, put a DOCTYPE at the top of your document. This forces IE into standards compliant rather than quirks mode (both euphemisms). For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
Secondly, if you want IE6 compatibility use floats (Andrew is quite correct in stating that display: inline-block only works in IE7 on elements with natural display: inline and <div> has natural display: block). This should work:
<div id="outer">
<div id="left"></div>
<div id="right"><>/div>
</div>
with:
#outer { overflow: hidden; }
#left { float: left; }
#right { float: right; }
as long as the widths of left and right are less than the width of outer including padding, borders and margins. If not, right will drop down to the next line.
In IE 6 and 7 inline-block works only on elements that have a natural display: inline. Are your boxes <div>s? If yes, it should work.. Do you have a test case? (See quirksmode.org for more info!)
IE block level element inline-block hack: this may be useful for you