Module -1 | Day 3

Day 3: CSS - Colors, Backgrounds, Borders, Margins, and Paddings

NiLBiTE Web Development Course

Day 3: CSS - Colors, Backgrounds, Borders, Margins, and Paddings

CSS: Colors, Backgrounds, Borders, Margins, and Paddings

CSS Colors

CSS allows you to set the color of text and other elements using the color property.


p {
    color: blue;
}
            

Colors can be specified by name, hex value, rgb value, or other methods.


p {
    color: #ff0000; /* Hex */
    color: rgb(255, 0, 0); /* RGB */
    color: red; /* Name */
}
            

CSS Backgrounds

Background properties allow you to set the background color or image for an element.


body {
    background-color: #f0f0f0;
}
            

You can also set background images:


body {
    background-image: url('path/to/image.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}
            

CSS Borders

Borders can be added to elements using the border property.


p {
    border: 1px solid black;
}
            

You can specify border width, style, and color.


p {
    border-width: 2px;
    border-style: dashed;
    border-color: blue;
}
            

CSS Margins

Margins are used to create space around elements, outside of any defined borders.


p {
    margin: 20px;
}
            

Margins can be set for each side individually:


p {
    margin-top: 10px;
    margin-right: 15px;
    margin-bottom: 20px;
    margin-left: 25px;
}
            

CSS Paddings

Paddings are used to create space around an element's content, inside of any defined borders.


p {
    padding: 20px;
}
            

Paddings can also be set for each side individually:


p {
    padding-top: 10px;
    padding-right: 15px;
    padding-bottom: 20px;
    padding-left: 25px;
}
            

Put It Together

Let's combine what we've learned about colors, backgrounds, borders, margins, and paddings into a single example.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Example</title>
    <style>
        body {
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            margin: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }

        h1 {
            color: #007bff;
            text-align: center;
        }

        p {
            color: #333;
            border: 1px solid #007bff;
            padding: 10px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Welcome to CSS Styling</h1>
        <p>This is a paragraph with a border, padding, and margin.</p>
        <p>Here's another paragraph with the same styles applied.</p>
    </div>
</body>
</html>

Result of this code

Summary

Today, we've covered:

  • CSS Colors: Learned how to set colors for text and other elements using the color property.
  • CSS Backgrounds: Learned how to set background colors and images using the background-color and background-image properties.
  • CSS Borders: Learned how to add borders to elements using the border property.
  • CSS Margins: Learned how to create space around elements using the margin property.
  • CSS Paddings: Learned how to create space around an element's content using the padding property.

Practice Exercise

Try creating a simple webpage that includes the following:

  • A paragraph with a border.
  • A div with a background image.
  • Two paragraphs with different margin and padding settings.
  • Use CSS to style your webpage with the following:
    • Set the background color of the body to light gray.
    • Set the text color of all paragraphs to blue.
    • Add a border to one of the paragraphs.

      Here is a sample project

      
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>3D Emoji Page - NiLBiTE</title>
          <style>
              body {
                  margin: 0;
                  height: 100vh;
                  display: flex;
                  flex-direction: column;
                  justify-content: space-between;
                  align-items: center;
                  background: linear-gradient(135deg, #c3ec52 10%, #0ba29d 100%);
                  overflow: hidden;
                  font-family: Arial, sans-serif;
              }
      
              .brand {
                  margin-top: 20px;
                  font-size: 2rem;
                  color: #ffffff;
                  text-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
              }
      
              .top-box {
                  display: flex;
                  justify-content: center;
                  align-items: center;
                  width: 90%;
                  height: 50%;
                  background: rgba(255, 255, 255, 0.2);
                  backdrop-filter: blur(10px);
                  border-radius: 15px;
                  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
                  position: relative;
                  overflow: hidden;
              }
      
              .center-emoji {
                  font-size: 6rem;
                  transition: transform 0.3s ease, opacity 0.3s ease;
                  opacity: 0;
                  pointer-events: none;
              }
      
              .center-emoji.show {
                  opacity: 1;
                  pointer-events: auto;
                  animation: pop 0.5s ease-out, float 3s ease-in-out infinite;
              }
      
              .bottom-box {
                  display: flex;
                  justify-content: center;
                  align-items: center;
                  width: 100%;
                  background: rgba(255, 255, 255, 0.2);
                  backdrop-filter: blur(10px);
                  padding: 10px 0;
                  box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.2);
              }
      
              .emoji-list {
                  display: flex;
                  gap: 15px;
              }
      
              .emoji {
                  font-size: 2rem;
                  padding: 10px;
                  border-radius: 15px;
                  background: rgba(255, 255, 255, 0.3);
                  backdrop-filter: blur(10px);
                  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
                  cursor: pointer;
                  transition: transform 0.3s ease, box-shadow 0.3s ease;
              }
      
              .emoji:hover {
                  transform: translateY(-5px) scale(1.1);
                  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
              }
      
              @keyframes pop {
                  0% { transform: scale(0); }
                  100% { transform: scale(1); }
              }
      
              @keyframes float {
                  0% { transform: translateY(0); }
                  50% { transform: translateY(-10px); }
                  100% { transform: translateY(0); }
              }
      
              @keyframes smile-widely {
                  0% { transform: scale(1); }
                  50% { transform: scale(1.2); }
                  100% { transform: scale(1); }
              }
          </style>
      </head>
      <body>
          <div class="brand">NiLBiTE</div>
          <div class="top-box">
              <div class="center-emoji" id="centerEmoji"></div>
          </div>
          <div class="bottom-box">
              <div class="emoji-list">
                  <div class="emoji" onclick="showEmoji('😀')">😀</div>
                  <div class="emoji" onclick="showEmoji('😂')">😂</div>
                  <div class="emoji" onclick="showEmoji('😍')">😍</div>
                  <div class="emoji" onclick="showEmoji('🤔')">🤔</div>
                  <div class="emoji" onclick="showEmoji('😎')">😎</div>
                  <div class="emoji" onclick="showEmoji('😢')">😢</div>
                  <div class="emoji" onclick="showEmoji('😡')">😡</div>
                  <div class="emoji" onclick="showEmoji('🥳')">🥳</div>
                  <div class="emoji" onclick="showEmoji('🤩')">🤩</div>
                  <div class="emoji" onclick="showEmoji('😴')">😴</div>
              </div>
          </div>
      
          <script>
              function showEmoji(emoji) {
                  const centerEmoji = document.getElementById('centerEmoji');
                  centerEmoji.innerHTML = emoji;
                  centerEmoji.classList.remove('show');
                  void centerEmoji.offsetWidth;  // trigger reflow
                  centerEmoji.classList.add('show');
                  if (emoji === '😀') {
                      centerEmoji.style.animation = 'pop 0.5s ease-out, float 3s ease-in-out infinite, smile-widely 1s ease-in-out';
                  } else {
                      centerEmoji.style.animation = 'pop 0.5s ease-out, float 3s ease-in-out infinite';
                  }
              }
          </script>
      </body>
      </html>
              

      Preview of this project

      copy this code and paste it on "Code Editor"