Place an image in the middle of a div with text aligned to the right side

How do I center an image within a div that has text on the right side? The page layout is based on percentages and I want the image to be perfectly centered with the text floated to the right.

This is what I currently have:


IMG.logo {
    display: block;
    margin-left: auto; 
    margin-right: auto 
}

<div id="logoDiv">
   <p class="bold marginleft5 floatright paddingtop60">Logout</p><p class="floatright paddingtop60">Hello Bill</p>
   <img alt="Logo" src="~/Content/Images/logo.png" class="logo" height="80" width="245" />
</div>

I know that if I move the text to the next line, the logo will be centered. Is there a way to keep them on the same line?

Answer №1

To achieve a floating effect, one could utilize absolute positioning and adjust the top and right properties accordingly.

img.logo {
    display: block;
    margin: 0 auto;
}
.floatright {
    display: inline-block;
    position: absolute;
    top: 0;
    right: 0;
    width: 400px;
    margin-right: 8px;
    text-align:right;
}
.line1 {
    top: 0px;
}
.line2 {
    top: 16px;
}
.bold {
    font-weight: bold;
}
<div id="logo-div">
    <p class="bold floatright line1">Logout</p>
    <p class="floatright line2">Hello Bill</p>
    <img alt="Britannia Logo" src="~/Content/Images/logo.png"
        class="logo" height="80" width="245" />
</div>

Answer №2

Here is a way to achieve it:

.logo {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
}

Feel free to see it in action at: CodePen

Answer №3

If you're looking for a responsive solution that adapts to both wide and narrow screens, check out this code snippet: http://codepen.io/mysite/pen/qwerty

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8>
<style>

.logoContainer {
    width: 100%;
}

.logo {
    text-align: center;
    position: absolute; 
    left: 0; 
    right: 0; 
}

.content {
    width: calc(50% - 130px); 
    float: right;
}

</style>
</head>
<body>

<div class="logoContainer">
    <div class="content">
      <p>Sign Out</p>
      <p>Hello there</p>
    </div>
    <div class="logo">
       <img alt="My Logo" src="http://placehold.it/245x80" height="80" width="245">
    </div>
</div>

</body>
</html>

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

Trigger oncopy on the body excluding specific class

Is there a way to execute a function on document.body.oncopy but exclude a certain class (defined for div elements) from this function call? I want to avoid calling the function on specific classes, is there a method to achieve this? ...

Steps for encoding a CSV export with HTML:

When I retrieve data from a string that is HTML encoded as Quattrode® I end up with Quattrode® after viewing it in Excel 2007. Here is the routine: Response.Clear(); Response.Buffer = true; Response.ContentType = "text/ ...

Switch website address without redirecting via JavaScript

I am seeking information on how to update the URL without a full page reload, similar to the functionality seen on this website . When clicking on tabs, the URL changes seamlessly without refreshing the entire page. While some sources suggest that this m ...

Mastering the Art of Incorporating jQuery, JavaScript and CSS References into Bootstrap with ASP.NET WebForms

Hey there, I'm currently working on a personal project as a beginner in Bootstrap. My main challenge for the past two days has been trying to integrate a dateTimePicker and number Incrementer using Bootstrap techniques. Despite my efforts in researchi ...

The custom icon font sourced from Flaticon and hosted on a separate server is not functioning properly

I recently obtained a unique icon font from Flaticon and decided to incorporate it into a SaaS online store. Since I don't have direct access to the shop's files, I attempted to host the font on an FTP server and integrate it on a demo page: here ...

Unable to establish React API communication on cloud-based IDE for MERN Stack development

Exploring the MERN stack through this informative tutorial: https://medium.com/@beaucarnes/learn-the-mern-stack-by-building-an-exercise-tracker-mern-tutorial-59c13c1237a1 I've opted to use goorm IDE, a cloud platform similar to cloud 9 IDE. As I pro ...

View the edited image preview instantly upon selecting the image

I have selected an image and previewed it before submitting the form. However, now I wish to be able to edit the file immediately after selecting it, preview the changes, and then submit the file. <input type ="file" accept="image/*" id="image" name="i ...

Incorporating .txt files into a static webpage using only HTML and JavaScript

Exploring the realm of web page creation for the first time has left me in need of some guidance. I have successfully added an upload feature for text files on my web page, but I am struggling to embed a text file directly into the page source. With Java s ...

How can I personalize the HTML code of images added to my WordPress articles?

I've been on an extensive search trying to find a solution for this issue. I'm aiming to modify the markup of an uploaded image in WordPress from its current form: <div id="attachment_906" style="width: 590px" class="wp-caption aligncenter"&g ...

Overlapping borders create a unique style for bootstrap labels

Below is a snippet of code with div elements that each have a 1px border defined in the labelStyle class. .labelStyle { text-align: left; padding: 0px; font-weight: 0px; font-size: 8px; border: 1px solid; margin-bottom: 0; bor ...

Differences in Print Layout Between Chrome and Firefox when Viewing a PHP Website

I have been working on creating a print command to print a specific div, and I have managed to successfully run the print command using default methods like CTRL + P and also with a button click. The Issue: However, I have encountered a problem where the ...

Retrieving Google Maps Geocoding JSON through an Express API

Recently, I have been teaching myself about Node.js and Express, specifically focusing on returning JSON results from a Google Maps Geocoding API request. I have managed to make it work using the `require` module, but I am determined to understand where I ...

Can I simultaneously execute nodemon and browser-sync using a single npm script for an Express application?

Within my Express application, I utilize both nodemon and browser-sync in conjunction. These npm scripts are included in my package.json: "scripts": { "start": "node ./bin/www", "start:nodemon": "nodem ...

Bigcommerce encounters a 401 error during the payment processing, triggered by API with error code 10001

I recently started working on integrating the Bigcommerce payment API and I am facing issues with solving the 401 unauthorized error. Below is a sample of the data that I have tried: { "payment": { "instrument": {}, "payment_method_id": "cod", "amoun ...

Using Vue.js to detect changes in a value and dynamically highlight the text

I am working on a simple script that generates random numbers every few moments. I want to change the background color whenever the random number is not equal to the one before it. Is this possible? For example, if the random numbers generated are 1, 1, an ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

Using Angular to store image data with base64 encoding for improved caching efficiency

Imagine I have an application that showcases various restaurants in a specific city. With numerous listings, the data volume is substantial even without including any images. If I aim to provide users with quick access to this data by implementing caching ...

I've been attempting to develop a React application, but I consistently encounter the following error: "npm ERR! cb() function was never invoked!"

Here is the issue at hand: HP@DESKTOP-1HP83V8 MINGW64 ~/Desktop/Web-Development (master) $ npx create-react-app my-app A new React app is being created in C:\Users\HP\Desktop\Web-Development\my-app. Packages are being installed. ...

Creating a customized list item design using Bootstrap 3 and incorporating div elements

I have designed a custom li element with Bootstrap 3, but I am looking to achieve something specific. . Here is my current setup- HTML- <ul class="list-group"> <li class="list-group-item" id="baal"> <div style="display: inlin ...

Overriding the Ajax URL

During an AJAX request in a Rails app triggered by onchange event, I am encountering an issue with the URL being called. The function for the request is a simple PUT operation: function quantityChange(id, val) { $.ajax({ url: 'cart_items/ ...