Responsive CSS styles inline on the fly - css

I am currently working on a highly design orientated site based on wordpress CMS.
Currently I have a responsive main stylesheet linked externally for the core css. As the site relies heavily on spacing and alignments of both text and images it has become necessary to add inline css using style= HTML to sometimes override the external CSS.
The problem I have is that in most cases certain elements such as margins need to be a different percentage in the mobile view than the desktop view to balance the visual composition. Is there any way to add responsiveness to the inline CSS based on screen width as can be done in an external style sheet?
So far the only way I can think of achieving this is through jQuery amending the external CSS based on the users screen width however this would mean setting up strict rules in the JS eg: for desktop view having margins set at 70% and for mobile setting them to 90%.
If it could be possible to do this inline using html style then this would give my client stricter control and more flexibility. Luckily my client is well versed in CSS.

You could always add a block of css inline with a style element:
<style type="text/css">
#media screen and (min-width:800px) {
.inlineOverride {
/* add more styles here */
}
}
</style>
<div class="inlineOverride">
</div>
It's worth mentioning that HTML5 has introduced a scoped attribute that you can set on the style element to limit the specified style information to the style element's parent element, and that element's descendants.
It isn't widely supported yet, so shouldn't be relied on, but it could be useful in the long term to help prevent inline styles like this from "leaking" into other parts of the document.

This question/answer might be helpful for you(read it thoroughly)
use #media for adjusting your properties of css according to device width-height.
What does #media screen and (max-width: 1024px) mean in CSS?

In modern Browsers you can (kind of) archive this by using custom css properties (css variables):
#media (max-width: 1100px) {
* {
color: var(--color-sm)
}
}
<a style="--color-sm: #11f">Text</a>
(Expand Snippet or open in full page to get the other behavior)
(Color is used for simplicity of presentation, just use margin or any other css property according to your use case.)

Related

Dealing with responsive media queries when printing

The issue: https://output.jsbin.com/donapohuci
This is a two column layout on desktop. Using a CSS media query, I have set it to be one column when the viewport is 800px or less:
div {
float: left;
width: 50%;
}
#media (max-width:800px) {
div {
width: 100%
}
}
The issue is that when you go to print this (at the moment, using Chrome) it decides that a letter sized piece of paper is smaller than 800px so prints using the media query styles.
We're using Bootstrap where it uses media queries like this. One solution is to modify the CSS so that it adds the 'screen' modifier:
#media screen and (max-width:800px)
Alas, this is a giant enterprise project with multiple teams all contributing so we'd really rather not mess with any of the foundational CSS at this time (as that usually causes a domino effect of other issues on other teams...)
Is there a way to workaround this short of writing a separate print style sheet just for this one particular page we need to have printed "as you see on screen"?
I'm thinking along the lines of some way to tell the browser "pretend the paper is wider than it is and use the desktop layout when you print..."
The way I would solve this is by adding the media screen attribute in the link to the regular CSS so it doesn't apply to print, and create a separate custom print stylesheet to be called after, again, using the print media attribute:
<link href="print.css" rel="stylesheet" media="print">
It is possible that the default Bootstrap has an include to a print file, but this should be easy to remove, and ultimately if it's not possible the latter stylesheet will still overwrite those styles.

Remove important styles in CSS

Im using google places api for a place autocomplete search - the user starts typing and results pop up.
I've styled the google container using !important to override the styles. So for my desktop css through media queries I have something like:
bottom: 100px !important;
top: auto !important;
Now on my mobile css, again through media queries, I need to move the position, I need the default styles back - the styles are controlled via google in the style tag on the element. But as I have used important i cannot remove them. I have tried:
bottom: auto !important;
Which fixes the bottom position, but how can I remove the top position so that it defaults to what is in the style tag on the element. I've tried:
top: auto !important;
top: inherit !important;
But with no luck.
Using that many !important 's is messy.
A few suggestions: (based on the little code your showing)
2. Don't override an :auto with an :auto. Try to override the styles with a number that give you similar look as :auto
3. Try removing all !important s and call the unique CSS within their perspective media query resolutions, properly. eg.
#media screen and (min-width:320px) and (max-width:480){
... // Your unique styles to Mobile Here
}
4. If all else fails; though, don't know why it would. You can .toggleClass with jQuery to attach a class within a condition. And .removeClass whenever you want. A simple fiddle of .toggleClass (demo) here.
But you really should just be able to clean up your CSS and put everything in their specific media query defined resolutions.
You should be able to do this by increasing specificity on your mobile css file and adding an !important value to this new value in the mobile stylesheet.
I'm not sure of your structure without seeing your html but if you can add an additional class or id to the parent container/element that is specific to mobile css and use that to target your mobile view
for example
#mymobile .classtooverride {newstyles !important;}

Can I define my custom CSS media?

I need to override main CSS of an application with my own CSS. Is there a good way of doing it ? One way is !important tag, which I want to avoid.
I was just thinking whether I can create a custom CSS media and define my CSS for that particular media. This way I can have main app CSS defined for all but my custom media.
CSS wil overwrite itself if you use the same selectors, so you won't need !important.
So:
.my-div .my-span {
color: green;
}
will be overwritten by:
.my-div .my-span {
color: red;
}
but not by:
.my-span {
color: red;
}
Yea, you can use media queries to target certain screen sizes. for example like:
#media screen and (device-width: 360px) and (device-height: 640px)
A main "feature" of Cascading Style Sheets is its cascading effect.
An amended quote on this from a number of places on the internet:
Cascade is the special part. A style sheet is intended to cascade
through a series of style rules, like a river over a waterfall. The
water in the river hits all the rocks in the waterfall, but only the
ones at the bottom affect exactly where the water will flow. The same
is true of the cascade in style sheets.
So as long as you specify the exact same rules but change some property values inside those rules, and you make sure they are loaded after the original rules, they will override the previously specified property values inside those rules. If you skip a property value in the new rule, the previously specified property value will remain in force for that property.
Media queries are the best answer to defining styles for a specific type of media. It lets you specify rules specifically for certain screen sizes.
If your particular target media cannot be properly identified by querying screen size but needs JavaScript to be identified. You could write some JavaScript which loads a style sheet when the document is loaded, in that case you only have to make sure it is loaded after the original style sheet, and it will then override styles with the same specificity.

Designing a Page for Screen and Print Medias - Font-Size

I've created a report which is shown on the screen and it should be printable as well. The application has to support IE 6.0 too.
What font-sizes should I be using?
I've read, I should be using em for Screen media (web page) and pt for print media (I know em is scalable and pt isn't...).
How would you design such a page in terms of the css elements?
e.g. creating a separate css file for print media and duplicating all your css classes there and just modifying the font-size? so much duplication.
Isn't there a better way?
Thanks
You don't need to duplicate that much.
Create your report for the Web.
Then, in the body of your stylesheet add a reference for print styles, like so
#media print {
div#content {background:#fff; width:90%; font-family:serif; font-size:12px;}
div#header, div#insideheader, div#topnav, div#footer,
div#navcontainer, p.pic img {display:none;}
div#main {border:none; background:none;}
a {color:black;}
}
In the example above, I am
setting the background-color to white and extending the content to fill the width of most of the page
removing most of the extra pieces, like the nav and footer, extra pics, etc.
changing the link colors
setting the font to a serif font, easier to read for print, and a size of 12px, which is pretty standard.
If you have the first style sheet is marked media all, and the second style sheet is media print, the second style sheet will effectively be an extension, and over-rider of the first for print media. Prefer points for print and ems for screen.
Have a look at this article for other things you might not have thought about.
http://www.alistapart.com/articles/goingtoprint/

Can CSS frameworks (ie: 960gs or Blueprintcss) be used without margins?

I don't see the point of using either http://960.gs or http://blueprintcss.org if they enforce margins other than for pretty magazine layouts/marketing-esq brochures. Is there a way I can use these to meet certain design requirements such as a navbar that can actually touch/wrap-around to the header? Any input to use these frameworks without margins (as they enforce browser compatability onto the less CSS guru level developers) would be ideal. (Note: we are using JSF, this is also a development shop not a web shop at all)
I use this technique as my ultimate CSS layout technique:
http://www.codeofficer.com/blog/entry/css_grid_frameworks_960gs_without_margins/
I had the same issue once, and since I use that one, all the margin headaches has dissapear.
I use !important overide when I need to use custom width/height.
The Code:
<div class="g_9 content_main">
This div should actually have 720px in width.
But I overide it using another class so now the width has become 700px,
and just in case it needs custom margin, we can always set it in the css :)
</div>
The CSS:
.c_12 .g_9, .c_16 .g_12 { width: 720px; } /* 960-full.css */
.content_main { width: 700px !important; margin-left: 15px;} /* style.css */
You don't have to use the grid classes for every div on your site. If you're header doesn't need to follow the grid layout then create your own styles for the header. You can also skip the grid completely and just use the form & typography markups. The frameworks are an attempt to bring a bit of consistency to your projects.
I'm using blueprint for the main content but the header area has it's own custom layout.
Here's a mod I wrote that works with the 960gs to help you create elements that touch, without rewriting the framework.
http://www.michaelhartmayer.com/css/960gs-margin-mod

Resources