How to style nested element that is styled by its parent already - html

An initial CSS styling cannot be overwritten by a style specificly assigned to it. How shall I structure my CSS or how can I style the nested element with blue type correctly?
js fiddle
HTML
<ul class="listing">
<li>
<p> There is plenty of wood.</p>
</li>
<li>
<div class="big">
<p> This is extra big</p>
</div>
</li>
</ul>
CSS
.listing li p{
font-size: 1em;
color: red;
}
.big p {
font-size: 2em;
color: blue;
}

You may try this (Example)
div.big p {
font-size: 2em;
color: blue;
}
it's all about specificity, check this article as well.
Update : Also you can be more specific like this
.listing li div.big p {
font-size: 2em;
color: blue;
}

While the answer you just accepted works, I'd recommend against using CSS selectors like that.
Not only do they slow down the page load when you begin to scale your site, it's bad practice in general. (Read Writing efficient CSS selectors, Code smells in CSS) You should be using the most efficient, straight-forward CSS selectors, and not giving yourself a gigantic headache by targeting descendants after descendants after descendant.
Here is a more minimal solution:
HTML
<ul class="listing">
<li>
<p> There is plenty of wood.</p>
</li>
<li>
<p class="big">This is extra big</p>
</li>
</ul>
CSS
.listing p {
font-size: 1em;
color: red;
}
.listing .big {
font-size: 2em;
color: blue;
}
http://jsfiddle.net/BKLrS/4/
Although I'd very much recommend taking this further and doing this: http://jsfiddle.net/BKLrS/5/

Related

How to remove a third party selector?

I imported a third party CSS (which I'm not allowed to modify) into my application. This CSS file is declaring a class name and a selector, e.g.:
.third-party-class{
color: blue;
}
.third-party-class:last-of-type{
color: red;
}
My goal is to remove the .third-party-class:last-of-type selector completely by using CSS only. Of course you could just override the .third-party-class:last-of-type selector and copy every property from .third-party-class. But this is really inconvenient if .third-party-class has a lot of properties.
.third-party-class{
color: blue;
}
.third-party-class:last-of-type{
color: red;
}
.desired-li{
color: blue;
}
<div>
Currently:
<ul>
<li class="third-party-class">one</li>
<li class="third-party-class">two</li>
<li class="third-party-class">three</li>
</ul>
Desired:
<ul>
<li class="desired-li">one</li>
<li class="desired-li">two</li>
<li class="desired-li">three</li>
</ul>
</div>
Question
Is it possible to override/remove a CSS selector completely, without redeclaring all properties of the "base" class by using CSS (no JS) only?
Since we are dealing with last-of-type and you cannot change the CSS, you can add an extra element that will trigger this selector and hide it (I suppose you are able to adjust the HTML):
.third-party-class {
color: blue;
}
.third-party-class:last-of-type {
color: red;
background:pink;
font-size:250px;
opacity:0.9;
display:flex;
vertical-align:sub;
/*doesn't matter what CSS you will have here*/
}
ul li:last-of-type {
display:none!important;
}
<ul>
<li class="third-party-class">one</li>
<li class="third-party-class">two</li>
<li class="third-party-class">three</li>
<li></li>
</ul>
I agree with the comment that BenM made to your original question.
But...If the CSS is as simple (only one line) as your fiddle leads me to believe you could just use "!important" on the selector you want to overwrite.
.third-party-class:last-of-type{
color: blue !important;
}
That would allow you, on a selector by selector basis (assuming there are more than just color being used), specify what you want the third party code to do.
You could use the [attribute^=value] Selector, it will overide every ellement who starts with "value" so you can make something like this:
.third-party-class{
background: blue;
height: 20px;
width: 20px;
margin-bottom: 10px;
}
.third-party-class:last-of-type{
background: red;
}
div[class^="third-party-class"]{
background: green;
}
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
<div class="third-party-class"></div>
Sorry if i do not understand it right

Difference between .navbar and .navbar a in CSS

what is the difference between that .navbar and .navbar a
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
I can't get to find what the difference is between those. Please don't get irritated I am a noob at web technologies
If you have the below HTML
<div class="navbar">
<p>Hello World>
Hello World
</div>
.navbar will get applied to your div and then subsequently to everything that is within it
.navbar a will only get applied to the <a> tag inside the div with class=navbar
To explain it consider the following html code:
<div class = "navbar">
<a></a>
</div>
The first div will apply the following style:
overflow: hidden;
background-color: #333;
font-family: Arial;
However <a></a> will apply the following style:
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
For <a> tag you need not to add any class attribute. It will automatically apply the style as you defined the style .navbar a
In this case .navbar is a css class that refers mostly to a parent element inside which your hyperlink is located (that is the <a> in your HTML file). It can be a div or some container element, that depends on your HTML.
While .navbar a refers to any <a> that is a child element inside a container element. Hyperlinks that are not children of .navbar elements will not be affected. For example, let's look at this html chunk:
<div class="navbar">
Click here!
</div>
Let's say the above is part of your index.html file or so. In your css file:
.navbar {
/*all css style here will affect your div class="navbar"*/
}
.navbar a {
/*put something here to style all hyperlinks that are child to .navbar elements*/
}
Please let us know. You can check sites like this and read and read a lot, your skills will be largely improved with practice and reading.

Add CSS Styles to Just One HTML element

I have just started learning about web development and I'm having some issues. In the website that I am currently creating I have a navigation menu. However, I also have other ul and li elements throughout the main content of the web page. I have been trying to get certain styles to apply to just my navigation bar and not the bulleted lists in my content but no matter what I try, I either get the styles on both my navigation and the content or on none. I have looked on google and a lot of different websites, I have tried having the .navigation and # in front of my styles but nothing seems to be working. I must be doing something wrong somewhere but I have no idea what it could be. If someone could help that'll be wonderful! The following is my navigation barcode:
<div id=navbar">
<ul>
<li>
Home
</li>
<li>
About Volleyball
</li>
<li>
Sign-Up
</li>
<li>
Announcements
</li>
<li>
Contact Us
</li>
<li>
Links
</li>
</ul>
</div>
and these are the styles on my separate css style sheet that I wish to apply to just the above code:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #4da6ff;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 75px;
text-decoration: none;
}
li a:hover {
background-color: #80bfff;
}
You can simply select only elements which are children of you navbar by prepending your selectors with #navbar which selects the element with the id navbar and the selectors after that will only search in its children:
#navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #4da6ff;
}
#navbar li {
float: left;
}
#navbar li a {
display: block;
color: white;
text-align: center;
padding: 14px 75px;
text-decoration: none;
}
#navbar li a:hover {
background-color: #80bfff;
}
<div id="navbar">
<ul>
<li>Home</li>
<li>About Volleyball</li>
<li>Sign-Up</li>
<li>Announcements</li>
<li>Contact Us</li>
<li>Links</li>
</ul>
</div>
<ul>
<li>No styles applied</li>
<li>No styles applied</li>
</ul>
In your .css file you can create classes to use across elements.
Try this in the .css file.
.hello {
color: blue;
text-align: center;
}
Then try this in your html
<h1 class="hello">Hello World</h1>
This will apply the style defined in the .css file to the element with the class "hello".
More information about classes can be found here: https://www.w3schools.com/cssref/sel_class.asp
If you want to add style to any specific element then you can add CSS code to that specific element like: for tag you can use p{color:red} OR by using class: p.my-element{color:red} or using ID: p#{color red}
You are in your initial phase of learning development. So read and write your own code.
Look into id's and classes.
This will allow you to style elements seperately.

Colors of links not working, 3 examples

I have these 3 links and they are not working properly. Usually one of the
css files are applied, the rest isn't.
For example the first 2 links are working with the hover, the third one isn't.
The first 2 aren't green when you don't hover over them, the third one is green.
Best thing is to try it out to see it.
Edit: make the links green, and when you hover over them make them black. Right now it is the one or the other. This is what I want
Anyone knows what the problem may be ?
<style>
.links {
padding-top: 2%;
color: white;
font-size: 20px;
}
a:link{
color: green;
}
a:hover {
color: black;
text-decoration: underline;
}
</style>
<div class="links">
Link
</div>
<div class="links">
Link
</div>
<div class="links">
<font color="green">Link</font>
</div>
I would remove the inline tags and their styles and just use CSS like so:
CSS
.links {
padding-top: 2%;
color: white;
font-size: 20px;
}
a {
color: green;
}
a:hover {
color: white;
text-decoration: underline;
}
HTML
<div class="links">
Link
</div>
<div class="links">
Link
</div>
<div class="links">
Link
</div>
You've got some tag <> out of place too.
The colour of the <a> will only affect the colour of the <font> inside it if the <font> has color: inherit (which it does not).
If you want want to change the colour of the font element, then you must select it in the CSS:
a:hover font {
color: black;
}
You should avoid the <font> element (it was deprecated when HTML 4 was published two decades ago and marked as obsolete in HTML 5) in favour of semantic markup and CSS (in this case that would mean applying CSS directly to the <a> and removing the <font> entirely).
a:link matches only anchors which are unvisited links. You need to also select a:visited to match the visited links too:
a:link,
a:visited { color: green; }
Or you could use a { color: green } to match all <a> elements, even if they don't have an href attribute.
you are using the style wrong , this is the correct way to do it :
<div class="links">
Link
</div>
<div class="links">
Link
</div>
<div class="links">
Link
</div>

CSS selector optimization

I was looking into ways to optimize css performance and was wondering if their is an existing tool that converts complicated selectors to more simplistic ones.
The tool would look at all css rules and create unique single-pathed selectors that would contain all the proper attributes. It would than looked at every dom node and search for any css matches, and if something is found it would add the simplified selector to the node. The css that is rendered only contains the simplified versions and the html would still have the original classes/ids so the existing setup wouldn't break.
An exaggerated example of what I mean:
#original {
padding: 5px;
background: blue;
}
#original .nav {
font-size: 24px;
}
#original .nav ul {
background: black;
}
#original .nav ul li {
list-style-type: none;
}
#original .nav ul li a {
text-decoration: none;
color: green;
}
#simplified {
padding: 5px;
background: blue;
}
._ranClass1 {
font-size: 24px;
}
._ranClass2 {
background: black;
}
._ranClass3 {
list-style-type: none;
}
._ranClass4 {
text-decoration: none;
color: green;
}
<div id="original">
<h1>Original CSS</h1>
<div class="nav">
<ul class="_ranClass2">
<li>Link 1
</li>
<li>Link 1
</li>
<li>Link 1
</li>
</ul>
</div>
</div>
<div id="simplified">
<h1>Simplified CSS</h1>
<div class="nav _ranClass1">
<ul class="_ranClass2">
<li class="_ranClass3">Link 1
</li>
<li class="_ranClass3">Link 1
</li>
<li class="_ranClass3">Link 1
</li>
</ul>
</div>
</div>
Of course this would require heavy tweaking and may cause more headaches than good, but was wondering if something like this exists.
EDIT:
I am not looking at a way to write more convenient CSS, SASS/SCSS exists for that reason. I am looking for a way to optimize CSS in terms of the browser.
EX: take the original selector
#original .nav ul li a {
text-decoration: none;
color: green;
}
simplified too
._ranClass4 {
text-decoration: none;
color: green;
}
Instead of the browser looking at every 'a' node, then checking to make sure the parents match, it just matches if the node has class ._ranClass4
I would avoid what you're doing in your example. Your selectors should not be too wordy, but it's also nice for them to have some level of description so you know what their intended purpose is.
You may want to read into BEM and other CSS methodologies that can help organize and speed up writing your CSS.
You're also probably looking for something like CSSO or another gulp/grunt task. CSSO has a feature you expressly desired, to parse your markup and remove useless selectors from your CSS. Be wary, if you have DOM modifiers in some JS scripts, you might run into issues where CSSO removes these selectors from your CSS because, at the time the task was run, they didn't exist in your markup.
Here is a good article on other methods of CSS optimization.