How can I align an image in relation to text?

I'm attempting to create an interesting layout where I have text centered with an image on each side, like this

|                    |
|        text[image] |
| [image]text        |
|                    |

My goal is to have the text centered within the div, while having images positioned on either side of the text. How can I achieve this effect? This is what my current HTML and CSS look like:

<div class="text-container">
  <h1 class="text">text1</h1><span class="image"><img src="image.svg"></span>
</div> 

text-container {
    text-align: center;
}
text {
    display: inline;
}

The problem I'm facing is that the image is being treated as an inline element as well, causing the entire structure to be centered instead of just the text. How can I resolve this issue?

Answer №1

To achieve the desired layout, use the CSS property display:flex and add padding-right when the img element comes before the text.

.text-container {
    display: flex;
    align-items: center;
    justify-content: center;
}

img{
width:50px;
height:50px;
}
.p-right{
 padding-right: 110px;
}
<div class="text-container">
  <div>
    <h1 >text1</h1>
  </div>
  <div>
     <img src="https://i.sstatic.net/3mG2d.jpg" />
  </div>
 
</div> 
<div class="text-container p-right">

  <div>
     <img src="https://i.sstatic.net/3mG2d.jpg" />
  </div>
   <div>
    <h1 >text1</h1>
  </div>
</div> 

<div class="text-container">
  <div>
    <h1 >text1</h1>
  </div>
  <div>
     <img src="https://i.sstatic.net/3mG2d.jpg" />
  </div>
 
</div> 
<div class="text-container p-right">

  <div>
     <img src="https://i.sstatic.net/3mG2d.jpg" />
  </div>
   <div>
    <h1 >text1</h1>
  </div>
</div> 

Answer №2

Here is the code to display an image next to text:

HTML:

<link 
 href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" 
    rel="stylesheet" id="bootstrap-css">
<script 
   src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"> 
</script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
   <!------ Include the above in your HEAD tag ---------->

   <div class="container">
    <div class="row">
      <h2>Some Title</h2>
     </div>

 <div class="block">
      <div class="row">
      <div class="span4">
          <img class="img-left" src="http://assets.barcroftmedia.com.s3-website-eu-west-1.amazonaws.com/assets/images/recent-images-11.jpg"/>
        <div class="content-heading"><h3>Experience &nbsp </h3></div>
          <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.</p>
     <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem 
    malesuada magna mollis euismod. Donec sed odio dui.</p>
    <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.</p>
     </div>
  </div>
<br/>
  <div class="row">
    <div class="span4">
     <img class="img-right" src="http://www.prelovac.com/vladimir/wp- 
  content/uploads/2008/03/example.jpg"/>
     <div class="content-heading"><h3>Experience &nbsp </h3></div>
     <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.</p>
      <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.</p>
    <p>Donec id elit non mi porta gravida at eget metus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.</p>
   </div>
   </div>
  </div>
 </div>

CSS:

 .span4 img {
  margin-right: 10px;
 }
 .span4 .img-left {
 float: left;
 }
 .span4 .img-right {
 float: right;
 }

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 Autoprefixer script crashes with the error message: TypeError - Patterns can only be a string or an array of strings

After successfully installing autoprefixer and postcss-cli, I embarked on setting up a simple build process with NPM scripts using my command prompt and VS code. As someone with only 2 months of coding experience, I was following a tutorial on CSS/SASS top ...

Utilize HTML5 Application Cache solely for storing and managing dependencies

My goal is to selectively cache certain files like JavaScript, CSS, fonts, and image sprites. Should I create a manifest file for these specific files or rely on the browser's caching mechanism? If using a manifest is preferred, can I still prevent ...

Creating a responsive card design with Material UI

Creating a responsive card layout for mobile applications is my current challenge const styles = { edit: { width: "40%", marginLeft: 270, background: "#76ff03" }, add: { width: "100%", background: "#18ffff", size: "large" ...

AngularJS: Issue with scope not updating across several views

Having one controller and two views presents a challenge. ClustersController angular.module('app.controllers').controller('ClustersController', [ '$scope', 'ClustersService', function($scope, ClustersService) { ...

How to insert additional spacing within an input tag class in dynamic HTML using jQuery

When creating an html table dynamically from json data, I encountered an issue with adding space inside my input button control. It seems that after separating two classes with a space, the code is not functioning properly in this snippet environment. Howe ...

Why isn't the right-clear property functioning correctly?

My understanding of `clear: left`, `clear: right`, and `clear: both` in CSS has always been a bit fuzzy. I know that `clear: both` means it prevents floating elements from appearing on both sides, but I recently did some testing here. I expected the layout ...

Improperly positioned Divs overlapping

Help needed: I am attempting to add a menu to a specific section. The menu div is extending off the page to the right, but it should be contained within the HOMEMENU section (the long black bar with no content). Despite using a clear:both div, the menu s ...

What causes certain content to be inaccessible when using Safari?

When I visit this page using Safari or my iPhone, the list of social network members does not appear. Can anyone explain why this might be happening? I am unable to figure out the reason behind this issue. Thank you in advance for your help! ...

Submitting the form with a target of _blank will result in the form being submitted on both a

I am currently working on a form that includes both a "Submit" button and a "Preview" button. While the submit button is functioning correctly, I am experiencing an issue with the Preview button. When clicked, it opens up a new page with the preview as in ...

Unable to remove the most recently added Object at the beginning

I am currently working on a project to create a dynamic to-do list. Each task is represented as an object that generates the necessary HTML code and adds itself to a div container. The variable listItemCode holds all the required HTML code for each list it ...

Thymeleaf allows for the splitting of tables across different pages when generating a PDF document

I have a custom HTML template containing a table structure <table id="people" border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Salary</th> </ ...

Utilizing jQuery or Javascript to obtain the title of the current webpage, find a specific string within it, and then apply a class to the corresponding element

Let's imagine I start with this: <title>Banana</title> and also have some navigation set up like this: <ul id="navigation"> <li> <a href=#">Banana</a> </li> </ul> Upon loading the sit ...

What could be the source of this margin or spacing?

Within this test, I have noticed that there are 2 inline flex containers. Specifically, I am referring to the test scenario that states Next The flex containers are laid out inline. Despite the fact that there is some space between the containers, upon in ...

What are the steps to create a dynamic background image that adjusts to various

When viewing the website on a computer screen, the entire cat is visible in the background image. However, when switching to a mobile screen size, only a portion of the cat is displayed, appearing as though it has been cut off. The image is not smaller on ...

Form featuring a mandatory checkbox that must be selected in order to proceed; failure to do so will result in an

So here’s the situation: I have a form with a checkbox for agreeing to the terms of service, and I want to make sure it is checked before proceeding with the donation process. I only have the HTML code and no idea how to implement this functionality. Ide ...

HTML and CSS are distinct entities with their own individual purposes and

Can you create custom entities? For example: &longline; --> ----------------------------- and then incorporate it in HTML code: <div> &longline; bla bl bla </div> ...

Minimize the styling of the input placeholder CSS

Here is the CSS I am using: #login-box ::-webkit-input-placeholder { color: #666; } #login-box :-moz-placeholder { color: #666; } #login-box ::-moz-placeholder { color: #666; } #login-box :-ms-input-placeholder { color: #666; } I attem ...

Unable to access the jumbotron in Angular with Bootstrap 5

Recently, I set up a brand new Angular installation and included Bootstrap 5 using the following command: npm install bootstrap --save After that, I updated the lines in the angular.json file to: "styles": [ "node_modules/bootstra ...

Pictures are not appearing correctly when utilizing the img-fluid class

Issues occur when I apply the "img-fluid" bootstrap class to my images, as they fail to appear on the page and are set at 0x0 pixels in size upon inspection. This is the HTML code: <div class="col"> <div class="row no-gutters"> &l ...

Arranging serialized information from a tabular format and retrieving specific data

: I'm currently attempting to utilize an AJAX POST request to send data from a dynamic webpage that includes information gathered from a table form, a date selection box, and comment text input. The webpage is generated as a PHP file on a GET request ...