Responsive thumbnail grid equal spacing - html

I have a grid of thumbnails:
.thumbnail-container {
border: 1px solid red;
float: left;
width: 25%; (depending on screen size)
}
.thumbnail {
display: block;
height: auto;
width: 200px;
margin: 0 auto;
padding: 0px;
}
you get the idea...
Sometimes there are 5 thumbs, sometimes 4, 3, 2... in the same row depending on the #media device screen width.
So as you can see in the picture, my problem is the horizontal spacing between the thumbnails. Since the width of each thumb-container is equal (25% in the picture) and thumbnails are centered, there is double spacing betwet 1st and 2nd, 2nd-3d, 3rd-4th. On the contrary, the 1st thumb has half of that space to the left, and the 4th has it to the right:
It's like this: 1(A)2(B)2(C)2(D)1
And I want it like this: 1(A)1(B)1(C)1(D)1
Any ideas of how to make the spacing the same?
Many thanks!

I made this responsive grid that does what you need using CSS.
It uses media queries and nth-child().
The horizontal and vertical margins are equal.
FIDDLE
It shows you can use calculation to position grid elements with equal margins between each other. The margin between elements and window is also the same.
Code for screens wider than 751px isn't written yet though but you get the idea. Here is the code :
HTML :
<div id="container">
<div class="wrap">
<div class="foto">1</div>
</div>
<div class="wrap">
<div class="foto">2</div>
</div>
<div class="wrap">
<div class="foto">3</div>
</div>
... And so on ...
</div>
CSS :
html, body {
margin:0;
padding:0;
min-width:150px;
}
.wrap {
float:left;
position:relative;
}
.foto {
width: 150px;
height: 150px;
background: gold;
position:absolute;
}
#warning{display:none;}
#media screen and (min-width: 631px) {
.wrap {
width:20%;
padding-bottom:25%;
}
.wrap:nth-child(4n+2), .wrap:nth-child(4n+3){
}
.wrap .foto {
top:-75px;
margin-top:100%;
right:-30px;
}
.wrap:nth-child(4n+2){
margin:0 5% 0 7.5%;
}
.wrap:nth-child(4n+3){
margin-right:7.5%;
}
.wrap:nth-child(4n+2) .foto{
left:50%;
margin-left:-75px;
}
.wrap:nth-child(4n+3) .foto{
right:50%;
margin-right:-75px;
}
.wrap:nth-child(4n) .foto{
left:-30px;
}
#container{
margin-top:-45px;
}
}
#media screen and (min-width: 481px) and (max-width: 631px) {
.wrap {
width:25%;
padding-bottom:33.3%;
}
.wrap:nth-child(3n+2){
margin:0 12.5%;
}
.wrap .foto {
top:-75px;
margin-top:100%;
right:-37px;
}
.wrap:nth-child(3n+2) .foto{
left:50%;
margin-left:-75px;
}
.wrap:nth-child(3n) .foto{
left:-37px;
}
#container{
margin-top:-37px;
}
}
#media screen and (min-width: 331px) and (max-width: 480px) {
.wrap {
width:33.3%;
padding-bottom:50%;
clear:left;
}
.wrap:nth-child(even) {
float:right;
clear:right;
}
.wrap .foto {
top:-75px;
margin-top:100%;
right:-50px;
}
.wrap:nth-child(even) .foto {
left:-50px;
}
.wrap:nth-child(4n+3) .foto, .wrap:nth-child(4n+4) .foto {
bottom:-75px;
margin-bottom:100%;
}
#container{
margin-top:-25px;
}
}
#media screen and (max-width: 330px) {
.wrap {
width:50%;
padding-bottom:100%;
clear:left;
}
.wrap:nth-child(odd) .foto {
right:-75px;
bottom:0;
bottom:-75px;
margin-bottom:100%;
}
.wrap:nth-child(even) .foto {
top:0px;
right:-75px;
top:-75px;
margin-top:100%;
}
}
#media screen and (min-width: 751px) {
#warning{
color:#fff;
display:block;
position:fixed;
width:100%;
height:50%;
top:25%;
left:0;
background:#000;
text-align:center;
font-size:30px;
}

Related

Style messing up when screen size is reduced

Please take a look at my practice site - http://codepen.io/silentarrowz/pen/rLJABR
I am having few problems with it:
At reduced screen size the eElectronics and cart at the top are not centered but appear at one side of the screen. I'd like it to be centered for small screen sizes. I am able to center the eElectronics logo but unable to center the cart div, even though I am using media queries.
The navigation bar overflows for small screen sizes and the links at the end such as OTHERS & CONTACT flow on to the next row. How do I fix this?
The carousel caption moves off the screen for reduced size. How do I fix this?
Please offer your suggestions and advice.
.navcolor{
background-color:#1abc9c;
}
.logo a{
padding-bottom:10px;
color:#999;
}
.logo a span{
color:#1abc9c;
}
.cart {
border:1px solid rgb(221,221,221);
font-family:"Titillium Web";
font-size:18px;
width:140px;
height:50px;
padding-top:10px;
padding-left:10px;
}
.cart a{
color:#666;
}
.cart a span{
color:#1abc9c;
font-weight:bold;
}
.cart:hover {
background-color : #1abc9c;
}
.cart:hover span, .cart:hover a{
color:white;
}
.no-underline :hover{
text-decoration:none;
}
.navbar-default .navbar-menu{
height:50px;
}
.navbar-default .navbar-menu li > a{
color:white;
font-weight:bold;
font-size:14px;
}
.navbar-default .navbar-menu > .active > a {
background-color:#333;
color:white;
}
.navbar-default .navbar-menu > .active > a:hover {
background-color:#333;
color:white;
}
.navbar-default .navbar-menu li>a:hover{
background-color:#333;
color:white;
}
#carousel1 img{
width:100%;
height:70%;
}
.logo{
font-family:"Titillium Web";
font-size:40px;
}
.cart{
font-size:18px;
}
.carouse-inner{
position:relative;
}
.carousel-caption{
background-color:rgba(0,0,0,0.5);
color:white;
display:block;
width:600px;
padding-left:20px;
text-align:left;
position:absolute;
left:600px;
bottom:150px;
}
.latest img{
border:1px solid grey;
}
.details p{
height:150px;
display:block;
border:1px solid #17a78b;
margin-top:30px;
margin-bottom:30px;
padding-top:10px;
padding-bottom:10px;
font-size: 30px;
font-weight:200;
color:#fff;
text-align:center;
}
.details p:hover{
background-color:black;
}
.details p>i{
font-size:50px;
font-weight:500;
}
.details{
background-color:rgb(26,188,156);
}
.latest{
text-align:center;
}
#media (max-width: 300px) {
.logo{
text-align:center;
}
.cart{
width:100%;
text-align: center;
}
}
#media (min-width: 501px) {
.logo {
float: left;
}
.cart {
float:right;
}
}
Problem 2: Your OTHER & CONTACT menu buttons go on the next row because your navbar is in a div.container with a width of 750px for resolutions under 768px, and there is no place for themin the same row.
#media (min-width: 768px){
.container {
width: 750px;
}
}
You could give it another class, with different width, since you have the .container class assigned to more divs, and make it wider.
You could also give a min-height of 330px to your .navbar-colapse.in class to make the collapsed menu higher when resolution is under 768px:
#media (min-width: 768px){
.navbar-collapse.in {
overflow-y: auto;
min-height: 330px;
}
}
Problem 3: As Steve said, change your .carousel-caption style, giving width in percents (100%) and margins set to auto.
.carousel-caption {
width: 100%;
left: auto;
right: auto;
}
If you are trying to center divs with text-align that's not the way to go. You need to set up margin on right and left to auto:
#media (max-width: 300px) {
.logo{
text-align: center;
margin: 0 auto;
}
.cart{
width:100%;
margin: 0 auto;
}
}
The thing is your eElectronics is not taking all the space of the div, center div with margin auto on sides and text-align center inside the div.
For the menu in collapse mode, i get a scroll bar and everything is fine except it is a little small. Override with overflow: visible; and set z-index if it overlaps under other items.
.navbar-collapse.in {
overflow-y: visible;
}
The carousel captions are fine for me on Firefox. You just need to set a width in % and set margins on each side to auto. That div needs to be adaptative. Right now it stay at a size of 600px set in .carousel-caption.

CSS: Four Column Div Layout Shrinks to Stacked Two Column Div [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a four-column div layout.
When viewing the page on mobile, I want the layout to shrink to a two-column div.
JsFiddle
CSS:
.style1 {
font-style:normal;
font-weight:bold;
color:#FFFFFF;
}
#footer {
clear:both;
float:left;
width:100%;
}
#footer p {
margin-left:2%;
padding-right:2%;
}
/* Start of Column CSS */
.container1 {
float:left;
width:100%;
position:relative;
}
#col1 {
float:left;
width:23%;
min-height:268px;
position:relative;
margin-right:10px;
/*left:77%;*/
padding:10px;
overflow:hidden;
background-color:#DDDDDD
}
#col2 {
float:left;
width:23%;
min-height:268px;
position:relative;
margin-right:10px;
/*left:81%;*/
padding:10px;
overflow:hidden;
background-color:#DDDDDD
}
#col3 {
float:left;
width:23%;
min-height:268px;
position:relative;
margin-right:10px;
/*left:85%;*/
padding:10px;
overflow:hidden;
background-color:#DDDDDD;
}
#col4 {
float:left;
width:23%;
min-height:268px;
position:relative;
/*margin-right:10px;
left:89%;*/
padding:10px;
overflow:hidden;
margin-bottom:15px;
background-color:#DDDDDD
}
#year_banner {
width:auto;
margin:0px 10px 0px 10px;
align-self:center;
background-color:#E60000;
text-align:center;
}
img.displayed {
margin:0 auto;
display:block;
max-width:100%;
height:auto;
}
Any help is appreciated! Many thanks!
You have quite a lot of repeating code. You can eliminate that by using classes; this will also make it much easier to change all of the columns at once when the screen changes size.
For example:
.col{
float:left;
width:23%;
min-height:268px;
position:relative;
margin-right:10px;
padding:10px;
overflow:hidden;
background-color:#DDDDDD
}
#media screen and (max-width:380px){
.col{
width:50%;
}
}
I have prepared a simple example: https://jsfiddle.net/rey9xc1h/
body {
margin: 0;
}
.row {
width: 100%;
min-width: 320px;
margin: 0;
}
.col {
box-sizing: border-box;
background-color: #fff;
float: left;
padding: 10px;
margin: 5px 1%;
width: 98%;
}
#media (min-width: 320px) {
.row {
overflow: hidden; /* Clearfix */
}
.col {
width: 48%;
float: left;
}
}
#media (min-width: 768px) {
.col {
width: 23%;
}
}

CSS For responsive sidebar and main content?

I found what im looking for ALMOST, (http://codepen.io/anon/pen/PqxQVN).
But the only problem is i need the container to have height of auto and the sidebar to have a height of 100%. When i do that the sidebar disappears.
#container { height:500px; width:100% }
.sidebar { background:red; float:right; height:100%; width:20% }
.content { background:green; height: 100px; width:80%; float:left; margin-top: 20px;}
#media screen and (max-width: 1000px) {
.sidebar { clear:both; width:100% }
.content { width:100%; }
}
Any help would be appreciated. :)
I have used the universal selector (*) to remove padding and margin, therefore the sidebar will be 100% of height.
For what concerns the green container, by simply removing the height from 100px to "auto" it will automatically fill the content.
Link to codepen
*{
margin: 0;
padding: 0;
}
#container { height:500px; width:100% }
.sidebar { background:red; float:right; height:100%; width:20% }
.content { background:green; height: auto; width:80%; float:left; margin-top: 20px}
#media screen and (max-width: 1000px) {
.sidebar { clear:both; width:100% }
.content { width:100%; }
}
This design isn't perfect as you might find some text going under the sidebar, but should be a good start.
Regards,
-Gallo
give the height to sidebar as .sidebar{height:500px;} not to container
*{
margin: 0;
padding: 0;
}
#container { height:auto; width:100% }
.sidebar { background:red; float:right; height:500px; width:20% }
.content { background:green; height: 100px; width:80%; float:left; margin-top: 20px;}
#media screen and (max-width: 1000px) {
.sidebar { clear:both; width:100% }
.content { width:100%; }
}

Layout breaks on browser resize

Got a weird problem,i.e,Layout of webpage breaks on resize of the browser/when accessed through mobile device/when switched to lower resolution ( 800*600 / 1024*768 )
Temporary link : http://krishnaspirit.hostoi.com/
P.S : Its a full-width website & Works fine on 1366*768.
CSS :
* { padding:0px; margin:0px; }
p,h1,h2,h3,h4,h5,h6,a{ font-family: 'Armata', sans-serif; }
/*header*/
.header { z-index:5000; position:fixed; top:0px; width:100%; min-width:960px; height:100px; background-color:#4E4E56;}
.header #nav li:last-child { margin-right:0px; }
.header #nav li { transition:1s ease all;line-height:40px;
display:inline-block; margin-right:3px; width:116px; height:40px;text-align:center; border-radius:5px;}
.header #nav li a { font-size:15px; color:#DCD0C0; display:block; height:100%; text-decoration:none; }
.header #nav li:hover { background-color: #DA635D; }
.header #nav li a:hover { color:#fff; }
.header #nav { position:absolute; right:10px; top:35px; width:600px; }
.current {background-color: #DA635D; cursor:none;}
.header #nav .current a { color:#fff; }
.header .logo img {width:100px; height:95px; position:absolute; top:5px; left:12px; line-height:100px; font-size:40px; color:#fff;}
.header .logo .name { font-size:40px; color:#ccc;
font-family: 'Armata', sans-serif; position:absolute; left:110px;top:29px; }
/*sub-head*/
.sub-header .top-logo { float:left; }
.sub-header .top-name { float:right; font-size:30px; padding-top:120px; color:#003366; }
.sub-header { width:85%; text-align:center; margin:150px auto; }
/*main-body*/
.content { float:left; }
.news { float:right; margin-right:5px; }
.news { box-shadow:3px 3px 5px #ccc; padding:0px; border-radius:3px; }
.news p { font-weight:bold; font-size:15px; padding-bottom:20px;}
.news { border:1px solid #ccc; width:27%; min-width:27%; padding:5px; height:300px;}
.news .news_body { padding:3px; text-align:center;}
.content { box-shadow:3px 3px 5px 0px #ccc; width:70%; height:300px; border-radius:3px; margin-left:5px; border:1px solid #ccc; font-size:15px; padding:5px;}
.content .sub-main { margin-top:15px; text-align:justify; padding:10px; }
.content #myMenu { width:100%; text-align:center; height:30px; }
.content #myMenu{ list-style-type:none; }
.content #myMenu li { border-bottom:1px solid #ccc; color:#999; display:inline-block; width:30%; line-height:30px;}
.content #myMenu li:hover { font-weight:bold; }
.content #myMenu li a { display:block; width:100%; }
.content #myMenu li a:hover { border-bottom:5px solid #DA635D; color:#DA635D; cursor:pointer;}
.body_wrapper { margin-bottom:500px; margin-top:70px; background-color:#ccc; }
/*footer*/
footer { width:100%; height:50px; line-height:50px; background-color:#4E4E56; }
footer p a{ color:#DA635D; text-decoration:none; font-weight:bold;}
This is not a weird problem. You need to use #media query to fix the problem.
The image on left side is large enough to occupy almost half of total screen width at a resolution 1024x768. You need to define style sheet on different screens using media query.
Example -
#media screen and (max-width: 1024px){
.main .sub-header .top-logo{
width: 250px;
}
.main .sub-header .top-logo img{
width: 100%;
}
}
This snippet will only take action in device screen width less than or equal to 1024px.
As you can see, for this type of device resolution I have reduced the width of .top-logo to make sure it can fit properly in small screen. You need to define .top-name and other elements accordingly to fit them properly for different device width. Hope you get the idea.
Another thing causing this problem is when one #media query has a missing closing brace }.
For example if your CSS has something like:
nav {
position: relative;
width: 95%;
margin: 10px auto 0 auto;
height: auto;
background-color: var(--magnolia);
font-family: Roboto;
}
#media screen and (min-width: 860px) {
nav {
margin: 0 auto;
}
<----------------- /* A closing brace "}" is missing here ! */
.navbar {
display: flex;
list-style-type: none;
flex-flow: row wrap;
justify-content: space-between;
align-content: center;
align-items: center;
margin: 0 auto;
padding: 0;
height: auto;
}
etc.
etc.
This makes all CSS below it revert to default, e.g. width: 100%.
Overflow abounds in such a situation.
Since W3C CSS Validator may not detect this, you have to closely read through all #media queries before the breaking elements looking for missing closing braces. Just one of these can screw up everything below that part of the web page. Fix it and everything is suddenly fine.

Media query to move heading to right of image

I have a site that needs the placement of an h3 moved depending on the screen size.
The html is currently a table like this:
<table><tr><td><img src="someimage"></td><td><h3>Heading</h3></td></tr></table>
On smaller screens, I would want the h3 to appear above the image, with the image full width, so I start with:
<div class="heading-image">
<h3>Heading</h3>
<img="someimage">
</div>
That just works, but on larger screens I want the h3 to drop to the right of the image, which would be at 50% of the screen:
.heading-image img { max-width: 50% };
.heading-image h3 { max-width: 50%; float:right };
Now the h3 drops next to the image on the right hand side. But then it comes to the age old problem of vertically centering multi-line text inside a div, where the height of the div is unknown (dependent on the size of the image and the current screen width), and the length of the text is unknown (these are entered into a CMS).
I have tried a variety of approaches found on the internet, but I am just not getting my head around it. I tried making both the img and h3 a table-cell to get me back to my original approach but I can't get the alignment to work.
Here's the code. Just change the size you want to collapse in the #media-query.
Working fiddle: http://jsfiddle.net/Hv6x3/
HTML
<div class="heading-image">
<h3>Headline text goes here...</h3>
<img src="http://placeimg.com/640/480/tech" alt=""/>
</div>
CSS
.heading-image img {
float: left;
max-width: 50%;
height: auto;
}
.heading-image h3 {
float: right;
width: 50%;
max-width: 50%;
text-align: left;
}
#media (max-width: 600px) {
.heading-image {
text-align: center;
}
.heading-image img {
float: none;
max-width: 100%
}
.heading-image h3 {
float: none;
max-width: 50%;
}
}
for vertical-align....use display:table-cell
make sure the parent div is set as display:table
jsfiddle demo
html, body {
width:100%;
height:100%;
margin:0;
padding:0;
}
#media only screen and (min-width : 600px) {
.heading-image {
width:100%;
height:100%;
margin:0;
padding:0;
text-align:center;
display: table;
}
.heading-image .img {
float:left;
height:100%;
}
.heading-image .h3 {
float:right;
display: table;
height:100%;
}
.heading-image .h3 h3 {
display: table-cell;
vertical-align: middle;
text-align: center;
}
}
#media only screen and (max-width : 600px) {
.heading-image {
width:100%;
margin:0;
padding:0;
text-align:center
}
.heading-image .img {
width: 100%;
}
.heading-image .img img {
width:auto;
height:auto;
}
.heading-image .h3 {
width: 100%;
vertical-align:middle;
}
}