简体   繁体   中英

Flexible grid & multiple columns in Win8 App

I have been working on a team to create apps for Windows 8. I am using CSS3 grids to layout the page, which has been mostly pretty darn easy. However, we are populating some areas of our app with data from an outside source and therefore cannot control the length of the text imported. Our app is scrolling left-to-right, so I need the grid column that is being filled to auto-size its width and to flow text into columns - so using the CSS grid and CSS columns, both set to "auto", should work just fine, right?

Well, it doesn't. I have tried many, many different combinations of CSS but I either need to give my div a definite width (which wouldn't work if the content being imported is very short), or define a definite number of columns that text can flow into (which doesn't work for both very short or very long content). I think this is mostly due to the fact that I'm trying to build a flexible page horizontally, which even the Win8 app seems to get confused about. I'm thinking I need to find a JS/jQuery workaround for this but I'm relatively new to JS/jquery and I'm not sure what exactly I would write.

Of course, if any other Win8 front-end developers have experience with this, your insight would be so greatly appreciated.

My .detailSection is in the middle of other columns, so it absolutely must expand and contract as needed for whatever content may fill it.

This is a snippet of my CSS as it stands now. Please let me know if I can provide any other information, of course.

.advisorDetail {
    height: 100%;
    -ms-grid-columns: 120px 560px 80px auto 80px auto auto;
}
.advisorDetail section[role=main].detailSection {
    -ms-grid-column: 4;
    display: -ms-grid;
    -ms-grid-rows: 24px 20px 1fr;
    -ms-grid-columns: auto;
}
    .advisorDetail section[role=main].detailSection .detailFeed {
        -ms-grid-row: 3;
        column-count: 3;
        column-width: 400px;
        column-gap: 40px;
    }

Your CSS snippet shows column-count being defined. From your description, you don't want to specify column-count or width on the multi-column element. I'm assuming that you're asking how you can remove column-count and still get the visual you want. Is that correct? Assuming it is...

When the multi-column element is trying to determine how it should size itself, its height is not yet defined. As a result, it sizes as though there is no restraint on height and you get a single column of text. If you specify height: 100% on the multi-column element and ensure that the height of its container is defined when it is sizing, you should get the visual you're looking for.

For example, let's say your .detailFeed element is -ms-grid-row-span: 3 (spanning all of the rows). Since the third row is using a fractional unit, the Grid needs to have its height defined in advance in order to know how big the third row should be. If you wanted the Grid element to always be 100% of the height of the window, you would typically specify height: 100% and ensure that the Grid's percentage value resolves in time by ensuring that it has an ancestor that is non-auto height (eg set height: 100% on all elements up the tree). The other option would be to use viewport height units (ie height: 100vh )

If the grid rows the multi-column element is in were all non-auto and non-fractional, the height of the Grid would not need to be defined in advance since the grid cell height would still be defined.

Here's some sample markup that shows what I am describing. You can run this in IE 10 or within an app. You'll notice that as you resize the window, the number of columns adjusts.

<!doctype html>
<html>
<head>
<style>
html, body, .advisorDetail, section
{
    height: 100%;
}
.advisorDetail section[role=main].detailSection {
    -ms-grid-column: 4;
    display: -ms-grid;
    -ms-grid-rows: 24px 20px 1fr;
    -ms-grid-columns: auto;
    height: 100%;
}
    .advisorDetail section[role=main].detailSection .detailFeed {
        -ms-grid-row-span: 3;
        column-width: 400px;
        column-gap: 40px;
        height: 100%;
    }
</style>
</head>
<body>
<div class="advisorDetail">
  <section role="main" class="detailSection">
    <div class="detailFeed">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.
Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.
Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.
Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy.
Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla.
Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien. Donec ut est in lectus consequat consequat.
Etiam eget dui. Aliquam erat volutpat. Sed at lorem in nunc porta tristique.
Proin nec augue. Quisque aliquam tempor magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Nunc ac magna. Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis.
Pellentesque porttitor, velit lacinia egestas auctor, diam eros tempus arcu, nec vulputate augue magna vel risus. Cras non magna vel ante adipiscing rhoncus. Vivamus a mi.
Morbi neque. Aliquam erat volutpat. Integer ultrices lobortis eros.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin semper, ante vitae sollicitudin posuere, metus quam iaculis nibh, vitae scelerisque nunc massa eget pede. Sed velit urna, interdum vel, ultricies vel, faucibus at, quam.
Donec elit est, consectetuer eget, consequat quis, tempus quis, wisi. In in nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.
Donec ullamcorper fringilla eros. Fusce in sapien eu purus dapibus commodo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Cras faucibus condimentum odio. Sed ac ligula. Aliquam at eros.
Etiam at ligula et tellus ullamcorper ultrices. In fermentum, lorem non cursus porttitor, diam urna accumsan lacus, sed interdum wisi nibh nec nisl. Ut tincidunt volutpat urna.
Mauris eleifend nulla eget mauris. Sed cursus quam id felis. Curabitur posuere quam vel nibh.
Cras dapibus dapibus nisl. Vestibulum quis dolor a felis congue vehicula. Maecenas pede purus, tristique ac, tempus eget, egestas quis, mauris.
Curabitur non eros. Nullam hendrerit bibendum justo. Fusce iaculis, est quis lacinia pretium, pede metus molestie lacus, at gravida wisi ante at libero.
Quisque ornare placerat risus. Ut molestie magna at mi. Integer aliquet mauris et nibh.
Ut mattis ligula posuere velit. Nunc sagittis. Curabitur varius fringilla nisl.
Duis pretium mi euismod erat. Maecenas id augue. Nam vulputate.
Duis a quam non neque lobortis malesuada. Praesent euismod. Donec nulla augue, venenatis scelerisque, dapibus a, consequat at, leo.
Pellentesque libero lectus, tristique ac, consectetuer sit amet, imperdiet ut, justo. Sed aliquam odio vitae tortor. Proin hendrerit tempus arcu.
In hac habitasse platea dictumst. Suspendisse potenti. Vivamus vitae massa adipiscing est lacinia sodales.
Donec metus massa, mollis vel, tempus placerat, vestibulum condimentum, ligula. Nunc lacus metus, posuere eget, lacinia eu, varius quis, libero. Aliquam nonummy adipiscing augue.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.
Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.
    </div>
  </div>
</div>
</body>
</html>

Disclosure: I am on the team that worked on Microsoft's implementation of the CSS Grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM