• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

(CSS) Reversing the order of Grid elements

Engaged ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

The following code will turn a two-column grid layout into a single-column one when viewport width falls under 760px :

 

.grid {
display: grid;
width: 100%;
grid-template-columns: 50% 50%;
grid-column-gap: 1em;
grid-row-gap: 1em;
}
.grid span { width:100% }
@media screen and (max-width: 760px)
.grid { grid-template-columns: 100% }
}

 

<div class="grid">
<span>Hi</span>
<span>Bye</span>
</div>

 

What's the least amount of code required to flip the order of "Hi" and "Bye" when the page switches to a single-column layout (ie, < 760px)? No need to worry about where a 3rd element would fall, it should always just be 2.

 

Ideally, I'd like to do this using existing selectors like :first-child (perhaps making it float:right on the resize would make it appear last) but if that's too much of an ask, I'd be open to assigning an ID to one or both of the spans. I'm also told Flexbox will reverse order easily without requiring ID tags; but if Grid does it too, I'd rather stick with that since I'm already using it.

 

Thanks!

TOPICS
How to

Views

13.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 14, 2023 May 14, 2023

Add

.grid {
    display: grid;
    width: 100%;
    grid-template-columns: 50% 50%;
    grid-column-gap: 1em;
    grid-row-gap: 1em;
}

.grid span {
    width: 100%
}

@media screen and (max-width: 760px) {
    .grid {
        grid-template-columns: 100%
    }

    span:first-child {
        order: 2;
    }
}
 

Votes

Translate

Translate
Community Expert ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

Add

.grid {
    display: grid;
    width: 100%;
    grid-template-columns: 50% 50%;
    grid-column-gap: 1em;
    grid-row-gap: 1em;
}

.grid span {
    width: 100%
}

@media screen and (max-width: 760px) {
    .grid {
        grid-template-columns: 100%
    }

    span:first-child {
        order: 2;
    }
}
 
Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

Actually I would code for mobile first for obvious reasons. The markup would look like 

<!doctype html>
<html>

<head>
    <base href="/">
    <meta charset="UTF-8">
    <title>Untitled Document</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/css/style.css" />
    <style>
        .grid {
            display: grid;
            width: 100%;
            grid-template-columns: 100% grid-column-gap: 1em;
            grid-row-gap: 1em;
        }


        .grid span {
            width: 100%
        }

        @media screen and (min-width: 761px) {
            .grid {
                grid-template-columns: 50% 50%;
            }

            span:first-child {
                order: 2;
            }
        }
    </style>
</head>

<body>
    <div class="grid">
        <span>Bye</span>
        <span>Hi</span>
    </div>
</body>

</html>
Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

Just curious, why code for mobile first? (I'm just more comfortable coding for PCs first.) What's the advantage to doing it the other way?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

quote

Just curious, why code for mobile first? (I'm just more comfortable coding for PCs first.) What's the advantage to doing it the other way?


By @Under S.

 

Its just one of those debatable issues and neither coding for mobile first or desktop first is 100% correct. Responsive frameworks such as Bootstrap use mobile first and I think that has influenced or more like brain washed the developers that use it into thinking mobile first is correct and nothing else can be true. That is the problem today, web-development is hugely opininated because there are too many options available and each developer think that theirs is correct.

 

I use the desktop first approach because its the completed jigsaw puzzle and I personally find it easier to remove pieces of the jigsaw for mobile than to think about what extra pieces are required for desktop. Is that correct, nope, is it incorrect, nope. Try to drown out the noise (not easy) and get accustomed to how you feel comfortable working.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

Ah, I see. The reason I do desktop first is because I can get as wild as I want with it, as long as I keep in mind that it all needs to be brought down to single-column for phones (the opposite end of that spectrum). Although I usually include an in-between state for pads (CNN recently went through a redesign that has no in-between state.. my Pad now pulls up the phone version, instead of the desktop one; and it looks horrible because the single column wasn't meant to extend that wide. Looks fine on phones, though).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

To illustrate why mobile first, let me re-write the HTML as

<div class="row">
    <div>Bye</div>
    <div>Hi</div>
</div>
All of the CSS that is required is
@media screen and (min-width: 761px) {
    .row {
        display: flex
    }

    .row div {
        flex-grow: 1;
    }

    .row div:first-child {
        order: 2
    }
}
Finishing up with
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <style>
        @media screen and (min-width: 761px) {
            .row {
                display: flex
            }

            .row div {
                flex-grow: 1;
            }

            .row div:first-child {
                order: 2
            }
        }
    </style>
</head>

<body>
    <div class="row">
        <div>Bye</div>
        <div>Hi</div>
    </div>
</body>

</html>

 

Don't believe me? Have a look at

https://youtu.be/0ohtVzCSHqs

 

@osgood_ I would love to know, where is the debate???

 

As a side note, coders prefer mobile first, designers prefer desktop first. 

https://youtu.be/HkX8eao2NZ0

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote
 

@osgood_ I would love to know, where is the debate???

 

 

Have you heard of Google search?

 

Even the sensible die hard mobile first debaters will tell you mobile first is not always correct if your target audience mainly uses desktop. However l doubt Bootstrapers, even if they were to acknowledge that fact, would change their approach as they are in too much denial and brain washed.

 

As Donald would say, 'fake news'. 

 

As for the 2 youtube links you provided.......l kind of lost faith in a lot of channels once l became a lot more knowledgable............again its a very opinionated arena. Thats not to say you should not take note but don't just take what is being taught for granted. Do your own investigation, learning and research as that will provide you with the knowledge to make a subjective assessment as to whether its right, wrong, indifferent or just not the way you personally like to do things.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote

Ah, I see. The reason I do desktop first is because I can get as wild as I want with it, as long as I keep in mind that it all needs to be brought down to single-column for phones (the opposite end of that spectrum).


By @Under S.

 

Exactly, an intelligent developer would use the approach they feel comfortable with, keeping in mind that both desktop and mobile are as equally important as to what you deliver to the user or dont based on the environment where the website will appear, without dismissing one approach or the other. There are too many biased articles and not enough common sense being deployed.

 

Web development these days has become a 'religion'. If your in one camp you can't accept the other camps view point and it's far easier to write misleading articles in the pursuit you can recruit more to join your 'religious ' cult. I just find it good entertainment, it generally provides a good laugh.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote

Web development these days has become a 'religion'. If your in one camp you can't accept the other camps view point and it's far easier to write misleading articles in the pursuit you can recruit more to join your 'religious ' cult. I just find it good entertainment, it generally provides a good laugh.


By @osgood_

 

@osgood_ , I provided two videos, the first one from a `coder` who decided to use mobile first, the second from a `designer` who decided on desktop first. I do not think that my articles or, indeed, I are misleading. Not sure what `religion` has to do with pure facts.

 

As a `coder` I created the CSS for a simple HTML example using mobile first.

 

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <style>
        @media screen and (min-width: 761px) {
            .row {
                display: flex
            }

            .row div {
                flex-grow: 1;
            }

            .row div:first-child {
                order: 2
            }
        }
    </style>
</head>

<body>
    <div class="row">
        <div>Bye</div>
        <div>Hi</div>
    </div>
</body>

</html>

 

 

As a `designer`, @osgood_ , would you please show your CSS for the HTML code using desktop first? This should give us all a fair indication of which camp to join. 

 

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <style>

    </style>
</head>

<body>
    <div class="row">
        <div>Bye</div>
        <div>Hi</div>
    </div>
</body>

</html>

 

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote
quote

Web development these days has become a 'religion'. If your in one camp you can't accept the other camps view point and it's far easier to write misleading articles in the pursuit you can recruit more to join your 'religious ' cult. I just find it good entertainment, it generally provides a good laugh.


By @osgood_

 

@osgood_ , I provided two videos, the first one from a `coder` who decided to use mobile first, the second from a `designer` who decided on desktop first. I do not think that my articles or, indeed, I are misleading. Not sure what `religion` has to do with pure facts.

 


By @BenPleysier

 

Im using 'religion' as an analogy of just how blinkered some developers have become, they only believe in their methods and disregard anyone elses, I think you would qualify for that category, most certainly.

 

You have an opinion, which is fine, but nearly always fail to accept anyone elses, that is what leads to misleading or mis-representation.

 

Talking about misleading. You yourself use Bootstrap, correct me if I'm incorrect? The code in your example is NOT what you would use. Your code would almost certainly be more verbose and not only that, contain 1000's of unesseary lines of Bootstrap css. So if youre concerned about a few extra lines of css used to create the desktop version first, maybe you should think again and revise what you indeed use as a workflow.

 

Even in Kevins video (the coder), I'm sure you have watched it - 65% in a conducted vote said they use a desktop first approach. Reading the comments you can quite clearly see there is no conculsive right or wrong, just opinions and shared experiences. Are the 65% correct, nope are they wrong, nope. The good news is it means 65% can't be using Bootstrap, I assume.

 

If you want to add another respected youtuber into the mix try, Traversy Media (over 2m subscribers). He ocassionally builds out a website from scratch, the last I think was about a year ago, a reproduction of the Starbucks website. Its not until the end of the desktop build when he starts introduicng media queries for mobile. I have no views, apart from that's how he does it and I share his way of working too.

 

Others have alternative ideas and that is good for them. What I dont like is misleading information by one party or the other party to fortify their argument. There is no evidence that Google will rank a website using the mobile first approach higher than one which uses a desktop first approach and one which is not even responsive. Using that misleading statement, in one of the pages you provided a url to, is complete fabrication.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote

Im using 'religion' as an analogy of just how blinkered some developers have become, they only believe in their methods and disregard anyone elses, I think you would qualify for that category, most certainly.


By @osgood_

 

I am glad that it is just your thought that I belong to the blinkered category. Perhaps you did not read the part where I stated what `I would do`. In doing so, I did not preach the gospel as you are suggesting.

 

All I have asked of you is to provide the CSS for a desktop first approach. This is your opportunity to show my erroneous approach.

 

 

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote
quote

Im using 'religion' as an analogy of just how blinkered some developers have become, they only believe in their methods and disregard anyone elses, I think you would qualify for that category, most certainly.


By @osgood_

 

I am glad that it is just your thought that I belong to the blinkered category. Perhaps you did not read the part where I stated what `I would do`. In doing so, I did not preach the gospel as you are suggesting.


By @BenPleysier

 

 

Well it sure does feel like you are preaching the gospel by providing code examples that you consider economical, but in reality using a different approach which is far from it.

 


@BenPleysier wrote:
quote

All I have asked of you is to provide the CSS for a desktop first approach. This is your opportunity to show my erroneous approach.

 


 

What could that possibly conclude? I'm way ahead and called you out.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote

What could that possibly conclude? I'm way ahead and called you out.


By @osgood_

 

So that was the exercise, to call me out? I thought that this was a discussion about coding.

 

Have a good day sir.

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

LATEST
quote
quote

What could that possibly conclude? I'm way ahead and called you out.


By @osgood_

 

So that was the exercise, to call me out? I thought that this was a discussion about coding.

 

Have a good day sir.

 


By @BenPleysier

 

No far from it, you did that job yourself l think. l was only enquiring why you felt so concerned that a few extra lines of css would be needed when you've been deploying 100s of extra lines for years and showed no concern.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

quote

Just curious, why code for mobile first? (I'm just more comfortable coding for PCs first.) What's the advantage to doing it the other way?


By @Under S.

 

Don't take it from me? Have a read of https://www.springboard.com/blog/design/what-is-mobile-first-design/

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

Like l said its hugely opinionated with a spattering of bullshite for good measure.

 

I like the way they specifically phrase:

‌Mobile-first is important because Google’s algorithm ranks mobile-friendly sites higher. 

 

lmfao, as if designing for desktop first wasn't as equally mobile-friendly assuming you ARE actually constructing a responsive website......not that anyone knows what Google likes anyway, that's always been a grey area but some unwashed upstart influencer is convinced they actually know for sure.

 

Another bit of claptrap:

‌Mobile-first is important because it forces a web designer to focus only on the essentials, thus providing a better user experience on mobile devices.

 

The above statement infers it's going to be a worse experience on desktop devices UNLESS the developer is going to consider desktop later (all the extra bells and whistle that can be included), just like those who design for desktop first will consider mobile later. Each should be given equal consideration.

 

Rubbish:

'Mobile-first is important because it strengthens desktop and tablet website design. Designing for smartphone devices is a content-first approach. When you expand to bigger screen sizes, you’ll know what is essential and expand on that without losing the foundation of what’s most important'.

 

How  does it strengthen desktop or tablet design if there's virtually nothing in the first place? I think anyone with half a brain can determine what is important to include at any stage of the build flow, regardless of what approach you use, after all the most important information has to be in both desktop and tablet and it dont matter which you cater for first.

 

Hell the noise out there is deafening. Bullcrap, Bullcrap, Bullcrap, all the time Bullcrap dont buy into it folks.

 

It says 'Get to Know other Design Students' at the end of the page. I dont know what that refers to but maybe the crap above that line of text was written by a design student, with little or no experience.

 

It's far easier to remove complete components from the dom than it is to insert them using javascipt unless youre just going to leave a lot of redundant tags in your mobile first code so you can shoehorn in the extra bits you might require for desktop version or worse still hide those component in a media query or use a verbose tangled mess of javascript to insert them when that's not strictly necessary......hummmm 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

Think even further, Why are you using CSS Grid?

 

You are targeting a single dimensional element. You should therefore use a 1D function such as Flexbox. CSS Grid is a 2D function.

 

See 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

I've been more-or-less using Grid as a replacement for Table (since there's so much less markup required) and am far less familiar with Flexbox. Could you show me what the Flexbox code would look like in this context? For whatever reason, I tend to pick things up faster reverse-engineering them. If it turns out the same can be achieved with as little code as Grid, I may start harnessing the unbridled power of Flexboxes more often 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2023 May 14, 2023

Copy link to clipboard

Copied

 

 

<!doctype html>
<html>

<head>
    <base href="/">
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <style>
        .row {
            display: flex;
            flex-direction: column;
            width: 100%;
            gap: 1em;
        }

        @media screen and (min-width: 761px) {
            .row {
                flex-direction: row;
            }

            span {
                flex-grow: 1;
            }

            span:first-child {
                order: 2
            }
        }
    </style>
</head>

<body>
    <div class="row">
        <span>Bye</span>
        <span>Hi</span>
    </div>
</body>

</html>

 

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

Trying to see how quickly I can pick up basic flex : could you tell me why the following code isn't displaying each of the 2 cells as equal width within the flexbox unless there's enough content to fill? I'm trying to keep them both equal width even if one contains 1 word and the other 50. (Currently, the single-word cell is only as wide as that word, with the fuller one occupying everything else.)

 

.flex {
display: flex;
width: 100%;
gap: 1.5em;
flex-direction: row
}
.flex .cell {
flex-grow: 1
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

quote

I've been more-or-less using Grid as a replacement for Table (since there's so much less markup required) and am far less familiar with Flexbox. Could you show me what the Flexbox code would look like in this context? For whatever reason, I tend to pick things up faster reverse-engineering them. If it turns out the same can be achieved with as little code as Grid, I may start harnessing the unbridled power of Flexboxes more often 😉


By @Under S.

 

Flexbox is much simpler than grid. Grid is more powerful but more complex to learn. If you already have gotten your head around Grid then Flexbox is going to be a walk in the park for you to understand and implement.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

I have an old iPad that's stuck at an obsolete iOS version, meaning I can no longer update any apps, including the browsers. I figure I'm probably not the only one who doesn't update his tech regularly, so I've been using this iPad as a "if it works there, it'll work everywhere that matters" gauge. Which is why I no longer use aspect-ratio without the padding hack as a fallback, for example (my iPad won't render aspect-ratio).

 

Is it safe to assume this (older) iPad will have less trouble with Grid than Flexbox? Based on what you're telling me, that might be the only reason to keep with Grid (my presumption that Grid was adopted before Flexbox).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2023 May 15, 2023

Copy link to clipboard

Copied

Make up your own mind:

 

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 16, 2023 May 16, 2023

Copy link to clipboard

Copied

quote

Is it safe to assume this (older) iPad will have less trouble with Grid than Flexbox? Based on what you're telling me, that might be the only reason to keep with Grid (my presumption that Grid was adopted before Flexbox).


By @Under S.

 

I would say Grid, being a more recent addition to the specifications and more complex would have MORE trouble on a device running an old browser. So if Grid is working on your older iPad then Flexbox should have no trouble.

 

Just to confirm, Flexbox was adopted many years before Grid.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines