How to centrally align an image with HTML tags <p></p>

https://i.sstatic.net/iTBOI.jpg

I need help with HTML alignment for my website

How can I center align the text with the image?

#main__img {
    text-align: center;
    height: 80%;
    width: 80%;
}

.main__content p {
    margin-top: 1rem;
    font-size: 1rem;
    text-align: justify;
    font-weight: 400;
}
<img id="#main__img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRb3FlgJlyfSfAyDxdt6bGxrG4fOxz2sIEOog&usqp=CAU" />

<div class="main__content">
<p> Visualizing Abolition maps the suppression of the African slave trade by tracing nearly 31,000 records of correspondence exchanged between the British Foreign Office and British commissioners, ministers, naval officers, and representatives of foreign governments around the world over the course of the nineteenth century. It provides users with three resources. First, a database that lists the names of the senders, recipients, places of origin and destination, dates, as well as the subject of the letters when available. Second, essays exploring different topics related to the suppression of the traffic. Finally, a gallery of images that provides visual context for the information available on the website. These resources allow students and researchers to further understand the history of the suppression of the African slave trade and expand our knowledge of the largest coerced migration in history. </p>
</div>

Answer №1

The ideal HTML tool for this task is the figure element. It consists of a display element (usually an image but can be anything) and a figcaption, typically the first or last child of the figure element, containing text related to the image.

Figures are useful for grouping visual elements with descriptions or accompanying text that can all be styled together. To achieve the desired styling, simply apply a text-align: center and margin: 0 auto to center the figure on the page.

figure  {
    width: 80%;
    margin: 0 auto;
    text-align: center
}


figcaption {
    margin-top: 1rem;
    font-size: 1rem;
    text-align: justify;
    font-weight: 400;
}
<figure>
  <img id="#main__img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRb3FlgJlyfSfAyDxdt6bGxrG4fOxz2sIEOog&usqp=CAU" />
  <figcaption>
    <p> Visualizing Abolition maps the suppression of the African slave trade by tracing nearly 31,000 records of correspondence exchanged between the British Foreign Office and British commissioners, ministers, naval officers, and representatives of foreign governments around the world over the course of the nineteenth century. It provides users with three resources. First, a database that lists the names of the senders, recipients, places of origin and destination, dates, as well as the subject of the letters when available. Second, essays exploring different topics related to the suppression of the traffic. Finally, a gallery of images that provides visual context for the information available on the website. These resources allow students and researchers to further understand the history of the suppression of the African slave trade and expand our knowledge of the largest coerced migration in history. </p>
  </figcaption>
<figure>

Answer №2

Make sure to include the following CSS code snippet:


.main__content {
    width: 100%;
}
.main__content p {
    margin-top: 1rem;
    font-size: 1rem;
    text-align: justify;
    font-weight: 400;
    width: 80%;
    margin: 0 auto;
}

This will ensure that the paragraph width matches the image and equal margins are set on both sides.

Alternatively, you can place both the image and paragraph in the same container to minimize style issues:

<div class="container">
  <img ...>
  <p>asdfsdfsdf</p>
</div>

Then apply the following styles:

div.container {
  width: 80%;
  margin: 0 auto;
}
div.container img {
  width: 100%;
}
div.container p {
  width: 100%;
}

Answer №3

Ensure both containers have equal width and apply margin-left/right = "auto".

#main__img {
    height: auto;
    width: 80%;
    display: block;

    margin-left:auto;
    margin-right: auto;
}

.main__content p {
    margin-top: 1rem;
    font-size: 1rem;
    text-align: justify;
    font-weight: 400;

    width: 80%;
    margin-left:auto;
    margin-right: auto;
}
<div class="main__content">
<img id="main__img" src="https://upload.wikimedia.org/wikipedia/commons/0/08/Astronotus_ocellatus.jpg" />

<p> Visualizing Abolition maps the suppression of the African slave trade by tracing nearly 31,000 records of correspondence exchanged between the British Foreign Office and British commissioners, ministers, naval officers, and representatives of foreign governments around the world over the course of the nineteenth century. It provides users with three resources. First, a database that lists the names of the senders, recipients, places of origin and destination, dates, as well as the subject of the letters when available. Second, essays exploring different topics related to the suppression of the traffic. Finally, a gallery of images that provides visual context for the information available on the website. These resources allow students and researchers to further understand the history of the suppression of the African slave trade and expand our knowledge of the largest coerced migration in history. </p>
</div>

Answer №4

Give this a shot

.main__content {
   padding: 15px;
}

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

The PHP file is encountering issues with running the script

My code isn't functioning as intended and is supposed to dynamically add fields related to guest information. The issue lies with the Add more button not working when clicked. This form is designed for guests to fill out using MySQL, PHP, JavaScript ...

Issue encountered when attempting to update a specific element within an array using Mongoose

Looking to update a specific object within an array. { "_id": "543e2f8e769ac100008777d0", "createdDate": "2014-10-15 01:25 am", "cancle": false, "eventDateAndTime": "2014-02-12 12:55 am", "eventstatus": true, "userPhone": "44444444 ...

I need to print out a table, but when I preview the print, I want to rotate the entire page for better visibility

Is there a way to rotate the print review using CSS and HTML? I am facing issues as the CSS styles do not seem to apply properly on it. Can someone help me solve this problem? ...

The `.babelrc` file is unable to locate presets within a nested node_modules directory

My file structure is organized as follows: > build > node_modules > webpack.config.js > .babelrc > .gitignore In my .babelrc file, the configuration appears like this: { "presets": ["es2015", "stage-0", "react"] } However, I ...

Calculator app with Next.js: Using keyboard input resets the current number state in real-time

While developing a basic calculator application with Next.js, I encountered an issue. The functionality works correctly when the user interacts with the HTML buttons, but when trying to input numbers using the keyboard, the state resets to 0 before display ...

The onclick button in React is not triggering

I am currently working on a component that processes card clicks and redirects users to a previous route, however, the OnClick function does not seem to be functioning properly. I'm trying to pinpoint where I might be going wrong. function Character ...

Issues with missing data in Selenium Python

Hi there! I'm attempting to scrape data from this webpage in order to retrieve all the values displayed on the table. So far, I've been using selenium for this task, but it seems to only fetch the first 6 values while the rest remain hidden someh ...

New to jQuery: struggling with click event and CSS... What am I missing here?

I have a very simple question for you: $j(".srch-txt").click(function() { $j(this).css("color:" "#155D97") }); $j is used to avoid conflicts It's quite straightforward: I want to change the color of the .srch-txt element to #155D97 when it is click ...

What is the importance of using $scope.$apply after assigning values to ng-model within forms?

Let's talk about a form: <form name="placeThis" id="placeThis" novalidate ng-submit="submitForm();"> <input type="hidden" ng-model="placeThis.target"/> </form> My goal is to assign a default value to placeThis.target from my co ...

Remove unnecessary margins from grid layout

I'm looking to create a grid display using divs with inline-block display and margin-right. However, I'm facing an issue where the last item on each row also has a redundant margin-right. Here's a snippet of my CSS: .item{ height:20px; ...

The function Router.use is looking for a middleware function, but instead received an object in node.js /

I encountered an issue while trying to setup routing in my application. Whenever I attempt to initialize a route using app.use() from my routes directory, I receive an error stating that Router.use() requires a middleware function but received an Object in ...

Encountering obstacles as a novice trying to master CSS styling techniques

I'm having trouble aligning buttons at the center top of the screen. Despite my efforts, I can't seem to get them perfectly aligned https://i.stack.imgur.com/j7Bea.png button { width: 13%; height: 50px; display: inline-block; fo ...

Tips for converting Numbers or JSON to strings while exporting data to an Excel spreadsheet in React

I have been using react-html-table-to-excel to export my table to Excel. However, I have encountered a specific issue. I am unable to convert a Number to a string in one column. When I try to cast the Number to a string using <td>{String(post.did)} ...

There is an issue with my HTML due to a MIME type error

I am currently facing an issue with my project. I have developed a node js server and now I want to display an HTML file that includes a Vue script loading data using another method written in a separate JS file. However, when I attempt to load the HTML f ...

Integrating Twitter bootstrap into Django version 1.5

I have been attempting to incorporate Twitter bootstrap into my django application. Within settings.py, I have set up the following: STATIC_URL = '/static/' # Additional locations of static files STATICFILES_DIRS = ( # Add paths for static ...

Encountering a server issue while incorporating ejs in expressjs

I am a beginner with nodejs and I am experimenting with using ejs to template my website. However, every time I try to set the view engine, I encounter the error 500 - Internal Server Error. Here is my app.js: var express = require('express'); v ...

Tips for dynamically resizing an image to suit any screen size, such as with Bootstrap 4

I am having trouble centering an image on the screen and making sure it fits properly without requiring the user to scroll (on screens larger than a tablet). Here is my desired outcome: https://i.sstatic.net/LbfV8.png The top row shows the expected resu ...

Encountered an error: [$injector:modulerr] while using AngularJS

I need help creating a registration form using AngularJS. I am encountering an error in my app.js file: (function (){ 'use strict'; angular.module('modulemain',[]).controller('ModuleController',ModuleController); ...

What is the best way to display a user's profile picture on my website by pulling it from the database?

I'm currently using this code for my user page, but it only displays their names from the database. userlist.php <?php include "header.php"; include "footer.php"; include "db_conn.php"; ?> <!DOCTYPE html& ...

Stopping a Javascript function from running when an asp.net webform is loaded

Here is the code I wrote to display my position on the map: <script src="http://maps.googleapis.com/maps/api/js"></script> <script> function initialize(x, y) { alert(x); alert(y); var mapProp ...