Why is margin-top behaving unexpectedly?

Greetings! I have a website created using css, html, and php. Take a moment to review this code:

    <?php
    session_start();

    include_once "mysql_connect.php";
     $log = null;
    if(isset($_SESSION["ID"])){
     $log = $_SESSION["ID"];
    }else{
     $log=null;
    }
      $_SESSION["prevpage"] = "home";
      $terms = $_POST["search"];
      if($terms == null){
            header("location:home");
      }
    ?>
    <html>
    <head>
     <title><?php echo $terms;?></title>

    <link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />


     <link rel="stylesheet" href="css/reset.css">
     <link rel="stylesheet" href="css/text.css">
     <link rel="stylesheet" href="css/960_24_col.css">
     <link rel="stylesheet" href="css/style.css">
     <META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
     <meta name="description" CONTENT="Find everything and anything on Omicrome">
     <META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
    </head>
         <body>
           <div class = "container_24">
           <header>
           <div id = "rectban">
           <h1>Omicrome</h1>
           <nav>
           <ul>
           <li><a href="home">Home</a></li>
           <li><a href="articles">Articles</a></li>
           <li><a href="about">About</a></li>
           <?php if($log != null){?>
           <li id = "myprofilebanner"><a href="MyProfile">My Profile</a></li>
           <?php }else{ ?>
           <li id = "myprofilebanner"><a href="createAccount">Create Account</a>        </li>
           <?php } ?>


           <li id = "loginbanner"><a href= <?php if($log != null){?>"logout.php"<?php }else{?>"login"<?php  }?>><?php if($log != null){?>Log Out<?php  }else{?>Log In <?php  }?></a></li>

         </ul>
         </nav>
        </div>
         <div id = "Postban">
          <form action="searchresults.php" method="post" enctype="multipart/form-data">
            <input class="searchbar"  type="text" name="search" size="30" maxlength = "500" placeholder="Search"/>
            <input type="submit" class = "searchbtn" value="[&nbsp;&nbsp;Go&nbsp;&nbsp;]" />
          <a class="Post" href="post" >
          [&nbsp;&nbsp;Post&nbsp;&nbsp;] 
           </a>
          </form>    
       </div>            
    </header>





      <div class = "main clearfix">
      <div id = "rectsearchresults">

       <?php 


               $fetchlast = mysql_query("SELECT * FROM posts WHERE id=(SELECT MAX(id) FROM posts)");
               $lastrow = mysql_fetch_row($fetchlast);
               $lastid = $lastrow[6];

               for ($i=1; $i <= $lastid; $i++) { 

                   $currentname = mysql_query("SELECT * FROM posts WHERE id=$i");

                   while ($row = mysql_fetch_array($currentname)) {
                       $title = $row[0];
                       $desc = $row[1];
                       $ID = $row[6];

                       $title2 = rtrim($title);
                       $donetitle = str_replace(" ", "-", $title2);
                       $url = "articles/".$ID."/".$donetitle."";

                       echo "<div id=\"result\"><img src=\"img/tempsmall.png\" alt = \"icon\" > 
                               <a id=\"resultheader\" href=\"$url\">$title</a><br>
                             </div>";

                   }

               }
       ?>

   </div>
   </div>







   </div>
   </div>

   </body>

   </html>

The code above is all on one page and is meant to display search results. I am trying to adjust the positioning of the <a> tag with the id 'resultheader'. While margin left works fine, I am having trouble with margin top. I attempted using display:inline-block as suggested by online resources, but it's not working. Can someone please help me understand why? Here is the CSS code I have:

#rectsearchresults{
    position: relative;
    margin-top: -10px;
    margin-left: -0px;
    background: #ffffff;
    padding-left: 20px;
    padding-right: 20px;
    padding-bottom: 20px;
    padding-top: 20px;
    width:916px;

}
#result{
    margin-bottom: 20px;
}
#resultheader{
    margin-left:10px;
    color:black;
    font-size: 15px;
    margin-top: -20px;
}

Here is the current output I am getting. I would like the header of each result to align with the top of the image.

https://i.sstatic.net/rDCS5.png

Answer №1

<a> (anchor tags) typically have a default display:inline style rule,

'inline' element types do not support negative top margins

There are various solutions to this issue:

1) Implement display: table. This provides the content-fitting properties of inline-block along with support for negative margins.

#resultheader {
    margin-left: 60px; //adjust as needed
    color: black;
    font-size: 15px;
    margin-top: -30px; //adjust as needed
    display: table;
}

2) Utilize the vertical-align property

#resultheader {
    margin-left: 10px;
    color: black;
    font-size: 15px;
    vertical-align: 10px; //adjust as needed
}

3) Apply position: relative & top properties

#resultheader{
    margin-left:10px;
    color:black;
    font-size: 15px;
    position: relative;
    top: -6px;
}

Choose the method that works best for your situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting issues with document.querySelectorAll in JavaScript

Can someone explain why document.querySelectorAll isn't functioning properly in this code snippet? I'm a beginner and could use some help. <p id="demo">This is a paragraph.</p> <h1 id="1demo"> Hello </h1&g ...

Issue with transition on the second attempt

In this code snippet, I have managed to achieve my desired outcome but it seems to only work on the first run. I am not sure why this is happening, so if anyone can help me out that would be greatly appreciated. Here is a demo of the code in action: HTML ...

Responsive design is causing issues with the stacking of rows in Bootstrap

Currently in the process of creating a website using HTML5, CSS3, and Bootstrap technologies. Encountering difficulties while attempting to establish a stacked row-column layout. On desktop view, the design appears as desired, resembling this example: ht ...

Automatic Full-Screen Switching Upon Video Playback in HTML5

Currently, I have a video clip set to play when the timer reaches a certain point. My goal is for the video to automatically enter full-screen mode when it starts playing and return to its original position when it stops, as the timer will continue ticking ...

Creating a Navigation Bar with a Dropdown Menu in HTML

Apologies if this question has been asked before, but I've searched everywhere and can't find an answer. All my searches just bring up dropdown menus with <nav></nav> Consider this table (serving as a navigation bar): <table cl ...

Tips for retrieving the count from HTML using JavaScript:

I need to determine the count of list items in an unordered list within dir-pagination-controls. How can I achieve this using JavaScript? <dir-pagination-controls min-size="1" direction-links="true" boundary-links="true" class="pull-right ng-isolate- ...

Troubleshooting display problems with the categories menu in Opencart version 1.5.1 caused by IE

For our opencart website, we utilize the li element for the sub categories items. displays properly on all modern browsers. However, we need to ensure compatibility with Internet Explorer 6 as well. ...

The way images appear can vary between desktop and mobile devices

I am in the process of creating a photography website with a simple goal of displaying images down the page one after another. However, I am encountering issues with uniform display across various desktop and mobile platforms. The site appears perfect on i ...

Transforming an interactive HTML webpage into React/JSX

Imagine a scenario where I have two files: example.html <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="u ...

Chrome does not support top.frames functionality

I have three HTML pages: main.html, page1.html, and page2.html. I am displaying page1.html and page2.html within main.html using the code below. <!DOCTYPE html> <html> <frameset frameborder="1" rows="50%, *"> <frame name="f ...

Ensuring Data Accuracy in Angular 2 Forms

Currently, I am working on form validation using Angular 2 and encountered an issue with the error message Cannot read property 'valid' of undefined. The HTML file I am working on contains a form structure like this: <form id="commentform" c ...

Ways to retrieve a variable from a separate TypeScript document

A scenario arises where a TypeScript script contains a variable called enlightenFilters$: import { Component, Input, OnInit } from "@angular/core"; import { ConfigType, LisaConfig } from "app/enrichment/models/lisa/configuration.model"; ...

Utilize express middleware for applying CSS styles in web development

How's it going? I've been experimenting with middleware in Express JS, like this: app.get ('/user/:school/:dual', function (req, res, next) {   ......   ...   ...... }); Everything seems to be working fine so far. But when I na ...

Issue: Node.js Express unable to access static files when the route is nested more than one folder level deep.Resolution

Before we delve into the question, let me share with you the folder structure of my node.js express app: /home server.js index.html /app /routes public.js /public /css main.css In my server.js file, ...

Does the Sass default compiler have built-in autoprefixing capabilities?

Suppose I have the following sample code: :fullscreen a { display: flex; } Is it possible for the default sass compiler to transform it into this: :-webkit-full-screen a { display: -webkit-box; display: flex; } :-moz-full-screen a { display: fle ...

I am looking for an HTML solution that allows me to neatly stack photos of different sizes in two columns, always prioritizing the column with more empty space

Is there an easy method to populate two columns, each with a width of 50%, with photos in a way that the next photo is always added to the column with more available space? The photos come in different sizes but should be able to fit within the width of t ...

Using Vue to alter data through mutations

Greetings! I am currently in the process of developing a website for storing recipes, but as this is my first project, I am facing a challenge with modifying user input data. My goal is to create a system where each new recipe added by a user generates a u ...

The continual appearance of "No file found" persists when utilizing the $redirectRoute

My goal is to make one of the links on my website lead to another page within the site. Below is one of the two links found on my index.html page: <html ng-app="myApp"> . . . <body> <h1>My Home Page</h1> ...

A PHP function with multiple parameters

I am currently in the process of updating a row on Parse using PHP and have created the following function: if (isset($_GET['updateHistory'])) { updateHistory($_GET['updateHistory']); } if (isset($_GET['ye ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...