Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Depending on the users roles, I want to display div blocks for each of their roles.

I want the div blocks to be set out in a particular way depending on how many will be shown:

1 role: If the user only has one role then he will be redirected to that page anyway
2 roles: There should be two divs on one row
3 roles: There should be three divs on one row
4 roles: There should be two divs on the top row and two on the bottom
5 roles: There should be three divs on the top row and two on the bottom

so for any case, row one has two divs in cases 2 & 4 and three divs in cases 3 & 5, and row two have either none in cases 2 & 3 and two in cases 4 & 5.

These divs will be evenly sized, large 'buttons' (kind of), so they need to be evenly spaced on the page. I would like to use css to place them which is in keeping with the rest of the site.

I can use pure css, c# code-behind or javascript if need be.

I would like suggestions on how I can set up the css and if I can avoid using c#
Thanks

EDIT:
I tried the table-like displays but I could not get the cells to justify evenly.

I managed to single out the top, bottom, left and right items with a combo of :first-child and :last-child

CSS
<style>
        div .HubRow {
        width: 100%;
    }
        div .HubRow:first-child div   {
            margin-top: 0;
        }
        div .HubRow:last-child div   {
            margin-bottom: 0;
        }

        div .HubRow div {
            width: 200px;
            height: 200px;
            position: relative;
            margin-left: auto;
            margin-right: auto;
        }

            div .HubRow div:first-child {
                margin-left: 0;
            }

            div .HubRow div:last-child {
                margin-right: 0;
            }
</style>


but the margin-top and margin-bottom have no effect >_<<br mode="hold" />
I have now been reduced to this:

CSS
<style>
    div .HubRow {
        width: 100%;
        height: 100%;
        text-align: center;
    }
        div .HubRow div {
            width: 200px;
            height: 200px;
            display: inline-block
        }

</style>


Not quite the desired effect, but close enough so that I can continue.

PS: The inner div size is only representative. They will all be the same size but will be individual controls. I just haven't written them yet ^_^


EDIT2:

I have tried solution 2 and it worked well in my case. The final style is as follows:

CSS
.roles > div
{
    box-sizing: border-box;
    display: inline-block;
    padding: 10px;
    width: 33.3333333%;
    margin-right: -4px;
    text-align: center;
}
.roles > div:nth-last-child(2n):first-child,
.roles > div:nth-last-child(2n):first-child ~ div
{
    width: 50%;
}
.roles > div:nth-child(n+3) ~ div
{
    width: 50%;
}


Thanks guys ^_^
Posted
Updated 7-Apr-15 23:29pm
v3
Comments
Sergey Alexandrovich Kryukov 7-Apr-15 12:26pm    
I'm afraid of being prosecuted by the purists, but I would use table instead. In my view, the fight against using table has more of religious character...
—SA
Andy Lanng 7-Apr-15 13:18pm    
I agree but I'm constrained by my ambition ^_^
Sergey Alexandrovich Kryukov 7-Apr-15 15:23pm    
Being constrained this way means being not free enough. But it you are constrained with your own logic, you are more free than if you would not be.
—SA
Andy Lanng 8-Apr-15 4:15am    
mind... blown... !
Sergey Alexandrovich Kryukov 8-Apr-15 10:06am    
:-)

Please consider my comment to the question as a preface.

But let me further clarify the idea. Indeed, using div instead of table elements help to abstract out presentation from logical markup. So, as a next step, you can still use div, but with table-like CSS properties. Please see:
http://www.w3schools.com/cssref/pr_class_display.asp[^].

Main values for the display property you could use are table-row and table-cell; in most cases, these two would be enough. Just try it; it works. If you are still clueless about the usage, please ask some follow-up questions, then I'll try to give you some HTML sample.

—SA
 
Share this answer
 
v2
Comments
Andy Lanng 7-Apr-15 13:19pm    
Great. Thank SA. I will review this first thing in the morning and mark/rate ^_^
Sergey Alexandrovich Kryukov 7-Apr-15 15:19pm    
You are welcome. Anyway, I'm ready to help a bit more if it doesn't work for you when you try.
—SA
Andy Lanng 8-Apr-15 5:05am    
I tried the table-like settings. I got the rows sorted but I couldn't adjust the cell width :S

i will update the question with what I currently have
Andy Lanng 8-Apr-15 5:30am    
This didn't work out for me but it was a precise answer to my question
Detecting the number of child elements is possible in CSS[^]. However, in this case, it sounds like you only need to detect whether there are an even or odd number of child elements, which is simpler:
CSS
.roles > div
{
    /* Styles for an odd number of children */
}
.roles > div:nth-last-child(2n):first-child,
.roles > div:nth-last-child(2n):first-child ~ div
{
    /* Styles for an even number of children */
}


EDIT: Using display: inline-block; will get you similar results to the Flexbox option I posted previously, with less problems and better browser support[^]:
CSS
.roles > div
{
    box-sizing: border-box;
    display: inline-block;
    padding: 10px;
    width: 33.3333333%;
    margin-right: -4px;
}
.roles > div:nth-last-child(2n):first-child,
.roles > div:nth-last-child(2n):first-child ~ div
{
    width: 50%;
}

The only problem you'll run into is that white-space in the markup isn't ignored, which can push the boxes onto a new line. There are several workarounds[^] - I've used the "negative margin" option here.

http://jsfiddle.net/ot2ks69o/4/[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 7-Apr-15 15:20pm    
The problem with this approach is hard-coded percentage. With Solution 1, it can be driven by the size of inner content, but made optional if one specifies sized; more flexibility, anyway.
—SA
Andy Lanng 8-Apr-15 5:15am    
Hmm - that's true but it might suit my needs in this case. I'll give it a go and report back
Andy Lanng 8-Apr-15 5:21am    
Ok - I see what ya did there ^_^

So nearly perfect, but line two (the last two items if item count >= 4) would need to be 50% width >_<
Andy Lanng 8-Apr-15 5:30am    
After a minor adjustment this worked great! ^_^
Richard Deeming 8-Apr-15 7:53am    
Glad you got it working.

It does sound like FlexBox[^] might be a better fit, if you could afford to drop support for older browsers. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900