Separators between elements without hacks - html

One thing I often want to do when laying out a website is to have some elements next to each other, with separators between them. For instance, if I have three elements, I'd want two separators between them, and nothing at either end.
I achieve this in various ways. For vertical stacking of elements, I sometimes use <hr />. Horizontally, I might do something like:
<div>
<span class="notend">things</span>
<span class="notend">stuff</span>
<span>items</span>
</div>
.notend {
border-right: solid black 1px;
}
Is there a more semantic way of doing this? I want to have separators between elements without putting styling elements into the HTML part, or using non-semantic classes. I don't mind of this requires hacky CSS, I just want to get stuff to do with styling away from the HTML files.

Use this:
#menu span + span {
border-left: solid black 1px;
}
http://jsfiddle.net/thirtydot/QxZ6D/
That will apply border-left to all except the first span.
The adjacent sibling selector (+) is supported in all modern browsers except IE6.
Another way to do it is this, which is sometimes nicer because you can keep all the declarations for the "menu buttons" in one block:
http://jsfiddle.net/thirtydot/QxZ6D/1/
#menu span {
border-left: solid black 1px;
/*
a: bunch;
of: stuff;
*/
}
#menu span:first-child {
border-left: 0
}
This has exactly the same level of browser support as the first solution.
Note that if you like this solution, it's better to use :first-child rather than :last-child, because :first-child (from CSS2) is supported in IE7/8 and :last-child (only introduced in CSS3!) isn't.

you can do like this also:
span {position:relative; margin-left:5px}
span:after {
content:"|";
position:absolute;
left:-5px;
}
span:first-child:after {
content:"";
}
In this method you can also use others separators like / , \ , .
http://jsfiddle.net/sandeep/UNnxE/

how about something like this in your example:
<div>
<span>things</span>
<span>stuff</span>
<span>items</span>
</div>
div span{
border-left: solid black 1px;
}
div span:last-child{
border:none;
}
no need for additional classes.

Well for a start, you can simplify it to this:
<div>
<span>things</span>
<span>stuff</span>
<span class="end">items</span>
</div>
span {
border-right: solid black 1px;
}
span.end {
border-right: none;
}
If you're willing to drop some support in older browsers, you can reduce that to this, using the :last-child pseudo-class:
<div>
<span>things</span>
<span>stuff</span>
<span>items</span>
</div>
span {
border-right: solid black 1px;
}
span:last-child {
border-right: none;
}

I often want to have a series of items with semi-colons between them.
Here's what I do for this:
.semi-list span:not(:last-of-type)::after {
content: "; ";
}
<div class="semi-list">
<span>Item One</span>
<span>Item Two</span>
<span>Item Three</span>
</div>
It's a pretty flexible solution.
Ref:
https://www.w3schools.com/cssref/sel_not.asp
https://www.w3schools.com/csSref/sel_last-of-type.asp

Something like this?
CSS:
#note_list span {
display:inline-block;
padding:0 10px;
}
.notend {
border-right:1px solid #000000;
}
HTML:
<div id="note_list">
<span class="notend">things</span>
<span class="notend">stuff</span>
<span>items</span>
</div>

Related

how to select last div if i give the last element display none?

Please check the fiddle: http://jsfiddle.net/C23g6/1255/
I want the last child (7777) border top to be display none.
I tried but i couldn't get please give me solution.
Only through css
CSS
.common.true{
display: block;
border-top: 1px solid red;
}
.common{
display: none;
}
.true:last-child{
border-top: none;
}
I dont want the last child border not to be displayed. But not using nth child. some other way
First thing, If you do display: none of last element then how can you use css on that element(hidden element).
If you want to get Id or class (attribute) to perform action on that, you should choose js or jquery.
Second thing, the last-child would be 8888, but you want to do with second last-child, then use this, it may work :
.common:nth-last-child(2){
border-top:none !important;
}`enter code here`
You have a basic mistake in the HTML.Your last child in HTML is 8888 and you want to hide border-top of 7777.Use the following:
<div id="my-section">
<div class="common true">1111</div>
<div class="common">2222</div>
<div class="common true">3333</div>
<div class="common true">4444</div>
<div class="common true">5555</div>
<div class="common true">6666</div>
<div class="common true">7777</div>
<div class="common">8888</div>
</div>
.common.true{
display: block;
border-top: 1px solid red;
}
.common{
display: none;
}
.true:last-child{
border-top: none;
}
#my-section .common:nth-last-child(2){
border-top:none !important;
}
I have added a new css selector,which hide the border of 2nd last child.

Limit CSS selector scope

This is a bit weird and maybe is wrong but that's what I want to do...
My HTML looks like this:
<div class="item is-active">
<div class="item-part"></div>
<div class="item small">
<div class="item-part">
</div>
</div>
</div>
And with CSS I want to do this:
.item.is-active .item-part {
outline:1px solid red;
}
The problem is that the inner .item-part will also be outlined which is not desirable. I want an .item-part to be outlined only if its closest .item is .is-active.
I'd rather not use JS for this nor a direct-sibling selector since the html may differ.
I also don't want to override the rule like this:
.item:not(.is-active) .item-part {
outline:none;
}
Here is a fiddle with a live example
Thank you.
How about (Assuming only one element has the is-active class):
.item.is-active .item-part {
outline:1px solid red; /* active item part style */
}
.item.is-active .item .item-part {/* e.g. child of iten that is not active*/
outline: none; /* disable active item part style */
}
If you can't modify the class names, won't use javascript, and the direct descendant solution doesn't work (e.g. because there may be wrapper-div's sometimes); this is the only solution I can think of...
Just override the style for when your element doesn't have an is-active class:
.item.is-active .item-part {
outline:1px solid red;
}
.item.is-active .item .item-part,
.item .item-part {
outline:none;
}
The .item.is-active .item-part has higher specificty than the .item .item-part selector, so this will always be applied to the .item-part descendants.
JSFiddle demo.

How can i change style of one element mouseovering on another?

I'm trying to change style of WORK div when hovering at one of the hexagons. I've put them all into a table as a container, but it doesn;t seem to work.
Maybe you can give me a hint, thank you.
Example
I just answered another question like this (but was specific to a task). I shall use the same example so you can have a look at how it works.
You can do this just using CSS:
HTML:
<img name="image1" src="./goal/images/normalButton.png" style="vertical-align: middle; width : 183px;" />
<h2 class="mnrImageH2"><span class = "mnrImageSpan">Haberler</span></h2>
CSS:
.mnrImageH2 {
position: absolute;
top:1px;
left: 0;
width: 100%;
}
.mnrImageSpan {
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
padding: 10px;
}
h2 {
color: white;
}
img:hover + h2 {
color: #000;
}
So using the + selector we can select the h2 when we hover over an img. Take this and do what you need to do with it.
DEMO HERE
If I correctly understand your point the answer is "You cannot with current schema."
You shall use + or ~ selector. They works if elements have the same parent so you can apply CSS rule if any of hexagon is hovered but you cannot determine particular one.
Add the rule to your example to see what i'm saying:
*:hover + * > * > .work-box{
border: solid red;
}
If your elements have the same parent solution is quite simple - Example
There is good site for Russian speakers about ~ selector
This can help you in your problem and if you are not satisfy by this then comment on this post i will try to solve that also.
<div>
<div class="e" >Company</div>
<div class="e">Target</div>
<div class="e" style="border-right:0px;">Person</div>
</div>
<div class="f">
<div class="e">Company</div>
<div class="e">Target</div>
<div class="e" style="border-right:0px;">Person</div>
</div>
And use hover like this,
.e
{
width:90px;
border-right:1px solid #222;
text-align:center;
float:left;
padding-left:2px;
cursor:pointer;
}
.f .e
{
background-color:#F9EDBE;
}
.e:hover{
background-color:#FF0000;
}

should I use span tags or p tags?

I made this sketch in photoshop and I am converting it to HTML & CSS.
HTML:
<div class="pricebox">
<p class="price">360kr</p>
<p class="min">40 min</p>
<p class="info green">Körlektion</p>
</div>
<div class="pricebox">
<p class="price">1700kr</p>
<p class="min">Riskutbildnig 2</p>
<p class="info yellow">Halkan</p>
</div>
<div class="pricebox">
<p class="price">500kr</p>
<p class="min">Riskutbildning 1</p>
<p class="info red">Riskettan</p>
</div>
CSS
body {
font-family: "Myriad Pro",Myriad,"Helvetica Neue",Helvetica,Arial,sans-serif;
}
.pricebox {
display:inline-block;
border: 1px solid #EEEEEE;
border-radius: 3px;
width: 100px;
height: 150px;
margin: 5px;
}
.price{
font-size: 28px;
font-weight:300;
color: #383838;
padding: 11px;
}
.min {
font-size: 11px;
color: #909090;
padding: 0 25px;
}
.info {
height: 35%;
margin-top: 15px;
color: #EEEEEE;
font-weight: 600;
font-size: 11px;
}
.green{background-color: #a7d155;}
.yellow{background-color: #eada42;}
.red{background-color: #e54e4b;}
But I am kinda lost on how to structure everything up, should use span or div tag instead of p-tags.
Check this jsfiddle: http://jsfiddle.net/upas3/1/
Any ideas or solutions are welcome.
I would use DL list for each such block and UL list to group them together semantically:
<ul>
<li><dl>
<dt>Körlektion</dt>
<dd>360kr</dd>
<dd>40 min</dd>
</dl></li>
<li><dl>
<dt>Halkan</dt>
<dd>1700kr</dd>
<dd>Riskutbildnig 2</dd>
</dl></li>
<li><dl>
<dt>Riskettan</dt>
<dd>500kr</dd>
<dd>Riskutbildning 1</dd>
</dl></li>
</ul>
Looks like a good place to use ul and li tags.
Using paragraph tags can work but is counter intuitive for UI design.
Well, div and p are "the same" in that they are block elements, but p has more default styling, so as between them, you will want to use div.
You might use span if you want these blocks to be treated as one line of text, which it kind of looks like you do.
Update: As noted in other answers, you could also use various list tags, and style them to be inline elements, like span. That can be nice for screen readers.
This looks like tabular data, and tabular data goes in tables. However, that doesn't mean it has to look like a table!
http://jsfiddle.net/jdEP4/
table.prices {
display: block;
}
table.prices thead {
display: none;
}
table.prices tr {
display: inline-block;
border: 1px solid;
}
table.prices td {
display: block;
text-align: center;
}
.info.yellow {
background: yellow;
}
.info.green {
background: green;
}
.info.red {
background: red;
}
The CSS is incomplete, of course, but the baseline is there for reformatting the table.
Generally speaking there is no unified format, so you may use just whatever you feel better. Practically I am always trying use elements which will require less workaround. Let's say p tag will require additional margin normalization, so I would use instead divs, if you need inilne element probably better would be use span not div and so on. Just one thing which annoys me a lot it class/id names, always trying avoid somethign like size1, size2, style124 :) and use instead something that makes sense in context and will be understandable by other developers
Try this one: http://jsfiddle.net/P4xwK/
About your question I think that does not really matter you can convert every that to that you like with display:inline and with display:block. However the order of the tags should be syntactically correct.
Except that little triangle that looks like you want.
body {
font-family: "Myriad Pro",Myriad,"Helvetica Neue",Helvetica,Arial,sans-serif;
}
.pricebox {
display:inline-block;
border: 1px solid #EEEEEE;
border-radius: 3px;
width: 100px;
height: 100px;
margin: 5px;
}
.price{
font-size: 28px;
font-weight:300;
color: #383838;
text-align:center;
}
.min {
font-size: 11px;
color: #909090;
text-align:center;
}
.info {
height: 35%;
margin-top: 15px;
padding-top:15px;
color: #EEEEEE;
font-weight: 600;
font-size: 11px;
text-align:center;
}
.green{background-color: #a7d155;}
.yellow{background-color: #eada42;}
.red{background-color: #e54e4b;}
This here is a trick to paint a triangle with css:
.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
I would organize it like so:
<div class="pricebox">
<ul>
<li class="price">360kr</li>
<li class="min">40 min</li>
<li class="info green">Körlektion</li>
</ul>
</div>
then style accordingly.
The only things that really matter here are author convenience and fallbacks.
For convenience, you would use div or span, which have no default rendering except for being block vs. inline, so there are no default settings you need to override.
For fallbacks, i.e. for non-CSS rendering, you would probably want to use p instead of div and span instead of div, since you want the items to be rendered as paragraphs (with empty lines or pauses between them) but internally just as text.
They look like list items to me. Not paragraphs. Programmatically, what you have is fine. Semantically, I'd try to think of them if CSS were shut off (That's why I'd make them a list)
<ul>
<li><h2>Header</h2></li>
<li>Price</li>
<li>Description</li>
</ul>
Couple of things: 1) I try to target the elements, but using ".pricelist" on the <ul> tags would be an option. 2) If I'm correct in thinking that "Header" is important, than you could use positioning on the <ul> and <li> elems to move the header to the last item for presentation, but maintain your semantics.
I like more semantic solutions:
HTML:
<dl class="pricebox">
<dt>Körlektion</dt>
<dd><abbr class="price" title="360 kronor">360kr</abbr></dd>
<dd><abbr class="min" title="40 minutes">40 min</abbr></dd>
</dl>
<dl class="pricebox">
<dt>Halkan</dt>
<dd><abbr class="price" title="1700 kronor">1700kr</abbr></dd>
<dd><abbr class="min" title="2 FrihDehBiDeUh">Riskutbildnig 2</abbr></dd>
</dl>
<dl class="pricebox">
<dt>Riskettan</dt>
<dd><abbr class="price" title="500 kronor">500kr</abbr></dd>
<dd><abbr class="min" title="1 FrihDehBiDeUh">Riskutbildning 1</abbr></dd>
</dl>
Inspired by #MaratTanalin solution
In CSS:
Replace .green, .yellow and .red by dl:nth-of-type(1) dt, dl:nth-of-type(2) dt and dl:nth-of-type(3) dt except if the choice of color depends on something else than the position of the pricebox.

Remove ':hover' CSS behavior from element

I have CSS that changes formatting when you hover over an element.
.test:hover { border: 1px solid red; }
<div class="test">blah</div>
In some cases, I don't want to apply CSS on hover. One way would be to just remove the CSS class from the div using jQuery, but that would break other things since I am also using that class to format its child elements.
Is there a way to remove 'hover' css styling from an element?
One method to do this is to add:
pointer-events: none;
to the element, you want to disable hover on.
(Note: this also disables javascript events on that element too, click events will actually fall through to the element behind ).
Browser Support ( 98.12% as of Jan 1, 2021 )
This seems to be much cleaner
/**
* This allows you to disable hover events for any elements
*/
.disabled {
pointer-events: none; /* <----------- */
opacity: 0.2;
}
.button {
border-radius: 30px;
padding: 10px 15px;
border: 2px solid #000;
color: #FFF;
background: #2D2D2D;
text-shadow: 1px 1px 0px #000;
cursor: pointer;
display: inline-block;
margin: 10px;
}
.button-red:hover {
background: red;
}
.button-green:hover {
background:green;
}
<div class="button button-red">I'm a red button hover over me</div>
<br />
<div class="button button-green">I'm a green button hover over me</div>
<br />
<div class="button button-red disabled">I'm a disabled red button</div>
<br />
<div class="button button-green disabled">I'm a disabled green button</div>
Use the :not pseudo-class to exclude the classes you don't want the hover to apply to:
FIDDLE
<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test nohover"> blah </div>
.test:not(.nohover):hover {
border: 1px solid red;
}
This does what you want in one css rule!
I would use two classes. Keep your test class and add a second class called testhover which you only add to those you want to hover - alongside the test class. This isn't directly what you asked but without more context it feels like the best solution and is possibly the cleanest and simplest way of doing it.
Example:
.test { border: 0px; }
.testhover:hover { border: 1px solid red; }
<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test testhover"> blah </div>
add a new .css class:
#test.nohover:hover { border: 0 }
and
<div id="test" class="nohover">blah</div>
The more "specific" css rule wins, so this border:0 version will override the generic one specified elsewhere.
I also had this problem, my solution was to have an element above the element i dont want a hover effect on:
.no-hover {
position: relative;
opacity: 0.65 !important;
display: inline-block;
}
.no-hover::before {
content: '';
background-color: transparent;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 60;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<button class="btn btn-primary">hover</button>
<span class="no-hover">
<button class="btn btn-primary ">no hover</button>
</span>
You want to keep the selector, so adding/removing it won't work. Instead of writing a hard and fast CSS selectors (or two), perhaps you can just use the original selector to apply new CSS rule to that element based on some criterion:
$(".test").hover(
if(some evaluation) {
$(this).css('border':0);
}
);