Combine [attribute=value] with :nth-child() - css

I'm using LESS and want to match an special input whose type is text.
Currently, I'm doing this:
td {
input[type=text] {
width: 100px;
}
}
For my second input of type checkbox, I need another width. I tried this:
td {
input[type=text] {
width: 100px;
&:nth-child(2) {
width: 40px;
}
}
}
But this won't work. Any ideas how to combine [type=text] with :nth-child()?

Your LESS should translate to the following CSS without any bugs:
td input[type=text] {
width: 100px;
}
td input[type=text]:nth-child(2) {
width: 40px;
}
However, if you have other elements as siblings of your text inputs, these may be interfering with the :nth-child() declaration, as :nth-child() only looks at an element's position relative to all its other siblings in the same parent, not only to other elements of its kind (i.e. input[type=text]). For example, if you had a label as the second child, then your input won't be the second child anymore as that spot has been taken by the label.
If the only inputs you have within your td are all of [type=text] you should be able to get away with using :nth-of-type() instead:
// LESS
td {
input[type=text] {
width: 100px;
&:nth-of-type(2) {
width: 40px;
}
}
}
/* CSS */
td input[type=text] {
width: 100px;
}
td input[type=text]:nth-of-type(2) {
width: 40px;
}
Just remember, though, that it only looks at the element name input and not the [type=text] attribute!
Or if you know you'll only have two text inputs you can use the general sibling selector instead to grab the one that follows the first input:
// LESS
td {
input[type=text] {
width: 100px;
& ~ input[type=text] {
width: 40px;
}
}
}
/* CSS */
td input[type=text] {
width: 100px;
}
td input[type=text] ~ input[type=text] {
width: 40px;
}

Related

Using only CSS is it possible to set a property based on class name?

Say I have some css like:
.modal-350 {
width: 350px;
}
.modal-400 {
width: 400px;
}
.modal-500 {
width: 500px;
}
etc. Using only CSS is it possible to set the width (or other property) just from the class name?
I know in javascript this is easy and also I could just use:
.modal-auto {
display: inline-block;
width:auto;
}
It's not production code, I'm just curious.
No. Even though we can use variables in CSS, we can only do so in property values and not in selector names. So something like this will not work:
.modal-$size {
width: ${size}px;
}
You can, however, use a CSS preprocessor such as LESS or SASS, and generate such rules automagically, given the requested sizes.
A SASS example:
$modal-sizes: 50 100 200 500;
%modal-default {
border-radius: 50%;
color: red;
background: green;
border-color: blue;
}
#mixin modals {
#each $size in $modal-sizes {
.modal-#{$size} {
#extend %modal-default;
width: #{$size}px;
}
}
}
#include modals;
This will compile as:
.modal-50, .modal-100, .modal-200, .modal-500 {
border-radius: 50%;
color: red;
background: green;
border-color: blue;
}
.modal-50 {
width: 50px;
}
.modal-100 {
width: 100px;
}
.modal-200 {
width: 200px;
}
.modal-500 {
width: 500px;
}

Sass ampersand and parent styles

My HTML:
<div id="form" class="special">
<div class="header">My special form</div>
<div>
And the following sass code:
#form {
.header {
padding: 20px 10px;
.special & {
background: $special-color;
}
}
}
Which produces:
#form .header {
padding: 20px 10px;
}
.special #form .header {
background: #7cc52c;
}
However this doesn't give me the result I want. Instead of the CSS above I want to have: .special#form .header (the element form with class special, not the element .special which has form)
You can achieve your desired css this way.
#form {
.header {
padding: 20px 10px;
#at-root #{selector-replace(&, '#form', '.special#form')} {
background: blue;
}
}
}
This scss compiles to the following css
#form .header {
padding: 20px 10px;
}
.special#form .header {
background: blue;
}
Using the selector-replace function, you can easily change the part of the compound selector that needs changing. In this example, that would be replacing #form with .special#form. The #at-root directive ensures that the selector is placed at the top level and not nested within any selector.
When the parent selector is appended and nested as you did in your question, it reverses the selector. There are situations where this might be useful, one such situation is when working with Modernizr as illustrated in one of the slides on here
Hope this helps.
The simplest way to do this is by using #at-root and interpolation
$special-color:#7cc52c;
#form {
.header {
padding: 20px 10px;
// use #at-root to move .special out of #form .header
#at-root {
// append #form .header using the & selector
// note! you need to use interpolation #{} to
// remove the white space between .special and #form
.special#{&} {
background: $special-color;
}
}
}
}

Issues with Locked Header CSS

I have some tables in my HelpDesk software that I wanted to lock the headers. I found this:
Scrollable table with fixed header in bootstrap
I have edited the CSS and HTML and it's working where the header does lock in to place. However, I have 2 issues that I can't figure out. First, the header row will not span the complete width of the table. Second, the final column which is a note column, I want to make larger than the rest of the columns. Here is my CSS:
.formatTable tr:nth-child(odd) {
background-color:lightgray
}
.hoverTable tr:hover {
background-color: #ffff99;
cursor:pointer;
}
/*.hoverTable tr:first-child:hover {
background-color: #0c268d;
cursor:default;
}*/
.formatTable tr:first-child {
background-color: #0c268d;
font-weight: normal;
color:white;
}
th {
white-space: nowrap;
}
#media screen and (min-width:361px)
{
table.TicketLists {
width: 100%;
}
.TicketLists thead, tbody, tr, td, th {
display: block;
}
.TicketLists tr:after {
content: ' ';
display: block;
clear: both;
}
.TicketLists tbody {
height: 600px;
overflow-y: auto;
}
.TicketLists tbody td {
width: 12.2%;
float: left;
}
.TicketLists th {
float: left;
}
.notesColumn {
width: 22%;
}
}
Here is a jsfiddle for the code: Header Locking not working fully
and here is a picture of what it currently looks like:
As you can see in the CSS, I have tried to create one column which is wider (called .notesColumn) and it has not done anything.
have a look at:
https://jsfiddle.net/t4zwsrdw/10/
your the th needs a width, because it's display block and floating.
.TicketLists th {
float: left;
width: 12.2%;
text-align:left;
}
UPDATE
.notesColumn {
width: 49.8% !important;
}
Make your .noteColumn wider and add the class to the last th:
<th class="notesColumn">
Opening Note
</th>
https://jsfiddle.net/t4zwsrdw/12/
this version has the header above the whole table.

CSS Syntax (very basic)

I would like to do the following thing and I am wondering about the best way to go about.
I have a div which is each 20% of the container's width (5 blocks). I would like to give a different background color to each block but only using one CSS class. What is the best way to do it?
In the past, I used to create 5 different classes where only the bg color is different (as everything else is the same - 20% width and same height) but I think there is a better way to do it.
Is it possible to create a class in the CSS that handles the different bg colors for each container?
I am not very sure if it will work, but but can try this - have a same class for all the divs (as you are already having - say, the class is 'myDiv'). Then In css -
.myDiv:nth-child(1){
background-color: red;
}
.myDiv:nth-child(2){
background-color: blue;
}
.myDiv:nth-child(3){
background-color: yellow;
}
and so on..
Hope this helps :)
See http://www.w3.org/TR/css3-selectors/
in this link see css3 selector for li,div and ...
tr:nth-child(2n+1) /* represents every odd row of an HTML table */
tr:nth-child(odd) /* same */
tr:nth-child(2n+0) /* represents every even row of an HTML table */
tr:nth-child(even) /* same */
/* Alternate paragraph colours in CSS */
p:nth-child(4n+1) { color: navy; }
p:nth-child(4n+2) { color: green; }
p:nth-child(4n+3) { color: maroon; }
p:nth-child(4n+4) { color: purple; }
/* Alternate division enter code herecolours in CSS */
.mydiv:nth-child(1) { color: navy; }
.mydiv:nth-child(2) { color: green; }
.mydiv:nth-child(3) { color: maroon; }
.mydiv:nth-child(4) { color: purple; }
Play around with :nth-child or adjacent sibling selectors
Like so
.parent { width: 100%; }
.parent > div { width: 20%; float: left; }
.parent > div:nth-child(1) { background-color: black; }
.parent > div:nth-child(2) { background-color: blue; }
.parent > div:nth-child(3) { background-color: purple; }
.parent > div:nth-child(4) { background-color: orange; }
.parent > div:nth-child(5) { background-color: yellow; }
Example here

css multiple class / id selectors?

I'd like to know how to write a css block that applies to either multiple ids or multiple classes:
Something like:
.class1, .class2 {
...
}
or
#id1, #id2 {
...
}
I'd like to know how to do both cases (which hopefully are cross browser compliant). Thanks.
Update: To make it more interesting, is this valid too?
#id tr, #id2 tr {
}
?
You are looking for something like this :
.oddBoxOut,
.evenBoxOut {
width: 12em;
padding: 0.5em;
margin: 0.5em;
border: solid 1px black;
}
.oddBoxOut {
float: left;
}
.evenBoxOut {
float: right;
}
Update :
p#exampleID1 { background-color: blue; }
p#exampleID2 { text-transform: uppercase; }
For your update it is also valid,
#id1 tr {
}
means that every child of node id #id1 will be CSS'ed.
you can do this too
tr#id1 {
}
Only tr will be affected if id == #id1

Resources