Responsive "list" in two columns - css

I'm trying to build a list of two types of items, let's call them green and red. In resolutions below 980 I want them stacked in one column, in resolutions above 980 I want them stacked in two columns.
This is what I got so far: http://kristofferk.se/list-test/
(Play around with your browser width to see the one/two column-thing.)
My question: is there an easy way to make the items stack (without the vertical space in between) using just CSS? Not interested in jQuery etc.
Thanks!

you can achieve your results through CSS media queries this is the roughly idea what i am mentioning below:-
#media screen and (max-width: 980px)
{
.class {
background: #ccc;
}
}
read more about media queries :- http://webdesignerwall.com/tutorials/css3-media-queries
UPDATED
just give clear:right to your .in class that vertical space will remove....
.in {
clear: right;
color: green;
float: right;
}

You can write like this:
.in {
clear: right;
color: green;
float: right;
}
.in + .in {
margin-bottom:-30px;
}

Related

Breaking 1 row with 4 divs into 2 rows and to colums with media queries

I am trying to break the following row with 4 images on screens that are equal or less than 800px:
https://i.ibb.co/1RmqDN2/1row.jpg
Into 2 rows and 2 columns, So it will look something like this:
https://i.ibb.co/DWJp7mM/2row.jpg
I am using media queries to try and achieve it, But I am not finding a solution. I am a coding noob and also on css and would love some help. Here is what I have tried so far:
#media only screen and (max-width: 800px) {
.AR_55.ob-strip-layout .ob-dynamic-rec-container /*This is the selector of each column (image and the text below it)
{
width:25%;
float:left;
}
}
This is the closest I got:
#media only screen and (max-width: 800px) {
.AR_55.ob-strip-layout .ob-dynamic-rec-container /*This is the selector of each column (image and the text below it)
{
width:45%;
float:left;
}
}
It breaked to 2 columns and 2 rows, But the alignment was bad and I couldn't fix it.
Any ideas? Happy to provide more info if needed. Thanks!
To achieve perfect alignment, you can have these divs as columns in a table. Below links details how you can split table columns into 2 different rows for smaller screens-
Split Table Row(s) for smaller screens
Using your method, the following should work:
.AR_55.ob-strip-layout .ob-dynamic-rec-container {
width: 25%;
float: left;
}
#media only screen and (min-width: 800px) {
.AR_55.ob-strip-layout .ob-dynamic-rec-container {
width: 50%;
float: left;
}
}
I also used min-width within the media query, my preference read more about it here
Also, feel free to update your question with your code shared with us in a codepen or similar.

How do I correctly use Media Query to cause one column to disappear and the other to adjust to the width of the screen?

In my class we are starting to use Media Queries and I am having a little trouble with an assignment. For a previous assignment we were tasked with remaking a website called "the Toast" as best we could, which I have here. Now for this assignment we are to use media query to do a few things:
This assignment is all about media queries and getting your site to be
responsive. We will be using the website The toast again for this
assignment. You will be laying out two columns for the content area.
When the screen size hits 960px the right column must disappear. The
articles in the left column must adjust to the width of the screen.
The images must get bigger and fill the article at 960 px as well.
At 760 px the support us button, love the toast text and the social
media must disappear.
In the code I have two columns, a "bigColumn" and a "adColumn". Now to my understanding to make the adcolumn disappear and adjust the bigColumn I simply have to add:
#media only screen and (max-width: 960px) {
.main {
.bigColumn {
width: 100%;
}
.adColumn {
display: none;
}
}
}
However this is not working. The ad never disappears and the rest of the content doesn't do anything in terms of filling the rest of the page when shrinking the window. If I change the background color in the .main the color changes, but changing anything in the two divs has no effect that I can see. I can get the social media icons to disappear at 760px just fine, so am I just missing something with the media query for the columns? Or could something else be interfering with it?
EDIT: Guess I should mention that yes, I am indeed using SASS in the project.
Here is the styling I have for the columns before I started the media query:
.main {
width: 90%;
display: flex;
min-height: 200px;
margin: 0 auto;
//column for main-page content
.bigColumn {
width: 800px;
height: 100%;
margin-top: 20px;
margin-right: 9%;
margin-left: 13%;
}
.adColumn {
margin-top: 20px;
position: relative;
min-height: 120px;
}
}
I don't believe you can nest your CSS like that unless you are using a preprocessor like LESS or SASS. Try taking the .bigColumn CSS out of the .main brackets and leave it on its own.
#media only screen and (max-width: 960px) {
.bigColumn {
width: 100%;
}
.adColumn {
display: none;
}
}
Based on your css I think you're close, but there appears to be a an error in the way you've structured your css. Give this a try. I'm assuming .bigColumn and .adColumn are children of .main:
/* All screens 960px or less */
#media only screen and (max-width: 960px) {
.main .bigColumn {
width: 100%;
}
.main .adColumn {
display: none;
}
}

Responsive CSS on Display-Listings-Shortcode

I installed a plug-in called Display-listings-shortcode, and added the columns-extension to allow for columns the blogs halfway down the homepage at RitaNaomi.com will be horizontally displayed on a web browser. It looked whacky at first with titles being scrunched beside and underneath the image, but eventually i figured out how to edit the .display-posts-listing class to change the display
.display-posts-listing .listing-item {padding-bottom:30;}
.listing-item
{
float:left;
width:22%;
margin: 40px
}
But when I look at it on a mobile device, they're all scrunched together as if it was still being displayed on a laptop. I want to have it listed vertically and not horizontally, because thats the way it would fit best.
I tried (and it didn't work) to use #media to change it through the css, but it didn't work.
#media handheld {
.display-posts-listing .listing-item {
clear: both;
display: block;
}
.display-posts-listing img {
float: left;
margin: 0 10px 10px 0;
}
}
You shouldn't be using #media handheld {} since it's been deprecated according to MDN.
You're better off targeting pixel-width values. You may need a couple queries, and some of the oldschool standards were 1023px, 767px. Feel free to replace the 900px below with whatever works for you.
#media only screen and ( max-width: 900px ){
.display-posts-listing .listing-item {
/* CSS Here */
}
}
Removed the custom CSS that was already added from the original theme. It was interfering with the Columns display.
Not using #media handheld {} because it was deprecated (thanks to xhynk for the response), and instead used the command (max-width: 768) , the point at which the title and image css look funky.
To make the title display on its own line on a bigger screen, i added this to my CSS:
.display-posts-listing .listing-item .title { display: block; }
And now i'm using the above media query to figure out how to style it on smaller devices.
Complete CSS: https://gist.github.com/billerickson/17149d6e77b139c868640a0ed3c73b3a

css components and responsive coupling - best practices?

Say I create a component like a table that I wish to reuse across different pages. The content and number of columns in the table will vary from page to page, but the look and feel should be the same.
.my-table {
background-color: ... etc
color: ... etc
}
When making this component responsive - should each page have it's own set of media queries that alters the look of this component, or should the component itself define the queries?
Ie. I may want to hide columns 3 and 5 of this table when viewing on mobile, but on another page there may only be 3 columns and I don't want column 3 to be hidden on that page. What is the better practice to use in this case? Should the responsive logic be decoupled from the component styles?
Another example - most css guides i've read recommend doing something like this:
.layout-columns-2 {
float: left;
width: 50%;
}
.layout-columns-4 {
float: left;
width: 25%;
}
Which you can reuse on layouts whenever you need columns to float side by side. But again you run into similar issues the moment you try applying responsive rules.
Say for example on one page, you want to break the float of a particular div when the width is 600px so that the columns are stacked vertically. But on another section of the page, you want the float to break at 800px. What is the best approach to this?
I'd suggest creating a Sass partial with some table mixins, which can be applied as needed. This will let you decouple your table "modules" from your actual tables. Decoupling is usually a good thing, as it lets you add and change styles with greater ease and consistency.
Since you want your breakpoints to vary from table to table, I would leave the table styles as "mobile-first" by default, and customize them one-by-one as appropriate.
_tables.scss:
table {
// Global, mobile-first styles go here
}
#mixin columns-2() {
td {
float: left;
width: 50%;
}
}
#mixin columns-4() {
td {
float: left;
width: 25%;
}
}
// Function to return a max breakpoint (1px less)
#function max-break($size) {
#return $size - .0625em;
}
Using your partial, you can apply your mixins as appropriate for any specific table ...
special-page.scss:
#import 'tables';
.my-table {
// Hide columns 2/4 below 800px
#media (max-width: max-break(50em)) {
td:nth-child(2),
td:nth-child(4) {
display: none;
}
}
// Use 2 columns at 600px
#media (min-width: 37.5em) {
#include columns-2();
}
// Use 4 columns at 600px
#media (min-width: 50em) {
#include columns-4();
}
}

Media Query Styles Not Overriding Original Styles

I'm attempting to use some media queries for a website I'm building. The problem I'm having however, is while the media query styles are actually being applied, they're being overridden. I can't for the life of me tell why because I'm using the same exact selectors. Can anyone point out something that I'm not seeing?
ORIGINAL CSS
#global-wrapper-outer > #global-wrapper-inner {
width: 85%;
height: 100%;
margin: 0 auto;
position: relative;
}
#global-wrapper-outer > #global-wrapper-inner > nav {
background: #fff;
padding-bottom: 20px;
box-shadow: 0 4px 2px -2px gray;
}
MEDIA QUERY CSS
#media screen and (max-width:1024px) {
#global-wrapper-outer > #global-wrapper-inner {
width: 100%;
}
#global-wrapper-outer > #global-wrapper-inner > nav {
display: none;
}
}
The second media query is working fine, where I set the nav to have a display of none. However, when I try to set the width of #global-wrapper-inner to 100% it doesn't apply. I can see the style being "applied" when I press F12 and select that element. However, the style itself is crossed out and not actually applied and it still has the original width of 85%.
The selectors in your original CSS have the same specificity as the selectors within your media queries (the first declarations are also targeting the same property - width) and because the media query rule set is being overridden I'm going to assume that it appears before the original rule set.
The second media query selector works because it's targeting a property that wasn't set in your original CSS, so specificity isn't relevant.
To have the first media query selector take precedence, prepend an ancestor element to it:
#media screen and (max-width:1024px) {
body #global-wrapper-outer > #global-wrapper-inner {
width: 100%;
}
#global-wrapper-outer > #global-wrapper-inner > nav {
display: none;
}
}
You need to link the media query file (queries.css) later than the normal css file (style.css). That way the rules in the queries.css will override those in style.css.
I have been at least 2 hours trying to find the override CSS problem till I found that my line comments where wrong... And the second definition of CSS wasn't working:
So, don't be so stupid as I !:
/* LITTLE SCREENS */
#media screen and (max-width: 990px) {
... whatever ...
}
/* BIG SCREENS */
#media screen and (min-width: 990px) {
... whatever more ...
}
never use: Double bar as I did:
// This is not a comment in CSS!
/* This is a comment in CSS! */
Here is the answer. (at least what worked for me)
I've had this problem before, and it took me a while to realize what I did, but once I figured it out it's actually pretty easy.
Ok so imagine I have this as the html
<main>
<div class = "child1"> </div>
<div class = "child2"> </div>
</main>
and then this as the CSS
main .child1{
height: 50px;
}
/* now let's try to use media quaries */
#media only screen and (max-width: 768px) {
.child1{
width: 75%;
}
}
The code above won't affect the .child. Just like someone mentioned above, the main .child1 overrides .child1. So the way you make it work is to select the element just like we did at the very beginning of the CSS above.
/* this will work */
#media only screen and (max-width: 768px) {
main .child1{
width: 75%;
}
}
So as a conclusion... select the elements the same way every time.
Meaning ... for example in the above code, in your CSS, you should either select it as main .child1throughout the whole CSS or .child1 or else they get mixed up, one ends up overriding the other.
From the code you submitted, this probably won't resolve your issue. However, in your CSS if you are nesting styles inside of one another:
.main-container {
.main {
background: blue;
}
}
A media query for .main won't work because of the nesting. Take .main out of .main-container and then the media query will work as assumed:
.main-container {
}
.main {
background: blue;
}
Check if your media query braces are equal.
Sometimes it is very subtle but when you miss a single brace the rest of the media queries mentioned for certain break points will not work
example:
#media(min-width: 768px) and (max-width: 991px){
#media (max-width: 767px){
.navbar-brand p {
font-size: .6em;
margin-top: 12px;}
.navbar-brand img {height: 20px;}
#collapsable-nav a {
font-size: 1.2em;
}
#collapsable-nav a span {
font-size: 1.2em;}
}
Here you can see i have started the braces for max-width:991px but forgot to end so the next set of codes in media query for max-width:767px will not work.
It is a very simple mistake but took hours because of lot of braces in the codes.
Hope it helps. Happy Coding!
What about using !important? If you range your media query from ( min-width: 176px ) and ( max-width: 736px ) or even up to 980px?
There can be some reasons because of which this type of error may occur.
I myself faced this issue where I was not able to understand what I am needed to do and was confused that, does media query just overrides the elements.
Here's what I understood:
MEDIA QUERY CSS:
#media screen and (max-width:1024px) {
#global-wrapper-outer > #global-wrapper-inner {
width: 100%;
}
#global-wrapper-outer > #global-wrapper-inner > nav {
display: none;
}
}
here you were able to override #global-wrapper-inner > nav i.e., 2nd media query selector, by display: none;
because you never added the display line in the original css, because of which there was nothing to override you just have given that display type should be none.
Whereas just in the 1st media query selector you already had given width:80%;
Basically media query doesn't override as far as I have understood but it take precedence, like already explained by one of them
by which media query comes to work:
https://stackoverflow.com/a/19038303/15394464
also if still did not get your doubt clear, https://www.youtube.com/watch?v=acqN6atXVAE&t=288s
then this might help.

Resources