Ultra-simple, automatic, non-float, 1-4 column, grid system
up vote
3
down vote
favorite
Has everyone moved over to CSS Grid and/or Flexbox? If not, I've maintained and evolved this system over a fair amount of time, the concept was derived originally, I believe, from Mary Lou, although I don't recall where the article is.
I was never a proponent of floats, although the using font-size:0
to clear whitespace isn't an ideal solution either, neither is a DOM Cleaner function in javascript. Probably minimized (non-formatted) HTML without whitespaces is ideal, just difficult to maintain... There's a lot of other "hacky" concepts, comments in HTML, bleh... but I landed on the font-size:0
. This served me well and I still use it in a handful of projects, though I've mostly moved to a CSSGrid/Flexbox combination as of late.
It's a similar to:
.table {display: table}
.table>div {display: table-cell}
With a higher degree of flexibility, if you need heights to match, the above or flexbox/css grid are probably a better solution. or min-height
on columns if it's known content.
Any ideas to improve or flaws, let me know.
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
css layout
add a comment |
up vote
3
down vote
favorite
Has everyone moved over to CSS Grid and/or Flexbox? If not, I've maintained and evolved this system over a fair amount of time, the concept was derived originally, I believe, from Mary Lou, although I don't recall where the article is.
I was never a proponent of floats, although the using font-size:0
to clear whitespace isn't an ideal solution either, neither is a DOM Cleaner function in javascript. Probably minimized (non-formatted) HTML without whitespaces is ideal, just difficult to maintain... There's a lot of other "hacky" concepts, comments in HTML, bleh... but I landed on the font-size:0
. This served me well and I still use it in a handful of projects, though I've mostly moved to a CSSGrid/Flexbox combination as of late.
It's a similar to:
.table {display: table}
.table>div {display: table-cell}
With a higher degree of flexibility, if you need heights to match, the above or flexbox/css grid are probably a better solution. or min-height
on columns if it's known content.
Any ideas to improve or flaws, let me know.
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
css layout
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Has everyone moved over to CSS Grid and/or Flexbox? If not, I've maintained and evolved this system over a fair amount of time, the concept was derived originally, I believe, from Mary Lou, although I don't recall where the article is.
I was never a proponent of floats, although the using font-size:0
to clear whitespace isn't an ideal solution either, neither is a DOM Cleaner function in javascript. Probably minimized (non-formatted) HTML without whitespaces is ideal, just difficult to maintain... There's a lot of other "hacky" concepts, comments in HTML, bleh... but I landed on the font-size:0
. This served me well and I still use it in a handful of projects, though I've mostly moved to a CSSGrid/Flexbox combination as of late.
It's a similar to:
.table {display: table}
.table>div {display: table-cell}
With a higher degree of flexibility, if you need heights to match, the above or flexbox/css grid are probably a better solution. or min-height
on columns if it's known content.
Any ideas to improve or flaws, let me know.
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
css layout
Has everyone moved over to CSS Grid and/or Flexbox? If not, I've maintained and evolved this system over a fair amount of time, the concept was derived originally, I believe, from Mary Lou, although I don't recall where the article is.
I was never a proponent of floats, although the using font-size:0
to clear whitespace isn't an ideal solution either, neither is a DOM Cleaner function in javascript. Probably minimized (non-formatted) HTML without whitespaces is ideal, just difficult to maintain... There's a lot of other "hacky" concepts, comments in HTML, bleh... but I landed on the font-size:0
. This served me well and I still use it in a handful of projects, though I've mostly moved to a CSSGrid/Flexbox combination as of late.
It's a similar to:
.table {display: table}
.table>div {display: table-cell}
With a higher degree of flexibility, if you need heights to match, the above or flexbox/css grid are probably a better solution. or min-height
on columns if it's known content.
Any ideas to improve or flaws, let me know.
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
.autogrid {
padding: .5rem; /* 8px */
/** remove whitespace from container */
font-size: 0;
box-sizing: border-box}
.autogrid>* {
box-sizing: inherit; /* inherit border-box */
display: block;
font-size: inherit; /* inherit 0px */
margin: 0 auto .5rem; /* 0 auto 8px */
padding: 0 .5rem .5rem /* 0 8px 8px */}
/** create vertical space in columns & prevent margin-collapsing */
.autogrid>*:before {
content: '';
display: inline-block;
vertical-align: 0; /* baseline or 0% */
width: 0;
height: .5rem /* 8px */}
@media (min-width: 33.75em) { /* 540px */
.autogrid>* {
display: inline-block;
margin: .5rem; /* 8px */
/** align columns/cards to top, middle, etc. */
vertical-align: top}
.autogrid>*:first-child:nth-last-child(1),
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(100% - 1rem) /* 100% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~*,
.autogrid>*:first-child:nth-last-child(2),
.autogrid>*:first-child:nth-last-child(2)~* {width: calc(50% - 1rem) /* 50% - 16px */}
}
@media (min-width: 50em) { /* 800px */
.autogrid>*:first-child:nth-last-child(3),
.autogrid>*:first-child:nth-last-child(3)~* {width: calc(33.333% - 1rem) /* 33.333% - 16px */}
.autogrid>*:first-child:nth-last-child(4),
.autogrid>*:first-child:nth-last-child(4)~* {width: calc(25% - 1rem) /* 25% - 16px */}
}
/**
###############
# DEMO ONLY #
###############
html:
`
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
`
*/
html {
font: 400 100%/1 sans-serif;
color: #333}
body,
p {
font-size: 1rem; /* 16px */
line-height: 1.45; /* 145% or 23.2px */
margin: 0}
.autogrid>* {text-align: center}
.autogrid>*:first-child,
.autogrid>*:nth-child(2) {color: #fff}
.autogrid>*:nth-child(3),
.autogrid>*:last-child {color: inherit}
.autogrid>*:first-child {background-color: #4af}
.autogrid>*:nth-child(2) {background-color: #fa4}
.autogrid>*:nth-child(3) {background-color: #def}
.autogrid>*:last-child {background-color: #fed}
<div class="autogrid">
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
<div><p>col</p></div>
</div>
css layout
css layout
edited 6 hours ago
asked 9 hours ago
darcher
439215
439215
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209684%2fultra-simple-automatic-non-float-1-4-column-grid-system%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209684%2fultra-simple-automatic-non-float-1-4-column-grid-system%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown