Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why is this css code not working

    why arent both buttons changing and why isnt the color of the text changing

    Code:
    <html>
    <head>
    <title>Testing</title>
    
    <style type="text/css">
    
    #submit {
    width: 200px;
    height: 75px;
    background-color: black;
    color: gray;
    text-align: center;
    line-height: 75px;
    font-size: 50px;
    border: 5px solid red;
    position: absolute;
    }
    
    #submit:hover {
    background-color: gray;
    color: black;
    border: 5px solid yellow;
    text-decoration: underline;
    cursor: pointer;
    }
    
    body {
    background-color: black;
    }
    
    #picture {
    visibility: hidden;
    position: absolute;
    top: 150px;
    }
    
    a:left
    
    </style>
    
    <script type="text/javascript">
    
    function show() {
    document.getElementById('body').style.backgroundImage = 'url(http://www.boallen.com/assets/images/randbitmap_true.png)';
    document.getElementById('submit').style.backgroundImage = 'url(http://www.ysb.on.ca/uploads/images/feature%20stories,%20other%20web
    
    %20text/Rainbow_Ocean__by_Thelma1.jpg)';
    document.getElementbyId('submit').style.color = 'black';
    }
    
    function hide() {
    document.getElementById('body').style.backgroundImage = 'none';
    document.getElementById('submit').style.backgroundImage = 'none';
    }
    
    </script>
    
    </head>
    <body id="body">
    
    <div id="submit" onClick="show();">
    Change
    </div>
    
    <div id="submit" onClick="hide();" style="left: 250px;">
    Original
    </div>
    
    <br>
    <br>
    <br>
    
    <div id="picture">
    <img src="http://www.boallen.com/assets/images/randbitmap_true.png">
    </div>
    
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Try using color codes instead of the names of the colors:
    http://www.december.com/html/spec/colorhex.html

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Using hex codes for colors is a good idea, but it won't fix your problem. You have a number of issues here:

    1. You do not have a valid doctype in your HTML (use the HTML Validator)
    2. You have more than one item in the page using the same ID (this problem is compounded by your javascript, which attempts to call two items by the same ID to change their style - this will never work)
    3. You have javascript errors
    4. You have an invalid a:left bit of text in your CSS that isn't doing anything at all.


    I'm not sure exactly what you're going for, but this is a cleaner step toward usable code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Testing</title>
    <style type="text/css">
    #submit,#submit1{
    	width:200px;
    	height:75px;
    	background-color:black;
    	color:gray;
    	text-align:center;
    	line-height:75px;
    	font-size:50px;
    	border:5px solid red;
    	position:absolute;
    }
    #submit:hover,#submit1:hover{
    	background-color:gray;
    	color:black;
    	border:5px solid yellow;
    	text-decoration:underline;
    	cursor:pointer;
    }
    body{
    	background-color:black;
    }
    #picture{
    	visibility:hidden;
    	position:absolute;
    	top:150px;
    }
    </style>
    <script type="text/javascript">
    function show() {
    	document.getElementById('body').style.backgroundImage = 'url(/randbitmap_true.png)';
    	document.getElementById('submit').style.backgroundImage = 'url(/Rainbow_Ocean__by_Thelma1.jpg)';
    	document.getElementById('submit').style.color = 'black';
    	document.getElementById('submit1').style.backgroundImage = 'url(/Rainbow_Ocean__by_Thelma1.jpg)';
    	document.getElementById('submit1').style.color = 'black';
    }
    function hide() {
    	document.getElementById('body').style.backgroundImage = 'none';
    	document.getElementById('submit').style.backgroundImage = 'none';
    	document.getElementById('submit1').style.backgroundImage = 'none';
    }
    </script>
    </head>
    <body id="body">
    <div>
      <div id="submit" onclick="show();">Change</div>
      <div id="submit1" onclick="hide();" style="left:250px;">Original</div>
      <br />
      <br />
      <br />
      <div id="picture"><img src="/randbitmap_true.png" alt="" /></div>
    </div>
    </body>
    </html>
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •