I am new to programming, I don't understand why should we use multiple classes in HTML. I mean in any way even if it is one class or multiple class, all CSS style is going to apply to the same content/text only, then what's the use of multiple classes when a single class will do the same thing?
You use multiple classes to keep you code DRY (Don't Repeat Yourself) for instance lets say you have two buttons a blue primary button and a red secondary button.
.red-button {
background-color: red;
border-radius: 3px;
color: #fff;
padding: 10px;
}
.blue-button {
background-color: blue;
border-radius: 3px;
color: #fff;
padding: 10px;
}
Here you have repeated css.
With multiple classes you can do something like
.button {
border-radius: 3px;
color: #fff;
padding: 10px;
}
.button.red {
background-color: red;
}
.button.blue {
background-color: blue;
}
It allows you the ability to re-use classes where you need similar characteristics. If you wrote the styles individually for each element, you would have a lot of duplicate code
A class is a way of marking an element as part of a group. Something can belong to multiple groups.
.agent {
background: #afa;
margin: 5px;
padding: 5px;
width: 10em;
list-style: none;
}
.double-agent {
background: #faa;
}
<ul>
<li class="agent">Edger Raven</li>
<li class="agent">Simon Sly</li>
<li class="agent double-agent">Sergei Skripal</li>
<li class="agent double-agent">Belgian Butcher</li>
<li class="agent">Jack the Mechanic
</li>
</ul>
I think you want to ask this:
.a{
height:20px;
width:50px;}
.b{background:red;}
<div class="a b"></div>
Why did I use "a" and "b" instead of just using "a" and applying the code of .b{} in .a{}
Consider this case:
.head{font-size:50px;}
.green{color:green;
margin:100px;}
<p class="head">This is an example</p>
<p class="head green">Hello World!</p>
<p class="green">i love maths</p>
<p class="green">3+3=3!</p>
So I wanted to make some content green, and some content big. The "Hello World!" line had both. That is why I used 2 different classes, so I won't have to repeat the same code again for "Hello World!"
The example I gave was a pretty lame one, but yes, you will have to use multiple classes so that you can use the same CSS code for other tags in your HTML without repeating the code.
Related
Any ideas on how i can execute this?
button {
background: #1c00b5;
width: 100px;
border: none;
outline: none;
color: #fff;
height: 35px;
border-radius: 30px;
margin-top: 20px;
box-shadow: 0px 5px 15px 0px rgba(28,0,181,0.3);}
i have tried to add "body .contact button" to let css file know it is the contact us page i am editing but it wont work it only goes to change the style on my other pages aswell so essentially.
body .contact button {
background:
but this doesnt work, any ideas how i can change the style of this button without affecting the others in my css file?
The .contact does not mean the contact page but instead refers to a class called contact.
You could give each of the buttons a different class for example if you wanted one to be red and the other blue:
HTML page 1:
<button class="button-red">This is a red button</button
HTML page 2:
<button class="button-blue">This is a red button</button
CSS file:
.button-red {
background-color: red;
}
.button-blue {
background-color: blue;
}
Just change the colours to your own styling.
Hope this helps.
You really need to search online for 'CSS selectors' and learn how and why to use them.
An example: create CSS rules that are true for all button and create exceptions to those rules using specific selectors. E.g. all buttons are green, except a contact button is red.
The generic rules can be put in a separate CSS file and <link>ed in a document. Create the specific rules in-document with a <style> block to override/modify/add to the linked generic rules.
There are many alternative ways to solve your issue, this is just one of them...
Tip: CSS rules are nothing more than an eleborate list of logical statements varying from easy to virtually unexplicable selectors to modify the style of your document:
if selector,
list-of-selectors,
very-special-selectors
what-does-this-one-do?!?-selector then { property: value }
example
/* put this in an external CSS file */
button { background-color: green }
/* put this in a <style> block */
button.contact { background-color: red }
<button>generic 1</button>
<button>generic 2</button>
<button>generic 3</button>
<br>
<button class="contact">contact</button>
Suppose we have a button
<button> your text </button>
and we ave to use this CSS/style
background: #1c00b5;
width: 100px;
border: none;
outline: none;
color: #fff;
height: 35px;
border-radius: 30px;
margin-top: 20px;
box-shadow: 0px 5px 15px 0px rgba(28,0,181,0.3);
so, we will add a id to button like
<button id="contact-btn"> your text </button>
and then add style using id selector
#contact-btn{
background: #1c00b5;
width: 100px;
border: none;
outline: none;
color: #fff;
height: 35px;
border-radius: 30px;
margin-top: 20px;
box-shadow: 0px 5px 15px 0px rgba(28,0,181,0.3);
}
it will work thanks
Say I have this custom button:
<div class="button-container">
<p class="button-text">Click me</p>
</div>
I want the div to have a background color, and the text to have a color in SCSS.
But I also need a different background color and text color when I'm hovering the whole div (not just the p tag and div separate, only when hovering the div)
I could write something like this:
div.button-container{
background-color: white;
p{
color: black;
}
&:hover{
background-color: red;
p{
color: blue;
}
}
}
But this does not look like a good idea, since this will become very complex and hard to manage if there are more elements involved. What is the best solution here?
I don't know exactly what the code I want would look like since I'm pretty new to SCSS, but I am thinking it would look something like this: (ignore syntax here, just an idea of how much shorter I would like it to be)
div.button-container{
background-color: white, red;
p{
color: black, blue;
}
}
Based on the html you have provided the following scss will be just fine:
.button-container {
background-color: black;
color: white;
&:hover {
background-color: gray;
color: black;
}
}
If there are several items involved, you can create some mixins that can be reused. For example. if there are several button-container elements that share the same style in the app, I will make something like this:
#mixin btnContainerBlack {
background-color: black;
color: white;
&:hover {
background-color: gray;
color: black;
}
}
In this case, you will simply add the mixin name to the element style:
.button-container {
#include btnContainerBlack;
}
There are many ways to make scss more clean and reusable. This is just one of the ideas.
Can I use something like this in css?
.bordered
{
border: 10px dashed yellow;
}
form input
{
use .bordered;
font-size: 10px;
}
Or how can this be achieved without writing each css code block to html element?
Well, you can always add class(es) to the input.
You could normalize/refactor your classes so that they are more generic (but do not overdo this). Example:
.bordered {
border: 10px dashed yellow;
}
.smaller {
font-size: 10px;
}
<form>
<input class="bordered">
<input class="smaller">
<input class="bordered smaller">
</form>
Fast answer:
.bordered, form input { border: 10px dashed yellow; }
form input { font-size: 10px; }
Solutions for re-usable code is to use CSS Preprocessor - LESS or SASS or you can create a new class and call it whatever you want and add styles to it.
Ten Reasons You Should Be Using a CSS Preprocessor - https://www.urbaninsight.com/2012/04/12/ten-reasons-you-should-be-using-css-preprocessor
References:
LESS
SASS
Unless you are using CSS preprocessors, you could do it like so:
.bordered,
form input {
border: 10px dashed yellow;
}
form input {
font-size: 10px;
}
or, if you want to avoid to list all selectors one by one, you could create a class with that style and add that class to your elements
I want to know is it possible to add some flexibility to css via this:
<div class='round5'></div>
where .round is a class with round corners and '5' determines the amount of radius. Is it possible? I have seen some where, but I don't know how the implementation takes place.
For anyone stumbling across this in 2018, whilst not fully supported CSS variables now give you the ability to pass a variable directly into your class.
<div class="round" style="--radius: 100%;"></div>
<style>
.round {
display: block;
height: 40px;
width: 40px;
border: 1px solid #BADA55;
border-radius: var(--radius);
}
</style>
You can also define root variables and pass them in as well
<div class="round" style="--radius: var(--rad-50);"></div>
<style>
:root {
--rad-0: 0%;
--rad-50: 50%;
--rad-100: 100%;
}
.round {
display: block;
height: 40px;
width: 40px;
border: 1px solid #BADA55;
border-radius: var(--radius);
}
</style>
This is also scoped to the element as well. If you set the --radius in one element is wont effect another element. Pretty jazzy right!
You can't define the border radius separate from its value because it's all one property. There's no way to tell an element to have rounded corners "in general" without also specifying how much to round them by.
However, you can do something kind of similar with multiple classes and different properties:
HTML:
<div class="rounded blue"></div>
<div class="rounded green"></div>
CSS:
.rounded {
border-radius: 5px;
}
.blue {
background: blue;
}
.green {
background: green;
}
The .rounded class adds the border radius and the .blue and .green classes add the background color.
(I like to name and order the classes such that they read logically, like <div class="large box"></div>, etc.).
Here is an answer that I came up with that requires a small amount of jQuery, and a small knowledge of Regex.
$(function() {
var number = $("div").attr("class").match(/\d+$/);
$("div").css({
"width": "100px",
"height": "100px",
"background-color": "green",
"border-radius": number + "px"
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='round54'>hello</div>
The .match() function uses Regex. Regex is used to detect parts of strings. The \d detects any digits. The + matches the previous selector 1 or more times. In other words, the number can be a multi digit number. And the $ means it has to be at the end.
So then the jQuery uses that in the border-radius property later. All you have to do is append px, and you are good to go.
Fiddle
You could do something similar but not exactly the way you've put it.
CSS
.radius{
border-radius: 10px;
border: 1px solid red;
}
.r5{
border-radius:5px;
}
HTML
<div class="radius">Hello World</div>
<br/>
<div class="radius r5">Hello World</div>
Working Example
In the example above the red border will be retained but the border-radius will change.
Note that you don't start class names with numbers, hence r5 rather than 5
You can use multiclassing on the element. Eg.:
HTML:
<div class="round">Box without border radius</div>
<div class="round rounded-5">Box with 5px border radius</div>
<div class="round rounded-10">Box with 10px border radius</div>
CSS:
.round {
border: 1px solid #000;
width: 100px;
height: 100px;
}
.round.rounded-5 {
border-radius: 5px;
}
.round.rounded-10 {
border-radius: 10px;
}
you can do this. but you have to create the css in the html document(not linked, but between the <style> tag). you can use php or javascript to make a loop. for example try this:
<style>
<?php
$round = 5;
for ($round = 50; $round <= 150; $round+=25){
echo "#round$round{
height: 300px;
width: 300px;
background: #f00;
border-radius : ".$round."px;
margin: 2px;
}
";
}
?>
</style>
<?php
for ($round=50;$round<=150; $round+=25){
echo "<div id='round$round'>
</div>
";
}
?>
hope this helps! :D
Maybe what you want is like this
CSS
.round {
border-radius: 4px; /*it's default when you juse using .round*/
}
.round.five {
border-radius: 5px;
}
.round.ten {
border-radius: 10px;
}
HTML
<div class="round five">something</div>
You can do what you are saying but you would have to reserve the keyword "round" for only this purpose. If you look at the following.
div[class*="round"] {
background-color: green;
color: white;
padding: 10px;
}
And then targeting specific variants of it using...
div[class="round5"] {
border-radius: 5px;
}
The first block of code selects all class names which contain the word round, this can be both a good thing and a bad thing.
This question already has answers here:
How do I prevent CSS inheritance?
(13 answers)
Closed 6 years ago.
Below are the sample code block i use.
I have two set of css, and want to apply onto two UL component.
however, the result come out, the inner "UL" will hold some of the css which defined for its parent.
and even some of the css defined in "b" will be override by "a"... nightmare...
how can i stop the inheritance???
<ul class="moduleMenu-ul">
/* for loop begin */
<li class="moduleMenu-li">
<a></a>
</li>
/* for loop end */
<li class="moduleMenu-li">
<a>On Over the div below will be show</a>
<div id="extraModuleMenuOptions">
<ul class="flow-ul">
/*for loop begin*/
<li class="flow-li">
<a class="flow-a"></a>
</li>
/*for loop end*/
</ul>
</div>
</li>
</ul
CSS:
.moduleMenu-ul {
width: 100%;
height: 43px;
background: #FFF url("../images/module-menu-bg.gif") top left repeat-x;
font-weight: bold;
list-style-type: none;
margin: 0;
padding: 0;
}
.moduleMenu-ul .moduleMenu-li {
display: block;
float: left;
margin: 0 0 0 5px;
}
.moduleMenu-ul .moduleMenu-li a {
height: 43px;
color: #777;
text-decoration: none;
display: block;
float: left;
line-height: 200%;
padding: 8px 15px 0;
text-transform:capitalize;
}
.moduleMenu-ul .moduleMenu-li a:hover {
color: #333;
}
.moduleMenu-ul .moduleMenu-li a.current{
color: #FFF;
background: #FFF url("../images/module-menu-current-bg.gif") top left repeat-x;
padding: 5px 15px 0;
}
#extraModuleMenuOptions {
z-index:99999;
visibility:hidden;
position:absolute;
color:#FFFFFF;
background-color:#236FBD;
}
#extraModuleMenuOptions .flow-ul {
text-align:left;
}
#extraModuleMenuOptions .flow-ul .flow-li {
display:block;
}
#extraModuleMenuOptions .flow-ul .flow-li .flow-a {
color:#FFFFFF;
}
lets say you have this:
<ul>
<li></li>
<li>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li></li>
<ul>
Now if you DONT need IE6 compatibility (reference at Quirksmode) you can have the following css
ul li { background:#fff; }
ul>li { background:#f0f; }
The > is a direct children operator, so in this case only the first level of lis will be purple.
Hope this helps
If the inner object is inheriting properties you don't want, you can always set them to what you do want (ie - the properties are cascading, and so you can overwrite them at the lower level).
e.g.
.li-a {
font-weight: bold;
color: red;
}
.li-b {
color: blue;
}
In this case, "li-b" will still be bold even though you don't want it to be. To make it not bold you can do:
.li-b {
font-weight: normal;
color: blue;
}
While this isn't currently available, this fascinating article discusses the use of the Shadow DOM, which is a technique used by browsers to limit how far cascading style sheets cascade, so to speak. He doesn't provide any APIs, as it seems that there are no current libraries able to provide access to this part of the DOM, but it's worth a look. There are links to mailing lists at the bottom of the article if this intrigues you.
Non-inherited elements must have default styles set.
If parent class set color:white and font-weight:bold style then no inherited child must set 'color:black' and font-weight: normal in their class. If style is not set, elements get their style from their parents.
The short story is that it's not possible to do what you want here. There's no CSS rule which is to "ignore some other rule". The only way around it is to write a more-specific CSS rule for the inner elements which reverts it to how it was before, which is a pain in the butt.
Take the example below:
<div class="red"> <!-- ignore the semantics, it's an example, yo! -->
<p class="blue">
Blue text blue text!
<span class="notBlue">this shouldn't be blue</span>
</p>
</div>
<div class="green">
<p class="blue">
Blue text!
<span class="notBlue">blah</span>
</p>
</div>
There's no way to make the .notBlue class revert to the parent styling. The best you can do is this:
.red, .red .notBlue {
color: red;
}
.green, .green .notBlue {
color: green;
}
Using the wildcard * selector in CSS to override inheritance for all attributes of an element (by setting these back to their initial state).
An example of its use:
li * {
display: initial;
}
Override the values present in the outer UL with values in inner UL.
you can load the new content in an iframe to avoid css inheritance.