开发者

Whats wrong if i do this?

开发者 https://www.devze.com 2023-01-29 05:24 出处:网络
CSS #web { position:absolute; left:26%; top: 60%; HTML <div class=\"web\"> HI </div> I don\'t know what\'s going wrong.Can you modify and tell me whats wrong? Thanks

CSS

#web {

position:absolute;
left:26%;
top: 60%;

HTML

<div class="web">
HI
</div>  

I don't know what's going wrong. Can you modify and tell me whats wrong? Thanks

http://jsfiddle.net/mzJV9/

The formatting doesn't effect the text inside..

The position i开发者_C百科s still the same ??


Change #web to .web

or

change class="web" to id="web".


And, if it is not just a miss while copypasting the code, you've missed the closing bracket in style.

All together, it should be like this:

CSS:

.web {
position:absolute;
left:26%;
top: 60%;
}

HTML:

<div class="web">
HI
</div>

It is also a good point not to use id's for styling whenever possible.


In CSS, there's a difference between Ids and class selectors.

<div id="div1" class="mydivclass">
  My content!
</div>

Your styling can be done various ways:

/* Set CSS based on a class (prefix . to the class name) */
.mydivclass {
  /* */
}

/* Set CSS based on the ID (prefix # to the ID) */
#div1 {
  /*  */
}

/* Set CSS based on the element type - no prefix */
div {

}

If your case you can solve the issue by changing your CSS selector to .web (to access the class of your div) or by changing your div ID to web. Also, make sure all your CSS styles have closing brackets.


Add the closing } in you css formatting and change this

<div class="web">
HI
</div>

to this

<div id="web">
HI
</div> 


To reiterate some of the other answers here...

A . prefix to your CSS selector is a class and a # prefix denotes an id. So, for example:

CSS:

.myClass{/*some style information here*/}

HTML:

<div class="myClass"><!-- Some content here --></div>

Is correct. As is:

CSS:

#myID{/*some style information here*/}

HTML:

<div id="myID"><!-- Some content here --></div>

And to follow on from unclenorton's comment about the us of class and id: I don't know if it is a strict standards requirement, but I've always done this and been given the same advice by several people:

Use ids for divs that are only used once - for example, layout divs that give the different 'sections' of your page. And use classes for repeating divs, for example a sidebarItem div.

Either way, be sure that you are consistent in your code - will make your life a whole lot easier; especially if you can maintain that consistency between projects as well, as it will become second-nature to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消