Selectively apply stylesheets to div - css

I'm trying to make a simple web page design with multiple divs. Each div should apply 1 or more external stylesheet from a list.
My problem is that, from the examples I've read so far, I'm not sure how to do this elegantly. It seems that external stylesheets are applied to the whole html file. So should I be looking at modularizing my divs into separate files? Or would something like iFrame be a neater solution?
Current Solution:
External CSS:
div.test1 {
color: purple;
background-color: #d8da3d
}
.test2 {
color: red;
background-color: #d8da3d
}
#test3{
color: green;
background-color: #d8da3d
}
HTML body code:
<div class="test1">
<p> Style1
</div>
<div class="test2">
<p> Style2
</div>
<div id="test3">
<p> Style3
</div>
My references:
Div with external stylesheet?
http://www.htmlhelp.com/reference/css/style-html.html

- 1
Take a look at this:
https://stackoverflow.com/a/17668004/1552518
- 2
Or just add a class to each div:
<div id="container">
<div class='div1'>
style1
</div>
<div class='div2'>
Style2
</div>
</div>
And in your external css:
.div1 {
// Style applied only to the first div
}
.div2 {
// Style applied only to the second div
}
- 3
Or if you can't add a class to the divs use this in css:
#container > div:first-child {
// Style applied only to the first div
}
#container > div:last-child {
// Style applied only to the second div
}

Are you just trying to style the div's differently? Have you looked into using a class for each of the divs?
From the second link you provided: http://www.htmlhelp.com/reference/css/style-html.html#class

When you use the css styling:
body{
color:purple;
background-color: #d8da3d
}
You are saying that you want to style the entire body of the document with the styling you have set.
In order to target specific elements you should give those elements an id or class.
For example:
<div id="test1"></div>
<div class="testing"></div>
<div class="testing"></div>
Please note that when using and id you must make sure to give the element a unique id. However many elements can share the same class. Therefore for the above example the styling:
#test1{
color:blue;
background-color:black;
}
.testing{
color:red;
background-color:white;
}
Will apply the first style (test1) to the div with the same id, and the second style (testing) to the two divs with the same class.

Related

How to target a deeply nested header element

I have a h3 tag which is deeply nested as follows and can't amend the component that contains
this styling.
Instead since I only have 1 h3 within this nest, trying to target it and amend its padding.
But this is not working. Can I know what I am doing wrong? I am doing something similar for another
div tag targeting the 6th position for that div. That works fine. Issue is with targetting this h3
tag. Tried adding !important to these styling which makes no diff. What am I doing wrong here?
This is the structure of the html currently.
// This is the only div I created passing in my custom styling and the component is wrapped within this.
<div className={styles.main}>
// all the following is coming from an external component I can't amend.
<div>
<div>
<div>
<div>
<span>some span text 1</span>
<div>some span text 2</div> <!-- also targetting this div and this works fine. See CSS below -->
</div>
<div></div>
</div>
</div>
<div>
<div>
<div>
<h3> <!-- This is the only h3 in entire nest -->
Some Random Text <!-- trying to give this a left padding -->
</h3>
</div>
</div>
</div>
</div>
</div>
My SCSS File
.main > div:nth-child(5) {
padding-top: 100px; // this works fine
}
// tried all the following. None of them works. I do not get the padding left 50px;
// The text is stuck to the left with no margin / padding.
.main > h3 {
padding-left: 50px !important; // Don't want to use !important. Tried with it just in case it works.
}
.main > h3:first-of-type {
padding-left: 50px !important; // Don't want to use !important. Tried with it just in case it works.
}
.main > h3:first-child {
padding-left: 50px !important; // Don't want to use !important. Tried with it just in case it works.
}
The problem is that you are using the direct descendant selector.
You are only selecting h3 elements that are direct children of .main
You need to modify your selector to select children/grandchildren/etc.
.main h3 {
...
}

Confused about CSS class naming convention

Trying to learn CSS, I was writing code and wanted to name three CSS classes. I searched CSS name convention and stumbled upon this answer. So I named my classes as suggested in the answer, but one of my installed atom packages is advising me against it. I am now confused if I am doing it right or not. for <div class="container top">I have:
.container.top {
background-color: #e4f9ff;
width: 200px;
height: 200px;
}
.container.top {} targets a div with both classes (container & top)
If you put a space inbetween them .container .top {} then it targets the child with a class of (top) inside the partent (container).
Everything looks correct on my end. Unless it's not accomplishing what you want it to accomplish.
.container.top {
background-color: #e4f9ff;
width: 200px;
height: 200px;
}
<div class="container top">
In the case of:
CSS: .container.top { color:red; }(no space)
HTML: <div class="container top">First text layer</div>
"ONLY in this case where a div has .container and .top as classes then apply css"
An example of this would be the "Night mode" on most website... Devs apply a .night class to the body tag via JS.
On the CSS side you would have a regular body tag related color like so body{ background-white; } and a night mode only color body.night{ background-color:black; }.
As soon as the .night class is added to the body, the background color will change because it has priority as it's more "targeted".
/------/
In the case of: CSS: .container, .top { color:red; }
HTML: <div class="container top">First text layer</div>
"If there is a .container class apply css. if there is a .top class apply css"

CSS rule for grandchild irrespective of its parent element

I have css rule as follows,
.wrapper > h3{
color:red;
}
The html code,
<div class='wrapper'>
<h3>Text1</h3>
</div>
<div class='wrapper'>
<div data-ui-view=''>
<h3>Text2</h3>
</div>
</div>
Here is the plunker. The Text1 is shown in red colour but Text2 is not. I understood that this rule
will take the immediate <h3> element under .wrapper. In angularjs most of the time the elements will be wrapped under tag. So, I want to make a rule such that whenever an <h3> tag comes inside .wrapper class then it has to be in red colour. irrespective of <h3>'s parent elements. Is there a way to do it?
Simply make the rule:
.wrapper h3 { color: red; }
This will make all <h3> elements within the .wrapper class red
If you did want to target the grandchild element as your question title suggests you could use this rule:
.wrapper > * > h3 { color: red; }

I cannot hide an element inside a DIV with different ID and CLASS via CSS

I cannot hide a H2 element inside a DIV using CSS, please help me.. Heres the CSS:
<style>
#block-views-projects-block h2 {
visibility:hidden;
}
</style>
And heres the actual html code
<div class="region region-projects">
<div id="block-views-projects-block" class="block block-views contextual-links-region">
<h2>PROJECTS</h2>
</div>
</div>
Can you give me the correct CSS to remove this H2 PROJECTS , I cannot remove it as it is produced automatically. Note: the DIV's from this code has a different css ID's and CLASS.
Try this
#block-views-projects-block h2 {
display: none;
}
if this doesn't work, then can you try this
#block-views-projects-block h2 {
display: none !important;
}

changing background colour of a two divs at once

So i need to make a div a link, and have the background colour change when hoverng over this div with the mouse. The problem is, this div has two child divs inside it and when i move the mouse in to the bounds pf the parent div it is actually on a child div. So while i can make it so that one of these child divs changes on hover the second one does not.
So i guess my question is, is there a way to make both child divs change when hovering one using css?
I dont mind changing code to use tables if thats easier but I need to find some way to make the entire div / tr change when hovering on one child / td.
What im actually looking to create here is something almost the same as th youtube recommended videos boxes (on teh right of the page)
Thanks in advance
CSS
#parent {
width: 318px;
height: 90px;
background-color: #FFFFFF;
margin: 5px 0px 5px 0px;
font-size: 10px;
}
#parent :hover {
background-color: #0000ff;
}
#child1 {
width:120px;
float:left;
}
child2 {
width:188px;
float:right;
}
HTML (with some other stuff)
<c:forEach var="item" items="${list}">
<a href="webpage?item.getinfo()">
<div id="parent">
<div id="child1">
<img src="img.jpg">
</div>
<div id="child2">
${item.getinfo2()} <br>
${item.getinfo3()} <br>
</div>
</div>
</a>
</c:forEach>
Code is something like that. Ive been hacking it up for the last while but that was something like what i had before
If the one you're able to hover over is the first, you only need CSS:
.mavehoverable > div:hover, .makehoverable > div:hover + div {
background-color: red;
}
With this HTML:
<div class="makehoverable">
<div>Child 1</div>
<div>Child 2</div>
</div>
Hovering over Child 1 will also highlight Child 2. Vice-versa doesn't work in CSS though, so that would need some JS.
I think you might just need to fix a line of your CSS. Change:
#parent :hover {
background-color: #0000ff;
}
to:
#parent:hover {
background-color: #0000ff;
}
That seemed to work for me.
Have you tried using jQuery? You could do something like this:
http://jsfiddle.net/UtdYY/
Html:
<div class='color'>
<div class='color child'>test123</div>
<div class='color child'>test456</div>
</div>
Javascript:
$('.color').hover(function(){ $(this).toggleClass('red'); });
CSS:
.red { color:red; }
.child {height: 50px; }
​
Edit: Cleaned up the javascript, thanks elclanrs
Try this http://jsfiddle.net/rsarika/rtGw5/1/

Resources