Need help in styling a tag

Viewed 44

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Technology-BBC News
  </title>
  <style>
    body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    #topbar {
      width: 1265px;
      margin: 0 auto;
      height: 40px;
      background-color: #FFFFFF;
    }
    
    #logo {
      margin-top: 8px;
      width: 85px;
      float: left;
      margin-right: 15px;
    }
    
    .topbar-section {
      float: left;
      border-left: 1px solid #CCCCCC;
      height: 100%;
    }
    
    #signin-img {
      width: 22px;
      margin-top: 11px;
      margin-left: 11px;
      float: left;
    }
    
    #signin-text {
      /* margin-top: 100px; */
      position: relative;
      top: 10px;
      left: 8px;
      font-weight: 600;
      font-size: 13px;
      padding-right: 50px;
    }
  </style>
</head>

<body>
  <div id="topbar">
    <img id="logo" src="images/BBC_logo.png" alt="logo">
    <div id="singin" class="topbar-section">
      <img id="signin-img" src="images/signin.png" alt="">
      <span id="signin-text"> Sign in</span>
    </div>
  </div>
  -
</body>

</html>

here I am trying to give margin to #signin-text but it's not working ... I used positioning that worked but margin-top didn't can someone please tell me why the margin-top property in #signin-text not working this is my first question of stackoverflow please forgive if the question is asked in a wrong way. :)

3 Answers

Since you're using css property top with margin-top it will not work.top always takes precedence over margin-top when using with position property. Check https://www.w3schools.com/Css/css_positioning.asp for reference. Try using top with a value 100px

span is an inline element. That's why it can't accept margin. If you change this (change its block level) by giving display: inline-block for example, the margin will affect.

span {
  margin-top: 15px;
}

.inline-block {
  display: inline-block;
}
<div>
  <span>natural</span>
</div>
<div>
  <span class="inline-block">inline-block</span>
</div>

spans are the inline elements that take margins horizontally only not vertically, Only the block and inline-blocks can take margins as vertically and horizontally.

Margin properties specify the width of the margin area of a box. The 'margin' shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements.

there are many solutions to your problem, either use div or specify span tag as inline-block in your style tag. Still, there are many other ways to achieve this

Related