Multiple class with same style but different value - html

I have something like
.padding-top {
10px;
}
.padding-upper {
1px;
}
<div class="padding-top padding-upper"></div>
Which will be prioritized? Is it random or is there a chronological order here?
I have tested the code and checked that there is only 1px applied, even if I try to interchange the order of the like so:
<div class="padding-upper padding-top"></div>
Only 1px is applied. Can someone enlighten me on this one?

Yes, it is applied by order. Try this because your classes are not defined properly (missing padding properties):
.padding-top {
padding-top: 10px;
}
.padding-upper {
padding-top: 1px;
}
div {
height: 100px;
background-color: red;
}
<div class="padding-top padding-upper"></div>
It will be applied by chronological order how you declared classes. Because you have padding-top class first declared it will be overwritten. It will be overwritten by class padding-upper which is declared after.
If you change the order of declaration, style of div element will be changed too. But if you change order in class attribute then style will remain same.
But if you have the situation that you want to keep original value you can achieve with !important for that property:
.padding-top {
padding-top: 10px !important;
}
.padding-upper {
padding-top: 1px;
}
Now order for padding-top property doesn't matter. 10px will be always applied because it is decorated with !important.

Actually, The css will overrides based on the specificity level. In the below snippet. div.padding-upper is more specific than others. For more info: https://www.w3.org/TR/CSS21/cascade.html
.padding-top {
padding:10px;
width:20px;
height:20px;
background:red;
}
.padding-upper {
padding:1px;
width:20px;
height:20px;
background:red;
}
div.padding-upper {
padding:100px;
width:20px;
height:20px;
background:red;
}
<div class="padding-top padding-upper"></div>
But in your case, both selectors are in same level. So, the recent rule will be applied.
.padding-top {
padding:10px;
width:20px;
height:20px;
background:red;
}
.padding-upper {
padding:1px;
width:20px;
height:20px;
background:red;
}
<div class="padding-top padding-upper"></div>
If you want to override that above default behavior you have to use !important.
.padding-top {
padding:10px !important;
width:20px;
height:20px;
background:red;
}
.padding-upper {
padding:1px;
width:20px;
height:20px;
background:red;
}
<div class="padding-top padding-upper"></div>

Order does matter. The last declared value of multiple occurrence will be taken.
Css works the way it is written. So if you are taking two classes in your html:
<div class="padding-upper padding-top"></div>
The class which is written at last in the css:
.padding-top {
10px;
}
.padding-upper {
1px;
}
Will execute first no matter how you interchange them in your html.
Below is a simple example of the same-
.demo {
color: blue;
}
.demo1 {
color: red
}
.demo2 {
color: green
}
<div class="demo demo2 demo1 ">Hello World!!</div>

Related

Change color of Icon-Image on :hover – what other ways are there?

I have the following problem and it drives me crazy:
Basicly I have a div-container with an background. This background should change when I hover it (see pichture). It is an png and instead of white it should turn red.
What I have done until now:
First: CSS sprite
Thought it will be the best solution but becuase the div changes it's size (responsive) and the icon does not have a fixed size it was not very clean: I had a small offset on hovering. Not sure why… mybe this can be fixed… 
Second: 2 separate images
But this is not an option in this case because I need to work with inline styles. :hover ist not available as inline style.
Thrid: tried mask-box-image
Was a woderful solution… but Firefox does not support it.
Does anyone has another idea how to solve it?
Give This a Try
CSS
.icon-cont{
height:300px;
width:300px;
background-color: #ff0000;
text-align:center;
}
.icon-cont:hover{
background-color: transparent;
}
.icon-cont:hover .icon,
.icon-cont:hover .icon::before,
.icon-cont:hover .icon::after
{
border-color:#ff0000;
}
.icon{
height:0px;
border-bottom:2px solid #fff;
width:60%;
line-height:300px;
position: relative;
margin:auto;
top:50%;
}
.icon::before{
content:"";
position: absolute;
top:0;
bottom: 0;
left:-30px;
margin:auto;
height:20px;
width:20px;
border:2px solid #fff;
border-radius:50px;
}
.icon::after{
content:"";
position: absolute;
top:0;
bottom: 0;
right:-30px;
margin:auto;
height:20px;
width:20px;
border:2px solid #fff;
border-radius:50px;
}
HTML
<div class="icon-cont">
<div class="icon"></div>
</div>
Link for reference
hope this helps..
May be it will help
I posted an example following
.box {
padding: 20px;
display: inline-block;
background:tomato;
}
.box:hover {
background: transparent;
}
.box:hover span {
color: tomato;
}
.box span {
display: inline-block;
color: #fff;
}
<div class="box">
<span>a</span>
<span>----</span>
<span>b</span>
</div>
You can't change color of .png with css. I think you should make a font out of your icons in order to change their color with css later.
I haven't done that myself, but I know those fonts, like font-awesome can change color. There are some automatic generators in google to make your own font.
Try this.

Labels falling out of the box

Yeah, my titles suck :p
So I have a container, which contains <div>s. Dotted in this container are <span>s that mark off labels. These <span>s have position:absolute to make them not interfere with the layout of the <div>s.
#container {
border:1px solid red;
width:270px;
}
#container>div {
display:inline-block;
width:80px;
height:80px;
border:1px solid blue;
margin:4px;
}
#container>span {
position:absolute;
background:#ccf;
}
<!-- Please forgive the lack of spaces - this DOM is dynamically generated -->
<div id="container"><span>Marker</span><div></div><div></div><span>Marker</span><div></div><span>Marker</span><div></div><div></div></div>
In Internet Explorer, this works fine.
In Chrome, it does not. The label falls out of the box.
I understand why this happens - it's because the <span> has zero width and height within the flow of the document, allowing it to squeeze into the zero remaining space.
But I'm wondering if there's any other way to achieve the effect I want here?
EDIT: Desired effect, Chrome's bad effect
don't really quite get where you want them, something like this ? added display block to the span.
#container {
border:1px solid red;
width:270px;
}
#container>div {
display:inline-block;
width:80px;
height:80px;
border:1px solid blue;
margin:4px;
}
#container>span {
display:block;
position:absolute;
background:#ccf;
}
<!-- Please forgive the lack of spaces - this DOM is dynamically generated -->
<div id="container"><span>Marker</span><div></div><div></div><div></div><span>Marker</span><div></div><div></div></div>
strong text
Borrowing ideas from #Billy and with help from #JacobGray in the comments, the following solution applies display:block to <span>s, but only if the immediately follow an Nth <div>, N being the number of columns.
It works, but I'm not too happy with it being dependent on a constant number of columns - not great for responsive design ;) Better solutions are of course welcome.
#container {
border:1px solid red;
width:270px;
}
#container>div {
display:inline-block;
width:80px;
height:80px;
border:1px solid blue;
margin:4px;
}
#container>span {
position:absolute;
background:#ccf;
}
#container>div:nth-of-type(3n)+span {
display:block;
}
<!-- Please forgive the lack of spaces - this DOM is dynamically generated -->
<div id="container"><span>Marker</span><div></div><div></div><span>Marker</span><div></div><span>Marker</span><div></div><div></div></div>
Adding display:block to the span is what I'd suggest, or putting a marker span inside every div you want to label.
If I understand well, try this. Put tags <span> into each <div> that you want have a "label". Add position:relative to all <div> and set the properties top and left for the span.
Ps. I've modified your code below, but you should use classes
#container {
border:1px solid red;
width:270px;
}
#container>div {
display:inline-block;
width:80px;
height:80px;
border:1px solid blue;
margin:4px;
position: relative;/* added */
}
#container>div>span {/* modified */
position:absolute;
background:#ccf;
top:-5px;/* added */
left:-5px;/* added */
}
<div id="container"><div><span>Marker</span></div><div></div><div><span>Marker</span></div><div><span>Marker</span></div><div></div></div>

Create contents of div using :before and :after

I have a single div element which I want to insert content into depending on it's class.
I am able to insert 2 elements into this div using the :before and :after attributes in the CSS, but I need to be able to insert at least 5 elements.
Here's what I have for 2:
div {
background: blue;
margin: 0 auto;
width: 50px;
height: 50px;
padding: 0;
display:table-cell;
vertical-align:middle;
text-align: center;
}
div:after {
content:'';
display:block;
border:1px solid #000;
width:40px;
height:40px;
margin:5px auto;
background-color:#DDD;
}
div:before {
content:'';
display:block;
border:1px solid #000;
width:40px;
height:40px;
margin:5px auto;
background-color:#DDD;
}
Is there any way I can create extra :before and :after? Something like div:after:after.
Nope, Sorry you can't
Useful to read : http://css-tricks.com/use-cases-for-multiple-pseudo-elements/
Just add some element inside your element
HTML :
<div>
<div>
</div>
</div>
CSS :
div:after {
content:'1';
}
div > div:after {
content:'2';
}
As Paulie_D comments you can't get :after:after in CSS.
Although you could do it in jQuery by Chaining
$("div").prepend(" *before* ").prepend(" *before before* ").append(" *after* ").append(" *after after* ");
See JSFiddle here
Although it uses jQuery it's a bit more valid a solution, as really you should just use :before and :after CSS selectors for styling.

Stretch paragraph to containing div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I can't seem to manage to stretch the paragraph to it's parent div. Normally I thought this worked with width:100% and height:100%, and making it display:inline. But I'm probably missing something here. I did a Google search for my question (copy paste the title) and no result.
Fiddle: http://jsfiddle.net/QuantumHive/D8rUa/
So lets see what we have here:
HTML:
<div id="bday">
<div id="day">
<p class="value">16</p>
</div>
<div id="month">
<p class="value">december</p>
</div>
<div id="year">
<p class="value">1989</p>
</div>
</div>
CSS:
#bday {
width:220px;
height:220px;
border:solid 1px black;
overflow:hidden;
position:relative;
}
#bday div {
height:10%;
border:inherit;
float:left;
margin:5px 0px 0px 3px;
position:relative;
}
#day {
width:15%;
}
#month {
width:45%;
}
#year {
width:25%;
}
.value {
width:100%;
height:100%;
cursor:pointer;
display: block;
margin: 0;
background: red;
}
We need to get the <p> to display: block; and give it a height + width. Also take away the margin or it will flow out.
DEMO HERE
I think this is your answer:
.value {
padding:0;
margin:0;
height:100%;
}
Remove all other properties from .value and put these 2 only.
If i can understand it must be like: http://jsfiddle.net/D8rUa/2/
#bday {
width:220px;
height:220px;
border:solid 1px black;
overflow:hidden;
position:relative;
padding: 5px 0px;
}
#bday div {
height:100%;
border:inherit;
display:inline-block;
padding-left:3px;
float:left;
margin:0px 0px 0px 3px;
position:relative;
}
#day {
width:15%;
}
#month {
width:45%;
}
#year {
width:25%;
}
.value {
display:inline;
width:100%;
height:100%;
cursor:pointer;
position:relative;
}
I think what you are missing is clearfix. You need to put this piece of code in and add clearfix to your classname.
http://davidwalsh.name/css-clear-fix
add
p.value {
display:inline-block; /*ammended*/
margin:0;
width:100%;
height:100%;
cursor:pointer;
padding-bottom:1000px;/*optional */
margin-bottom:-1000px; /*optional */
border:1px solid #CCC;
}
demo
if you remove padding-left:3px; in #bday div, the left gap would disappear too!!

HTML CSS Buttons Hover Wont Work Through Class

I don't understand why does this work :
.button:active {
position:relative;
top: 2px;
left:2px;
}
But this wont work :
.button:hover {
font-size:17px;
}
It works when I use id but I want it to activate for all buttons :
#btnhome:hover{
font-size:17px;
}
This works fine but with class it wont? What am I doing wrong?
Using id and it works so sure something has to do with the specificity, over riding, try this
Demo
Demo + !important
.button:hover {
font-size:17px !important; /* Actually you don't even need !important */
}
Try:
.button *:hover { font-size:17px; }
Definitely there is an external .css file included by you via using link tag, in which .button:hover {font-size:somethingelse !important;} is defined. That's why you can't change it unless using !important again.
It is specificity. The ID is taking precedent over the pseudo style. You need to either re-write the CSS to be more general, and only put the unique values on the IDs or use the IDs plus the pseudo selector.
I took the common stuff from the ID's and moved them to a base .button class:
.button{
width: 84px;
height: 46px;
background-color:#000;
border: none;
color: white;
font-size:14px;
font-weight:700;
}
.button:hover {
font-size:17px;
}
Check out this fiddle to see it in action: http://jsfiddle.net/kJfmR/
If anyone is still facing the same problem:
I had one similar to the above, my code was like this:
<style>
.btto:hover{
background-color: grey ;
cursor: pointer;}
</style>
<body>
<button class="btto" style="color:white;
background-color:black;
padding:4%;
width:50%;
border:none"> Buy Tickets </button>
</body>
I think background-color of button inside body was overriding the one inside style
so what i did was:
<style>
.btto{
color:white;
background-color:black;
padding:4%;
width:50%;
border:none;
}
.btto:hover{
background-color: grey ;
cursor: pointer;
}
</style>
<body>
<button class="btto">Buy Tickets</button>
</body>