How do I get a CSS selector to skip over <dd> and apply to all the <dt> only? I am trying to only apply border-bottom to the first <dt>.
I tried: dl > dt:not(:last-child), but that didn't work either.
CSS:
dt:not(:last-child) {
border-bottom: 5px solid #dddddd;
}
HTML:
<dl>
<dt>Dates of Operation</dt>
<dd style="display: none;"></dd>
<dt>Dates of Operation</dt>
<dd style="display: none;"></dd>
<dt>Dates of Operation</dt>
<dd style="display: none;"></dd>
</dl>
You are looking for the :first-of-type CSS pseudo-class:
dl > dt:first-of-type {
border-bottom: 5px solid #dddddd;
}
See demo fiddle here.
As always, make sure you check for target browser support before using it. If you have a large audience, the standard way of achieving what you want is using a regular class.
Use :first-child selector
dl > dt:first-child {
border-bottom: 5px solid #dddddd;
}
Fiddle: http://jsfiddle.net/2SZ9f/
you can use first-child
It works exacly the same as last child but it's the first child. should work fine. Here is a codepen :
http://codepen.io/anon/pen/eFkbq
It would depends on which is the special element. If the first is the only one with border then you should use
dt:first-child{
border-bottom: 5px solid #dddddd;
}
But if you want to have border on every child except the last one, then try this:
dt {
border-bottom: 5px solid #dddddd;
}
dt:last-child {
border-bottom: none;
}
This will override the border bottom style added to dt elements.
If you only have 2 elements then you can add id, class or first-child. If the last child is the special one then add the css for it and you may have many other dt elements.
Related
I have this navigation links and upon hovering the last link (styled as button), I want it to have a property of bottom border of color pink instead of brown upon hover to complement the color red button. Your help is appreciated. Thanks in advance!
.header-nav a:hover {
border-bottom: 5px solid rgb(56,47,50); /*brown border-bottom*/
}
.header-nav a:last-of-type:hover {
border-bottom: 5px solid rgb(255,144,171); /*pink-border-bottom*/
}
<nav class="container header-nav">
<ul>
<li>Home</li>
<li>Cupcakes</li>
<li>Cakes</li>
<li>Events</li>
<li>Order Now!</li>
</ul>
<br>
</nav>
Here's the fiddle:
https://jsfiddle.net/ssqLspsn/
i DID IT!!! https://jsfiddle.net/Please_Reply/9hk6okpd/2/ hope this is what you want!!!
.header-nav a:hover {
border-bottom: 5px solid rgb(56,47,50); /*brown border-bottom*/
}
.header-nav .btn:hover {
border-bottom: 5px solid rgb(255,144,171); /*pink-border-bottom*/
}
I DID IT!!! Look here https://jsfiddle.net/Please_Reply/9hk6okpd/2/ hope this is what you want!!!
.header-nav a:hover {
border-bottom: 5px solid rgb(56,47,50); /*brown border-bottom*/
}
.header-nav .btn:hover {
border-bottom: 5px solid rgb(255,144,171); /*pink-border-bottom*/
}
Try to put some class to the button (last element)
You css is fine although
like
<div class="header-nav">
<a>some text</a>
<a>some text</a>
<a class="btn">some text</a>
</div>
css
header-nav .btn:hover {
border-bottom: 5px solid rgb(255,144,171); /*pink-border-bottom*/
}
If you want to keep your css selectors, you should select on li, not a
Raplace .header-nav a:last-of-type:hover with .header-nav .li:last-child a:hover
maybe you will put id or class name for last li and make css code the specific id :)
The best thing to do when checking which css is applied on hover is to have the hover attribute active from the console.
You can do this both in firefox and chrome by going to the inspector.
Here is the guide to seeing how to activate the hover attribute
See :hover state in Chrome Developer Tools
As for your specific problem it would help if you also posted your html, because css will not work if you're applying the style to a wrong element.
.header-nav li:hover {
border-bottom: 5px solid rgb(56,47,50); /*brown border-bottom*/
}
.header-nav li:last-of-type:hover {
border-bottom: 5px solid rgb(255,144,171); /*pink-border-bottom*/
}
Good Luck!
I have a table where certain rows are highlighted. I want the first row of the highlighted rows to have a border applied to the top of it.
I'm trying the following, but it's not working.
.highlighted{
background-color: lightyellow;
}
table:first-child tr.highlighted td{
border-top: solid 1px gray;
}
Here is a fiddle.
http://jsfiddle.net/k2ky6/
The problem I'm having is that the border is added to every highlighted row, not just the first.
Is this possible with pure css?
You can do it like this, with the selector .highlighted ~ .highlighted, and applying a display:block to your <tr>. Here is the updated jsFiddle
.highlighted {
display:block;
border-top:1px solid lightgrey;
background-color:lightyellow;
}
.highlighted ~ .highlighted {
border:0;
}
What that does is the .highlighted applies to the first one, and the .highlighted ~ .highlighted applies to everything but the first one
The :first-child pseudo-selector is about being the first child of a parent element. If your <TR>s are all children of one common <TABLE> then tr:first-child applies only to the first row in the table.
What you actually wrote - table:first-child - applies to <TABLE> elements which are first children of their particular parent (first <TABLE> in a <DIV> for example).
You might get something working by putting each group of highlighted cells in a <TBODY> if that is possible for you - because then the <TR> would indeed be the first child of that <TBODY>.
As far as I know what you actually asked for - first <TR> with a particular class - is not possible at all in pure CSS, not even with the new CSS3 selectors.
Is there a benefit/difference to this .highlighted ~ .highlighted ?
...as opposed to creating a style for just the first highlighted one such as
.highlighted{
background-color: lightyellow;
}
.highlighted-2 td{
border-top: solid 1px gray;
background-color: lightyellow;
}
If i have a ul, how do i set a border-bottom on all the li items except the last one? I'm also trying to make the width of the border 180px. here's my code:
HTML
<ul class="sideNav">
<li>History</li>
<li>Mission</li>
<li>Associations</li>
<li>Careers</li>
</ul>
CSS
.sideNav {
margin:0;
padding:0;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
width:216px;
background-color:#017dc6;
}
.sideNav li {
list-style-type:none;
text-align:center;
text-transform:uppercase;
width:180px;
}
.sideNav li a {
border-bottom:1px solid #80bee3;
width:180px;
color:#ffffff;
text-decoration:none;
display:block;
padding:18px;
}
Dec 13th, 2018: Note that there is no need to use this solution in today's modern browsers. You should feel free using the answer below mine li:not(:last-child) { border-bottom: 1px solid red; }
Without using JavaScript and not having to support IE7 and below (IE8 fails on the second one) there are three options you can use: :first-child, :lastchild and the + selector:
:first-child
li { border-top: 1px solid red; }
li:first-child { border-top: none; }
:last-child
li { border-bottom: 1px solid red; }
li:last-child { border-bottom: none; }
+ selector
li+li { border-top: 1px solid red; }
The problems arise if you need to support IE8 and your design doesn't allow you to put a border on the top of your elements as opposed to the bottom.
EDIT:
The fix to your width issue is that you're adding 180px to 2*18px of the a element, remove the left right padding, and set padding: 18px 0; and you'll be golden. (updated jsfiddle: http://jsfiddle.net/NLLqB/1/)
Here's a jsfiddle of it: http://jsfiddle.net/NLLqB/
Use :not(:last-child).
.sideNav li:not(:last-child) a {
/* your css here */
}
One way: You can override for the last one using a rule like below with :last-child (Since you tagged css3):
.sideNav li:last-child a {
border-bottom:0px; /*Reset the border for the anchor of last li of this ul*/
}
Demo
There are polyfills available for IE8, but if you can provide a classname for the last one and apply rule to it to reset the style would be of better support, rather than using css3 (if your intention is to support older browsers as well).
if you are using scripting language like jquery you can easily add a class to the last child as jquery takes care of cross-browser compatibility.
You can also use in-line CSS to correct this problem.
<li><a style="border-bottom:none" href="/careers.asp">Careers</a></li>
This will remove the border from the "Careers" link. Note that it will only work if you put that code in the <a> tag since that is what the border is being applied to, not the <li>.
The downside of this is that if you add something to the list, then the second-to-last list item will have no bottom border, and the last will.
Not the best solution, but an alternative one that accomplishes the same thing. Cheers!
I have the following CSS:
#form1,#form2,#form3,#form5,#form6,#form7,#form8 div{
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
For some reason, the last id does not get the style. (ie #form8 does not get the style).
If I switch the css like this (Without changing any html code):
#form1,#form2,#form3,#form5,#form8,#form6,#form7 div{
Now #form7 does not have the style.
Did I code the structure wrongly please? Its very strange
It's probably an HTML markup issue. Can you provide it?
A wild guess is that your code looks like:
<div id="form8">
...
</div>
And the last part of your CSS selector (#form8 div) actually targets a markup like:
<div id="form8">
<div>
...
</div>
</div>
Here's a meta advice: if your selectors list is so long and apparently targets the same type of element (a form), use a class!
.form{
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
Seems you are targeting div#form1, div#form2 ... and so on... You can skip writing div for the selector. Try this
#form1, #form2, #form3, #form5, #form6, #form7, #form8 {
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
Or even better ... give all of them a class name like <form class="myform" id="whatever"></form> and use:
.myform {
padding:10px;
border:1px solid blue;
background-color: grey;
font-family:"lucida grande",tahoma,sans-serif;
}
You should just use #form1,#form2,#form3,#form5,#form6,#form7,#form8
#foem8 div refers to the all child divs of the element with this is #foem8
I have a variable number of table rows (n), and I would like the border bottom to apply to rows 0..(n-1)
how do I do that?
You have two options: (1) adding a specialized class in the HTML to the last row; or (2) using the :last-child pseudo class in your CSS.
Option 1: Specialized Class
If you can apply classes to your HTML, you can add a specialized class to the final row. If your markup is being generated by a server-side script (eg. a PHP script), you will need to edit that script to add similar markup.
HTML:
<table>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr class="last">
<td>
</td>
</tr>
</table>
CSS:
table
{
border-collapse:collapse;
}
tr
{
border-bottom: 1px solid #000;
}
tr.last
{
border-bottom: none;
}
Option 2: CSS Pseudo Class
The alternative is to use the :last-child CSS pseudo class. Using the :last-child class doesn't require any changes to the HTML and so may be a better choice if you aren't able to change the HTML. The CSS is almost identical to the above:
CSS:
table
{
border-collapse:collapse;
}
tr
{
border-bottom: 1px solid #000;
}
tr:last-child
{
border-bottom: none;
}
The drawback of this approach is that versions of Internet Explorer before 9 don't support the :last-child pseudo class.
I know this is an old question, but it's worth mentioning that now with CSS3 all you have to do is us the not() selector in your CSS, like this:
tr:not(:last-child) {
border-bottom: 1px solid #E3E3E3;
}
tr:last-child td {
border-bottom: none;
}
Saves you putting a class on the last tag.
this is used with any class call in html
tr:last-child {
border-bottom: none;
}
You can also use this way
table tr:not(:last-of-type) { border-bottom: none; }
If you're using jQuery you can use the following script
$(document).ready(function() {
$(".tableClass tr:not(:last) > td").css('border-bottom', ' 1px solid #DDD');
});