CSS: Interaction between files - html

I'm working on a CSS file and I'd like it to interact with anothet CSS file.
How? Let's say I have A.css and B.css. In A.css I want to do the "overflow: hidden" referred to B.css and all the elements that it controls.
Is anything like that impossible?
Like:
#import "field.css"
.sky .field {
overflow:hidden;
}
So basically this what I actually have:
.sky {
width: 90%;
height: 100%;
background: blue;
opacity: 0.7;
position: absolute;
z-index: 1;
}
.field {
width: 100%;
height: 20px;
background: green;
position: fixed;
top: 90%;
z-index: 2;
}
.field > p {
width: 100%;
height: 40px;
background: black;
}
Now I want that "p", which is a sub-tag of .field to not show outside of the bounds of .sky.
How do I do that?

No need to import one CSS file into the other simply link to both CSS files in your HTML. For example if you had the following two files
File A:
.sky .field {
overflow:hidden;
}
File B:
.sky {
color: black;
}
Sky would inherit both properties of overflow hidden and color black. If the rules contradict each other for example file A says sky color is blue and file B says black then the CSS rule sheet which is linked last will take presidence.
Edit: Generally it isn't good practise to do this for organization purpose. If Sky is a single objection consider putting all CSS references to it in a single file.

Load both the CSS files into your page. You can actually have multiple files which define style rules on same element. So lets say you have two file
File 1
.sky{
background-color: Red;
}
And File 2
.sky.field {
overflow:hidden;
}
And lets say the page has a element with class div and field.
<div class='sky field'></div>
Now this will have both the combined CSS rules.
Also make sure you get yourself familiar with CSS Priorities, If 2 files have the different CSS rule on the same element then what happens??
Example
//File 1
.sky{
background-color: Red;
}
//File 2
.sky.field {
background-color: Blue;
}
Now the file that is placed last in the HTML DOM will have more priority over other rules. Note that its NOT the last file loaded but the last file in the DOM hirarchythat gets the priority.

Related

How to remove overridden styles from css?

We have a large CSS file with many styles that contains overriden styles like this:
.block {
width: 100px;
}
...
.block {
width: 200px;
}
We want to automatically remove unused styles and get as result only 1 style property value for the class like this:
.block {
width: 200px;
}
https://purifycss.online/ - This does not solve our problem.
purifycss
How can I do it having only html and CSS files?

Getting element by ID in coffeescript

I'm trying to create a HTML widget:
HTML:
<div>
<h1 class="title" data-bind="title">Title</h1>
<div>
<h1 id = "dc1" class="dc">DC1</h1>
</div>
<div>
<h1 id = "dc2" class="dc">DC2</h1>
</div>
<p class="updated-at" data-bind="updatedAtMessage"></p>
</div>
And I need to be able to set the background color of the id="dc1" and id="dc2" elements dynamically in CoffeeScript. I plan to do this by adding a class with a background color setting:
SCSS:
&.up {
background-color: green;
}
&.down {
background-color: red;
}
.dc {
background-color: orange;
font-size: 30px;
float: left;
width: 50%;
}
So far I have managed to set the whole widget background but not the child elements mentioned above:
I have been using:
CoffeeScript:
$(#node).removeClass('up down')
$('#dc1').removeClass('up down')
$('#dc2').removeClass('up down')
$(#node).addClass('down')
$('#dc1').addClass('down')
$('#dc2').addClass('up')
Note ultimately I will add the classes depending on some data rather than hard coding them to 'up' or 'down' in the coffeescript.
But nothing happends.. Am I getting selecting the id="dc#" elements correctly?
If it helps with context I'm doing this for Dashing
Your SCSS doesn't make sense so I'd guess that your missing an error from the SCSS-to-CSS conversion. An & in SCSS is a reference to the parent selector:
& will be replaced with the parent selector as it appears in the CSS
so have &.up at the top level makes no sense and should generate an error. If we fix the SCSS so that .up and .down apply only to .dc:
.dc {
/* ... */
&.up {
background-color: green;
}
&.down {
background-color: red;
}
}
then everything seems to work just fine.
Demo: http://jsfiddle.net/ambiguous/9y9uywm9/
You can use Sassmeister (and other similar online tools) to see what SCSS thinks of your original SCSS.

My image won't show up but the background color shows

First while I was styling the css I realized I didn't create a folder for it, so I decided to create a folder for css. Which I named screen.css but right after creating the css folder my images stopped showing. I have check the spelling and the tag but nothing seems to help. I did change link tag from screen.css to css/screen.css
Everything was working fine until I created a folder for the css so I'm guessing the problem might lay there.
An example of the html
body
{
background: url(images/wallpaper.png);
background-repeat: repeat-y;
margin: 0;
background-color: #e4c17f;
font-family: 'Nova Square',helvetica, sans-serif;
}
#banner
{
width: 900px;
background-color: #a65900;
margin: 0 auto;
height: 150px;
background: url(images/banner.jpg);
}
#footer
{
width: auto;
background-color: #a65900;
height: auto;
background: url(images/name.jpg);
}
You created the folder CSS therefore probably need to reference the images from your CSS relatively to the images folder...
A relative path is the path to a file from the current directory.
-- images
---- wallpaper.png
-- css
---- screen.css
body {
background: #e4c17f url(../images/wallpaper.png) repeat-y;
}
Try this :
The Browser will start looking for the image from the folder where you have kept your css
background: url(../images/wallpaper.png);
It worked! :D
I changed the tag to:
background: #e4c17f url(../images/wallpaper.png);

Replicate background-style:contain on img?

Notice that I need to declare the img source from the html (this will be dynamic), so i dont use background here.
HTML
<div class='some-form'>
<form>
<button>...<button>
<img id="some-img" src="something"/>
<input id="some-input"/>
</form>
</div>
CSS
.some-form {
display: block;
position: relative;
}
.some-form #some-input {
background-color: rgba(255,255,255,0);
border: 1px solid #2F2F2F;
width: 300px;
color: #000;
opacity: 1;
}
.some-form #some-img {
position:absolute;
background-color: #FFF;
z-index: -1;
//background-size: contain; //this does not work
//background-position: center right 50px; //so this will not work
}
How can I get the image to act like contain so that I can align it the way i want?
Keep your code as-is, but change #some-img from an img to a div (and specify width and height as needed based on the image dimensions). It's not possible (at least not in a simple way) to make an img element behave as if it was using background-size and background-position properties since img elements are not backgrounds. So in order to do so, you instead make the image a div with a background-image.
Since you are dynamically populating the image src, you can instead use inline styles to define a background-image on the div, as this lets you call a PHP or other server-side function to echo the image url (which you can't do in a CSS file).
So for example, keep the CSS you have now (but add height/width or other styles to the #some-img div as needed) but replace <img id="some-img" src="something"/> with something like this:
<div style="background-image: url(<?php theDynamicImageURL(); ?>);"></div>
or equivalent in whatever language or method you are using to populate the image dynamically.
There are better ways to do this as inline CSS is generally something that should be avoided, but the use in this case is not too dangerous but it'll work in a pinch and most other methods would either be equally sloppy or a lot more work.
If you include jquery, you can write a script to cheat this:
<script type="text/javascript">
height = $('#some-img').height();
width = $('#some-img').width();
src = $('#some-img').attr('src');
$('#sime-img').wrap('<div id="contain"></div>');
$('#contain').height(height).width(width);
$('#contain').css('background',"url('" + src + "')");
$('#contain').css('background-sizing','contain');
$('#some-img').css('opacity','0');
</script>
It isn't nice. You can do the same thing w/o JQuery, I just used it for convenience.
If I understand correctly, you're looking to constrain an image to the size of its containing element and center it vertically and horizontally.
This will get you pretty close, but the image will only scale up to its actual size, no bigger.
HTML
<div class='some-form'>
<form>
<button></button>
<img id="some-img" src="http://lorempixel.com/300/200/sports"/>
<input id="some-input" />
</form>
</div>
CSS
.some-form {
display: block;
position: relative;
width:400px;
height:180px;
background: rgba(255,255,0,.1); /* for checking that it fits*/
}
.some-form #some-input {
background-color: rgba(255,255,255,0);
border: 1px solid #2F2F2F;
width: 300px;
color: #000;
opacity: 1;
}
.some-form #some-img {
position:absolute;
background-color: #FFF;
top:0;
bottom:0;
left:0;
right:0;
margin:auto auto;
max-width:100%;
max-height:100%;
z-index: -1;
}
fiddle: http://jsfiddle.net/XNR38/
Good luck!

Update css with jquery in ajax?

I'm trying to update the css (the css is located in my main.css) of divs that do not exist on my main html file but do in the files i am injecting. Is this possible? if so how?
ok so here is what I have in my main html file
<div id="container">
<div id="page">
<!placeholder>
</div></div>
sorry about the bad formatting i just can't get the tabs and new lines to work with the code input system on this site.
next is what I have for main.css
#container {
position: fixed;
margin-right: 0px;
overflow-x: hidden;
overflow-y: scroll;}
#page {
position: absolute;
width: 100%;
height: 1600px;
z-index: 10;
border-left: 1px solid #CCCCCC;}
#recposts {
position: absolute;
left:0;
top:1200px;}
.child {
height: 400px;
border-top: 1px solid #CCCCCC;
border-bottom: 1px solid #CCCCCC;
z-index: 11;
background-color: #EDEDED;
width: 100%;
padding-right: 10px;}
alright and now what I'm injecting
<div id ="page">
<div id="recposts" class="child">
<h1> Recent Posts </h1>
</div></div>
So I need to be able to edit the position top of #recposts and the height of .child.
I fixed my issue the problem was that I wasn't editing the css on the html load. I put the function into the .load() and now it works.
Your question is unclear, from what I understood, you can solve it using Jquery live() api.
http://api.jquery.com/live/
You can change whatever you want to the newly added items by using Jquery live() api.
You can trigger a function call after insertion, by using jquery's DOM change() api .
http://api.jquery.com/change/
eg :
$('#container').change(function() {
$('#page').css("height",200);
});
EDIT: Since the change() api will not work on divs, here's the correct version
$('#container').bind("DOMSubtreeModified", function() {
$('#page').css("height",200);
});
If the CSS rules are in a file that is already loaded (your main.css), then any new elements that are added later (say, from whatever HTML you're talking about injecting), and that match selectors in the CSS file, will automatically have the styling rules applied. Is that what you're after?
I am not sure if this is what you were looking for, but this will add a span with class "someClass" to the div id="insertion".
css:
.someClass{ background-color:black; }
html:
<div id="insertion"></div>
js:
<script>
var toInsert = document.createElement("span");
toInsert.className = "someClass";
document.getElementById("insertion").append(toInsert);
</script>