Last active
January 15, 2023 00:07
-
-
Save JayPanoz/a3f547dcfcad75bddeb6a7a65be57f46 to your computer and use it in GitHub Desktop.
eBook CSS to make the image float and the text flood when the image doesn’t fit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.float-image { | |
width: 100%; | |
float: left; | |
padding: 0; | |
display: block; | |
text-align: center; | |
margin: 0.75em 0; | |
margin-top: 1vh; /* will collapse if float + margin-top because your entire CSS will be ignored by legacy RMSDK if you’re using margin: 1vh 0; */ | |
margin-bottom: 1vh; | |
} | |
.float-image > img { | |
vertical-align: top; /* Look ma, no additional spacing as the line-height now exactly matches the image’s height */ | |
width: auto; | |
max-width: 100%; | |
height: auto; | |
} | |
.float-image + * { | |
widows: 1; /* prevent text cut off if image doesn’t float */ | |
} | |
/* Since bullets don’t play well with this trick, we must clear lists (consequently, they won't flood) */ | |
.float-image ~ ul, .float-image ~ ol { | |
clear: both; | |
} | |
/* Basically, the following are margin-top and margin-bottom */ | |
/* This is the trigger */ | |
.float-image:before { | |
display: block; | |
content: ''; | |
width: 1px; | |
height: 0; /* Don’t ask me why, you get text cut off if the image floats and you’ve got any other value, even 1px */ | |
margin-top: 1vh; /* will collapse if float */ | |
float: right; | |
clear: right; | |
} | |
/* This should be the clearer—except it currently doesn’t do its job when bullets (either span or list-style-type) are involved. */ | |
.float-image:after { | |
display: block; | |
content: ''; | |
width: 100%; | |
height: 1vh; | |
float: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To sum up, using this snippet, the image will float to the next column if it doesn’t fit. But since it is outside the flow (because float), it won’t prevend text from flooding the whitespace it doesn’t fit. Else, it will behave as your typical image.
It ships with a few caveats though:
It somehow works not that badly with the worst test suite I can throw at this but well, the float should ideally be added using JavaScript if some conditions are met.
But since JavaScript is optional…