CSS - Writing classes the right way - html

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!

Related

How to add color to an item using data-selected

When a user clicks on an option to select it, a data-selected attribute is added to the .item. How do i style this state of the .item DIV and give it a border color.
I've tried this but doesnt seem to work
div[data-selected=".item"]{
border-color: #333;
}
Add style like this
div.item[data-selected] {
border: 2px solid #333;
}
div.item[data-selected] {
border: 2px solid #333;
}
<div class="item" data-selected="">
ABC
</div>
More Specifically if you want to select with the attribute value, you can do like the snippet below
This type of selection is called Attribute Selector
div.item[data-selected="value"] {
border: 1px solid #000;
}
<div class="item" data-selected="value">
Having Border
</div>
<div class="item">
Not Having Border
</div>
You can style it with following selector
div.item[data-selected] {
border: 1px solid rgba(0,0,0, 0.5);
}
Here is a fiddle for this
https://jsfiddle.net/3hp2v70r/

How can i add border to a form box ?

i want to add border to the bootstrap form box.i had added border style properties but its not working . suggest please
thia is the form box class:
<div class="form-box">
<div class="form-top">
<div class="form-top-left">
And this is the css :
.form-box {
margin-top: 0px;
border-radius: 25px;
border-bottom-style: solid;
border-color: #50e54b;
}
Because of other classes, use the "!important"
border: solid 2px #50e54b!important;
You can add border to your box by using the border CSS property [border](https://developer.mozilla.org/en-US/docs/Web/CSS/border)
Here's an example usage:
border: 1px solid #FFFFFF;
The code above will add a solid border of 1px in thickness and white in colour.
Click the link above to see more about the border property.
From what I can tell, the code works fine. But if you want you can add an 8px padding so the content has room for placement instead of being crammed in there with the border. By the way, a 2px or 4px border radius looks better for the border, but it's up to you.
.form-box {
padding: 8px; /*makes it look neat*/
border-radius: 4px; /*or 2px*/
border: 1px solid red;
}

Separator after each div, except the last

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)!

Unstyling <hr> when it's wrapped in a div

I have a div that wraps around my <footer> tag. In the div is an <hr>, which needs to be in the div to have the positioning properties applied. However, it also carries over the coloring of the links in my footer. I don't want the <hr> to be the same color as the links. Is there any way to "escape" this or change the property.
I tried <hr style="color: black;"> but that didn't change anything. If you have any input on how to change the properties despite the set CSS in the div, I would greatly appreciate it.
JS Fiddle: http://jsfiddle.net/o6vmz7t5/1/
HTML
<div id="footer_style">
<hr>
<footer>
Contact
Privacy Policy
Create Account
</footer>
</div>
CSS
#footer_style {
margin: 0 auto;
position: fixed;
bottom:0;
width: 100%;
padding: 20px;
}
#footer_style a {
color: #f2f0e1;
}
#footer_style a:hover {
color: black;
}
hr tags simply have a border-top applied on them
override the hr as below
#footer_style hr {
border-top: 1px solid black;
}
#footer_style hr {
background-color: black;
height:1px;
}
JSFiddle
Whoa, it had me struggling for a minute. Apparently since the hr has no height and you cant see its internal "fill", affecting the color does nothing.
What you actually see is the border, so using border-color did it for me.
Please try below code I have try this code. Using this code solve your problem.
border-color: red;
Instead Using the color: black;
Try using in this way
border: 1px solid #000;
border-width: 1px 0px 0px 0px;
Try it

2 pixel line different color beside text css

Is a simple exercice, probably some solution better than others, but I wonder which is the best to create this kind of structure in html and css:
What I want is the text, then create 2 pixel line, 1px red and other 1 px green.
Not sure what is the best solution for crossbrowser , want to lines end same time.
Already tried with border, hr , background .. but seems not perfectly finish.
ps-looking for a solution without recurring to a image
Simple answer is to use a simple tag (<i> for example) and apply CSS styles to it.
<p>Your text <span class="line"></span></p>
CSS might look like this:
.line {
position: relative;
display: inline-block;
* display: inline; /* fix for IE bugs */
* zoom: 1; /* fix for IE bugs */
height: 1px;
width: 100px;
background-color: #f00;
border-bottom: 1px solid #00f;
vertical-align: middle;
margin-bottom: 5px;
}
CSS:
#lines{
border-bottom: 1px solid red;
border-top: 1px solid green;
display: inline-block;
height: 5px;
margin-left: 10px;
width: 100px;
}
Markup:
<span id='text'>My text</span>
<span id='lines'></span>
Here is my 2 cents... similar to Rodolfo but no spacers
http://jsfiddle.net/c4HjQ/
Use the CSS :after along with content:
<div class="container">
<div class="linetext">Text</div>
</div>
.container {
padding: 15px;
border: 4px solid black;
}
.linetext:after {
content: "";
display:inline-block;
width: 50px;
height:1px;
border-top: 1px solid green;
border-bottom: 1px solid red;
margin-left: 6px;
}
Try it: http://jsfiddle.net/wBTqV/
Documentation
CSS :after pseudo-selector on MDN - https://developer.mozilla.org/en/CSS/:after
CSS content property on MDN - https://developer.mozilla.org/en/CSS/content
you probably have a 'spacer' image (1x1 transparent image), so you can just do a
<div style="float:left">Your text</div>
<div style="float:left">
<div style="background-color:green"><img src="spacer.gif" width="100px" height="1px"></div>
<div style="background-color:red"><img src="spacer.gif" width="100px" height="1px"></div>
</div>