Floating div won't align to the left in a responsive design - html

I have ASP:REPEATER (dynamic list) with floating divs to the left that looks like this:
+---------------------+ +---------------------+ +---------------------+
| | | | | |
| | | | | |
| | | | +---------------------+
| | | | +---------------------+
+---------------------+ +---------------------+ | |
| |
| |
| |
+---------------------+
+---------------------+ +---------------------+ +---------------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+---------------------+ +---------------------+ +---------------------+
But the 4th div is supposed to apear in another line no matter the height of the 3rd one. The problem is that I have a responsive design so I can't give it a specific height. It should be looking like this:
+---------------------+ +---------------------+ +---------------------+
| | | | | |
| | | | | |
| | | | +---------------------+
| | | |
+---------------------+ +---------------------+
+---------------------+ +---------------------+ +---------------------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+---------------------+ +---------------------+ +---------------------+
+---------------------+
| |
| |
| |
| |
+---------------------+
Here's the class of each div:
.wrapProducts
{
float: left; width: 30%;
margin: 0% 3% 5% 0%;
}
How can I fix it?
Thanks a lot!

You can't use float in this situation, what is happening is the normal behavior of float. Read this CSS-Tricks article for more infos.. You can clear every third elements but then your layout won't be responsive anymore.
You only have two responsive solutions to achieve your list layout:
You can either use a table layout with table, tr and td, or you can use inline-block divs instead of floating divs.

Try using clear:both on the styling of the final div.
If you remove the final div do you otherwise achieve your design?

Related

Dynamic positioning based on screen width and window width only

I've trawled through various 'solved' dynamic positioning queries but cannot find anything that helps.
I have the following page...
+-----------------------------------------------------------------------+
| +-----------+ +------------------+ +------------------+ +-----------+ |
| | IMG 1 | | | | | | IMG 4 | |
| | width:20% | | IMG 2, width:30% | | IMG 3, width:30% | | width:20% | |
| | aspect | | aspect ratio 4:3 | | aspect ratio 4:3 | | aspect | |
| | ratio | | | | | | ratio | |
| | 3:4 | +------------------+ +------------------+ | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
| +-----------+ +-----------+ |
| | IMG 5 | | IMG 6 | |
| | width:20% | | width:20% | |
| | aspect | | aspect | |
| | ratio | | ratio | |
| | 3:4 | | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
| +-----------+ +-----------+ |
| | IMG 7 | | IMG 8 | |
| | width:20% | | width:20% | |
| | aspect | | aspect | |
| | ratio | | ratio | |
| | 3:4 | | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
+-----------------------------------------------------------------------+
(1) I want the images to resize based on the window width only, i.e. if the user resizes the window vertically I don't want the images resized.
(2) Also, I only want this resizing to happen until the window is 50% of the screen width, then they will remain the same size, i.e. IMG 1 width would remain at 10% of the screen width.
I can achieve (1) for the top row. The problem is for IMGs 5 -8 since the 'top' value is calculated from the window height.
Is there some way I can achieve what I want dynamically?
I can't use calc() since it bases its result for top based on window height.
Also, there appears to be no CSS for screen.width and JS is not dynamic for resizing.
Any help would be greatly appreciated. (further images are below might help explain what I mean)
magicĀ²
Full Screen
+-----------------------------------------------------------------------+
| +-----------+ +------------------+ +------------------+ +-----------+ |
| | IMG 1 | | | | | | IMG 4 | |
| | width:20% | | IMG 2, width:30% | | IMG 3, width:30% | | width:20% | |
| | aspect | | aspect ratio 4:3 | | aspect ratio 4:3 | | aspect | |
| | ratio | | | | | | ratio | |
| | 3:4 | +------------------+ +------------------+ | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
| +-----------+ +-----------+ |
| | IMG 5 | | IMG 6 | |
| | width:20% | | width:20% | |
| | aspect | | aspect | |
| | ratio | | ratio | |
| | 3:4 | | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
| +-----------+ +-----------+ |
| | IMG 7 | | IMG 8 | |
| | width:20% | | width:20% | |
| | aspect | | aspect | |
| | ratio | | ratio | |
| | 3:4 | | 3:4 | |
| | | | | |
| +-----------+ +-----------+ |
+-----------------------------------------------------------------------+
width: 50% height:100%
+-------------------------------------------+
| +------+ +---------+ +---------+ +------+ |
| |IMG 1 | | IMG 2 | | IMG 3 | |IMG 4 | |
| | | | | | | | | |
| | | +---------+ +---------+ | | |
| +------+ +------+ |
| +------+ +------+ |
| |IMG 5 | |IMG 6 | |
| | | | | |
| | | | | |
| +------+ +------+ |
| +------+ +------+ |
| |IMG 7 | |IMG 8 | |
| | | | | |
| | | | | |
| +------+ +------+ |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+-------------------------------------------+
width: 50% height: 50%
+-------------------------------------------+
| +------+ +---------+ +---------+ +------+ |
| |IMG 1 | | IMG 2 | | IMG 3 | |IMG 4 | |
| | | | | | | | | |
| | | +---------+ +---------+ | | |
| +------+ +------+ |
| +------+ +------+ |
| |IMG 5 | |IMG 6 | |
| | | | | |
| | | | | |
| +------+ +------+ |
| +------+ +------+ |
| |IMG 7 | |IMG 8 | |
+-------------------------------------------+
width: 33% height: 50%
+-------------------------+
| +------+ +---------+ +--|
| |IMG 1 | | IMG 2 | | |
| | | | | | |
| | | +---------+ +--|
| +------+ |
| +------+ |
| |IMG 5 | |
| | | |
| | | |
| +------+ |
| +------+ |
| |IMG 7 | |
+-------------------------+
So this method isn't perfect, but in order to do this you need a value that represents the size of the screen without using %. In CSS there are 2 values you can use, these are vw and vh, for your purposes you will want vw for the width of the viewport.
vw is equal to the viewport width / 100
vh is equal to the viewport height / 100
These values are dynamic, if the window is resized so are these, so in order to keep constant proportions you can use either vw or vh, here's an example.
.square
{
width: 2vw;
height: 2vw;
background-color: lightblue;
position: relative;
top: 5vh;
left: 5vw;
}
<div class="square">
</div>
You could do more advanced calculations by working out the aspect ratio of the screen, but you get the idea.
Found what I required, it was the javascript onresize event.
<html>
<head>
</head>
<body onresize="myFunction()">
<span id="demo"><img src="img1.jpg" width="20%"></span>
<script>
function myFunction() {
if (window.innerWidth > (screen.width/2))
{
var txt = "<img src='img1.jpg' width='20%'>";
} else {
var txt = "<img src='img1.jpg' width='";
txt = txt + screen.width/10;
txt = txt + "'>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
This resizes the image # 20% window.innerWidth until window.innerWidth <= screen.width, then the image size remains the same # 10% screen.width.

Make div pull to the right and up with Bootstrap

I made a layout with Bootstrap. At desktop resolutions it looks like this:
+---------------+ +-------------+
| | | |
| A | | |
| | | |
+---------------+ | |
| | | C |
| B | | |
| | | |
+---------------+ | |
| | | |
| | +-------------+
| | xxxxxxxxxxxxxxx
| D | xxxxxxxxxxxxxxx
| | xxxxxxxxxxxxxxx
| | xxxxxxxxxxxxxxx
+---------------+ +-------------+
| | | |
| E | | F |
| | | |
+---------------+ +-------------+
| |
| G |
| |
+-------------+
On mobile devices it looks like this:
+---------------+
| |
| A |
| |
+---------------+
| |
| B |
| |
+---------------+
| |
| |
| C |
| |
| |
+---------------+
| |
| |
| |
| D |
| |
| |
+---------------+
| |
| E |
| |
+---------------+
| |
| F |
| |
+---------------+
| |
| G |
| |
+---------------+
My problem is that I need to get rid of the space between C and F indicated by the X's.
Here is my markup:
http://pastebin.com/KTJj72Z0
on div F put negative top margin. It'd pull it above.
like so
#media(min-width:768px){
#F {
margin-top: -100px;
}
}

How to spacing between images without any space with their container

I'm trying to create a div with images inside it, and there is space between them. For example, I want the space between them is 2px, I can use margin:1px; so it will become 2px when one's left-margin meet other's right-margin, same for top and bottom. But there is also space between image and div's border, so the div will become like this:
+------------------------------------------+
| |
| +---------------+ +---------------+ |
| | | | | |
| | | | | |
| | | | | |
| | IMG1 | | IMG2 | |
| | | | | |
| | | | | |
| | | | | |
| +---------------+ +---------------+ |
| DIV |
| |
| +---------------+ +---------------+ |
| | | | | |
| | | | | |
| | IMG3 | | IMG4 | |
| | | | | |
| | | | | |
| | | | | |
| +---------------+ +---------------+ |
| |
+------------------------------------------+
When what I'm actually trying to do, is this:
+---------------+---+---------------+
| | | |
| | | |
| | | |
| IMG1 | | IMG2 |
| | | |
| | | |
| | | |
+---------------+ +---------------+
| DIV |
| |
+---------------+ +---------------+
| | | |
| | | |
| IMG3 | | IMG4 |
| | | |
| | | |
| | | |
+---------------+---+---------------+
How to do this, without set style for every single image?
If adding classes or IDs is not an option and you do not want to style the images individually. Then you could try using img:nth-child(n) or img:nth-of-type(n).
First assign a right margin to every odd image:
img:nth-of-type(odd){
margin-right: 1px;
}
Then a left margin to every even:
img:nth-of-type(even){
margin-left: 1px;
}
And finally a top margin to every image but the first two:
img:nth-of-type(n+3){
margin-top: 2px;
}
You could also leave out one of the left or right margins and increase the one you leave in... thereby reducing the size of your styles a little.
Here an example: jsfiddle. In this example the container div has a set width of two images plus margins, and the images are floating left within.
jsfiddle example
You can set your bottom margins and align your left column images with the float property in your CSS:
img {
margin-bottom: /*your margin*/;
float: left;
}
And then move the right column images to the right of the div container:
nth-child(even) {
float: right;
}
give all images a same class & define padding for it
try this
<div>
<img src="1.jpg" class="myimage"/>
<img src="2.jpg" class="myimage"/>
<img src="3.jpg" class="myimage"/>
<img src="4.jpg" class="myimage"/>
</div>
CSS
.myimage {
display:block;
padding:2px 2px 0 0;
}

How to (y) align divs above the bottom of previous line?

I'm trying to create a column layout by filling in fixed-width divs inside a width:100% container div, letting them "wrap" when there is no room left in the container, thus creating the columns. To change the # of columns, I just change the width of the contained divs. Those contained divs can be pretty tall..
Here is what I have now:
+------------------------------+
|+----+ +----+ +----+ +----+ |
|| | | | | | | | |
|| | | | | | | | |
|| | | | | | | | |
|+----+ +----+ +----+ | | |
| | | |
| | | |
| | | |
| +----+ |
|+----+ +----+ +----+ +----+ |
|| | | | | | | | |
|| | | | | | | | |
|| | | | | | | | |
|+----+ +----+ +----+ +----+ |
| |
See this big waisted space where the sized divs wrap and align on the bottom of the tallest div of the previous line? I guess that is how HTML's box-model works.
But in this very case, what I would like to obtain is this:
+------------------------------+
|+----+ +----+ +----+ +----+ |
|| | | | | | | | |
|| | | | | | | | |
|| | | | | | | | |
|+----+ +----+ +----+ | | |
|+----+ +----+ +----+ | | |
|| | | | | | | | |
|| | | | | | | | |
|| | | | | | +----+ |
|+----+ +----+ +----+ +----+ |
| | | |
| | | |
| | | |
| +----+ |
| |
Any idea?
Check out something like this, it's almost identical to Pinterest...
http://www.wookmark.com/jquery-plugin

What is the use of style="clear:both"?

I happened to see a div which had the style clear:both! What is the use of clear in style?
<div style="clear:both">
clear:both makes the element drop below any floated elements that precede it in the document.
You can also use clear:left or clear:right to make it drop below only those elements that have been floated left or right.
+------------+ +--------------------+
| | | |
| float:left | | without clear |
| | | |
| | +--------------------+
| | +--------------------+
| | | |
| | | with clear:right |
| | | (no effect here, |
| | | as there is no |
| | | float:right |
| | | element) |
| | | |
| | +--------------------+
| |
+------------+
+---------------------+
| |
| with clear:left |
| or clear:both |
| |
+---------------------+
Just to add to RichieHindle's answer, check out Floatutorial, which walks you through how CSS floating and clearing works.
When you use float without width, there remains some space in that row. To block this space you can use clear:both; in next element.