What is the best way to position an image in the center with text aligned to the left or right below it?

Having trouble aligning an image and text beneath it? I'm able to center both the image and text, but things get messy when I try to move the text left or right. The text ends up overflowing outside of the image boundary and I can't figure out how to keep it contained.

Here's my current HTML setup:

<p align="center"><img src="/someimage.jpg" height="300" width="600">
    <b>text under it</b></p>

I've tried wrapping the text in a span and floating it left or right, but that just causes it to extend past the image. Any suggestions on how to properly contain the text within the image border?

Answer №1

Is this what you had in mind?

I have applied the style display: flex to the wrapper and then used flex-direction: column to ensure that they are arranged in a column layout rather than a row.

You can adjust the text-align property on the b element to your preference (left, center, or right).

p {
  display: flex;
  flex-direction: column;
  width: 600px;
  margin: auto;
}

img {
  width: 600px;
  height: 150px;
}

b {
  text-align: left;
}
<p align="center"><img src="http://via.placeholder.com/600x150">
  <b>text under it</b></p>

Answer №3

By placing both the image and text inside a parent div with the same width as the image itself, you can easily align the text using the text-align property. Whether you want to center, left-align, or right-align the text, simply apply the corresponding value.

For a visual reference, check out this example on JSFiddle: https://jsfiddle.net/e653vfdk/2/

/* Here is the HTML */

<div class="container">
  <div class="holder">
    <img src="https://www.bmw.com.mt/content/dam/bmw/common/all-models/4-series/gran-coupe/2017/images-and-videos/images/BMW-4-series-gran-coupe-images-and-videos-1920x1200-12.jpg.asset.1519121502869.jpg" class="img1">
    <div class="text">Lorem Ipsum is simply dummy text of the printing </div>
  </div>
</div>

/* And here is the CSS */

.container
{
  border: 1px solid;
  padding: 20px;
}

.img1
{
  width: 500px;
  height: 500px;
}

.holder
{
  width: 500px;
  margin: 0 auto;
}

.text {
   /* Uncomment any of the below 3 lines to see the text left-aligned, right-aligned, or center-aligned based on your preference */
   text-align: left;
  /* text-align: right; */
  /* text-align: center; */
}

Answer №4

Here is my approach to solving this problem:

#wrapper {
  position: relative;
  display: block;
  width: 800px;
  margin: 0 auto;
}

#picture {
  position: relative;
  display: block;
  width: 800px;
  height: 400px;
  background-image: url("https://www.example.com/image.jpg");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

#content {
  position: relative;
  display: block;
  font-size: 1.5em;
  color: blue;
}

<div id="wrapper">

<div id="picture"></div>
   <div id="content">
   Some sample text goes here 
   Some sample text goes here
   Some sample text goes here 
   Some sample text goes here
</div>

https://jsfiddle.net/abc123xyz/2/

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

Transparent static navbar displayed above header image

Scenario I managed to develop a transparent navbar that is superimposed on top of a header image with an SVG curve attached to it. To create the transparent navbar, I modified some classes associated with it by removing bg-light. By adding the class fix ...

Guide to smoothly animate dragging a component in React

I am having trouble dragging a component with CSS transform animation applied to it. The drag functionality doesn't seem to work properly as the component only moves based on the animation and cannot be dragged. Is there any solution to make the drag ...

Generating dynamic paragraph numbers with HTML and JavaScript

I am currently working on a list where elements can be dynamically added using JavaScript with the following code: function getMovementButtons(size, articleId, articleTitle, articleType){ var lowerArticleType = articleType.toLowerCase(); var html = ...

What happens when data is managed in getStaticProps and utilized in useEffect within a component?

On my page, I utilize the getStaticProps function to fetch a list of objects containing book titles, authors, and character lists. Each object is then displayed within a component that formats them into attractive pill-shaped elements. The component rende ...

Resizing an HTML table causes the background to break

I have incorporated a tailwind table with a max-width setting. In my html, I am utilizing the default Laravel Jetstream layout. However, I am facing an issue wherein the background doesn't repeat in the browser when trying to use the scrollbar on sma ...

Steps for deactivating a button based on the list's size

I am trying to implement a feature where the user can select only one tag. Once the user has added a tag to the list, I want the button to be disabled. My approach was to disable the button if the length of the list is greater than 0, but it doesn't s ...

Tips for adding a border to a 3D pie chart in Highcharts

I created a 3D pie chart using the Highchart plugin and wanted to add borders to each portion. I tried adding the following code: borderWidth: 4, borderColor: "red" within the plotOptions: pie: section. However, I noticed that the portions were getting b ...

[Vue alert]: "Maximum" property or method is not declared in the instance but is being referenced during the rendering process

Here is my custom Vue component: Vue.component("product-list", { props: ["products", "maximum-price"], template: ` <div> <div class="row d-flex mb-3 align-items-center p-3 rounded-3 animate__animate ...

Images and CSS are functioning properly when viewed locally, but are not displaying as expected on GitHub pages

There seems to be some issues with transferring my website from a custom domain on Hover. When I try to import it to anthematics.github.io, only the HTML loads while images and CSS are missing. <link rel="stylesheet" href="theme.css" type="text/css"> ...

The information I input into this form is failing to be stored in the WAMP database

After implementing this PHP code, there are no error messages being displayed when I submit the HTML form. However, the data is not being added to the WAMP table. <?php error_reporting(E_ALL); ini_set('display_errors', 1); //$user_name = "roo ...

I'm having trouble figuring out the issue with the HTML code I wrote to include a favicon on my website

Take a look at this code: <link rel = "shortcut icon" type = "image/x-icon" href = "C:/Users/SOMEBODY/Desktop/SOME FOLDER/ANOTHER FOLDER/favicon.ico"/> Even though I have the favicon in the same folder as my website, ...

Issue with Sidebar Menu Title Display in AdminLte Interface

The AdminLTE theme sidebar menu title does not display properly when the title is long. Instead, it breaks the line and shows the text below the icon. I would like it to start below the first letter of the first line. I have attached a screenshot for refer ...

Adjusting the content of mat-cards to fill in blank spaces

I have encountered an issue with the alignment in a list using mat-card. Here is my current layout: https://i.stack.imgur.com/VKSw4.jpg Here is the desired layout: https://i.stack.imgur.com/8jsiX.jpg The problem arises when the size of content inside a ...

Ways to align the `<ol>` number at the top vertically

I'm having trouble aligning my <ol> numbers vertically at the top of the list. I've created a sample on CodePen. ol { list-style-type: decimal-leading-zero; font-size: 11px; } a { font-size: 80px; tex ...

What is the best way to enable a visible bootstrap tab to be clicked more than once?

Is there a way to override the default behavior on bootstrap tabs that prevents clicking on the currently shown tab? I need the user to be able to click on the tab again even if it is already active, as I am using AJAX to load content and reloading the pag ...

dropdown menu center

I'm currently working with a Bootstrap drop-down menu and I'm trying to figure out how to center the menu directly over the button. Currently, it's aligning to the left or right of the button depending on available space. Any help would be a ...

Guide on forwarding from one HTML page to another in the local disk

Currently, I am working on an employee appraisal application where users can submit appraisals that are stored in the backend. The initial page is a login screen. Once the user successfully logs in, they should be redirected to another page displaying in ...

Aligning bootstrap side by side fields poses a challenge when labels are missing

I am struggling to design a basic bootstrap layout as I am facing challenges in aligning controls that lack labels. The problem can be seen in the provided fiddle. The controls (textbox and button) in the third column are not aligned next to the Last name ...

Modify information within a form

I have successfully used a django form and template to insert data into the database. I can also view the data on a basic HTML page. However, I am struggling to understand how to edit the data. I assume that I can repurpose the same django template used fo ...

What is the method to close the picker when using type="datetime-local"?

Currently, I am utilizing Vue with the "datetime-local" type for input. While I can successfully select the date and time, my goal is to have the picker close automatically after a selection has been made. I've experimented with adding an onchange ev ...