How to replace @media (max-width) using Stylish or Greasemonkey?
One way to do this is to:
- Find the offending
<link>
using the constant part of the text in thehref
. - Record that link's
href
. - Delete that link.
- Use
GM_xmlhttpRequest()
to fetch the file again (hopefully it's cached). - Use regex to fix the fetched CSS.
- Use
GM_addStyle()
to add the fixed CSS.
Here's a complete Greasemonkey script that illustrates the process:
// ==UserScript==
// @name _Replace bad CSS link
// @include http://www.fleaflicker.com/nfl/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
var badCSS = $("link[href*='global-cdn-']");
var badURL = badCSS.attr ("href");
badCSS.remove ();
GM_xmlhttpRequest ( {
method: "GET",
url: badURL,
onload: function (rsp){
var betterCSS = rsp.responseText.replace (
/max-width:990px/g, "max-width:500px"
);
GM_addStyle (betterCSS);
}
} );
Notes:
- For faster/better performance, if the CSS does not change often, hand edit it and save it in the same folder that you install your script from. Then use
GM getResourceText()
to get the CSS, instead ofGM_xmlhttpRequest()
. - If page "flicker is an annoyance, due to start-up delays, that is a whole other problem, that can probably be solved with
@run-at document-start
and mutation observers.
how to override @media (max-width) using stylish
To override a media query you just need to load another media query - that also applies to your device - after it.
Well...you want a blunt media query that applies to everything. The best way is to use @media (min-width: 1px)
since that includes all devices.
Now, put it all together - along with some other CSS cleanups like padding
and margin
removal and setting a new width
for .mainContainer
and you get this
#sidebar {
display: none;
}
@media (min-width: 1px) {
.mainContainer {
margin: 0 auto;
width: 100vw;
padding: 0;
}
body>.container {
padding: 0;
}
}
New code: (with different selector for width)
#sidebar {
display: none;
}
@media (min-width: 1px) {
.mainContainer { /*example styles*/
margin: 0 auto;
width: 100vw;
}
body>.container {
padding: 0;
}
body>.mainContainer>main {
max-width: 100vw!important;
}
}
You still have to adjust the padding to your preference as setting the padding to 0 breaks the design a little bit but this should be good starting point.
Before:
After:
How to inject css in another page
If I understand your question well, you want to create mobile-specific CSS to fix your website's viewing, is that right?
If so, you can use CSS's media queries for so, like this:
@media only screen
and (min-device-width : xxx px)
and (max-device-width : xxx px)
and (orientation : landscape or portrait)
and (min-resolution: xxx dpi){
// your css goes here
}
Here are some links that might help you:
http://help.campaignmonitor.com/topic.aspx?t=164
How to apply different CSS for mobile devices not just based on media width/height
http://css-tricks.com/snippets/css/retina-display-media-query/
Related Topics
How to Check Parent Element Has Specific Class in Sass
Issue on Ie7 When Page Is Scrolling.Text Is Not Proper
Pagespeed-Penalty for Using Srcset
Rails 4.1 and Bootstrap 3 Glyphicons Are Not Working
How to Make Vertically Rotated Links in HTML
Nested Rows in Bootstrap 3 Fail to Respect The Grid
How to Make a Div Grow in Height While Having Floats Inside
How to Change The Font of Some Textoutput Elements
How to Change a Div Padding Without Affecting The Width/Height
Do I Still Need to Include Type="Value" in HTML5
Adding Background Image to Div Using CSS
Preventing Page Breaks in a Table When Printing
How to Use Tick/Checkmark Symbol (✓) Instead of Bullets in Unordered List
External CSS Does Not Load in Web Page
The CSS Selector for an Element That Its Id Is in The "Foo:Bar" Form
Keeping Top Level Menu in Hover State When Moving Down to Sub Menus