What is the best way to use CSS to align an image on a webpage?

I attempted to implement this but I couldn't figure out how to do it or what attribute to use. Can someone assist me with this? Below is my code:

<!DOCTYPE html>
<html lang="en">
<head>
<!--Bootstrap-->
<meta name="viewport" content="width=device-width, initial- scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--Declaration-->
<meta charset="utf-8">
<title>Home</title>
</head>
<body style="height: px"> 
<!--Content-->
<!--navbar-fixed-top (fix that)-->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
  <span class="icon-bar"></span>
<span class="icon-bar"></span>
  <span class="icon-bar"></span>
  </button>
<a class="navbar-brand" href="#">Directory 41</a>
  </div>
  <div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav">
    <li class="active"><a href="#">Home</a></li>
<!--first tab-->
    <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">Topics<span class="caret"></span></a>
    <ul class="dropdown-menu">
    <li><a href="#">Computer Building</a></li>
     <li><a href="#">Cryptocurrencies</a></li>
  <li><a href="#">Hacking</a><li>
    <li><a href="#">Programming</a></li>
    <li><a href="#">Troubleshooting</ a></li>
</ul>       
<!--second tab-->
  <li class="dropdown">
<a class="dropdown-toggle" data-    toggle="dropdown" href="#">Books<span class="caret"></ span></a>
<ul class="dropdown-menu">
  <li><a href="#">CWNA Guide to Wireless LANs</a></li>
    <li><a href="#">Learn CCNA</a></li>
</ul>
<!--Third Tab-->
  <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">Definitions<span class="caret"></span></a>
    <ul class="dropdown-menu">
    <li><a href="#">Computer Building</a></li>
    <li><a href="#">Cryptocurrencies</a></li>
    <li><a href="#">Hacking</a></li>
    <li><a href="#">Programming</a></li>
    <li><a href="#">Troubleshooting</a></li>
</ul>        
<!--Fourth Tab-->
  <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">About Us<span class="caret"></span>       
    <ul class="dropdown-menu">
    <li><a href="#">Our Story</a></li>
    <li><a href="#">Contact us</a></li>
    <li><a href="#">Survey</a></li>
 </ul>
 </ul>
   <ul class="nav navbar-nav navbar-right">
     <li><a href="#"><span class="glyphicon glyphicon-user"></span>Sign Up</a></li>
     <li><a href="#"><span class="glyphicon glyphicon-log-in"></span>Login</a></li>
     </ul>
 </div>
 </div>
 </nav>
 <!--End of Nav Bar-->
 <!--Beginning of Content-->
 <h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque gravida, lectus vitae gravida pharetra, arcu mi commodo orci, non tempus ex diam sit amet lorem. Nulla eros tortor, dictum nec lacus mattis, interdum pharetra leo. Phasellus lobortis nulla id ex tristique iaculis. Donec nec facilisis mi. Vestibulum at est elementum, laoreet eros sit amet, mollis dolor. Suspendisse sit amet varius mauris.</p>
<!--Gallery(Books)-->

<!--first gallery-->
<div class="gallery">
<a target="_blank" href="#">
<img src="image" alt="" height="200" width="150"></a>
<div class="desc">Description goes here</div></div>

<!--second gallery-->
<div class="gallery2">
<a target="_blank" href="#">
<img src="image" alt="#" height="200" width="150"></a>
<div class="desc">Description goes here</div></div>
</body>
</html>
<style type="text/css">
/*content*/
body {
  background-color: light gray;
}
/*p {
 padding: 10px;
 margin: 10px;
 background-color: black;
 color: yellow;
}*/

/*gallery*/
.gallery {
  margin: 5px;
  border: 1px solid #ccc;
  float: left;
  width: 170px;
}
.gallery:hover {
  border: 1px solid #777;
}
.gallery img {
  width: 100%;
  height: auto;
}
.desc {
  padding: 15px;
  text-align: center;
}
/*gallery 2*/
.gallery2 {
  margin: 5px;
  border: 1px solid #ccc;
  float: right;
  width: 180px;
}
</style>

Apologies for omitting some code. If anyone can assist with fixing that, I would greatly appreciate it. Thank you for any help.

Also, I did not include the image on img src due to technical limitations.

Answer №1

Thank you for taking the time to read this

<!DOCTYPE html>
<html lang="en"
<head>
<!--Bootstrap-->
<meta name="viewport" content="width=device-width, initial- scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--Decleration-->
<meta charset="utf-8">
<title>Home</title>

<style type="text/css">
  /*content*/
  body {
    background-color: light gray;
  }
  /*p {
   padding: 10px;
   margin: 10px;
   background-color: black;
   color: yellow;
  }*/

  /*gallery*/
  .gallery {
    margin: 5px;
    border: 1px solid #ccc;
    float: left;
    width: 170px;
  }
  .gallery:hover {
    border: 1px solid #777;
  }
  .gallery img {
    width: 100%;
    height: auto;
  }
  .desc {
    padding: 15px;
    text-align: center;
  }
  /*gallery 2*/
  .gallery2 {
    margin: 5px;
    border: 1px solid #ccc;
    float: right;
    width: 180px;
  }
  </style>
</head>
<body style="height: 100%"> 
<!--Content-->
<!--navbar-fixed-top (fix that)-->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
  <span class="icon-bar"></span>
<span class="icon-bar"></span>
  <span class="icon-bar"></span>
  </button>
<a class="navbar-brand" href="#">Directory 41</a>
  </div>
  <div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav">
    <li class="active"><a href="#">Home</a></li>
<!--first tab-->
    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#">Topics<span class="caret"></span></a>
      <ul class="dropdown-menu">
          <li><a href="#">Computer Building</a></li>
           <li><a href="#">Cryptocurrencies</a></li>
        <li><a href="#">Hacking</a><li>
          <li><a href="#">Programming</a></li>
          <li><a href="#">Troubleshooting</a></li>
      </ul> 
    </li>


<!--second tab-->
  <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Books<span class="caret"></span></a>
    <ul class="dropdown-menu">
        <li><a href="#">CWNA Guide to Wireless LANs</a></li>
          <li><a href="#">Learn CCNA</a></li>
      </ul>
    </li>


<!--Third Tab-->
  <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Definitions<span class="caret"></span></a>    <ul class="dropdown-menu">
      <li><a href="#">Computer Building</a></li>
      <li><a href="#">Cryptocurrencies</a></li>
      <li><a href="#">Hacking</a></li>
      <li><a href="#">Programming</a></li>
      <li><a href="#">Troubleshooting</a></li>
  </ul>   </li>


<!--Fourth Tab-->
  <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">About Us<span class="caret"></span></a>    <ul class="dropdown-menu">
      <li><a href="#">Our Story</a></li>
      <li><a href="#">Contact us</a></li>
      <li><a href="#">Survey</a></li>
   </ul></li>


 </ul>
   <ul class="nav navbar-nav navbar-right">
     <li><a href="#"><span class="glyphicon glyphicon-user"></span>Sign Up</a></li>
     <li><a href="#"><span class="glyphicon glyphicon-log-in"></span>Login</a></li>
     </ul>
 </div>
 </div>
 </nav>
 <!--End of Nav Bar-->
 <!--Beggining of Content-->
 <h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque gravida, lectus vitae gravida pharetra, arcu mi commodo orci, non tempus ex diam sit amet lorem. Nulla eros tortor, dictum nec lacus mattis, interdum pharetra leo. Phasellus lobortis nulla id ex tristique iaculis. Donec nec facilisis mi. Vestibulum at est elementum, laoreet eros sit amet, mollis dolor. Suspendisse sit amet varius mauris.</p>
<!--Gallery(Books)-->

<!--first gallery-->
<div class="gallery">
<a target="_blank" href="#">
<img src="image" alt="" height="200" width="150"></a>
<div class="desc">Desc goes here</div></div>

<!--second gallery-->
<div class="gallery2">
<a target="_blank" href="#">
<img src="image" alt="#" height="200" width="150"></a>
<div class="desc">Desc goes here</div></div>
</body>
</html>

Answer №2

This solution should help you achieve your desired outcome. However, I am uncertain about how the behavior will be impacted by nesting the image within the anchor tag. For more information, you can refer to the bootstrap documentation on image topped cards at: https://getbootstrap.com/docs/4.1/components/card/#images

<div class="gallery card">
    <a target="_blank" href="#">
        <img src="image" alt="" height="200" width="150" class="card-img-top">
    </a>
    <div class="desc card-body">
        <p class="card-text">Description goes here</p>
    </div>
</div>

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

How can you use JavaScript/jQuery to scroll to the top of a DIV?

When a link is clicked, I have a <div> popup that appears. This popup contains scrollable content when there is overflow. If the same link is clicked again, I want the <div> to be positioned at the top of the scrollable area. While this functi ...

Issues with pop-up windows on YII2 gridview

I am currently developing a web application using the Yii2 framework for my company to efficiently manage their storage. I have incorporated various plugins from kartik-v to enhance the functionality of the application. However, I am facing an issue with i ...

The resizing issue persists with Angularjs charts

I have recently developed a small web application using AngularJS and I have implemented two charts from the AngularJS library - a bar chart and a pie chart. Although both charts are rendering correctly, they are not resizing properly as the display size c ...

Bootstrap 4 input fields extend past padding boundaries

I have created a basic form using the following code: <div class="container"> <div class="jumbotron" style="width:100%"> <h1> ConfigTool Web Interface </h1> <p> Edit the con ...

The value of the variable is failing to be included in the Ajax request being sent to the server via jQuery

I'm encountering an issue with my contact form where I can't seem to retrieve the $_POST values via ajax for saving to a text file. It seems like there might be a problem with my javascript code. Check out the jQuery code snippet below: functio ...

I seem to be having an issue with the decimal point on my calculator - I only want to see one dot

Only need one dot for the calculator to function properly. The current situation with multiple dots is causing confusion. I intend to address other aspects of the code later, but I'm currently struggling with this issue. function dec(d) { let ...

Finding your site's First Contentful Paint (FCP) can be done by analyzing the

After doing some research on Google insights, I came across information about FCP. However, I'm still unsure about how to determine my site's FCP and other relevant metrics. If anyone could provide me with more information or share detailed link ...

Filtering with checkboxes (looking for help with logic functionality)

Yesterday, I sought assistance with a similar question and was able to make progress based on the response provided. However, I have encountered another issue that has left me stuck. Current behavior: Clicking on a checkbox changes the background color of ...

Asynchronous JavaScript function within a loop fails to refresh the document object model (DOM) despite

I have been working on a function that utilizes ajax to retrieve instructions from a backend server while the page is loading. The ajax code I've written retrieves the instructions based on the number provided and displays them using the response.setT ...

I'm having trouble figuring out the code for the path in the POST request for my basic login form application

I have built a basic login form application in node.js. Although I can successfully display the login form and input login data, my POST request to capture the login data is not functioning correctly. I am encountering a POST http://localhost:3000/client 4 ...

Displaying complex JSON data in an HTML format using JavaScript

How can I convert the second array of entities into an HTML format from the JSON data using a for loop or similar method? To access the necessary data, I currently use console.log(data.response[0].entities.urls); $(document).ready(function () { $.getJSO ...

"An issue was found on line 75, with column 16 reporting an error stating: AttValue expected either a double or single quote

I can't seem to figure out what's wrong with the first line: <a xlink:href=“#dccomics” xlink:title=“DC Comics”> <rect x="1023.509" y="-612.968" fill="none" stroke="#000000" stroke-width="0.5" stroke-miterlimit="10" width ...

Managing text size in HTML email designs

How can I maintain consistent font sizes in HTML EMAILS? The fonts look great on the website, but in my email they appear small and close together. Check out Live Demo I've attached a screenshot of how the email appears to me. Any suggestions on h ...

Any tips for avoiding a new line when generating html attribute values to prevent my json string from breaking?

When working with JSON string values in button element attributes, I have encountered an issue where a single quote causes the value to break and create newlines. For example: var $json = JSON.stringify('{"Long_text":"This is \'my json stri ...

Unable to retrieve data with Selenium web driver

I have been attempting to extract the product description from a webpage using ID and tagname. However, when I try to print it out, the result is coming up empty. I am certain that I have used the correct locators to find the element. Page Source < ...

When setting an attribute on load, Javascript may not properly apply the change to the attribute

I designed a webpage where images have a fade-in effect using a CSS class. Initially, 4 images load with the desired effect. However, I encounter an issue when trying to apply the same effect to a new image that appears upon clicking a button. Below is th ...

Continual dark mode throughout page navigation with the ability to switch between light and dark modes

I've been experimenting with creating a persistent dark mode feature for web pages. My goal is to remember the user's preference as they navigate between pages. Additionally, I included a toggle button for switching between dark and light modes t ...

The content at the center of the page is shifting to the right side when the browser is resized

I am currently working on creating a random quote generator at the following link: http://codepen.io/Kestvir/pen/peqjZZ. During the html/css aspect of the project, I have run into an issue with the content inside a transparent background. Whenever I resi ...

Grid of domes, fluid contained within fixed structures and beyond

I've been grappling with this issue for a while now, and have had to rely on jQuery workarounds. I'm curious if there is a way to achieve this using CSS or LESS (possibly with javascript mixins). The page in question consists of both fixed and f ...

"Tricky HTML table layout challenge: Margins, paddings, and borders causing

Working with HTML <table cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <img src="..."> </td> </tr> </tbody> </table> Applying CSS rules * { border: none; ...