Styled-components media queries doesn't working in React App - javascript

I'm trying to add breakpoints to my React App created using CRA. But styled-components seems to ignore the pixels given in the rule declaration unless it passes the 1000px, not allowing me to work properly. Here's the code:
import styled from "styled-components";
export const Margin = styled.div`
padding-top: 5vh;
height: 100vh;
overflow: hidden;
margin: auto;
width: 90vw;
#media (min-width: 768px) { //here is where the problem is
width: 50vw;
}
}`
So, as you can see I've set a breakpoint when screen oversize the 768px (using mobile first). It will ignore that and just set that rule to default, for example, now the width will be 50vw no matter the screen size is minor than 768px. Only if I set pixels above 1000px it'll work properly. How can I fix this?

Related

nth-child being applied at all screen widths

Currently I have the following for a react components css
export default styled.div
#media (min-width: getEmFromPx(400)) {
margin: ...
width: ...
:nth-child($2n+2) {
margin-right: 0px;
}
}
#media (min-width: getEmFromPx(700)) {
margin: ...
width: ...
:nth-child($3n+3) {
margin-right: 0px;
}
}
#media (min-width: getEmFromPx(1000)) {
margin: ...
width: ...
:nth-child($3n+3) {
margin-right: 0px;
}
}
I am setting which element in the div to apply the margin-right 0px while the other have a margin. This works perfectly on small screen sizes but for medium and large it seems the small nth selector is present and overriding the nth selectors in the medium and large... Is there a way to overcome this?
On chrome developer tools I can disabled the nth child selector for small screens which makes medium screens work perfectly. It was my understand that at different media screens the selector would be updated?
your first media query #media (min-width: getEmFromPx(400)) will be apply to all screens having 400px or greater than 400px, because you are using min-width (it is it should applicable min this and up all).
in your 2nd and 3rd media query you are doing same thing, so no need to write it twice.
Here you are setting margin-right: 0 to nth-child(3n+3), but you will have to reset nth-child(2n+2) element which is defined in previous media query. Because nth-child(2n+2) will be applicable all screens 400px above.

Make Intro.js tooltip responsiv

I'm using intro.js to guide users through my application the first time they log in. It's a great tool and it works, the only downside I see is that the introjs-tooltip element is not responsiv.
As you can see below the tooltip has a min-width and max-width in px which really don't make it very responsiv. Also this makes the image I include in some of the steps really small and kind of useless, because of the small size.
CSS - Simplyfied
.introjs-tooltip {
position: absolute;
visibility: visible;
padding: 10px;
min-width: 200px;
max-width: 300px;
}
I have been working on this "issue" for a few days now, and can't figure it out.
So my questions are: Is there a way to make the tooltip use % instead of px without having to change to much in the intro.js file? If not, what would you guys recommend me to do?
You can provide own implementation for .introjs-tooltip in your custom css file:
.introjs-tooltip {
min-width: 100%; // change to desired
max-width: 100%; // change to desired
}
or you can use an additional class eg: .tooltip-large:
HTML
<div class="introjs-tooltip tooltip-large">
...
</div>
CSS
.tooltip-large {
min-width: 100%; // change to desired
max-width: 100%; // change to desired
}
In my case, "min-width" and "max-width" css rules are not working as expected, at least for floating tooltips in a responsive desing. In ended up doing the following. Maybe this is helpful for someone (tourTooltip is a custom class I defined for tooltips):
/* Size < Small */
#media screen and (max-width: 756px) {
.tourTooltip {
min-width: 300px;
}
}
/* Size > Small */
#media screen and (min-width: 756px) {
.tourTooltip {
min-width: 500px;
}
}

Reduces image by width first, then after max-width, proportionally

I have a image that have 1200 x 200px. On center of this image I have a space with 500 x 200px that is the main content of full image. On each side of this image, I have an additional content. Note: it is on a single image.
If the window width is reduced, then first it should consumes the additional content of the image, but cutting it, keeping image height intact. But if I reduces the window width below of the main content width (in this time, all additional content was cutted off), then now the image should be resized proportionally, affecting the height.
My doubts:
Is possible do it only with CSS?
If not, there are some JS library to do that?
If not, how I should structure the HTML, CSS and JS to do it works?
It's an example banner with full width: Note that is have a main content and two sides with additional content.
This image below should help understand: I tried to simulate a window width resize, on 1200 px, 1000 px and 500 px (that not affect height yet) then by 350 px (that affect and resize image proportionally).
#banner {
background-image: url("http://i.stack.imgur.com/csRha.png");
width: 100%;
height: 200px;
background-repeat: no-repeat;
margin: 0 auto;
display: block;
border: 1px solid red;
}
#media all and (min-width: 1200px) {
#banner {
width: 1200px;
}
}
#media all and (max-width: 1200px) {
#banner {
background-position: 50% 0;
}
}
#media all and (max-width: 500px) {
#banner {
background-size: 240%;
}
}
<div id="banner"></div>
Is possible do it only with CSS?
Yes, and you only need 1 <div>. Treat the image as a background image, positioned dead center with background-position. Then resize the <div> using media queries, setting widths to the designated breakpoints.
You might use a simple media query like this:
#media max-width: 500px {
#your-image {
width: 100vw;
}
}
Use media queries for adding images/elements and changing css.
As for your problem use percentages for re-sizing images.
I suggest to look up progressive enhancement and Responsive design.
Also look up view-port in case you are not using it.
Don't use VW there is not enough support yet in IE,EDGE(no support for VMAX) and Opera mini, ie8 (no support at all).

How to get the browser border to lock to a div when resizing?

I'm looking at this 3 fixed-column CSS layout on http://www.vanseodesign.com. I want to use this layout however I'd like to change the way it behaves when I resize the browser.
When the browser is open nice and wide the columns are centered nicely on the page:
Then we reduce the width of the browser and it locks to the left side of the left-most column like this:
What I'd like to do is change the CSS (or javascript if necessary) so that the browser locks to the left side of the middle column instead when the browser becomes too narrow:
I'm not sure how to achieve this though?? Can anyone suggest how to change the code and most importantly why your solution works?
EDIT:
For those reading this question: I marked Salem Ouerdani's answer as the correct one because he was the first to answer with a solution that worked the particular way I wanted. However, it became clear that people were interpreting the question in slightly different ways. So it is worth reading through because there are some really great answers which might suit your situation better. Please upvote them as such.
Try to modify your container div from this :
#container { width: 960px; margin: 20px auto;}
to this :
#container {
width: 960px;
margin: 20px auto;
position: absolute;
left: 50%;
margin-left: -480px; /* half the fixed width */
}
EDIT :
Also you need to add this in order to lock to the left side of the center div whenever your browser size is beyond the fixed 960px :
#media (max-width: 960px) {
#container {
left: 0;
margin-left: -240px; /* Primary Sidebar width */
}
}
UPDATE : As #media (max-width: 480px){} did better solve the issue rather than 960px then I'm adding the related code pen example with the final solution : >> Codepen sample code
Revised Solution (based on OP comment)
Solution by #spenibus is the best and I recommend that. The solution does not work with browsers <= IE9 (IE9 only partially supports it). If you wish to make it cross browser compatible, then I suggest using javascript to manually set the scroll.
Fiddle: http://jsfiddle.net/dp7gwcn5/4/
$(function () {
$(window).resize(function () {
var width = $(window).width();
if (width <= 960) {
if(width <= 480) $(document).scrollLeft($('#content').offset().left);
else $(document).scrollLeft((960-width) / 2);
}
});
});
Old Solution
Solution #1 (Scroll: Do not hide anything, just cling)
If you wish to keep the sidebar still accessible by scrolling but only make the browser's left edge stick to the left edge of your main content #content. Then manipulate the scroll
Fiddle: http://jsfiddle.net/dp7gwcn5/. The fiddle will let you change the size of your preview window, as soon as you shrink it enough to hide the secondary sidebar the window clings to the left edge of #content
$(function(){
$(window).resize(function(){
if ($(window).width() < 720) {
$(document).scrollLeft($('#content').offset().left);
}
});
});
Note: The width 720 was given based on the widths you have set on your sidebars and conents, you can modify it to be calculated on the fly if you wish.
Solution #2 (Static: Hide primary sidebar)
If you wish to hide the primary sidebar then simply use a media query to hide the primary sidebar when your window size is small
Fiddle:http://jsfiddle.net/dp7gwcn5/1/
#media all and (max-width:720px) {
#primary {
display:none;
}
#container {
width:720px;
}
}
Solution #3 (Dynamic: Hide primary sidebar)
Solution #2 is static and there is not much you can do about it, however, if you wish to compute the widths on the fly and hide the side bar, use a script
Fiddle: http://jsfiddle.net/dp7gwcn5/2/
Script
$(function(){
$(window).resize(function(){
if ($(window).width() < 720) $('#container').addClass('small');
else $('#container').removeClass('small');
});
});
CSS
#container.small {
width:720px;
}
#container.small #primary {
display:none;
}
My Suggestion
My suggestion is to use Solution #1 if you do not wish to make it mobile compatible. If you wish to make it mobile compatible I suggest using Solution #2 with a menu button to show your primary sidebar.
Note: Solution #1 and Solution #3 will work on most browsers. Solution #2 will not work on browsers < IE9
Update 2015-08-03 01:59 +0000
Reading your comment on another answer about staying centered until the left edge of the centered column meets the browser edge made things much clearer.
This will keep the middle column centered until the browser is as large as the column, then it will stick to the left edge of the column.
html, body {
margin:0;
padding:0;
}
#media (max-width: 480px) {
#container {
margin-left:-240px;
}
}
#media (max-width: 960px) and (min-width:481px) {
#container {
margin-left:calc( (960px - 100%) / -2 );
}
}
Original anwser
Pure css. The values of "960" and "720" should be adjusted to account for body margin. Works under Firefox, no guarantees elsewhere. Using calc() for adaptive negative margin based on browser width.
/* fixed negative margin below 720px width */
#media (max-width: 720px) {
#container {
margin-left:-240px;
}
}
/* adaptive negative margin vetween 720px and 960px width */
#media (max-width: 960px) and (min-width:721px) {
#container {
margin-left:calc( (960px - 100%) * -1 );
}
}
Preview
From what I can see, this template uses the margin 0 auto in the container. The way the margin is set up, it centres the "container" which contains all three columns. This means that the content is currently centred by treating each of the three columns as one big one.
This layout is also not responsive. This means that it does not adapt to larger or smaller browser sizes. It has a fixed width and height.
A simple solution is to change the pixel units to %. This will create a fluid layout that will adapt more easily to the browser size as the measurements are based on a portion of the window size instead of having a fixed size.
For example:
body {
width: 50%; }
No matter what size the browser window is, the body will only account for half the size.
If you are looking to have all three columns visible in the exact same layout or just visible in the browser without scrolling to the side no matter what device or screen size, then you need to use media queries. Media queries are css properties that let you set specific css styles for specific resolutions or screen sizes.
For example:
#media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
}
#media screen and (max-width: 600px) {
body {
background-color: purple;
}
}
In my example (although very rough), the background of the body will be purple up to a small size of 600px. It will then change to blue until a size of 300px.
You can read more about media queries here: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
1 - remove the margin from your BODY CSS.
2 - wrap all of your html in a wrapper ... all your body content
3 - Define the CSS for the wrapper:
This will hold everything together, centered on the page.
#conatiner{
margin-left:auto;
margin-right:auto;
width:960px;
}
.container {
max-width: 960px !important;
margin: 0px auto;
position: relative;
}
it will work definitely .
html, body
{
overflow: scroll !important;
ms-overflow-y: auto;
float: none !important;
overflow-y: auto;
overflow-x: visible;
}
try this in css
or in script use
function enableScrolling()
{
document.body.scroll = "yes";
document.body.overflow="hidden";
}
#media(max-width : 960px)
{
#primary{width:0;}
#content{width:66.7%}
#secondary{width:33.3%}
#container{width:100%}
}
Hope this will work for you.
You can do this easily with media queries
At the bottom of your CSS, just add something like:
#media screen and (max-width : 960px) {
#container {
margin-left: -260px;
}
}
When the window size becomes less than 960px, it will shift the container to the left by the width of the left column, leaving it fixed on the left side at the middle column
Here is a fiddle: https://jsfiddle.net/Locwv54q/
EDIT:
With the addition of CSS transitions to make things smoother you can use something like this
At the bottom of your CSS add a media query:
#media (max-width : 960px) {
#container {
left: 460px;
margin-left: -700px;
}
}
Then during the definition of container earlier in the CSS, use something like this:
#container {
position: absolute;
left: 50%;
width:960px;
margin-left: -480px;
margin-top: 20px;
margin-bottom: 20px;
-webkit-transition: margin-left 1s ease-in-out;
-moz-transition: margin-left 1s ease-in-out;
-o-transition: margin-left 1s ease-in-out;
transition: margin-left 1s ease-in-out;
}
Of course can fiddle with the values/timings to get things exactly as you want, but this general approach works on my end
updated jsfiddle: https://jsfiddle.net/8L5n58jy/
EDIT2:
This should now work as intended
#media (max-width : 480px) {
#container {
margin-left: -240px;
left: 0px;
}
}
#container {
position: absolute;
width: 960px;
left: 50%;
margin-left: -480px;
margin-top: 20px;
margin-bottom: 20px;
}
I would suggest to always center the #container with
position: absolute;
left: 50%;
transform:translateX(-50%);
Then when it will only fit the center column and 1 column (max-width: 720px) you fix the position:
#media (max-width: 720px) {
#container {
left:240px;
transition: left 2s;//make it smooth
}
}
A plus for the crazy tab resizers would be to add transition to animate the change transition: left 2s; (but thinking about 'normal' users it may not be necessary)
fiddle
You should definitely read about Twitter-Bootstrap framework, bro:
<div class="container">
<div class="row>
<div class="col-sm-3">
<h2>Left</h2>
</div>
<div class="col-sm-6">
<h2>Center</h2>
</div>
<div class="col-sm-3">
<h2>Right</h3>
</div>
</div>
</div>
would do your work, as it is RESPONSIVE. Just saying!
Why not use Bootstrap to create the columns and use the col-sm- or the col-xs- to pull the middle container into place?
Here is the example at the Bootstrap Website

CSS media query height greater than width and vice versa (or how to imitate with JavaScript)

The majority of desktop and laptop screens nowadays have a width greater than the height. The screen is "wide" not "tall." Smart phones have done something rather cool by enabling the orientation of the phone to influence how the content is presented.
I'd like to do this with media queries, so that if someone on a mac with a big monitor has their browser window sized so that it's very "tall" (height is greater than width) they would see a header and footer. But if they went fullscreen or "wide" (width is greater than height) they would see a sidebar on the left and maybe also the right.
I'm trying to take full advantage of wide screens, and orientations and such. How to do this with media queries or javascript?
I'm sure you have it by now, but here is an example for others who pass by. Like the previous person said, people should take the time to read this: http://www.w3.org/TR/css3-mediaqueries/
Now, here is an answer. You can put "landscape" or "portrait" in conjunction with widths and heights in your #media rules. This assumes that height is greater than the width and vice versa. I usually only use min-width and then have a few separate #media rules for those specifically. One example would be landscape: horizontal scroll (desktop) and portrait: regular vertical (tablet/phone )
Those 2 wouldn't do it alone though, you'll need some combinations. I think we can assume your sidebar would be a hindrance on screens smaller than 600px wide.
/* 01 */
#media (min-width: 0) {
/* this is the same as not using a media query... */
.main-content {
width: 100%;
float: left;
}
.side-bar {
width: 100%;
float: left
}
}
/* 2 */
#media (min-width: 600px) and (orientation:landscape) {
.main-content {
width: 70%;
float: left;
}
.side-bar {
width: 30%;
float: left
}
}
HERE is a jsfiddle - note that box-sizing: border-box; is used for padding issues.
2017 UPDATE
I think most people would use flexbox now: https://jsfiddle.net/sheriffderek/egxcgyyd/
.parent {
display: flex;
flex-direction: column;
}
#media (min-width: 600px) and (orientation:landscape) {
.parent {
flex-direction: row;
}
.child-1 {
min-width: 260px; /* or flex-basis: xx - try them both */
}
.child-2 {
flex-basis: 100%; /* "if at all possible... please try to be this size..." */
}
}
I know that this is an old question, but the solution that I used was this:
.main-content {
/* Default for when the height is greater than the width of the screen*/
width: var(--content-width-small);
}
#media screen and (min-width: 100vh) {
/* The width is greater than the height */
.main-content {
width: var(--content-width-wide);
}
}
This is much more intuitive, and works perfectly well (at least for me, because I wanted to have an image with dimensions based on whether the screen's height was greater than it's width or vise versa).
Media Queries are probably going to be your solution here for the modern browsers that support it. You can grab a copy of the documentation from here:
http://www.w3.org/TR/css3-mediaqueries/
But you might find the following tutorial useful (Google for: Media Queries Tutorial):
http://webdesignerwall.com/tutorials/css3-media-queries
Once you pick up the basics doing things like hiding elements if the screen falls below a specific resolution:
#media screen and (max-width: 600px)
{
.sidebar
{
display: none;
}
}
Hope this helps.
As stated prior, media queries are the way to go.
More specifically, if you are attempting to detect if the viewport is taller than it is wide (height > width), you might take a look at the aspect ratio documentation.
For example, let's say you wanted to hide or show a different title based on when the viewport is tall or wide. Since a 1/1 aspect ratio is a perfect square, you can use a combination of min-aspect-ratio and max-aspect-ratio to detect when a change between "tall" and "wide" occurs.
The code might look like this:
#media (max-aspect-ratio: 1/1) {
body {
background-color: cornflowerblue;
}
.wide {
display: none;
}
}
#media (min-aspect-ratio: 1/1) {
body {
background-color: whitesmoke;
}
.tall {
display: none;
}
}
#media (aspect-ratio: 1/1) {
.wide {
display: block;
}
}
<div class="wrapper">
<h1 class="tall">I'm taller than I am wide</h1>
<h1 class="wide">I'm wider than I am tall</h1>
</div>
There is a caveat, though. You might have noticed a third media query that checks if the aspect ratio is a perfect square. Because of how media queries currently work with min and max values, there is a 1px point where some weird stuff can happen, and both are active. Having a query that checks for this perfect square scenario prevents the screen from not displaying either title in the case where it is a perfect square.

Categories