CSS select a class preceded by another class - css

Reading CSS documentation I'm trying to create a selector for a class preceded by another class:
<div class="AAAAA">
<div class="CCCC"></div>
</div>
<div class="AAAAA">
<div class="BBBB"></div>
<div class="CCCC"></div>
</div>
I need to create a selector for .CCCC preceded by .BBBB, here my code:
.CCCC {
width: 100px;
height: 20px;
border: 1px solid #000;
}
.CCCC~.BBBB {
width: 10px;
border: 1px solid #000;
}
So in my example the first div with CCCC class whould have a width of 100px, the second div with CCCC that id preceded by the div with class BBBB should have a width of 10px.
Any idea why this is not working?

You need to reverse your order. Note, this will select all .CCCC that are siblings following .BBBB.
.BBBB ~ .CCCC {
width: 10px;
border: 1px solid #000;
}
If you only want the next one, which must immediately follow it, then this:
.BBBB + .CCCC {
width: 10px;
border: 1px solid #000;
}
It is helpful to remember that with the current capabilities of CSS, the very last item in your selector string is always going to be the item you are actually selecting and applying the properties to (if a parent selector ever comes about, then that may not be so). So your incorrect .CCCC ~ .BBBB should have flagged you as incorrect because the .BBBB is the last item, but you were wanting to change the .CCCC.

Related

How can i export shorthand CSS properties (border, border-left, border-right...) as CSS variables

If i do this:
.a {
border: var(--a-border);
border-left: var(--a-border-left);
border-right: var(--a-border-right);
border-top: var(--a-border-top);
border-bottom: var(--a-border-bottom);
}
Setting --a-border never works because individual properties like a-border-left have reset the value. What i'm thinking is that, i'm not giving any default values on those exported variables. Therefore, if --a-border is set, it should take precedence over other unset values.
As I explained in this previous answer when using border-left: var(--a-border-left) it means that border-left will always have a valid value whataver the definition of the CSS variables. Considering this your shorthand property will always be ignored because the longhand ones will always override it.
One fix is to consider the fallback property and redefine the variable used inside the shorthand in each individual propery:
.a {
border: var(--a-border);
border-left: var(--a-border-left,var(--a-border));
border-right: var(--a-border-right,var(--a-border));
border-top: var(--a-border-top,var(--a-border));
border-bottom: var(--a-border-bottom,var(--a-border));
width: 200px;
height: 100px;
display:inline-block;
}
:root {
--a-border: 5px solid red;
}
<div class="a"></div>
<div class="a" style="--a-border-bottom:5px solid green"></div>

Why jQueryUi selectable 'background-color' not work?

I tried to make work my library and I found that "background-color" CSS property not work when it used by jQueryUI. On the other hand if I simple use the "background" property with color value.
I make examples on jsfiddle:
- Working example
- Not working example
However is I simply make a class and apply to a div element in the html markup it works well.
Code:
HTML markup. Same in both cases:
<div id="selectable">
<div class="ui-widget-content">1</div>
<div class="ui-widget-content">2</div>
<div class="ui-widget-content">3</div>
</div>
Javascript. Also same in both case:
$(document).ready(function(){
$("#selectable").selectable();
});
CSS working scenario:
.ui-selected
{
background: rgb(255,0,0);
border: 1px solid yellow;
width: 100px;
height: 30px;
}
CSS non-working scenario:
.ui-selected
{
background-color: rgb(255,0,0);
border: 1px solid yellow;
width: 100px;
height: 30px;
}
LIVE DEMO
hi Peter, try putting the id in front of css, then it works.
#selectable .ui-selected { background: rgb(255,0,0); color: white; }

Optional bottom border on a div

I have two HTML samples... Basically there is always a name div in the info div but the total number of could be more.
1)
<div class="person">
<div class="info">
<div class="name">Isabelle of_Bavaria</div>
</div>
</div>
2)
<div class="person">
<div class="info">
<div class="name">King of France Charles_V the_Wise</div>
<div class="title"><label>Title:</label>King of France</div>
<div class="date"><label>Birth:</label>Jan 21st, 1337</div>
<div class="date"><label>Death:</label>Sep 16th, 1380</div>
</div>
</div>
I'm using this bit of CSS to add a line under the name as well as a box around the person div.
.person .name
{
position: relative;
text-align: center;
border-bottom: 1px dotted black;
}
.person
{
position: relative;
width: 250px;
height: auto;
border: 1px solid black;
margin-top: 5px;
padding: 5px;
}
Is there anything I can do with the css to prevent the name from having a border in sample 1 while leaving it in sample 2 without the need for additional classes or divs?
Try adding this piece of CSS at the top of your .person .name entry
.person .name:only-of-type
{
border-bottom: 0px transparent;
}
This piece of CSS means that if there is only one of the element type using the .name class in .person (in this example it's a div), it will not have a bottom border. You have to put it before the .person .name in order to overwrite it.
EDIT :
After thinking about it a bit more, i think the pseudo class :only-child would be better suited for your needs instead of :only-of-type since it will only apply if the .name is the only child of .person. So here's the updated CSS
.person .name:only-child
{
border-bottom: 0px transparent;
}
Something logically like this?
.person > .info[having more than one div child] > .name
{
border-bottom: ...
}
sadly, i don't think there's a [...] selector quite like that, however, there are "adjacent sibling selectors" ( http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors )
Maybe as #PhilPerry has suggested, you could change the concept to being "put a top-border on the div immediately following the first-child (or name)", like so:
.name:first-child + div
{
border-top: ...
}

I can't make wildcard work in CSS3

I have this html:
<div>
<input class="orderrow-1" type="text"></input>
<input class="orderrow-2" type="text"></input>
</div>
and this css:
input[class^='orderrow-']
{
border: 1px solid #e2e2e2;
color: #333;
font-size: 1em;
margin: 5px 5px 6px 0px;
padding: 5px;
width: 100px;
text-align: right;
}
I try to style all input fields with a class that begins with 'orderrow-' but above doesn't work. Any suggestions?
Edit: Sorry, above does work. The problem is that I have two casses. Like this:
<div>
<input class="trigger orderrow-1" type="text"></input>
<input class="trigger orderrow-2" type="text"></input>
</div>
The selector doesn't work when there is something before orderrow-1. Ideas?
The problem is that your using a prefix match ^= which means it will match the value of the class attribute (which includes ALL class names) which begin with orderrow- The actual value of the class attribute it 'trigger orderrow-2' if you want a wildcard selector the specification reccomends this: *= it looks for a value that contains the string at any point.
example:
input[class ^="orderrow-"], input[class *=" orderrow-"]
{
border: 1px solid #e2e2e2;
color: #333;
font-size: 1em;
margin: 5px 5px 6px 0px;
padding: 5px;
width: 100px;
text-align: right;
}
input.orderrow-1 {
border: 1px solid #F00;
}
it is important to note that because attributes selectors and clas selectors have the same specificity you should place the overribe below the attribute to ensure proper cascadence* of the styles (as per my example - jsfiddle).
EDIT: as per comments to use more precise selectors in the example.
docs see the docs for the three different wildcard selector.
*I sometimes make up words, but so long as you get my meaning...
it should be fine check out my code pen test of various css3 pseudo classes here http://codepen.io/jamiepaterson/pen/cADfs
The one you want is the purple text color on this test.

Apply CSS outside the selector

I want to make the border of my error select box outside div.wwctrl color red. The problem is it doesn't changed it color using advance css selector. I want to use css and not javascript to implement this. Is this possible? Thank you.
<div class="wwctrl">
<select id="error" name="summaryData.type"></select>
</div>
.wwctrl {
position: relative;
border: 1px solid blue;
}
select {
border: 1px solid pink;
}
select#error + div {
position: absolute;
z-index: -1;
top: 0; left: 0;
width: 100%; height: 100%;
background: yellow;
}
​
SAMPE CODE - JFIDDLE
Sure you can do so with CSS.
Most important: IDs (#) are only allowed to be used on one element in the whole document, so if you want to apply an identifier to several elements, you have to use a class (.)!
You just declare the border for the selectbox outside wwctrl.
select.error{
border:1px solid red;
}
Now you override this selector with a more specific selector for the error boxes inside wwctrl:
.wwctrl select.error{
border-color:pink;
}
Because the rule is more specific, it overrides the previous rule.
<div class="wwctrl">
<!-- has a pink border -->
<select class="error" disabled="disabled"><!--...options...--></select>
<select><!--...options...--></select>
</div>
<!-- outside wwctrl - has a red border -->
<select class="error" disabled="disabled"><!--...options...--></select>
Your modified example

Resources