Separator after each div, except the last - html

Between each article, I have a horizontal separator:
.article {
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
<div id="articles">
<div class="article">Hello1</div>
<div class="article">Hello2</div>
<div class="article">Hello3</div>
<div class="article">Hello4</div>
</div>
How to remove, with CSS, the useless horizontal line after the last child of #articles? (useless because there is no next article, so no separation needed)

With this CSS:
.article:last-child { border-bottom: none; }
DEMO: https://jsfiddle.net/lmgonzalves/r8pbLaas/

Use :last-child pseudo selector:
.article:last-child { border-bottom: none; }
Basically, what this selector does is ask the question "Am I the last direct child element of my parent?", and if so, applies the rules.
Note: :last-child, as well as :first-child, are often misinterpreted by CSS beginners. It does not mean "find my last child element".
.article { margin-bottom: 20px; padding-bottom:20px; border-bottom: 1px solid #EEE; }
.article:last-child {
border-bottom: 0 none;
}
<div id="articles">
<div class="article">Hello1</div>
<div class="article">Hello2</div>
<div class="article">Hello3</div>
<div class="article">Hello4</div>
</div>
Find more information on it here:
https://css-tricks.com/almanac/selectors/l/last-child/

Something like this
.article:not(:last-child) {
margin-bottom: 20px;
padding-bottom:20px;
border-bottom: 1px solid #EEE;
}
<div id="articles">
<div class="article">Hello1</div>
<div class="article">Hello2</div>
<div class="article">Hello3</div>
<div class="article">Hello4</div>
</div>
OR
.article {
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #EEE;
}
.article:last-child {
border-bottom:0;
}
<div id="articles">
<div class="article">Hello1</div>
<div class="article">Hello2</div>
<div class="article">Hello3</div>
<div class="article">Hello4</div>
</div>

There are a few of other solutions to this problem that are worth a mention.
:first-child
You can use :first-child along with setting your border on the top of teach element - first child then removes the border on the first element and the visual output is the same.
As :first-child is in the css2 spec you can count on wider browser support than :last-child. This is an edge case for sure, but one that could conceivably be hit, especially considering the browsers in question are IEs.
It's also easier to compute in the browser than last child. The browser doesn't have to do anything to look at all the elements and work out the last, it can just stop at the first that matches. Worth considering if your front end is complex.
Adjacent selectors
Adjacent selector rules allow you to target an element only if it has another element of the specified type as a sibling. So:
p + p { border-top: 1px solid #888; }
will set a border top on a p tag only if it's preceded by a p tag.
Again this will cover edge case browsers that :last-child might not though I think it's cited as a performance concern, suffering somewhat similar issues to :last-child.
More info
A class
You can also just use a class on the last element. While this isn't necessary visually for this problem it's worth considering if you want to do more complex things with the last element, or if the containing HTML may change for some reason.

I prefer to use :first-child instead :last-child because IE7+ supports it (JSFiddle):
.article {
padding: 20px 0;
border-top: 1px solid #eee;
}
.article:first-child {
padding-top: 0;
border-top: 0;
}
<div id="articles">
<div class="article">Hello1</div>
<div class="article">Hello2</div>
<div class="article">Hello3</div>
<div class="article">Hello4</div>
</div>
See browsers support of :first-child vs :last-child.

.article:not(:last-child) {
border-bottom: 1px solid #eee;
}

Use .article:last-child { border-bottom: none; }
Each other .article element will have the defined style(/the border)!

Related

Why can't I use nth-child on mark tags?

Nothing in my code overwrites this selector, so I'm confused as to why it's not working. I've googled about it and asked a few friends and they don't know. I checked the server wasn't just taking a while to update the page by updating text and it seems fine.
CSS
mark {
color: #CCC;
background: #333;
padding: 5px;
margin: 5px;
}
mark:nth-child(even) {
background: #000;
}
HTML
<p><mark>warri0r</mark>Yes</p>
<p><mark>j3w1sh</mark>No</p>
<p><mark>MrGuy</mark>I don't know</p>
<p><mark>explode_</mark>Maybe...</p>
<p><mark>USAUSAUSA</mark>Why not?</p>
<p><mark>Samuel01</mark>Absolutely</p>
mark:nth-child(even) doesn't work because it is an only child of <p>.
Rewrite your CSS:
p:nth-child(even) mark {
background: #000;
}
(select <mark> of even <p>)
http://jsfiddle.net/hbxk3owh/
Because :nth-child looks for the parent element to find the child.
To easily understand it:
Wrap your code inside a div. Access the even paragraph using nth-child(2n) which is even children of the parent div mark.
You need not have parent div mask for your case because <body> is the parent. Just for explanation purpose I have added the class mask
mark {
color: #CCC;
background: #333;
padding: 5px;
margin: 5px;
}
.mark p:nth-child(even) mark {
background: #000;
}
<div class="mark">
<p>
<mark>warri0r</mark>Yes</p>
<p>
<mark>j3w1sh</mark>No</p>
<p>
<mark>MrGuy</mark>I don't know</p>
<p>
<mark>explode_</mark>Maybe...</p>
<p>
<mark>USAUSAUSA</mark>Why not?</p>
<p>
<mark>Samuel01</mark>Absolutely</p>
</div>
Because nth-child use in a element like this:
mark {
color: #CCC;
background: #333;
padding: 5px;
margin: 5px;
}
mark:nth-child(even) {
background: #000;
}
<p>
<mark>warri0r</mark><span>Yes</span> <br>
<mark>j3w1sh</mark><span<No</span> <br>
<mark>MrGuy</mark><span>I don't know</span>
</p>
Now p element has 3 child in yourself.

CSS - Writing classes the right way

I guess this is quite simple, although I can't seem to wrap my head around why my CSS code doesn't work.
This is my code:
.box{
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
.box .warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
<div class="col-xs-4">
<div class="box warning">dfda</div>
</div>
And a fiddle to show it in action..
http://jsfiddle.net/91b21z8k/
**My problem is, why doesn't the <div> have the .warning class assigned to it?
Don't give the space after the .box selector because it's in the same class:
.box.warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
Because including a space in the definition means the selector will target .warning elements which are children of .box; not .box elements which also have a class of .warning.
To target the <div class="box warning"></div>, simply remove the space in your selector's name:
.box.warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
Please see this updated jsFiddle
Because when you make .box .warning you're referring to a son with the class "box" of an element with the class "warning". It's never going to work.
If you did .col-xs-4 .warning, you'd have the style applied
Doc about selectors and how to apply them:
http://www.w3.org/TR/css3-selectors/#selectors
Update
check #C-link Nepal answer to get the last point (if you have to accept an answer, he gave the right answer first, XD): Removing the space between classes makes styles only applied to elements which will have all the classes present on the rule:
About the classes selector: http://www.w3.org/TR/css3-selectors/#class-html
Examples
To paint in red the paragraph:
<div class="perry">
<p class="mason">Hi</p>
</div>
.perry .mason {
color: red;
}
<div>
<p class="perry mason">Hi</p>
</div>
.perry.mason {
color: red;
}
Remove space like this FIDDLE
.box.warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
Or simply write FIDDLE
.warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
If you want style div nested into div.box then write
<div class="col-xs-4 box">
<div class="warning">dfda</div>
</div>
Space between classes in CSS means hierarchy of elements defined by selectors.
Remove the spase between two class names
.box{
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
.box.warning{
background: #FFF7F2;
border: 1px solid #ffefe5;
}
Here's JSFiddle Demo!

How to override css grouped styles

I'm looking for a way to override a bootstrap accordian style.
.panel-group .panel-heading+.panel-collapse .panel-body {
border-top: 1px solid #ddd;
}
I am using an accordian inside a <div class="settings"><div>. I can therefore overide certain styles with the settings selector.
.settings .panel-heading {
padding: 0;
border: 0;
}
But when I use the settings selector with the grouped styles at the top it doesn't work...
.settings.panel-group .panel-heading+.panel-collapse .panel-body {
border-top: 1px solid #ddd;
}
This also doesn't work...
.settings.panel-group .settings.panel-heading+.settings.panel-collapse .settings.panel-
body {
border-top: 1px solid #ddd;
}
Please can someone point me in the right direction?
If your markup looks like this:
<div class="settings">
<div class="panel-group" id="accordion">
...
</div>
</div>
Then you need to have a space in your css selector between the .settings and .panel-group to indicate a descendant element.
.settings .panel-group ... { }
If you have .settings.panel-group without a space, it means the markup would have to be <div class="settings panel-group">.

css make the text center

this is my html
<div class="logoArea">
<img src="images/oifcoman-logo.jpg"/>
<div class="titleClass">Call Center Dashboard</div>
</div>
this is my css
.logoArea {
background-color: #f5f5f5;
border: 1px solid #e3e3e3;
border-radius: 4px;
}
.titleClass {
color: #343434;
font-weight: normal;
font-family: 'Ultra', sans-serif;
font-size: 36px;
line-height: 42px;
text-transform: uppercase;
text-shadow: 0 2px white, 0 3px #777;
margin:auto;
background-color:red;
width:40%;
top:10px;
}
This is what the result:
I want it to be this:
Set the image float:left; and the text display:inline-block; and the .logoArea text-align:center;.
Working fiddle
There are few ways to solve this. Here is one with minimal changes to your existing styling.
.logoArea img {
float: left;
}
Usually it requires additional changes in the code for actual centering in the parent window, but it seems to go well with the other styles you already have.
EDIT
Looking again at the result, I'm having second thoughts. My solution is good only for non-dynamic elements (elements that won't change dynamically but remain the same). Since it appears to be a header and therefore a relatively static element, my solution may still be valid, only with adding a required amount of padding-top to the center div. I don't know how much because in your example you used a very large font-size and I have no idea of the size of the image.
You can use CSS vertical-align:middle if the element is td (not div) or try this trick: http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/
Try using:
<div class="logoArea" style="display:table-cell; vertical-align:middle">
<img src="images/oifcoman-logo.jpg"/>
<div class="titleClass">Call Center Dashboard</div>
</div>
Try this:
HTML:
<div class="logoArea">
<img src="http://i.stack.imgur.com/O3d6S.jpg?s=128&g=1"/>
<div class="titleClass">Call Center Dashboard</div>
<div style='clear:both;'></div> </div>
CSS:
.logoArea {
background-color: #f5f5f5;
border: 1px solid #e3e3e3;
border-radius: 4px;
}
.logoArea img {display:block;width:100px;height:100px;float:left;}
.logoArea .titleClass {float:left;}
JavaScript (must include jQuery first)
$(document).ready(function(){
var h=$('.logoArea').height();var ch=$('.logoArea .titleClass').height();
var pTop=((h-ch)/2)+'px';
$('.logoArea .titleClass').css('paddingTop',pTop);
});
Demo: http://jsfiddle.net/zcAjq/

CSS :first and :last-of-sequence pseudo-selectors like?

I need select some nodes in sequences via CSS. Is basically something like .button:first-of-sequence. Currently it doesn't exists, so I'm searching for an alternative method. See this case:
<div class="paginator-widget">
<div class="page">First</div>
<div class="page">Previous</div>
<div class="separator"></div>
<div class="page">1</div>
<div class="page">2</div>
<div class="page">3</div>
<div class="page">4</div>
<div class="separator"></div>
<div class="page">Next</div>
<div class="page">Last</div>
</div>
It's a paginator, and I need style each group of .page turning the first-of-sequence left border rounded, the middle-sequence (default) without border rounded and the last-of-sequence right border rounded (note that .separator breaks the sequences). Something like:
.page { background-color: black; color: white; }
.page:first-of-sequence { border-radius: 5px 0 0 5px; }
.page:last-of-sequence { border-radius: 0 5px 5px 0; }
Is it possible with pure CSS, or I need specify a new class to match this specific elements? (like this example)
Nothing like that exists, but you can use containing elements to achieve the same result.
HTML:
<div class="paginator-widget">
<section>
<div class="page">First</div>
<div class="page">Previous</div>
</section>
<section>
<div class="page">1</div>
<div class="page">2</div>
<div class="page">3</div>
<div class="page">4</div>
</section>
<section>
<div class="page">Next</div>
<div class="page">Last</div>
</section>
</div>
CSS:
.page { background-color: black; color: white; }
section div:first-child { border-radius: 5px 0 0 5px; }
section div:last-child { border-radius: 0 5px 5px 0; }
jsFiddle Link
You can use sibling combinators to simulate :first-of-sequence, but not :last-of-sequence.
For example, even if the only elements in your parent element were .page and .separator, you could match .page:first-of-sequence using .page:first-child, .separator + .page, but you wouldn't be able to select .page elements directly preceding a .separator. That's because CSS doesn't provide a previous sibling selector.
This is as far as you could go in replicating those selectors with pure CSS:
.page { background-color: black; color: white; }
.page:first-child, .separator + .page { border-radius: 5px 0 0 5px; }
.page:last-child { border-radius: 0 5px 5px 0; }
You can see in this example that the page 4 link doesn't have rounded corners like it's supposed to.
However, in your specific case, if you can rely on the first, second, second last and last elements being your first-previous and next-last pagination links, you could simply use combinations of :nth-child() and :nth-last-child():
.page { background-color: black; color: white; }
/* [First], [Next], [1] */
.page:first-child, .page:nth-last-child(2), .page:nth-child(4) { border-radius: 5px 0 0 5px; }
/* [Last], [Previous], [4] (or whatever ends up being the last page number) */
.page:last-child, .page:nth-child(2), .page:nth-last-child(4) { border-radius: 0 5px 5px 0; }
Notes:
:nth-child(3) and :nth-last-child(3) are .separator elements, so we skip those and count to 4 instead.
I think Chrome has a bug with :nth-last-child() which may force you to have to use :nth-last-of-type() instead, but I don't know if that's been fixed yet.
If all of this is too complex, the simplest alternative would be to group your end links (first-previous, next-last) and your page number links into their own parent elements separately if possible, which makes it easy for you to just target .page:first-child and .page:last-child, as Lochlan's answer shows.
I think you can use a pseudo selector available in css3 for first and last elements.
You can do something like this:
.page:first-child {
border-radius: 5px 0 0 5px;
}
.page:last-child {
border-radius: 0 5px 5px 0;
}
And the nth-child pseudo selector for select elements using the index.
for example:
.page:nth-child(2){
/* your custom style for second element */
}
Keep in mind, all css3 pseudo selectors don't work in old browsers, if you want compatibility, you should use classes for your particular elements.
you can do this by :first-child, :last-child, :nth-child
.paginator-widget div.page:first-child,
.paginator-widget div.page:nth-child(4),
.paginator-widget div.page:nth-child(9){
padding-left: 15px;
border-radius: 10px 0 0 10px;
}
.paginator-widget div.page:last-child,
.paginator-widget div.page:nth-child(2),
.paginator-widget div.page:nth-child(7){
border-radius: 0 10px 10px 0;
padding-right: 15px;
}
updated jsFiddle File
Use like this
.page:first-child { border-radius: 5px 0 0 5px; }
.page:last-child { border-radius: 0 5px 5px 0; }
and verify it with compatibility chart.
:first-child is supported IE9 properly, and IE7 and IE8 sort of (see chart).
:last-child is supported by IE9+ only.
Both of them are supported well by the good browsers.
You can also use nth-child(even) AND nth-child(odd)