Snap container width to width of number of visible thumbnails? - html

See my site here: http://www.element17.com. It may be desirable to change to a different album than the default in order to have more thumbnails available. The Paris album has the most images in it.
If you click the "Show Photo Grid" button in the very bottom-right, you'll see the photo/thumbnail grid I've contrived.
Right now it scales with the viewport, and I'd like it to continue to do so, but instead of smoothly increasing in size with the viewport I'd like it to only be as wide as the number of thumbnails that currently fit in the viewport, so that there's no empty whitespace to the right of the number of displayed thumbnails. I'd like it to remain 20px from the right of the viewport with any empty space being displayed on the left.
How can this be accomplished?
The current code follows:
HTML
<div id="grid_outer">
<div id="grid_inner">
<div class="grid_thumb button">
<span class="thumb_title">TITLE</span>
<img class="thumb_image" src="image.jpg" alt="TITLE">
</div>
<div class="grid_thumb button">
<span class="thumb_title">TITLE2</span>
<img class="thumb_image" src="image2.jpg" alt="TITLE2">
</div>
</div>
</div>
CSS
#grid_outer {display:none; position:absolute; background-color:rgba(0,0,0,0.75); right:20px; left:20px; bottom:60px; padding:20px 10px 10px 20px; border-radius:20px; max-height:480px; overflow:hidden;}
.grid_thumb {position:relative; float:left; margin:0 10px 10px 0; width:150px; height:150px;}
.thumb_image {position:absolute; top:0; left:0; border-radius:5px;}
.thumb_title {position:absolute; bottom:0px; left:0px; z-index:100; padding:8px; background:#000; border-radius:0 5px 0 5px;}
.button {cursor:pointer;}
So I've discovered that if I set the width of #grid_outer to auto, it will behave how I want if there's less than one row of thumbnails, but will stretch to the far left side of the viewport if there is more than one row of items. Can anyone help?

The CSS only solution (http://caniuse.com/#feat=css-sel3, http://caniuse.com/#feat=css-mediaqueries)
http://jsfiddle.net/coma/G7bx8/6/embedded/result/
div.grid {
font-size: 170px;
position: fixed;
max-width: 1em;
height: 3em;
bottom: 20px;
right: 20px;
padding: 5px;
background-color: rgba(0, 0, 0, .75);
border-radius: 20px;
overflow: auto;
}
div.grid:after {
display: block;
content: "";
clear: both;
}
div.grid > div {
width: 160px;
height: 160px;
float: left;
margin: 5px;
border-radius: 10px;
background-color: #009dff;
}
#media screen and (min-width: 410px) {
div.grid {
max-width: 2em;
}
div.grid > nth-child(2n) {
clear: right;
}
}
#media screen and (min-width: 550px) {
div.grid {
max-width: 3em;
}
div.grid > nth-child(3n) {
clear: right;
}
}
#media screen and (min-width: 720px) {
div.grid {
max-width: 4em;
}
div.grid > nth-child(4n) {
clear: right;
}
}
#media screen and (min-width: 890px) {
div.grid {
max-width: 5em;
}
div.grid > nth-child(5n) {
clear: right;
}
}
#media screen and (min-width: 1060px) {
div.grid {
max-width: 6em;
}
div.grid > nth-child(6n) {
clear: right;
}
}
#media screen and (min-width: 1230px) {
div.grid {
max-width: 7em;
}
div.grid > nth-child(7n) {
clear: right;
}
}
#media screen and (min-width: 1400px) {
div.grid {
max-width: 8em;
}
div.grid > nth-child(8n) {
clear: right;
}
}
#media screen and (min-width: 1570px) {
div.grid {
max-width: 9em;
}
div.grid > nth-child(9n) {
clear: right;
}
}
#media screen and (min-width: 1740px) {
div.grid {
max-width: 10em;
}
div.grid > nth-child(10n) {
clear: right;
}
}
The jQuery solution
http://jsfiddle.net/coma/G7bx8/7/embedded/result/
var size = 170;
var margin = 40;
var win = $(window);
var adjust = function() {
$('div.grid').each(function() {
var width = win.width() - margin;
width = Math.floor(width / size) * size;
$(this).width(width);
});
};
win.resize(adjust);
adjust();

Related

How to set a css property depending on desktop or mobile view?

I would like to have 2 different margin for the same div depend on mobile and desktop view.
The code below can perform a margin-left: 70px; on desktop.
The problem arises on mobile view, it doesn't reset to margin-left: 0px;
How can I do it ?
Thanks.
.body-container {
padding-left: 0px;
padding-right: 0px;
max-width:1350px;
}
.content {
margin-left: 70px;
}
.content-collapsed {
margin-left: 0px;
}
You're not resetting margin in .content
You need to use media queries
The solution below uses non-mobile approach
body {
margin: 0;
}
.content {
background: red;
margin-left: 70px;
}
#media (max-width: 800px) { /*choose the width you prefer*/
.content {
margin-left: 0;
}
}
<div class="content">content</div>
The solution below uses mobile approach (recommended, also is the same used by bootstrap 4)
body {
margin: 0;
}
.content {
background: red;
margin-left: 0;
}
#media (min-width: 800px) { /*choose the width you prefer*/
.content {
margin-left: 70px;
}
}
<div class="content">content</div>
You can use media queries (MDN).
Example :
body {
margin: 0;
}
div {
width: 100px;
height: 100px;
border: 1px solid black;
margin-left: 0px; /* mobile*/
}
/* screen width higher than 750px */
#media screen and (min-width: 750px) {
div {
margin-left: 70px;
}
}
<div></div>
Use media query. Add:
/* For mobile*/
#media only screen and (max-width: 768px) {
div {
margin-left: 0;
}
}
This is the solution that worked the best for my case:
<style>
.body-container {
padding-left: 0px;
padding-right: 0px;
max-width:1350px;
}
.content {
margin-left: 70px;
}
##media (max-width: 800px) { /*choose the width you prefer*/
.content {
margin-left: 0px;
min-width: 100%;
}
}
</style>
Thank you. You made my day.

Move columns based upon viewport using media queries?

As a test, I created 4 columns (with corresponding colors) with 25% width each. I floated them so that they will span the entirety of the page side by side.
I wanted to use media queries in order to cause there to only be two columns side by side if the viewport became small enough, and then one per line if the viewport was even smaller. I'm just doing this within one HTML document as I don't really care to create an accompanying CSS document.
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#media only screen and (max-width: 787px) {
.column1, .column2, .column3, .column4 {
width: 50%;
}
}
#media only screen and (max-width: 420px) {
.column1, .column2, .column3, .column4 {
width: 100%;
}
}
.column1, .column2, .column3, .column4 {
width: 25%;
height: 300px;
}
.column1 {
background-color: red;
float: left;
}
.column2 {
background-color: blue;
float: left;
}
.column3 {
background-color: yellow;
float: right;
}
.column4 {
background-color: black;
float: right;
color: white;
}
</style>
</head>
<body>
<div class="column1">
<p>breakfast</p>
</div>
<div class="column2">
<p>lunch</p>
</div>
<div class="column3">
<p>dinner</p>
</div>
<div class="column4">
<p>snack</p>
</div>
</body>
I feel like I am doing something incredibly wrong. Thank you in advance.
media queries should be below your css styles and not above them.
check this fiddle ~ https://jsfiddle.net/g4j4ewpp/
.column1,
.column2,
.column3,
.column4 {
width: 25%;
height: 300px;
}
.column1 {
background-color: red;
float: left;
}
.column2 {
background-color: blue;
float: left;
}
.column3 {
background-color: yellow;
float: right;
}
.column4 {
background-color: black;
float: right;
color: white;
}
#media only screen and (max-width: 787px) {
.column1,
.column2,
.column3,
.column4 {
width: 50%;
}
}
#media only screen and (max-width: 420px) {
.column1,
.column2,
.column3,
.column4 {
width: 100%;
}
}
An explanation for the answer provided by GvM -
The browser parses code top to bottom. You could have two thousand
#media only screen and (min-width: 748px){
.class {
background-color: some-color;
height: some-height;
}
}
statements. As long as they are all for the same height, the last one will take effect and all previous will be ignored.
Also, say you have three queries that set particular attributes you might want if its a medium size, and one extra if it is a large size.
#media only screen and (min-width: 320px) {
/*give a div 40px height and blue text*/
.this-div {
color: blue;
height: 40px;
background-color: lemonchiffon;
}
/*declare a div, but hide it until the screen is big enough*/
.hidden-div {
display: none;
width: 50%;
background-color: orange;
}
}
#media only screen and (min-width: 480px) {
/*style a different div to have blue text and assign it height*/
.that-div {
color: blue;
height: 30%;
background-color: magenta;
}
/*change the first div's font to red, but keep its height the same*/
.this-div {
color: red;
}
#media only screen and (min-width: 748px) {
/*change the first div's font again, and now it takes up the entire screen.*/
.this-div {
color: green;
height: 100%;
}
/*split the second div and a new third div*/
.that-div {
width: 50%;
}
.hidden-div {
display: inline;
}
}
The background colors remained the same through all media queries. But, the heights and widths may have changed depending on which query was most recently accepted - depending on the size of the viewport.

I want element to decrease in size based how much space does the element next to it take space on the screen

So I want the divider line on the picture to decrease or increase in size based on the All publications link. Currently I have done it like this, but I'm wondering if there's a better solution for this?
#media (max-width: 767px) {
.dividing .col-xs-6 {
width:75%;
}
}
#media (max-width: 600px) {
.dividing .col-xs-6 {
width:65%;
}
}
#media (max-width: 423px) {
.dividing .col-xs-6 {
width:55%;
}
}
You could use a :before element:
h2{
margin: auto;
margin-top: 2em;
text-align: right;
display: flex;
align-items: center;
color: #34495e;
padding-bottom: 1em;
}
h2:before{
content: '';
flex: 1 0 0;
margin-right: 0.25em;
height: 2px;
background: #34495e;
}
<h2>Test</h2>

Responsive Divs are overlapping

I have a code at http://jsfiddle.net/N7nj5/
CSS:
/* ================================================================ Pre-footer Advert pfa =========================================================== */
#pre-footer-ad-container {
width: 95%;
height: 100px;
margin-left: 2.5%;
margin-right: 2.5%;
}
.pre-footer-ad {
width: 100%;
height: 100px;
}
/* SECTIONS */
.sectionpfa {
clear: both;
padding: 0px;
margin: 0px;
}
/* COLUMN SETUP */
.colpfa {
height: 100px;
display: block;
float:left;
margin: 1% 0 1% 1%;
background-color: #f00;
}
.colpfa:first-child { margin-left: 0; }
/* GROUPING */
.grouppfa:before,
.grouppfa:after {
content:"";
display:table;
}
.grouppfa:after {
clear:both;
}
.grouppfa {
zoom:1; /* For IE 6/7 */
}
/* GRID OF TWO */
.pfaspan_2_of_2 {
width: 100%;
}
.pfaspan_1_of_2 {
width: 49.5%;
}
/* GO FULL WIDTH AT LESS THAN 480 PIXELS */
#media only screen and (max-width: 480px) {
.colpfa {
margin: 1% 0 1% 0%;
}
}
#media only screen and (max-width: 480px) {
.pfaspan_2_of_2 {
width: 100%;
}
.pfaspan_1_of_2 {
width: 100%;
}
}
/* ================================================================ Footer Content ================================================================ */
#footer-container {
margin-top: 40px;
width: 100%;
height: auto;
background-color:#16a085;
color: #ffffff;
}
.footer {
width: 95%;
height: 300px;
margin-left:2.5%;
margin-right: 2.5%;
background-color:#16a085;
}
/* SECTIONS */
.sectionfooter {
clear: both;
padding: 0px;
margin: 0px;
}
/* COLUMN SETUP */
.colfooter {
display: block;
float:left;
margin: 1% 0 1% 2%;
}
.colfooter:first-child { margin-left: 0; }
/* GROUPING */
.groupfooter:before,
.groupfooter:after {
content:"";
display:table;
}
.groupfooter:after {
clear:both;
}
.groupfooter {
zoom:1; /* For IE 6/7 */
}
/* GRID OF THREE */
.footerspan_3_of_3 {
width: 23%;
}
.footerspan_2_of_3 {
width: 24%;
}
.footerspan_1_of_3 {
width: 49%;
}
/* GO FULL WIDTH AT LESS THAN 480 PIXELS */
#media only screen and (max-width: 480px) {
.colfooter {
margin: 1% 0 1% 0%;
}
}
#media only screen and (max-width: 480px) {
.footerspan_3_of_3 {
width: 100%;
}
.footerspan_2_of_3 {
width: 100%;
}
.footerspan_1_of_3 {
width: 100%;
}
}
HTML:
<!-- Pre-Footer Advertisement pfa -->
<div id="pre-footer-ad-container">
<div class="pre-footer-ad">
<div class="sectionpfa grouppfa">
<div class="colpfa pfaspan_1_of_2">
This is column 1
</div>
<div class="colpfa pfaspan_1_of_2">
This is column 2
</div>
</div>
</div>
</div>
<!-- Footer Content -->
<div id="footer-container">
<div class="footer">
<div class="sectionfooter groupfooter">
<div class="colfooter footerspan_1_of_3">
Testimonials
</div>
<div class="colfooter footerspan_2_of_3">
Legal
</div>
<div class="colfooter footerspan_3_of_3">
Other Links
</div>
</div>
</div>
</div>
Problem Definition:
There are two parts:
1. the columns in red color
2. the column in torquise color
They are responsive.
When I reduce the width of the screen, the red ones overlap the torquise one.
I DONT WANT THAT TO HAPPEN!!!
I want that when the site is displayed on a mobile screen, the red and torquise regions should not overlap.
Currently when i reduce the screen resolution, the red ones come one below the other, which is perfect. but the same should happen with the torquise one also.
Please Help!!!
Just clear the footer-container
#footer-container {
margin-top: 40px;
width: 100%;
height: auto;
background-color:#16a085;
color: #ffffff;
clear: both; /* <--- */
}
FIDDLE
Use min-height for test instead height, or reset height too via your queries.
Tes with min-height: http://jsfiddle.net/N7nj5/1/

Responsive css tiles stack in pyramid shape

I have a row of 3 inline-blocks the span the width of the page horizontally:
When the page reaches 1000px wide I want the tiles to stack themselves as a pyramid:
And then at 460px they need to stack vertically:
My current html/css structure is:
<div class='tile-row'>
<div class='tile'></div>
<div class='tile'></div>
<div class='tile'></div>
</div>
.tile-row{
margin: 0 auto;
max-width:1485px;
}
.tile{
width:32%;
display: inline-block;
height: 200px;
}
How would I set up media queries to accomplish the above scenarios? Is there an easier way to do this without using media queries?
Fiddle: http://jsfiddle.net/C2pjx/
.tile-row{
margin: 0 auto;
}
.tile{
width:32%;
float: left;
height: 100px;
background: red;
margin-left:10px;
margin-bottom: 10px;
}
#media only screen and (max-width: 1000px) {
.tile-row > div:nth-of-type(1)
{
float: none;
margin: 10px 100px;
}
}
#media only screen and (max-width: 460px) {
.tile-row > div:nth-of-type(1){
margin: 10px 0;
}
.tile{
float:none;
background: blue;
margin: 10px 0;
}
}