Web Design Forum: How to apply styles to .img after applying global rules - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to apply styles to .img after applying global rules All images within main div have borders but now need to define no bord

#1 User is offline   garethsimpsonuk 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 62
  • Joined: 02-June 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 10 April 2009 - 05:25 PM

Hi,

I have a css style that applies a 1px border to all images within the main div. I now need to create a style that is identical but without the borders. So basically 'all images within "main" will have "1px border" unless I apply other style'.

Here is the current style:

.main img.right {clear:both; float:right; margin:3px 0 7px 10px; padding:1px; border:1px solid rgb(150,150,150);}


Here is one possibility:

.main img.right-noborder {clear:both; float:right; margin:3px 0 7px 10px; padding:1px; border:0px solid rgb(150,150,150);}


Or even just this:

.noborder {border:0px;}


Is this the correct way of doing it? Could someone please advise.

Also what does the period/dot symbolise in class names (i.e. the dot between img.right). Also can you put spaces in class names.

Any help would be appreciated.

Thanks
0

#2 User is offline   bocaj 

  • Retired
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,402
  • Joined: 11-January 09
  • Reputation: 99
  • Gender:Male
  • Location:New Sarum
  • Experience:Web Guru
  • Area of Expertise:Entrepreneur

Posted 10 April 2009 - 05:47 PM

yeah that should work

img means it will apply to all tags img tags, img.classname means it'll apply to all img tags that also have that class name.

just using .classname means you could place that class in a <p> tag and it'll still apply the style rules, whereas if you've explicitly stated it to only apply to img tags it wont.

It's useful to explicitly state tags when the css properties can act a little different depending upon the html tag (commonly block and inline elements behave differently, also some browsers have quirks, and there's an array of other reasons why this can be useful).

so you could do this

img.myclass{}
p.myclass{}
div.myclass{}


And then when you apply 'myclass' to elements, you can have individual rules explicitly set depending upon whether it's paragraph tag, or an image tag and so on. If that makes sense.

You can be as specific as you like, take for example this

div#navigation ul.leftnav li.selected{color:red}
div#navigation ol.leftnav li.selected{color:blue}


That would only apply to a list item (<li>) with the class 'selected' if it's contained within and unordered list (<ul>) that has the class leftnav that's contained within a div that has a id of 'navigation'

So it would apply red selection to this

<div id="navigation">
  <ul class="leftnav">
	 <li>Item</li>
	 <li class="selected">This would be red</li>
	 <li>Item</li>
	 <li>Item</li>
  </ul>
</div>


and a blue selection to this

<div id="navigation">
  <ol class="leftnav">
	 <li>Item</li>
	 <li class="selected">This won't be red, it'll be blue</li>
	 <li>Item</li>
	 <li>Item</li>
  </ol>
</div>


but would have no effect on this

<div id="navigation">
  <ol class="rightnav">
	 <li>Item</li>
	 <li class="selected">This won't be red, or blue</li>
	 <li>Item</li>
	 <li>Item</li>
  </ol>
</div>


whereas if you just used

li.selected{color:green}


all the list items with the class 'selected' would be green.

Hope that makes some sense?

the . represents that it's a class style, and if you use a # it represents it's a unique id.

You can't put spaces in class names, because when using a class in elements, you can apply as many classes as you like.

Say we take this simple css

.right{float:right}
.left{float:left}
.red{color:red}
.bold{font-weight:bold}


and this html

<p class="right red bold">Text</p>
<p class="left red">Text</p>


The first paragraph with be floated to the right, with bold red text.

The second, will be floated to the left, with normal red text.

Using multiple classes in element can be extremely useful for setting out common parameters (like floats and widths).
0

#3 User is offline   garethsimpsonuk 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 62
  • Joined: 02-June 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 10 April 2009 - 07:02 PM

Wow! What a great reply.

That has helped a great deal, not only with my current project but with my understanding for the future. Thankyou.

The only thing I'm not sure about is the spaces in class names.

To apply 2 classes, I've always done this:

<p class="right" class="red" class="bold">Text</p>
<p class="left" class="red">Text</p>


This validates but of course your way is less code so I will try to adopt it in the future.

So if I have a class name with 2 words in how would I seperate them? Like this?

.dark-red{color:#C000000}


That's the way I've always done it.

Once again thankyou for your valuable time.
0

#4 User is offline   bocaj 

  • Retired
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,402
  • Joined: 11-January 09
  • Reputation: 99
  • Gender:Male
  • Location:New Sarum
  • Experience:Web Guru
  • Area of Expertise:Entrepreneur

Posted 10 April 2009 - 07:18 PM

Yeah, i tend to use hyphens, obviously you don't have to seperate words, you save a little from not using hyphens, but reading your code through a year later, the hyphens really do help.

<p class="right" class="red" class="bold">Text</p>


I wasn't aware you could actually do that, but class names are space delimitery, so that could be written as

Quote

<p class="right red bold">


which is a considerable saving on code (well assuming you have 20 of these on a page, the savings mount up).

For me, i use a modified framework on some of my projects, so i often end up with bits like this..

<div class="column span-15" id="dynamic">


where column sets the properties required to create a floating column, and span-15 correlates with a width (span-1 through to span-24 are varying widths).

//Below is a little unrelated

I find this makes my efficiency alot quicker (although, time is needed to trim code of unused elements etc.)

So to create a quick layout i'd just write

<div class="span-24">
  <div class="column span-8"></div>
  <div class="column span-16"></div>
  <div class="column span-6"></div>
  <div class="column span-6"></div>
  <div class="column span-6"></div>
  <div class="column span-6"></div>
</div>

And that would create a layout with two top columns, the left being twice as wide as the right, then underneath there would be four columns of equal width.

As i say, taking some time to devise a basic frame system will save you heaps in the future. And it removes alot of the hassle of playing with the numbers (especially when using floats).

i have a lot of class names set up for applying background colours/font colours/basic typography/and structure. Anything needed other than that i then add in addition too within my css.

Takes a while to set up, and get used to, but if you ever get some free time, i recommend having a play with the 960 framework. While the code is a little heavy, it'll help you understand the usefulness of having a framework, and you can create a smaller custom one designed for yourself. With logical class names you don't need to remember, because the names are intuitive to yourself.

like dark-red-text would be an obvious choice for setting the text colour to a dark red, and then something like dark-red for setting the bg colour to dark red, and so on. Whatever makes logical sense to you.
0

#5 User is offline   garethsimpsonuk 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 62
  • Joined: 02-June 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 10 April 2009 - 07:44 PM

yes your so right. my designs get so complicated as i've got divs that go really deep. sometimes it hard to get your head round 'where' an item actually is.

i've added '960 framework' to my 'to google' list. it looks interesting. layout seems to be one of the hardest things to get your head round. i feel that once i've got that nailed the rest will fall into place.

i find going through the source code of sites from top to bottom. looking at each element and reading the corresponding div and seeing how they react with each other.

thanks again for the help m8!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users