Can't figure out basic CSS [duplicate] - css

This question already has answers here:
How do I select an element that has a certain class?
(5 answers)
Closed 6 years ago.
So I have few buttons that are arranged in this fashion:
<button>Home</button>
<button>Blog</button>
<button>Forums</button>
<button>Contact Us</button>
I want to have a "selected" class or ID that changes the background of the selected button.
I tried doing this:
button > .selected {
background: red !important;
}
And changed button in HTML to this:
<button class="selected">Home</button>
It didn't do anything at all, button background stayed the same color. I also tried using ID instead of class to no avail.
How can I solve this task easily?

Use:
button.selected {
background: red !important;
}
For a button with a class (Make sure there's no space between them, or you will be selecting child elements)
You should also be able to remove the !important unless there's actually a good reason to keep it. Since the selector including a class is more specific than a standard selector, it will overwrite the background colour without the use of !important.
The > symbol means a direct child of an element.
Here's a few beginers resources for leaning about CSS selectors:
https://css-tricks.com/how-css-selectors-work/
https://css-tricks.com/child-and-sibling-selectors/

try these code, it will work
button{
background:green;
}
button.selected {
background:red;
}

button:active,button:focus {
background:red;
}
<button>Home</button>
<button>Blog</button>
<button>Forums</button>
<button>Contact Us</button>

Related

How to target parents from elements with a class? [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed last year.
So, .search-chat-search-btn > form { } targets the form "inside" the element with class .search-chat-search-btn, but can I make it go the other way? As in, target the form that is outside .search-chat-search-btn?
<form>
<button class="search-chat-search-btn"></button>
</form>
Apparently this is suppose to work, but it doesn't. Target every form that has a button.
form:has(> button) {
display: inline;
border: red 1px solid;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/:has
(but then I just realised it's only supported in Safari, development version?)
You can't target a parent element in CSS. It's not possible (at least not yet). Also concerning :has, no major browser supports it yet.
As in, target the form that is outside .search-chat-search-btn?
Based on the question, I assume you want to select style all the form that not inside the .search-chat-search-btn, then you probably looking for css :not pseudo-class
Maybe you are looking for :not :
:not(.search-chat-search-btn) form{
color:blue
}
.search-chat-search-btn form{
color:red
}
<div class='search-chat-search-btn'>
<form>Yes</form></div>
<form >Hello</form>
<form >Hello</form>

Applying tr:hover but only to a certain class

I have some CSS that colors a row on my table on hover.
tr:hover {
background: gray !important;
}
However, it also highlights the filter row on the table. So I did Inspect and find it has <tr class="MuiTableRow-root MuiTableRow-hover"...etc
So, my question is, how can I modify the above code so that it applies only to that class shown above?
Edit: First attempt at apply class.
.MuiTableRow-root MuiTableRow-hover {
tr:hover {
background: gray !important;
}
}
As pointed out in the comments, please take a look at the documentation for class selectors.
You are having trouble to combine the class with the element's tag.
In this case they are written together like this:
tr.MuiTableRow-hover:hover {
background: gray !important;
}
When the HTML tag has the class: Write the tag and . and then the class
When the HTML tag has some element inside with a certain class, separate them with a space
Do yourself a favor and search for CSS tutorials to teach you the basics. It's not very hard to learn if you can spare the time
A little bit advanced is trusting CSS Specificity and leaving out !important. If your selector is more specific (or your CSS was loaded later) your style will be applied even without use of !important.
tr.MuiTableRow-hover:hover {
background: gray;
}
The css rule should look like this:
tr.MuiTableRow-hover:hover {
background: gray !important;
}
Note that using !important is not best practice so better if you try to avoid it if possible

How to select the ID that's before another ID [duplicate]

This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Closed 8 years ago.
I'm trying to do a CSS selection.
I want to select an ID that's before another ID selector.
This is the example:
<div id="wrapper">
<div class="aside_left">Left content...</div>
<div class="main_page">Main content...</div>
</div> <!-- end of wrapper -->
My objective is that the main_page stay on the left, and the aside_left change its position to the right.
Both the aside_left and the main_page have the property float:left I can't change the aside_left property to float:right because it is in many pages.
Is it possible to select the ID or CLASS that is before another ID?
My selector should be something like this: select the .aside_left that are before an .main_page
You cant do this with CSS selectors per se.. your best bet is to use something like jQuery's very accessible .parent() method.
You can see here for CSS3 and here for CSS2, this is not present in the current spec.
The speculative design for CSS4 does provide such a selector using a ! operator, but is not presently supported in any browser.
With this in mind, perhaps think about changing the logic behind what you're trying to do- can you not give the altered elements different class names to more easily identify them? Or progress down from your wrapper element?
Or, have a look into the nth-of-type selector, by using:
#wrapper .aside_left:nth-of-type(odd)
See THIS FIDDLE
This will select only the .aside_left elements which are the first child of the #wrapper element. The first child, as in the first in the DOM, as opposed to the first displayed (using float may visually produce results that dont reflect actual DOM positioning in which case you're back to using jQuery).
Only if HTML Structure Cooperates is Pure CSS Possible
I noted in my comment and ExtPro has noted in his answer that such is not possible by pure css, at least under most conditions. However, there is one set of conditions that it is possible. That is if there end up being more child elements of #wrapper in the html when something other than .main_page is present. This would be a very specifc case requirement, and may not match your situation (probably not based off your comment to ExtPro), but if so, then this code works:
#wrapper > .aside_left:nth-last-of-type(2) {
float: right;
}
See an example fiddle here. You see how this requires that there be two elements only in the case that the .main_page is there, and would demand more elements be present if .main_page is not there. So this technically does not key in on .main_page itself, but rather is using the html structure to change a preceding element based off the number of sibling elements present.
in pure CSS you could use display:flex and order , despite position in the flow of .main_page : (hover it to see them both switching sides).
/* using your HTML */
#wrapper {
display: flex;
flex-flow: row wrap;
height:200px;
width:80%;
margin:auto;
}
#wrapper > div {
width:20%;
box-shadow:inset 0 0 1px;
order:2;
}
#wrapper .main_page {
width:80%;
}
#wrapper > div.aside_left {
background:gray;
}
#wrapper > div.main_page:hover {
order:1;
}
live démo at http://codepen.io/gc-nomade/pen/Iywbj see some tips to use it here : http://css-tricks.com/snippets/css/a-guide-to-flexbox/
You could as well reset direction on #wrapper if you style your 2 div as inl'ne-boxes and restore direction on the childs divs

Bootstrap: Remove animation from transition.js [duplicate]

This question already has answers here:
disable Bootstrap's Collapse open/close animation [duplicate]
(4 answers)
Closed 7 years ago.
I am using twitter bootstrap and the transition.js plugin to make a collapse like described here.
http://getbootstrap.com/javascript/#collapse
I have two problems with it:
How can i remove the animation from the effect? it should instantly open and close.
How can i give the open panel a different background color?
To remove the animation effect you need to override the .collapsing class, add this to your CSS:
.collapsing{
-webkit-transition: none;
transition: none;
}
Now, to style the active panel you'd need to implement some jQuery code to add a custom class when a panel is shown since by default only the panel-body container is marked with the in class, and you wouldn't be able to target the heading with that. Something like this should work:
$('#accordion').on('show.bs.collapse','.panel-collapse',function(e){
$(this).closest('.panel').addClass('active').siblings('.panel').removeClass('active');
});
Then you can add your CSS rules as:
#accordion .active .panel-heading{
background: darkgrey;
}
#accordion .active .panel-body{
background: #ccc;
}
Here's a working demo
*Note: don't forget to add the "active" class to the pane that is open by default (if any)

Hovering a div to change the css on another div

I'm trying to change the color of a text while the mouse is over another div.
It will be better explained with a jsFiddle **:
What I want to do (with CSS) it that when I move the mouse on the first cicle it changes the color of the text ( Result 1).
I have tried what I found searching previous questions on stackoverflow, by using something like that :
div.circleoff:hover ~.test {
color: #fff
}
If you're looking for a color change of nested elements (not a sibling element), use
#schema>div:hover span { color:white; }
This means that hovering over #schema's direct children(division elements) will cause all its nested spans become white.
jsfiddle
Eric Meyer has a few demos on his site that will help with exactly this. Look at the 'popup' section. it can easily be adjusted to fit this.
meyerweb | css/edge
You need to check the specificity of your css selectors. Also, you should not be using the sibling operator. I think this is what you are trying to do (replaced the last 2 rules with the code below).
#schema div.circleoff:hover + .circleoff2 .test,
#schema div.circleon:hover + .circleoff2 .texte {
color: #fff
}

Resources