1

I don't know if this will make sense and excuse me for the bad terminology (just started learning) but what I'm trying to do is keep a piece of code separate from another so its tags don't affect the code I don't want to be affected.

I changed up some code in codepen to make a carousel for a page. I typed up the page code in another project. I tried importing that carousel code into the main page's code, but as some tags from the carousel code are the same as the main page's, it isn't laid out as I want it to be as it's interfering. I would change the tags, but they're "universal" ones such as img or a.

Is there a way of separating that CSS code from the main code? Like assigning it a separate div and applying that div to the container for the carousel in the HTML?

Here's the carousel and the main code (trying to add the carousel underneath the about sections).

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
nonono
  • 591
  • 2
  • 8
  • 14
  • Isn't already the CSS you are using is separated from the content you have right? – Praveen Kumar Purushothaman May 03 '16 at 08:22
  • Yes, I mean I want to put a piece of CSS from another project into the main project, but it overrides the main CSS. I was wondering if I could import it without having to change every tag. – nonono May 03 '16 at 08:24

3 Answers3

1

Well it is very simple, the best approach in styling with CSS is to:

Never apply styles to HTML tags directly because this will affect all the pages where your style is included, so it would be better to:

Use classes and ids to style some specific elements in your pages, this way including your css in the page will only affect these specific elements:

#myElementId{
    ...
    ...
}
.myElementsClass{
    ...
    ...
}

Note:

Use id for a unique element in the page and a class for more than one elements in your page.


Nested CSS classes:

To answer your question about using nested classes, you can't do it with CSS only, you should use SASS or LESS

References:

For further reading you may take a look at :

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

In that case, you would need to create assign a class or id to the tag you want customised and in your css, identify that class or id. For example:

<div class="myheader">
    <p>hello</p>
</div>

<div id="myfooter">
</div>

<style>
    .myheader{
        /*ur css for myheader*/
    }

    .myheader > p {
        /*css for <p> tag in myheader class*/
        color:blue !important;
    }
    #myfooter{
        /*ur css for myfooter*/
    }
    p {
        color:red;
    }
</style>

if you noticed, class in css is identified with a . and ids are identified with a #. Classes and id can be applied to any tag you need.

Should you have overlapping css as shown above, just use an !important to specify which takes precedence.

For more info: w3s Does that answer your question?

MrWitts
  • 135
  • 1
  • 10
0

This is called CSS conflicts, you better never apply much styling attributes on tags directly, use namespace with your classes, like-

If you want to apply/change predefined attributes classes, then you can define classes like-

// same classes with a parent Css class, 
// to show it's effects only for that partcular section

.home .carousel{
    // your css goes code here
}

OR

.someOther .carousel{
    // your css goes code here
}
// Then few more nested classes

OR, if you gotta define whole of bunch new classes for your project, you can do something like-

.home-carousel{
    // your css goes code here
}

Hope solves your query!

Himanshu Aggarwal
  • 1,060
  • 8
  • 13
  • Can you nest classes within one class? Because the code I want to import has a lot of classes – nonono May 03 '16 at 08:52
  • offcourse you can do that, seems you're new to css, please search the web, you can get lot of tutorials for that – Himanshu Aggarwal May 03 '16 at 10:42
  • @l-emi to answer your question about nested css classes, you can't do it with CSS only, you should use [**SASS**](http://sass-lang.com/) or [**LESS**](http://lesscss.org/), take a look at the EDIT on my answer for further reading.. – cнŝdk May 03 '16 at 11:23