Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you apply a style to only one div in the HTML?

I had working code until I tried to make the style.css page apply only to a certain div on the index.html file. This is the current code I have: http://codepen.io/anon/pen/LEXxeM

All that I did (which made it stop working) was completely wrap the style.css page in "adder { }", and put all the code in the body in a div tag.

Any ideas as to why this was an incorrect step?

In case codepen can't be accessed, below is the code.

HTML:

<!DOCTYPE html>
<!--

-->
<html lang="en">
    <head>
        <link type="text/css" rel="stylesheet" href="css/styleok.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="js/init.js"></script>





    </head>



   <body>


        <div class="adder">
        <div id="container">
            <header>
                 <h1>Task ListO</h1>
              <a href="#" id="clear">Clear all</a>

            </header>

            <section id="taskIOSection">
                    <div id="formContainer">
                    <form id="taskEntryForm">
                        <input id="taskInput" placeholder="Add your interests here..." />
                    </form>
                </div>
                <ul id="taskList"></ul>
            </section>
        </div>
        </div>






    </body>
</html>

CSS

    @import url(http://fonts.googleapis.com/css?family=Open+Sans:400, 300, 600);


    adder {

     * {
        padding:0;
        margin:0;
    }
    body {
      background:url('');
        background-color:#2a2a2a;
        font-family:'Open Sans', sans-serif;
    }
    #container {
        background-color: #111216;
        color:#999999;
        width:350px;
        margin: 50px auto auto auto;
        padding-bottom:12px;
    }
    #formContainer {
        padding-top:12px
    }
    #taskIOSection {
    }
    #taskInput {

        font-size:14px;
        font-family:'Open Sans', sans-serif;
        height:36px;
        width:311px;
        border-radius:100px;
        background-color:#202023;
        border:0;
        color:#fff;
        display:block;
        padding-left:15px;
       -webkit-transition: all 0.30s ease-in-out;
      -moz-transition: all 0.30s ease-in-out;
      -ms-transition: all 0.30s ease-in-out;
      -o-transition: all 0.30s ease-in-out;
    }
    #taskInput:focus{
      box-shadow: 0px 0px 1pt 1pt #999999;
     background-color:#111216; 
      outline:none;
    }
    ::-webkit-input-placeholder {
        color: #333333;
        font-style:italic;
        /* padding-left:10px; */
    }
    :-moz-placeholder {
        /* Firefox 18- */
        color: #333333;
        font-style:italic;
    }
    ::-moz-placeholder {
        /* Firefox 19+ */
        color: #333333;
        font-style:italic;
    }
    :-ms-input-placeholder {
        color: #333333;
        font-style:italic;
    }
    header {
        margin-top:0;
        background-color:#F94D50;
    width:338px;
    height:48px;
    padding-left:12px;
}
header h1 {
    font-size:25px;
    font-weight:300;
    color:#fff;
    line-height:48px;
  width:50%;
  display:inline;
}
header a{

  width:40%;
  display:inline;
  line-height:48px;
}
#taskEntryForm {
    background-color:#111216;
    width:326px;
    height: 48px;
    border-width:0px;
    padding: 0px 12px 0px 12px;
    font-size:0px;
}
#taskList {
    width: 350px;
    margin:auto;
    font-size:16px;
    font-weight:600;
}
ul li {
    background-color:#17181D;
    height:48px;
    width:314px;
    padding-left:12px;
    margin:0 auto 10px auto;
    line-height:48px;
    list-style:none;
  overflow:hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
}    

}

JS:

  $(document).ready(function () {
                        var i = 0;
                        for (i = 0; i < localStorage.length; i++) {
                            var taskID = "task-" + i;
     $('#taskList').append("<li id='" + taskID + "'>" + localStorage.getItem(taskID) + "</li>");
                        }
                        $('#clear').click(function () {
                            localStorage.clear();
                        });
                        $('#taskEntryForm').submit(function () {
                            if ($('#taskInput').val() !== "") {
                                var taskID = "task-" + i;
                                var taskMessage = $('#taskInput').val();
                                localStorage.setItem(taskID, taskMessage);
    $('#taskList').append("<li class='task' id='" + taskID + "'>" + taskMessage + "</li>");
                                var task = $('#' + taskID);
                                task.css('display', 'none');
                                task.slideDown();
                                $('#taskInput').val("");
                                i++;
                            }
                            return false;
                        });

                        $('#taskList').on("click", "li", function (event) {
                            self = $(this);
                            taskID = self.attr('id');
                            localStorage.removeItem(taskID);
                            self.slideUp('slow', function () {
                                self.remove();
                            });

                        });


                    });

Thank you.

like image 685
Sherri Avatar asked Nov 01 '25 17:11

Sherri


2 Answers

Normally for these one off or different pages I would consider adding a style class directly to the body tag, rather than wrapping all the content in an additional div, since it serves no semantic purposes and it is really for styling only purposes.

<body class="adder">
  <div id="container">
    <!-- other code -->
  </code>
</body>

Then add some specific styles for your custom styles pages in the same stylesheet. These need to be declared after the original definition.

/* adder overrides */
.adder #container {
    background-color: #0094FF;
}

.adder header {
    background-color:#00FF7F;
}

.adder #taskEntryForm {
    background-color:#0043FF;
}

Since the style .adder #container is more specific in this instance this is what will get applied. It's a compound set of styles, so first the stlye #container will be applied and then the styles from .adder #container will override anything that is specific in this class.

If you are using Google Chrome then press F12 and you can see the style chain in the Elements tab (as well as change them in that window for learning/demo purposes)

Demo on CodePen

like image 85
jammykam Avatar answered Nov 03 '25 07:11

jammykam


your CSS syntax is incorrect. Unless you were working with SASS in which case it would be ok. Remove adder.

I also see no point to wrapping your CSS in this adder class? It add's nothing HTML/CSS wise. If you explain what you're actually trying to achieve then maybe we can assist a bit more.

like image 23
Joe Corby Avatar answered Nov 03 '25 07:11

Joe Corby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!