:first-child selector not applying in emotion css - css

I have a styled component as below
const StyledCustomComponent = styled.aside`
min-width: ${gridPts(19)};
background-color: ${HIGHLIGHT_BLUE};
flex: 0 0 auto;
${fontSize(13)}
padding: ${gridPts(2)};
#media only screen and (max-width: ${MQ_320}) {
padding: ${gridPts(1)};
}
#media only screen and (min-width: ${MQ_1285}), (min-width: ${MQ_575}) and (max-width: ${MQ_961}) {
&:first-child {
margin-top: 0;
}
}
`
When I inspect it in the browser and resize to match the media query, the :first-child style does not apply.
And the child components of the above component are styled as below
const StyledCustomComponentChild = css`
margin: ${gridPts(1)} ${gridPts(1)} 0;
#media only screen and (max-width: ${MQ_575}), (min-width: ${MQ_961}) and (max-width: ${MQ_1285}) {
display: inline-block;
float: left;
margin-right: ${gridPts(1)};
& * {
display: inline-block;
margin-right: ${gridPts(1)};
}
}
#media only screen and (min-width: ${MQ_1285}), (min-width: ${MQ_575}) and (max-width: ${MQ_961}) {
display: block;
line-height: 1.5em;
margin: ${gridPts(3)} 0;
white-space: nowrap;
&:first-child {
display: block;
}
}
`
This style is applied to the component itself instead of its first child
here is the screen shot of how the style is interpreted in the browser dev tools
Notice there is no space before :first-child
If I manually add a space through dev tools then the style is applied as expected
import styled from 'react-emotion'
const StyledDiv = styled.div`
color: turquoise;
& :last-child {
color:green;
}
& :first-child {
color: red;
}
`
render(
<StyledDiv>
<p>First</p>
<p>Second</p>
<p>third</p>
<p>last</p>
</StyledDiv>
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Can anyone guess what might be the issue?

Both selectors work for me. Use a tag like <div/> or <p/> etc. before :last-child (depending on your code, it may be also another tag).
export const blogSection = css`
disaply:flex
& div:last-child {
background: #ff0000;
}
`;

Related

SCSS to hide DIV in mobile & tablets and display in desktop browsers

Below is a CSS class used to render a div with some links on one of the pages. I want to hide the div in mobile view(Mobile and tablets) and display only in desktop browsers. I am using SCSS.
What changes should I make to the CSS class?
.ps-widget__content {
#extend %list-reset;
ul {
border: 1px solid #d1d1d1;
li {
border-bottom: 1px solid #d1d1d1;
a {
display: block;
padding: 15px 20px;
line-height: 20px;
font-size: 16px;
font-weight: 500;
color: $color-heading;
text-transform: capitalize;
i {
margin-right: 10px;
}
&:hover {
background-color: $color-1st;
color: #fff;
}
}
&:last-child {
border-bottom: none;
}
&.active {
background-color: $color-1st;
a {
color: #fff;
}
}
}
}
}
You need to use media queries that that compiled by SCSS into following CSS out put or similar.
/* for desktop start */
div {
background-color: green;
}
/* for desktop end */
/* for tablet start */
#media screen and (max-width: 992px) {
div {
background-color: blue;
}
}
/* for tablet end */
/* for mobil start */
#media screen and (max-width: 600px) {
div {
background-color: yellow;
}
}
/* for mobile end */
<div>css automaticaly adjusts rules as view changes</div>

Why don't Sass respect the order of CSS property using nested media query?

Why don't Sass respect the order of CSS property using nested media query?
Input Sass:
margin-top: 3px appears after the media query.
.myContainer {
margin: 1px;
#media only screen and (min-width: 768) {
margin: 2px;
};
margin-top: 3px;
}
Output CSS:
margin-top: 3px now appears before the media query.
.myContainer {
margin: 1px;
margin-top: 3px;
}
#media only screen and (min-width: 768) {
.myContainer {
margin: 2px;
}
}
In this example, you can see that the position of margin-top:3px is changed.
How could this happen and is it expected?
It is the expected result. Otherwise, it will have to create two separate classes like in the second snippet below. This wouldn't make sense. Try this:
.myContainer {
margin: 1px;
#media only screen and (min-width: 768px) {
margin: 2px;
};
& {
margin-top: 3px;
}
}
Output:
.myContainer {
margin: 1px;
}
#media only screen and (min-width: 768px) {
.myContainer {
margin: 2px;
}
}
.myContainer {
margin-top: 3px;
}

Why doesn't this CSS get scoped by vue.js?

Inside a Vue single file component I have a scoped style section that looks like this:
<style scoped>
#media screen and (max-width: 767px) {
#suggestions, #form, #mapcontainer, #searchcontainer {
width: 100%; padding: 6px;
}
#mapcontainer div#mapx {
height: 400px
}
}
#media screen and (min-width: 768px) {
#workarea {
display: grid;
grid-template-columns: 1.5fr 1fr;
}
#mapcontainer {
grid-column: 1;
}
.blurb {
grid-column: 2;
margin-left: 12px;
}
#mapcontainer div#mapx {
height:600px;
width: 100%;
}
}
#suggestions tr { border-bottom: thin solid silver; }
#suggestions td { padding: 6px 3px 6px 3px }
#suggestions table { margin-bottom: 12px }
</style>
After checking the rendered source in Chrome's inspector I see the data tag in the rendered component like this:
<div data-s-0="" id="appspace">
but I don't see any of the expected changes to the CSS as documented in the section about scoped CSS in the vue.js docs
It seems to happen every time the scoped style contains a #media query.
What do I need to change so that styles stay scoped?

Removing a class in a CSS stylesheet

I am trying to make my site mobile accessible. The problem I have is with the nav. I believe it has to do with the class being used on the nav. So what I want to do is remove the class when a mobile device is being used.
I have it in a media query and have changed a number of things to make it look correct on mobile. I read that it is possible to remove a class using
$( "p" ).removeClass( "myClass yourClass" );
I believe mine would be
$( "nav" ).removeClass( "navigation" );
but when put in the stylesheet and put through a css validator, I get
Lexical error at line 104, column 4.
Encountered: "(" (40), after : "$" ( "nav" ).removeClass( "navigation" );
I have no idea how to, or even if I can, fix this. Other pages I have read say this is impossible to do so I am getting conflicting information.
Here is me trying to post code.
This everything in my CSS affecting my nav.
nav ul { list-style-type: none;
margin: 0;
padding: 0; }
header nav a { text-decoration: none;
padding: 10px;
display: block;
background-color: #a8e6a8;
border-bottom: 1px solid #228B22; }
nav a:link { color: #228B22; }
nav a:visited { color: #568b22; }
nav a:hover { color: #869DC7;
background-color: #EAEAEA; }
.navigation { float: right;
width: 200px;
font-weight: bold;
letter-spacing: 0.1em; }
Here is my unfinished media query.
#media only screen and (max-width: 1024px) {
body { margin: 0; padding: 0; background-color: #fff; }
#wrapper { width: auto; min-width: 0; margin: 0; }
h1 { margin: 0; text-align: center; font-size: 2em; line-height: 200%; }
nav { float: none; width: auto; }
nav li { display: inline-block; }
nav a { padding: 1em; border-style: none; font-size: 1.2em; }
nav ul { text-align: center; padding: 0; margin: 0; }
main { margin: 0; padding: 0 1em; font-size: 90%; }
dd { margin-bottom: 1em; }
footer { margin: 0; }
.navigation { float: none;
width: auto;
letter-spacing: 0.1em; };
}
Actually, you could add a class with jQuery but I don't believe this is the best way to achieve what you need.
What I have seen many designers doing is having two navs. One for big screens and another for small screens. It can be very handy, as you can work with different html codes too. For example, you can add FontAwesome icons to your mobile nav, add or remove links, etc. So, your html would look like this:
<nav class="nav-big">
<ul>
<li>Link 1</li>
<li>Link 2</li>
...
</ul>
</nav>
<nav class="nav-small">
<ul>
<li><i class="fa fa-plus"></i>Link 1</li>
<li><i class="fa fa-minus"></i>Link 2</li>
...
</ul>
</nav>
Then, your CSS would look like this:
.nav-big {
...your styles
}
.nav-small {
...your styles
}
#media screen and (min-width: 1024px) {
.nav-small {
display: none;
}
}
#media screen and (max-width: 1023px) {
.nav-big {
display: none;
}
}
You can see something like this working in my Blog and also in another website I developed.
Hope to have helped. If I did, please mark the answer as useful or correct! Thanks!
Welcome on board! Please try to be more concise on your questions :-)
My answer:
The code you're posting is JavaScript. jQuery, to be precise. This certainly doesn't work in CSS.
But the better solution is to use the media query not to remove a class, but to alter it. Example:
.foo { width:200px; }
#media print
{
.foo { width:100px; }
}
EDIT after the CSS was added to the question:
The trailing ; looks like a syntax error to me: Change letter-spacing: 0.1em; }; to letter-spacing: 0.1em; }
Other than that the media query looks good in principle. Try adding background:red to see if it works at all :-)
$("nav").removeClass("navigation"); is JavaScript, not CSS. Conceptually, you can not add/remove a class in CSS - it is only a style description language.
What you can do in CSS, however, is show/hide content for a specific device width ("breakpoint"):
.navigation {
display: block;
/* your style definitions for nav */
}
#media only screen and (max-device-width: 800px) {
.navigation {
display: none;
}
}

Bootstrap: How to collapse navbar earlier

I want to collapse my navbar in earlier resolution than 768px, for example 992px, how would I do that? thanks! (I know I can customize it on the bootstrap page, but I don't want to start my project over again with new bootstrap.css file)
If you don't want to manipulate Bootstrap by using Less/Sass (maybe because you want to load it via a CDN), this is what did the trick for me:
#media (min-width: 768px) and (max-width: 991px) {
.navbar-collapse.collapse {
display: none !important;
}
.navbar-collapse.collapse.in {
display: block !important;
}
.navbar-header .collapse, .navbar-toggle {
display:block !important;
}
.navbar-header {
float:none;
}
}
Demo: https://jsfiddle.net/0pmy8usr/
Add this in a separate CSS file and include it after bootstrap.css
UPDATE for Bootstrap 4:
#media(max-width:900px) {
.navbar .navbar-brand {float:none;display: inline-block;}
.navbar .navbar-nav>.nav-item { float: none; margin-left: .1rem;}
.navbar .navbar-nav {float:none !important;}
.nav-item{width:100%;text-align:left;}
.navbar-toggler {display: block !important;}
.navbar-toggleable-sm.collapse {display:none !important}
.navbar-toggleable-sm.collapse.in {display:block !important}
}
Demo: https://jsfiddle.net/mkvhbgnp/3/
In variables.less, change
#grid-float-breakpoint: #screen-sm-min
to
#grid-float-breakpoint: #screen-md-min;
If you need to collapse your navbar in earlier resolution than 768px so you will need to use #media min-width and max-width, and you don't need to start new project for doing that simply create new .css file ( example: custom.css) and inset it under your main bootstrap.css to override its values. and write this code inside it :
CODE:
#media (min-width: 992px) {
.collapse {
display: none !important;
}
}
Also, you can have a look at this post: change bootstrap navbar collapse.
I hope this will give you the solution.
You can also override the bootstrap classes that close the gap. Bellow is the working code that overrides basic navbars with dropdown menus. Not all classes are overriden bellow, so depending on what you are using, you may need to override other classes.
#media (min-width: 768px) and (max-width: 991px) {
.navbar-nav .open .dropdown-menu {
position: static;
float: none;
width: auto;
margin-top: 0;
background-color: transparent;
border: 0;
-webkit-box-shadow: none;
box-shadow: none;
}
.navbar-nav .open .dropdown-menu > li > a {
line-height: 20px;
}
.navbar-nav .open .dropdown-menu > li > a,
.navbar-nav .open .dropdown-menu .dropdown-header {
padding: 5px 15px 5px 25px;
}
.dropdown-menu > li > a {
display: block;
padding: 3px 20px;
clear: both;
font-weight: normal;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
}
.navbar-header {
float: none;
}
.navbar-toggle {
display: block;
}
.navbar-collapse {
border-top: 1px solid transparent;
box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
}
.navbar-collapse.collapse {
display: none!important;
}
.navbar-nav {
float: none!important;
/*margin: 7.5px -15px;*/
margin: 7.5px 50px 7.5px -15px
}
.navbar-nav>li {
float: none;
}
.navbar-nav>li>a {
padding-top: 10px;
padding-bottom: 10px;
}
.navbar-text {
float: none;
margin: 15px 0;
}
/* since 3.1.0 */
.navbar-collapse.collapse.in {
display: block!important;
}
.collapsing {
overflow: hidden!important;
}
}
click here for the live demo code
The solution is actually very simple. Use .navbar-expand-lg or .navbar-expand-xl class with <nav> tag.
Example :-
<nav class="navbar navbar-expand-lg navbar-light" role="navigation">
</nav>
Thank you all.
I would suggest bringing in bootstrap into your project in lieu of a CDN and simply look for the media query that looks like this:
#media (min-width: 768px) {
.navbar-expand-md {
-ms-flex-flow: row nowrap;
flex-flow: row nowrap; //...etc.
And change it to:
#media (min-width: 900px) {
.navbar-expand-md {
-ms-flex-flow: row nowrap;
flex-flow: row nowrap; // ...etc.
Or if you are using a CDN, create an override to that specific media query.
If you want it is menu to be collapsable from medium devices
You can directly do by
toggleable="md"
Else you can go for other force collapsable approach by setting sccs/ less of bootstrap.

Resources