How to differentiate the common css class by using other css class - html

<div style="position: absolute; left: 0px; top: 5px;" class="v-caption v-caption-top_header">
<div class="v-captiontext">Create User Wizard</div><div style="clear: both; width: 0px; height: 0px; overflow: hidden;">
</div>
</div>
<div style="top: 70px; left: 10px;" class="v-absolutelayout-wrapper">
<div style="height: 5px; width: 1257px;" class="v-label v-label-intro_key intro_key">
</div>
</div>
<div style="position: absolute; left: 10px; top: 55px;" class="v-caption v-caption-intro_key">
<div class="v-captiontext">User Details.
</div>
I have to apply some styles to Create User Wizard text. But I cannot refer v-captiontext css class directly becoz the same css class is used by other text also for example User Detailstext. How can apply changes to only Create User Wizard text.

You can refer to nested classes. In your current HTML you could refer to v-caption-top_header with v-captiontext inside it like this:
.v-caption-top_header .v-captiontext {
...some styles here...
}
or
.v-caption-top_header>.v-captiontext {
...some styles here...
}
(the first of these specifies that v-captiontext is somewhere beneath v-caption-top_header in the DOM, whereas the second one specifies that it is a direct child; ie immediately beneath it in the DOM. The second one is probably preferable, except that it doesn't work in IE6, so if you need to support IE6 then use the first one)
The other options you have would require you to change the HTML code.
You could give the Create User Wizard element a specific ID and use that instead of the class:
<div class='v-captiontext' id='wizard_element'>....</div>
#wizard_element { ..... }
or use multiple classes:
<div class='v-captiontext wizard_element'>....</div>
.wizard_element { ..... }
In this case, the choice between ID or class would depend on whether you is bit of styling on this element is going to be unique - if it is specific to this element then use an ID; if you want to use it elsewhere then use a class.
If you still have problems, you could try using some trickier solutions:
CSS supports attribute selectors, which allow you to select elements based on specific HTML attributes. This can be very useful, but I don't think it'll help you here (since you don't have much in the way of attributes other than styles and classes anyway). Also this again doesn't work in IE6.
Another option could be pseudo-selectors such as :first-child or :nth-child(). Using these you could for example specify that the first matching element gets one style and others get something else. These may actually be useful for you, in conjunction with other techniques above. However you'll have problems with these with all current versions of IE, so probably not recommended.
There's a very good overview of the available CSS selectors, along with which browsers support them at Quirksmode.org: http://www.quirksmode.org/css/contents.html

As the div you want to style a child of v-caption v-caption-top_header but v-caption v-caption-top_header is not the parent of the other div you do not want to touch, this should work:
.v-caption.v-caption-top_header .v-captiontext {
/* whatever style you want */
}
Of course the better approach is simply to add an id="someuniqueid" to the div you would like to style and then add
#someuniqueid { /* style goes here */ }

give it an ID as opposed to CLASS

Is the class v-caption-top_header of the parent div unique? If yes you can use a descendant selector:
.v-caption-top_header .v-captiontext {
...
}
http://www.w3.org/TR/CSS2/selector.html#descendant-selectors

Related

Need help styling a div with multiple classes + pseudo classes externally

i have a website that i don't have the source code to. I am trying to make changes externally by placing html's before certain divs to alter how the website looks.
I have a div like this:
<div class="absolute zoom-image hidden group-hover:block top-0 border-2 border-[#f3f3f3] overflow-hidden bg-white" style="width: 721px;height: 541px;left: 721px;z-index: 50;"> CONTENT HERE </div>
I need to style it making it left:753px , but i can't define the classes of the div like i normally do on other examples.
I am trying this:
<style>
.absolute.zoom-image.hidden.group-hover:block.top-0.border-2.border-[#f3f3f3].overflow-hidden.bg-white
{
left:753px !important;
}
</style>
Sadly i can't define pseudo classes like this thus it doesnt work, does anyone have any idea how i can do this ?
Clarification:
The problem is i can not modify the div i posted earlier. I must write an additional code to modify it with !important. As i said i do not have the source code so these are not editable to me. I can only add a free html code on somewhere in the page. How can i edit this externally ? I need to write a code that refers this class since the div doesnt have any tags or id's. It has to be a code that refers to that class. I hope i made my point
Please don't be so confused about how to use the class & style attributes for the CSS. You can use only one class name in order to style your components, also, you don't need to use both inline styles ( here -> style= "width: 721px; height: 541px; left: 721px;z-index: 50;" <- ) and class styling to give CSS to your components.
Style attributes are used to write CSS along with the HTML tags. and id or class styling is used to write the CSS commands in a separate space.
This is a separate way to write the styling.
<div style="width: 721px; height: 541px; left: 721px; z-index: 50;"> CONTENT HERE </div>
Another way can be: -
(Here I used the class name as absolute)
<div class="absolute"> CONTENT HERE </div>
<style>
.absolute
{
left:753px;
top:0;
border: 2px solid #f3f3f3;
overflow:hidden;
background-color:white;
zoom:normal; // zoom-image hidden is confusing for me.
}
.absolute:hover{
dispaly:block;
</style>
// I wrote these style commands after decoding whatever you wrote in the class attribute.
Do try this code, and also do refer to MDN Docs or any other documentation before write the code.

prevent styles from applying by cascade when using LESS nested rules

We're developing a large plugin based front-end system and trying to figure out how to compose CSS rules. Basically there are two ways:
Specify parents in the class name
Specify parents in the selector (by using LESS nested rules)
And the corresponding HTML:
<div class="dashboard">
<span class="dashboard__title"></span>
<div class="custom-widget">
<span class="dashboard__custom-widget__title"></span>
</div>
</div>
And for second approach:
<div class="dashboard">
<span class="title"></span>
<div class="custom-widget">
<span class="title"></span>
</div>
</div>
I want to specify specific style for .title inside .dashboard, so using the first approach CSS will look like that:
.dashboard__title {
//styles here
}
With the second approach CSS will look like this:
.dashboard .title {
//styles here
}
which takes advantage of LESS nesting capabilities:
.dashboard {
.title {
//styles here
}
}
We really like the second approach as it makes LESS stylesheets easy to read by collapsing rules specific to any particular element and it also allows for short style names. However, it has a problem, which is cascading - in the case at hand styles defined for .title inside .dashboard will also be applied for .title inside .custom-widget. If we use the first approach the doesn't happen.
Please advice on ways to use LESS nesting capabilities but avoid the problem described.
EDIT:
It seems that I've found a solution which combines two approaches:
.custom-widget {
&__save-icon {
&--active {
padding: 777px;
background-image : url('');
}
}
}
Produces this kind of class:
.custom-widget__save-icon--active
Just choose your element by selector like below:
This style affects only for immediate children of a .dashboard element which have class .title.
.dashboard > .title {
//styles here
}
I think the question is a bit ambiguous because there is no »do it that way« answer possible. BUT some reasoning will hopefully give some hints. I think the first question you should pose yourself is: will a span.title happen to appear in other places too, so that the style rules for it can be reused.
In this case you could start out by abstracting the style definitions like that:
html:
<div class="plugin dashboard">
<span class="title">Hey There!</span>
</div>
scss:
//general rules for each title in each plugin
div.plugin {
span.title {
}
}
//override or specify
div.dashboard {
span.title {
}
}
This way, by adding a second class you can take advantage of cascading nature of CSS and with tools like LESS or SASS you can guarantee that the order of style rules will be preserved.
Otherwise, if you just want to make sure that your style rules only apply for elements inside the .plugin__container, just scope it as you did in the question and take care that the outermost »main entry point« is unique enough.
div.plugin-namespace {
//all your rules here…
}
I hope It helped! Good Luck!

Access Style property through CSS

Is there anyway i can access the style property for the particular div? For example, I have a code like below
<div class="testing" style="background-color:#ff00ff;">
This is my test Paragraph
</div>
I want to apply some other background color for this division. But i don't want to modify the class "testing", because it is being used in some other places also. Is there anyway using CSS only to access the style property and apply my new color?
I think attribute selectors may be what you are looking for:
div.testing[style="background-color:#ff00ff;"] {
background-color: new_color !important;
}
You can create another class and overwrite necessary property:
.testing-active {
background-color: red;
}
and use it like this:
<div class="testing testing-active"></div>
You need to make a style that has higher priority than the style. You could use the !important attribute to do that:
<div class="testing" style="background-color:#ff00ff;background-color:red !important;">
Big important caveat: whatever it is you're trying to do doesn't sound like a good idea, because the code will be very difficult to maintain. What is the underlying problem that you are trying to solve?
You can access the elements with this certain style like this:
.testing[style="background-color:#ff00ff;"] {
/* put your attributes here*/
}
but you cannot change the background-color attribute since this one has a higher priority in the html.
see this:
.testing[style="background-color:#ff00ff;"] {
background-color: #00f; /* not possible */
margin: 30px; /* possible */
}
what you can do is add a new attribute to your html like this:
<div class="testing" changecss="true">
This is my test Paragraph
</div>
and add this css:
.testing[changecss="true"] {
background-color: #00f;
}
See the JsFiddle as well.
"Think it is a dynamic code. How can i add new class without using javascript? "
The Answers is You cannot add a new class using CSS dynamically/ runtime. The only way to do it is by using javascript/jquery:-
HTML:
<div id="mydiv" class="testing" style="background-color:#ff00ff;">
This is my test Paragraph
</div>
JQUERY:
$('#mydiv').css('background','#ColorCode');
This way your class also wont change( since its being used in other places) and you can change the background also.
Can i ask why you are trying to achieve this using CSS?

Is it a bad practice to use custom HTML attributes and style them with CSS?

Is there any problem creating a CSS class like this:
[test] { font: 13px; }
and use it in an HTML attribute as this:
<div test></div>
Would the performance in some browsers be affected by using this method?, I've tested it with Mozilla Firefox and Google Chrome and they seem to work with no problems.
Your custom attributes are not valid HTML. You must use data-* attributes if you want to put custom data on your elements. This makes what you are doing bad practice.
In addition, there are CSS classes already that should meet your needs, unless there is more to your question than you have described.
While there is no problem in applying styles this way, and sure it does work in the browsers, you have to understand that this is not a standard way of applying styles.
Since you have also asked from a 'practice' perspective, then, yes, this surely is not the right practice. The idea is: HTML is used to define the elements to be shown within the browser window, CSS is used to apply any styling that needs to be applied on these elements and JavaScript is used to perform any 'action' on it. So, from a practice perspective, this surely is bad practice!
On another note, why the reluctance to create a class and apply it on the div? After all, this class can be reused as and when required. If you need it only once, then why not create an id selector?
HTML:
<div class="rightApproach">The right way of applying styles</div>
CSS:
.rightApproach { color:Red; }
See this fiddle where you can see your approach as well as the correct way of applying styles, be it class selector or id selector.
http://jsfiddle.net/JkRPt/
It's better to use classes. This way will not work in older browsers and it's not professional.
However it doesn't have any performance issues.
HTML:
<div class="test">
CSS:
.test { font:13px; }
its good to use classes. Example:
<div class="module accordion expand"></div>
/* All these match that */
.module { }
.accordion { }
.expand { }

inheriting from another css class

I have a problem here that i can't seem to figure out, till now my css has been a little slapdash and it was always a case of hack away till it looks right but i've decided to learn it properly and i'm trying to categorize things as much as i can.
So i have a layout that has an unordered list, this list has three li tags, within each of these li tags are two div each.
Now i have a class for each of these containers, they can be called container_1 container_2 and so on.
Now they have some unique attributes to each of them but they al also follow a set style for example, the divs in each li are side by side so its sets of two divs also they are all going to have round corners.
So i thought i could make a class class rounded_corners plus float_left and float_right so instead of re typing the code to round the corns or float something i could just reference thing class like this:
.container_1 .rounded_corners .float_left
{
}
.container_2 .rounded_corners .float_right
{
}
But when i use this i loose my styling so i used a comma and this allowed the sty;ing for the div to come back but the corners and floats didn't work.
So where am i going wrong with this?
This is my code, i have taken the code out that breaks the layout, but if you remove the comments you can see what happens.
http://jsfiddle.net/ragebunnykickass/g3Zaz/
The naming is a little different but you'll know what is meant.
Thanks.
CSS classes cannot inherit so what you have to do is split them to be as much atomic as possible. For example if you have a rounded-corners class and it may be applicable to containers:
.rounded-corners
{
/* Your CSS to define rounded corners */
}
Note that you define ONLY the properties for rounded corners. Now let's say you have a class to style containers (for example with a proper padding):
.container
{
/* Your CSS to define a nice container */
}
How to combine them together? This won't be done in CSS but in HTML, in this example this <div> inherits from both container and rounded-corners:
<div class="container rounded-corners">
</div>
Now suppose you need rounded corners for a non container object:
<div class="rounded-corners">
</div>
This is how CSS works. Do not compare them (because of name) with classes of object oriented languages. Each class define a set of attributes that will be applied to all elements that belong to that class. Final element style is the composition of the attributes inherited from each class that element belongs to.
NOTE: to summarize: answer is yes, you may have to repeat some code. You'll have trouble to manage your code (both HTML and CSS) if you use classes as short names for a style: you'll see you missed the point to separate content from style (because in HTML you'll define, using a class like rounded-corners, an explicit appearance). Imagine: next month you have to change your web-site style and fashion requirements impose you have square corners. You have to change your HTML code (unless you accept to have a rounded-corners class to apply a squared border). Much better if you simply say container and you let your CSS to define (and know) how a container should be rendered.
It may be applicable to you or not (it depends on your preferences, taste and development environment) but you may take a look to LESS. It's implemented as a JavaScript that will parse your CSSs. Of course you won't write a pure valid CSS but you'll gain many new features. In your case you may find mixins are what you need:
.rounded-corners
{
/* Your CSS here */
}
.float-left
{
/* Your CSS here */
}
.container
{
.rounder-corners
.float-left
}
You could have a CSS code like:
.container_1 {
}
.rounded_corners {
}
.float_left {
}
and then set a class to HTML element in this way:
<div class="container_1 rounded_corners float_left">...</div>
So the DIV element will inherit every style of every class!
Obviously, DIV it's just an example, you could use every tag!
If i get it well, you want a set of classes to apply to each div?
I'd break it up like that :
css
.rounded_corners {}
.float_left {}
.float_right {}
.container {}
and in the html
<li id="container_1" class="container float_left rounded_corners">...</li>
<li id="container_2" class="container float_right rounded_corners">...</li>
etc...