One place for hosting & domains

      ltdivgt

      How To Style the HTML <div> element with CSS



      Part of the Series:
      How To Build a Website With CSS

      This tutorial is part of a series on creating and customizing this website with CSS, a stylesheet language used to control the presentation of websites. You may follow the entire series to recreate the demonstration website and gain familiarity with CSS or use the methods described here for other CSS website projects.

      Before proceeding, we recommend that you have some knowledge of HTML, the standard markup language used to display documents in a web browser. If you don’t have familiarity with HTML, you can follow the first ten tutorials of our series How To Build a Website With HTML before starting this series.

      Introduction

      This tutorial will introduce you to styling the HTML Content Division element—or <div> element—using CSS. The <div> element can be used to structure the layout of a page and break up a webpage into separate components for individual styling. In this tutorial, you will create and style <div> elements, as well as learn how to add and style other elements inside a <div> container. These skills will prepare you to use <div> elements as layout tools later on in the series when you begin recreating the demonstration website.

      The <div> element is used by adding opening and closing </div> tags to an HTML document. On its own, the <div> element typically has little visual effect on the presentation of a webpage. To specify the size, color, and other properties of a <div> element, you can assign it style rules using CSS.

      Prerequisites

      To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.

      Exploring the <div> Element in Practice

      Let’s try a hands-on exercise to study how the <div> element works. Erase everything in your styles.css file (if you added content from previous tutorials). Next, add the following CSS rule for the <div> tag selector:

      styles.css

      div {
       background-color: green; 
       height: 100px;
       width: 100px;
      }
      

      Save the styles.css file. Next, return to your index.html file, erase everything that’s there (except for the first line of code: <link rel="stylesheet" href="https://www.digitalocean.com/community/tutorials/css/styles.css">) and add the following code snippet:

      index.html

      <div></div>
      

      Notice that the <div> element has opening and closing tags but does not require any content. Save the index.html file and reload it in your browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).

      Your webpage should display a green box 100 pixels wide and 100 pixels tall as specified by the CSS rule:

      Webpage with green <div> box.

      Now that you have a styling rule for your <div> element, every <div> element you add to your page will be styled in the precisely the same manner. However, when creating a website, it is unlikely that you will want all of your HTML <div> elements to be styled in the same way. For this reason, developers often create classes that they can use to style <div> elements in different ways.

      To practice creating classes for <div> elements, erase the CSS rule you just created and add the following new three CSS rulesets to the styles.css file:

      .div-1 {
        background-color: blue; 
        height: 50px;
        width: 50px;
      }
      
      .div-2 {
        background-color: red; 
        height: 100px;
        width: 100px;
      }
      
      .div-3 {
        background-color: yellow; 
        height: 200px;
        width: 200px;
      }
      

      In this code snippet, you have created styling rules for three different classes: div-1, div-2, and div-3. Note that you have added a . before the class selector as required when declaring CSS rules for classes.

      Save the styles.css file and return to your index.html file. Erase the <div> element you just created and, add the three <div> elements to your index.html file, applying a class to each one that corresponds to the CSS class selectors that you defined in styles.css:

      index.html

      <div class="div-1"></div>
      <div class="div-2"></div>
      <div class="div-3"></div>
      

      Note that you have added the class as an attribute to the <div> tag by adding the class attribute and class name to each opening <div> tag. Save the file and reload it in your browser. You should receive something like this:

      Webpage with three <div> elements styled with different classes

      Your webpage should display three <div> elements, each styled with a different color and size according to their assigned CSS style rules. Note that each <div> element starts on its own new line as <div> elements are block-level elements and have this default behavior.

      Adding and Styling Text in a <div> Container

      You can put text inside a <div> container by inserting text in between the opening and closing <div> tags. Try adding text inside each of the <div> elements in your index.html file:

      index.html

      <div class="div-1">Blue</div>
      <div class="div-2">Red</div>
      <div class="div-3">Yellow</div>
      

      Save the file and reload it in your browser. You should now have text displayed in each of your <div> containers:

      Webpage with `<div>` elements containing text

      You can add additional HTML elements to your text inside the <div> elements. For example, try adding the HTML heading tags (<h2> to <h4>) to your text inside the <div> tags in your index.html file:

      <div class="div-1"><h2>Blue</h2></div>
      <div class="div-2"><h3>Red</h3></div>
      <div class="div-3"><h4>Yellow</h4></div>
      

      Save the file and reload it in your browser. The text inside the <div> containers should now be styled according to the default properties of the <h1> to <h4> tags:

      Webpage with header text inside `<div>` containers

      Note that the <div> elements have also adjusted their positions slightly. This repositioning is caused by the default margin properties of the <h2> through <h4> elements. You’ll learn more about margins in the next tutorial on the CSS Box Model, but for now it is fine to ignore them

      To style text inside the <div> containers, you can specify text property values in the rulesets for your <div> classes. Try adding the properties and values to your rulesets in your styles.css file as highlighted in the in the following code snippet:

      styles.css

      .div-1 {
        background-color: blue; 
        height: 50px;
        width: 50px;
        font-size: 10px; 
        color: white; iu
      }
      
      .div-2 {
        background-color: red; 
        height: 100px;
        width: 100px;
        font-size: 20px; 
        color: yellow; 
      }
      
      .div-3 {
        background-color: yellow; 
        height: 200px;
        width: 200px;
        font-size:30px;   
        color: blue; 
      }
      
      

      Save your styles.css file and reload the index.html file in your browser. The text inside the <div> containers should now be styled according to the CSS rules in your styles.css file:

      Webpage with styled header text inside <div> containers

      Conclusion

      In this tutorial you explored how to style the color and size of a <div> element and how to add and style text inside a <div> element. You will use the <div> element to control the layout of a page when you begin building the website. In the next tutorial, you will learn about the CSS Box Model, and how to use it to adjust the size of an element’s content, padding, borders, and margin.



      Source link

      How To Use a <div>, the HTML Content Division Element



      Part of the Series:
      How To Build a Website With HTML

      This tutorial series will guide you through creating and further customizing this website using HTML, the standard markup language used to display documents in a web browser. No prior coding experience is necessary but we recommend you start at the beginning of the series if you wish to recreate the demonstration website.

      At the end of this series, you should have a website ready to deploy to the cloud and a basic familiarity with HTML. Knowing how to write HTML will provide a strong foundation for learning additional front-end web development skills, such as CSS and JavaScript.

      The HTML Content Division element (<div>) acts as a container to structure a web page into separate components for individual styling. This tutorial will teach you how to create and style <div> containers on your webpage.

      On its own, the <div> element has little effect on the layout of the page and is usually given attributes to adjust its size, color, positionality, or other features. Typically, developers style <div> elements with CSS, but this tutorial will give you an understanding of how <div> elements work by styling them directly in an HTML document.

      The <div> element is styled with the HTML style attribute. As demonstrated in the example below, a <div> element can contain multiple style properties:

      Notice that the <div> element has opening and closing tags but does not require any content. To understand how the <div> element works in practice, clear your index.html file and paste the code below inside. (If you have not been following the tutorial series, you can review instructions for setting up an index.html file in our tutorial Setting Up Your HTML Project.

      <div style="width:300px;height:200px; background-color:red;">
      </div>
      

      Save the file and reload it in the browser. (For instructions on loading the file in your browser, see our tutorial here.) You should receive a red box with a width of 300 pixels and a height of 200 pixels like this:

      Red div

      You can also add content to a <div> element by adding content in between the opening and closing <div> tags. Try adding text in between your <div> tags like so:

      <div style="width:300px;height:300px; background-color:red;">
         This is text inside a div. 
        </div>
      

      Save the file and reload it in the browser. You should receive something like this:

      Div with text

      You can also put <div> containers inside <div> containers. Try adding a yellow <div> container inside the red

      container like so:

      <div style="width:300px;height:300px; background-color:red;">
      <div style="width:100px;height:100px; background-color:yellow;">
      </div>  
      </div>
      

      Save the file and reload it in the browser. You should receive something like this:

      [Div inside div](assets.digitalocean.com/articles/new-learners/div-inside-div.png)

      Note that <div> elements are block-level elements and will start on their own line and push subsequent elements down to the next line. Try adding another <div> element to your file to understand how it is pushed down to the next line (even though there appears to be enough space for it to fit next to the second <div> element:

      <div style="width:300px;height:300px;background-color:red;">
      <div style="width:100px;height:100px; background-color:yellow;">
      </div>
      </div>
      <div style="width:100px;height:100px; background-color:blue;">
      </div>
      

      Save the file and reload it in the browser. You should receive something like this:

      Three divs

      You should now have a basic understanding of how <div> elements work. We will return to <div> elements when we begin building our website in the third section of this tutorial series.



      Source link