CSS Block float left - css

I have this html file
<div class="con" style="width:100px;height:200px;">
1
</div>
<div class="con" style="width:100px;height:200px;">
2
</div>
<div class="con" style="width:100px;height:400px;">
3
</div>
<div class="con" style="width:100px;height:400px;">
4
</div>
<div class="con" style="width:100px;height:100px;">
5
</div>
<div class="con" style="width:100px;height:100px;">
6
</div>
<div class="con" style="width:100px;height:100px;">
7
</div>
<div class="con" style="width:100px;height:100px;">
8
</div>
And I want with this html have this result :
But I write this css :
<style>
body {
width:440px;
height:440px;
}
.con{
float:left;
background:#bebebe;
margin:1px;
}
</style>
And I got this result! :-(
I repeat that I would have the result of the first image using only the html code that I wrote.

change the width of the body to 408px; then float:right; the div 3 & 4.
demo: http://jsbin.com/imire5
Updated demo with float left only: http://jsbin.com/imire5/2

Firstly, you shouldn't have multiple items with the same id. This is what class is for. id should be unique within the page.
The solution is to your problem is to float the two tall blocks to the right and everything else to the left.
This will of course only work if the boxes are in a container (ie the body) that is just the right width for all four, otherwise you'll just end up with a gap in the middle of your layout.
The trouble is that because you've got the same ID for everything, you can't easily specify which ones to float right and which ones to float left.
There are ways to do it, but the correct solution would be to use classes.
<div class="con" style="width:100px;height:200px;">
1
</div>
<div class="con" style="width:100px;height:200px;">
2
</div>
<div class="con tall" style="width:100px;height:400px;">
3
</div>
<div class="con tall" style="width:100px;height:400px;">
4
</div>
<div class="con" style="width:100px;height:100px;">
5
</div>
<div class="con" style="width:100px;height:100px;">
6
</div>
<div class="con" style="width:100px;height:100px;">
7
</div>
<div class="con" style="width:100px;height:100px;">
8
</div>
<style>
body {
width:408px;
height:440px;
}
.con {
float:left;
background:#bebebe;
margin:1px;
}
.tall {
float:right;
}
</style>
If you absolutely cannot (or will not) change the HTML code, there is still one other solution I could suggest - the CSS [attr] selector will allow you to specify a different CSS style for elements that have particular attributes. In this case, you could use it to pick out the tall blocks and float them right.
#con[style~='height:400px;'] {float:right;}
This will only affect the elements that have id="con" and where the style attribute includes height:400px;.
However please note that the [attr] selector does not work on older version of IE, so you may need to excersise caution if you decide to use it.
This solution also involves floating certain elements to the right, which you seem to be dead set against, but it is still the only viable CSS-only solution.

The basic issue you have is that CSS float works in a way that is different to how you want it to work.
Another answer to your problem might be to use Javascript to hack it.
There is a jQuery plug-in called Masonry which looks like it does what you're after. You can use float:left; for everything, and then use Masonry to close up the gaps.
It would mean relying on a solution that isn't entirely CSS-based though, which probably isn't ideal - especially since I've already given you a working CSS-only solution.
(also once you start using Javascript, you definitely need to fix your id vs class problem -- Javascript will disown you if you have all those elements with the same id)

Related

Non 12-divisible Equal columns on Bootstrap 4

I have a little web app that I want to show 5 columns responsive equal width.
But I only want this layout for a devices with ≥992px of width.
For devices <992px of width I want to show the 5 HTML elements in one full-width row.
Equal-width columns can be broken into multiple lines, but there was a
Safari flexbox bug that prevented this from working without an
explicit flex-basis or border.
Two workarounds have been documented in a reduced test case outside
Bootstrap, though if the browser is up to date this shouldn’t be
necessary.
Source: https://getbootstrap.com/docs/4.0/layout/grid/
So, I'm a bit confused in how can I achieve this responsive behaviour that I want using Bootstrap 4.
I have this "idea", but I think will pretty ugly, what do you think about it?
Let's consider this markup
<div class="container">
<div class="row">
<div class="col">Column</div>
<div class="sep"></div>
<div class="col">Column</div>
<div class="sep"></div>
<div class="col">Column</div>
<div class="sep"></div>
<div class="col">Column</div>
</div>
</div>
Then, with jQuery I can select .sep and add bootstrap4 class w-100 in the case of width <992px.
Thanks for reading and please forgive my bad english.
Maybe I don't understand the question. Why not just use the lg auto layout columns (col-lg)?
https://www.codeply.com/go/OohsSfM7Zu
<div class="row">
<div class="col-lg">
</div>
<div class="col-lg">
</div>
<div class="col-lg">
</div>
<div class="col-lg">
</div>
<div class="col-lg">
</div>
</div>
The first thing to remember about Bootstrap is that rows must contain 12 columns. If you have a row with a number that doesn't go into 12 (such as 5), you should be making use of offsets.
For example, 12 / 5 is 2, with 2 left over. So you want to make use of columns that occupy a width of 2, for a total of 10 columns. From here, you would offset by 1 on the left. Considering you now have a total of 11, you've automatically also offset by 1 on the right.
This can be demonstrated in the following:
.row {
margin: 0 !important; /* Prevent scrollbars in the fiddle */
text-align: center; /* Helps illustrate the centralisation */
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<div class="row">
<div class="col-sm-2 offset-sm-1">One</div>
<div class="col-sm-2">Two</div>
<div class="col-sm-2">Three</div>
<div class="col-sm-2">Four</div>
<div class="col-sm-2">Five</div>
</div>
If you're not happy with this offset, then you can simply make use of a custom media query such as width: calc(100% / 5) ...though this would completely violate the point of using Bootstrap; another framework might be more suitable :)
Hope this helps!

AngularJS: Move a div inside a placeholder div

I have the following html:
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
The output would be displayed as:
Div 1 Div 2
Div 3 Div 4
Div 5 Div 6
Given some information on page load Div 1 or Div 2 may be hidden. If Div 1 is hidden I want to move Div 2 to the placeholder for Div 1 and move Div 5 to the placeholder for Div 2. I always want Div 3 and Div 4 on the same line. Currently if I hide a div the next one moves up in its place causing Div 3 and Div 4 to be on two different lines. I have researched AngularJS and can't find a way to do this? Any thoughts?
UPDATE
<div id="placeholder1"></div> <div id="placeholder2"></div>
<div id="placeholder3"></div> <div id="placeholder4"></div>
<div id="placeholder5"></div> <div id="placeholder6"></div>
<div id="memid">Member ID</div> <div id="otherid">Other ID</div>
<div id="fName">First Name</div> <div id="lName">Last Name</div>
<div id="dob">DOB</div> <div id="otherInfo">Other Info</div>
Given the site the member id or other id may be hidden and I would like to move DOB to placeholder2 and OtherID to placeholder1 or leave member id in placeholder1 while keeping first and last name on the same row. I am also using bootstrap grid for the layout.
i dont understand, where is needed angular. The problem could be solved only with css:
div{
float:left;
width:50%;
}
Your best bet is likely going to be to use javascript to handle this. You can do this without Angular, though. You could manually move the elements around within the set of nodes containing the six divs. You could also try taking the html content from one div and moving it to another.
if (/*condition that hides div1*/){
Div1.html(Div2.html());
Div2.html(Div5.html());
Div5.hide();
}
You could also try applying css classes that manually place the elements on the page.

Bootstrap - resize specific column

I don't know how to make this kind of col 3 and 6 size.
Middle column has no padding, but it is not enough.
I was trying to make different sizes of col.
#media (min-width:992px){
.col-md-6 { width: 52,5641%;}
.col-md-3 { width: 23,7179%;}
}
but no success.
With Bootstrap you dont need to add media queries or your own width, just use the BS grid system (you can read more here) and let it handle all the tough work. Based on your picture a 3 column layout would use something like this:
<div class="row">
<div class="col-md-3">.col-md-3</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
Make sure you columns total 12 like above (3+6+3) If you need extra padding in between columns just add a nested DIV and apply the spacing you want to those.
<div class="row">
<div class="col-md-3">
<div class="myclass">
this will have extra padding
</div>
</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
.myclass {
padding: 20px;
}
Updated
Based on your comment if you want column 6 to be slightly larger than it is you will either need to expand that column and "shrink" the outer 2 columns to something like this:
<div class="row">
<div class="col-md-2">.col-md-2</div>
<div class="col-md-8">.col-md-8</div>
<div class="col-md-2">.col-md-2</div>
</div>
If that's not what you are going for then you can create your own table within bootstrap.
<div class="row">
<div class="custom-col">My custom left side</div>
<div class="custom-main">my main column</div>
<div class="custom-col">My custom right side</div>
</div>
Sizing each of the column as you need.
Maybe Bootstrap is not the best option for your problem. It works if only you can divide the screen in 12 equal parts. Rewrite this rule could break other stuff.
What about using flexboxes or other CSS framework more flexible?

Bootstrap: Class for margin-right?

I believe to provide standard margin-left we can use class "Offset" in bootstrap. At the same time what is the class that can be used to provide standard margin-right?
example:
<div class="row-fluid">
<div class="offset2 span8"></div>
</div>
for some reason I need to give margin-right equivalent to offset2. Some solution will be of great help. Thank you.
There is no equivalent class to offset-x for margin-right and for good reason, it is not needed. Think of it this way, if you need a 6 column div that is offset both right and left 3 columns, you would use:
<div class="row">
<div class="span6 offset3">Your content...</div>
</div>
Also, if you have a 6 column div that needs to only be offset 2 columns BUT, the offset should be 2 columns on the right, the code would be:
<div class="row">
<div class="span6 offset4">Your content...</div>
</div>
Keep in mind you are always working in 12 columns (unless changes in variables.less) so you can use span-x AND offset-x to achieve position desired. If you are looking to tweak additional pixels, add an additional class(or ID) to your content container inside of your span. For example:
<div class="row">
<div class="span6 offset4">
<div class="tweaked-margin">Your content...</div>
</div>
</div>
The CSS:
.tweaked-margin {
margin-right: 4px; // or whatever you need
}

2 column float wasted space

I have a container div, inside which I want to pack a variable number of divs of unknown (variable) height but with a given min-width. My requirements are:
If the container is wide enough to accommodate two columns, I want them to distribute themselves nicely in two columns without unnecessary whitespace.
It not, they should just go above each other.
Currently, I've given the divs width:48% margin-right:2%;float:left; which works nicely in the one-column state but when I resize the browser window, making room for two columns, every div which ends up in the left column insists on aligning itself horizontally with the bottom of the last div which went to the right:
what I have http://img602.imageshack.us/img602/5719/whatihave.png
This is how I would like them to go (no wasted space):
what I want http://img96.imageshack.us/img96/6985/whatiwantu.png
I would like a pure CSS solution if possible.
Thank you! /Gustav
EDIT:
This markup illustrates my problem:
<html>
<head>
<style type="text/css">
.box {
width: 48%;
min-width:550px;
margin-right:2%;
margin-bottom: 1.5em;
background:blue;
color:white;
height:180px;
float:left;
}
.tall {
height: 250px;
}
</style>
</head>
<body>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box tall">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div style="clear:both"/>
</body>
</html>
The .boxes are generated dynamically, and so are their heights, I just threw in one taller to illustrate.
I don't think you can achieve the desired effect with pure CSS. I've used jQuery Masonry to replicate the effect you're after and it worked really well.
I'd love to see a pure CSS solution for this but haven't seen anything come close yet.
I believe that if you have a div for each column into which you put the numbered divs you will get what you want. Something like this:
<div class="containerDiv">
<div class="column">
<div class="content">
1
</div>
<div class="content">
4
</div>
</div>
<div class="column">
<div class="content">
2
</div>
<div class="content">
3
</div>
</div>
</div>
The next step appears to be "how do I balance my columns". Some code somewhere is generating the boxes you mentioned. It is deciding on the height of each box. This code will need to generate a balanced list of boxes for each column prior to forwarding the request to the JSP for presentation. By balanced, I mean "the height of column1 is similar to the height to column2"

Resources