Knockout foreach binding - 2 on a row - css

<div data-bind='foreach:images'>
<figure>
<img data-bind='attr:{src:src}' ></img>
<figcaption data-bind='text:name' contenteditable='true' ></figcaption>
</figure>
</div>
What this does is displays images vertically - since figure ( an fig caption are block elements).
What I am trying to achieve is this
1 2
3 4
5 6
..
..
i.e 2 images in each row.
I've tried a no. of things but could not some up with anything. Do I need to use knockout templates ?

Untested, but with the help of some CSS, you should be able to achieve it by using the index observable Knockout exposes within loops to get the current index:
CSS:
.container { overflow: hidden; }
.l { float: left; }
.clear { clear: left; }
Markup:
<div data-bind='foreach: images' class='container'>
<figure class='l'>
<img data-bind='attr: {src:src}'></img>
<figcaption data-bind='text: name' contenteditable='true'></figcaption>
</figure>
<!-- ko if: ($index() + 1) % 2 === 0 -->
<div class='clear'></div>
<!-- /ko -->
</div>
If your images are of a standard width then you could achieve the wrapping without the clear div by floating the figure tags as above, and setting the width of the container div to twice the image width.
I've created a JSFiddle showing the first method here.

Related

How to get paragraphs within a div to pop up side by side using flex?

I am trying to figure out how to get p tags within a div to pop up side by side and display somewhat like a table, but using flex instead of any floats.
<div class="summary">
<img src="life.jpg" alt="Life's great">
<div>
<div>
<p>Chapter 1:</p>
<p>0-10</p>
</div>
<div>
<p>Chapter 2:</p>
<p>11-20</p>
</div>
<div>
<p>Chapter 3:</p>
<p>21-30</p>
</div>
</div>
</div>
So the output should look something along these lines, except I will have borders and such to design a box around it. I have to use flex which is throwing me off.
Chapter 1: 0-10
Chapter 2: 11-20
Chapter 3: 21-30
Should just be as easy as applying display: flex on the container you want to have flex-laid out children within and giving a little margin. The snippet below lays out the markup in the manner you showed in your question. It uses terrible selectors, but I didn't want to change your HTML structure at all. However, I'd recommend putting classes on elements you wish to target so that you can avoid using element names as selectors.
.summary > div > div {
display: flex;
}
.summary > div > div > p:first-child {
margin-right: 10px;
}
<div class="summary">
<img src="life.jpg" alt="Life's great">
<div>
<div>
<p>Chapter 1:</p>
<p>0-10</p>
</div>
<div>
<p>Chapter 2:</p>
<p>11-20</p>
</div>
<div>
<p>Chapter 3:</p>
<p>21-30</p>
</div>
</div>
</div>

3 Scaling images inline

So, I created an image in photoshop that is 1920x1080 and I've spliced it into sections, so that the images that require a link can have an javascript tag attached to them for a link.
I want the images to scale depending on the type of monitor you're using, as long as you're in a 16:9 ratio. The problem I'm having, is in regards to the way the images align 3 in a row. Either the 3rd image in the row gets pushed down, for sections with 3 images. Or, the image is the full size 1920x1080, but no scaling and gaps between images.
My HTML looks like this:
<main>
<section>
<img src="images/NuclearFuelCycle1.png"/>
</section>
<section>
<div><img id="1" src="images/NuclearFuelCycle2.png"/><span style="cursor:pointer" onclick="openMining()"><img id="2" src="images/NuclearFuelCycle3.png"/></span><img id="3" src="images/NuclearFuelCycle4.png"/></div>
</section>
<section>
<img src="images/NuclearFuelCycle5.png"/></section>
<section>
<img src="images/NuclearFuelCycle6.png"/><span style="cursor:pointer" onclick="openConversion()"><img src="images/NuclearFuelCycle7.png"/></span><img src="images/NuclearFuelCycle8.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle9.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle10.png"/><span style="cursor:pointer" onclick="openEnrichment()"><img src="images/NuclearFuelCycle11.png"/></span><img src="images/NuclearFuelCycle12.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle13.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle14.png"/><span style="cursor:pointer" onclick="openReactor()"><img src="images/NuclearFuelCycle15.png"/></span><img src="images/NuclearFuelCycle16.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle17.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle18.png"/><span style="cursor:pointer" onclick="openSpentFuel()"><img src="images/NuclearFuelCycle19.png"/></span><img src="images/NuclearFuelCycle20.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle21.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle22.png"/><span style="cursor:pointer" onclick="openFuelFabrication()"><img src="images/NuclearFuelCycle23.png"/></span><img src="images/NuclearFuelCycle24.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle25.png"/></section>
<section>
<img src="images/NuclearFuelCycle26.png"/><span style="cursor:pointer" onclick="openStorageDisposal()"><img src="images/NuclearFuelCycle27.png"/></span><img src="images/NuclearFuelCycle28.png"/>
</section>
<section>
<img src="images/NuclearFuelCycle29.png"/>
</section>
</main>
My CSS looks like this:
* {
margin: 0;
padding: 0;
border: 0;
border-collapse: collapse;
}
body {
max-width: 100% !important;
height: auto;
}
main {
display: inline-block;
padding: 10px;
}
section div img{
width: auto;
}
Some of this code is not useful... It's me experimenting to try and create a solution. Any help would be greatly appreciated, I have to submit this in 2 days P
The 'spaces' problem is most likely due to the way display:inline works, which is what an image and a span is. Any newlines, etc get treated as a space char. An easy fix could be something that uses display:flex.
The second (and main part) of your problem is that you want to scale based on width. A little bit of javascript and use of transform: scale(...) fixes that. Below is a snippet (which can't show resizing), so here's a CodePen too: https://codepen.io/anon/pen/PpXpwO
window.onresize = resizer;
resizer();
function resizer() {
console.log("resized")
var _main = document.querySelector("main");
var image_width = 600; // the original image width
var ratio = _main.offsetWidth/image_width;
_main.style.transform = "scale("+ratio+")";
};
main {
transform-origin: 0 0;
}
section {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
section * {
flex: 0 0 auto;
display: block;
}
<main>
<section>
<img src="https://placehold.it/600x150g" />
</section>
<section>
<img id="1" src="https://placehold.it/100x150" />
<a style="cursor:pointer" onclick="openMining()"><img id="2" src="https://placehold.it/300x150/370000" /></a>
<img id="3" src="https://placehold.it/200x150" />
</section>
<section>
<img src="https://placehold.it/600x150g" />
</section>
</main>
If I'm understanding correctly your sliced image segments are repositioning incorrectly as the browser window resizes.
Rather than slicing up your image and using javascript, why not try a pure HTML solution and use the <map> tag to insert your links at various parts of the image?
Here's a Fiddle demonstrating a scalable HTML image map (uses the image-map-resizer plugin)
You can learn more about image maps here.

Having a single div in a row with the others?

I've read a bunch of SO questions/answers on side-by-side (row) divs, but I don't think I'm grasping it as I can't find a solution for my specific setup:
http://www.trforums.com/h18-iwaku
Basically, I want the div on the very bottom to be to the right to the "Hey, What's up", "Navigation," and "Connect" boxes, instead of just under them. Is this possible to do?
The divs themselves are just classes with their corresponding html content. It's very basic but here's my code:
http://pastebin.com/AQrqqewA
My html is very rusty so other suggestions are more than welcome. Thanks for any help.
Try surrounding each column in a DIV:
<!-- Left column -->
<div id="leftColumn">
<div class="ex">
<center><img style="display: inline;" src="http://i.imgur.com/wCP3WP3" alt="" /></center>
<h1>Hey, what's up?</h1>
</div>
<div class="navbar">
<b>NAVIGATION:</b> Zukan | Movies | Retsuden | Photography |
</div>
<div class="connect">
<b>CONNECT:</b> Livejournal
</div>
</div>
<!-- Right column -->
<div id="rightColumn">
<div class="slideshow">
Bottom div that I want in a row with the top one...
</div>
</div>
CSS:
#leftColumn {
float: left;
width: 410px; // Width of this column
}
#rightColumn {
float: left;
width: 500px; // Width od this column
}
Move the div with a class slideshow next to the div with a class ex and apply floats, as you can see in JS Bin
The final result can be seen here
Use float:left or display:inline on the divs that you want next to each other.

Bootstrap Element 100% Width

I want to create alternating 100% colored blocks. An "ideal" situation is illustrated as an attachment, as well as the current situation.
Desired setup:
Currently:
My first idea was to create an div class, give it a background color, and give it 100% width.
.block {
width: 100%;
background: #fff;
}
However, you can see that this obviously doesn't work. It's confined to a container area. I tried to close the container and that didn't work either.
The container class is intentionally not 100% width. It is different fixed widths depending on the width of the viewport.
If you want to work with the full width of the screen, use .container-fluid:
Bootstrap 3:
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6"></div>
<div class="col-lg-6"></div>
</div>
<div class="row">
<div class="col-lg-8"></div>
<div class="col-lg-4"></div>
</div>
<div class="row">
<div class="col-lg-12"></div>
</div>
</div>
</body>
Bootstrap 2:
<body>
<div class="row">
<div class="span6"></div>
<div class="span6"></div>
</div>
<div class="row">
<div class="span8"></div>
<div class="span4"></div>
</div>
<div class="row">
<div class="span12"></div>
</div>
</body>
QUICK ANSWER
Use multiple NOT NESTED .containers
Wrap those .containers you want to have a full-width background in a div
Add a CSS background to the wrapping div
Fiddles: Simple: https://jsfiddle.net/vLhc35k4/ , Container borders: https://jsfiddle.net/vLhc35k4/1/
HTML:
<div class="container">
<h2>Section 1</h2>
</div>
<div class="specialBackground">
<div class="container">
<h2>Section 2</h2>
</div>
</div>
CSS: .specialBackground{ background-color: gold; /*replace with own background settings*/ }
FURTHER INFO
DON'T USE NESTED CONTAINERS
Many people will (wrongly) suggest, that you should use nested containers. Well, you should NOT.
They are not ment to be nested. (See to "Containers" section in the docs)
HOW IT WORKS
div is a block element, which by default spans to the full width of a document body - there is the full-width feature. It also has a height of it's content (if you don't specify otherwise).
The bootstrap containers are not required to be direct children of a body, they are just containers with some padding and possibly some screen-width-variable fixed widths.
If a basic grid .container has some fixed width it is also auto-centered horizontally.
So there is no difference whether you put it as a:
Direct child of a body
Direct child of a basic div that is a direct child of a body.
By "basic" div I mean div that does not have a CSS altering his border, padding, dimensions, position or content size. Really just a HTML element with display: block; CSS and possibly background.
But of course setting vertical-like CSS (height, padding-top, ...) should not break the bootstrap grid :-)
Bootstrap itself is using the same approach
...All over it's own website and in it's "JUMBOTRON" example:
http://getbootstrap.com/examples/jumbotron/
This is how you can achieve your desired setup with Bootstrap 3:
<div class="container-fluid">
<div class="row"> <!-- Give this div your desired background color -->
<div class="container">
<div class="row">
<div class="col-md-12">
... your content here ...
</div>
</div>
</div>
</div>
</div>
The container-fluid part makes sure that you can change the background over the full width. The container part makes sure that your content is still wrapped in a fixed width.
This approach works, but personally I don't like all the nesting. However, I haven't found a better solution so far.
There is a workaround using vw. Is useful when you can't create a new fluid container.
This, inside a classic 'container' div will be full size.
.row-full{
width: 100vw;
position: relative;
margin-left: -50vw;
left: 50%;
}
After this there is the sidebar problem (thanks to #Typhlosaurus), solved with this js function, calling it on document load and resize:
function full_row_resize(){
var body_width = $('body').width();
$('.row-full').css('width', (body_width));
$('.row-full').css('margin-left', ('-'+(body_width/2)+'px'));
return false;
}
In bootstrap 4, you can use 'w-100' class (w as width, and 100 as 100%)
You can find documentation here:
https://getbootstrap.com/docs/4.0/utilities/sizing/
If you can't change the HTML layout:
.full-width {
width: 100vw;
margin-left: -50vw;
left: 50%;
}
<div class="container">
<div class="row">
<div class="col-xs-12">a</div>
<div class="col-xs-12">b</div>
<div class="col-xs-12 full-width">c</div>
<div class="col-xs-12">d</div>
</div>
</div>
Demo: http://www.bootply.com/tVkNyWJxA6
Sometimes it's not possible to close the content container.
The solution we are using is a bit different but prevent a overflow because of the
firefox scrollbar size!
.full-width {
margin-top: 15px;
margin-bottom: 15px;
position: relative;
width: calc(100vw - 10px);
margin-left: calc(-50vw + 5px);
left: 50%;
}
Here is a example: https://jsfiddle.net/RubbelDeKatz/wvt9253q
Instead of
style="width:100%"
try using
class="col-xs-12"
it will save you 1 character :)
Sorry, should have asked for your css as well. As is, basically what you need to look at is giving your container div the style .container { width: 100%; } in your css and then the enclosed divs will inherit this as long as you don't give them their own width. You were also missing a few closing tags, and the </center> closes a <center> without it ever being open, at least in this section of code. I wasn't sure if you wanted the image in the same div that contains your content or separate, so I created two examples. I changed the width of the img to 100px simply because jsfiddle offers a small viewing area. Let me know if it's not what you're looking for.
content and image separate: http://jsfiddle.net/QvqKS/2/
content and image in same div (img floated left): http://jsfiddle.net/QvqKS/3/
I would use two separate 'container' div as below:
<div class="container">
/* normal*/
</div>
<div class="container-fluid">
/*full width container*/
</div>
Bare in mind that container-fluid does not follow your breakpoints and it is a full width container.
I'd wonder why someone would try to "override" the container width, since its purpose is to keep its content with some padding, but I had a similar situation (that's why I wanted to share my solution, even though there're answers).
In my situation, I wanted to have all content (of all pages) rendered inside a container, so this was the piece of code from my _Layout.cshtml:
<div id="body">
#RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
<div class="container">
#RenderBody()
</div>
</section>
</div>
In my Home Index page, I had a background header image I'd like to fill the whole screen width, so the solution was to make the Index.cshtml like this:
#section featured {
<!-- This content will be rendered outside the "container div" -->
<div class="intro-header">
<div class="container">SOME CONTENT WITH A NICE BACKGROUND</div>
</div>
}
<!-- The content below will be rendered INSIDE the "container div" -->
<div class="content-section-b">
<div class="container">
<div class="row">
MORE CONTENT
</div>
</div>
</div>
I think this is better than trying to make workarounds, since sections are made with the purpose of allowing (or forcing) views to dynamically replace some content in the layout.
Though people have mentioned that you will need to use .container-fluid in this case but you will also have to remove the padding from bootstrap.
The following answer is not exactly optimal by any measure, but I needed something that maintains its position within the container whilst it stretches the inner div fully.
https://jsfiddle.net/fah5axm5/
$(function() {
$(window).on('load resize', ppaFullWidth);
function ppaFullWidth() {
var $elements = $('[data-ppa-full-width="true"]');
$.each( $elements, function( key, item ) {
var $el = $(this);
var $container = $el.closest('.container');
var margin = parseInt($container.css('margin-left'), 10);
var padding = parseInt($container.css('padding-left'), 10)
var offset = margin + padding;
$el.css({
position: "relative",
left: -offset,
"box-sizing": "border-box",
width: $(window).width(),
"padding-left": offset + "px",
"padding-right": offset + "px"
});
});
}
});
This must work (Mobile phone as well as Desktop screen):
class: alignfull and class: img-fluid will do the magic.
<div class="alignfull">
<img class="img-fluid" style="background-size: cover;
background-position: center ;
background-repeat: no-repeat;
height: auto;
min-width: 100%;
width: -moz-available; "
src="{{ $image->image }}" alt="An image">
</div>

Creating complex div structure using CSS

I'm attempting to create a complex div structure using CSS.
I want it to be made up of four columns. On the left is just a list of images. On the right is the complex div structure that I can't figure out a way to create. There should be two large vertical boxes containing various details. In-between these vertical boxes are any number of horizontal boxes.
My problem is that I cannot work out how to create this div structure in a way that 'scales', i.e. there could be any number of horizontal boxes between the two vertical boxes.
This is the div structure I was attempting to use:
<div class="result">
<div class="detail_1">
<p>Detail 1</p>
</div>
<div class="details">
<p>Details</p>
</div>
<div class="details">
<p>Details</p>
</div>
<div class="detail_2">
<p>Detail 2</p>
</div>
</div>
Any help would be greatly appreciated!
EDIT: I have fixed this problem by just using tables. Thanks for the replies.
Update 2
Your question is: How to make the price & flight_number div the same height as the parent div (container)..
1) Use the technique described here: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
2) update your CSS so that the flight number and the price are vertical aligned in the middle of their div.
I think that mine HTML structure is better then yours because it's more clear and easier to work with.
So based on my HTML structure: The parent container (flight_info) is stretchend as long as the content inside (the table with the rows will be the longest). the div's flight_number and price are also the total height of the parent container thanks of the technique described in step 1 above. The extra CSS (step 2) will align the price and flight number nicely in the middle.
OLD
<ul id="flights">
<li>
<ul class="images">
<li><img src="img1" alt="your image" /></li>
<li><img src="img2" alt="your image 2" /></li>
</ul>
<div class="flight_info" id="flight_EK49">
<div class="flight_number">
EK49
</div>
<table>
<thead>
<th>date</th>
<th>from</th>
<th>to</th>
</thead>
<tbody>
<tr>
<td>1/1/2013</td>
<td>departure airfield</td>
<td>destination airfield</td>
</tr>
...
</tbody>
</table>
<div class="price">
€999,99
</div>
</div>
</li>
// duplicate the above for a new flight..
</ul>
And for the CSS style (you must do the rest on your own because this is just an example. I didn't test any of the code):
<style>
#flights .images {
float: left;
width: 250px;
}
.flight_info {
float: left;
width: 700px;
}
.flight_info .flight_number,
.flight_info .price {
float: left;
width: 150px;
}
.flight_info .price {
float: right;
}
.flight_info table {
float: left;
width: 400px;
}
</style>
I think you will get the idea.
EDIT 1
Changed all the position absolutes to floats because it easier with the li's automatic heights.
I also added the leg images of the flight as well, but as I mentioned, you have to do the rest yourself ;)

Resources