Guide to shifting an image to the right within a Bootstrap 4 card header

When working on my website with Bootstrap, I ran into an issue with aligning a text to the left side of the header and an image to the right side. I attempted to use a row container along with justify-content-end, but they did not line up at the same height as the header. My goal is to have both elements aligned at the same height.

This is what I tried:

<div class="card" style="margin-bottom: 25px;">
  <div class="card-header">
  <h4 class="card-title Anton"><?php echo($thread['title']); ?>
    <span class="row justify-content-end">
        <img height="32" width="32" src="assets/img/edit.png" href="/test" style="padding: none; margin: none;">
    </span>
  </h4>

<hr />
...

However, the result looked like this: The Image is more on the bottom than the header!

Answer №1

I have replicated the scenario you described by utilizing Flex Box. I set up a container div with two child divs (left and right) both with a flex value of 1 to give them equal width. The image in the right div has justify-content: flex-end applied, which aligns it to the far right within the div.

HTML

<html>
  <body>
    <div class="header">
      <div class="title">
        <p>Text</p>
      </div>
      <div class="img">
        <img src="smiley.gif">
      </div>
    </div>
  </body>
</html>

CSS

.header {
  display: flex;
  width: 100%;
  height: 45px;
  background: red;
}

.title {
  flex: 1;
  background-color: green;
}

.img {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  background-color: yellow;
}

Here are some quick references:

justify-content: flex-start aligns content to the left
justify-content: center centers content horizontally
justify-content: flex-end pushes content to the right

You can view the implementation on this fiddle: http://jsfiddle.net/k5uj04dt/6/

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

Easily accessible button and span located on the right side

One issue I'm facing is that the button and span are not aligned on the same line. Is there a way to resolve this? <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/c ...

What is the best way to organize my defaultdict for optimal structure?

I have a defaultdict within my views.py file that looks like this: defaultdict(<class 'list'>, {'List of tubes': ['2324', '98', '7654', 'List of auto:': [147, 10048, 1009, 10050, 10, 1647, 1 ...

magnificPopup experiencing difficulties when attempting to invoke a class that is dynamically generated within the HTML document

Currently, I am encountering an issue with the magnificPopup. Whenever I try to trigger the popup using the class 'popup-with-zoom-anim', it doesn't seem to work as expected. Has anyone else faced a similar problem before? <body> < ...

Error displayed inline

I am facing an issue with a hidden textbox that I need to make visible based on a certain condition. Despite checking that the control is being triggered by the change event, I am unable to get it to display. I have experimented with different methods with ...

Visuals Divide the Text

"Incorporate visual elements to enhance the content." Can anyone explain the concept? How can it be implemented using html or css? Much appreciated. ...

Is there a way to modify the margin of items in my table affected by the Angular Material ng-tns class without encountering a different variant of ng-tns when using CSS for the adjustment?

In my Angular project, I am utilizing Angular Material and material table components. One issue I have encountered is that all the text in my table seems to have a 10px margin applied at the top and bottom due to classes like .ng-tns-c5-1 or .ng-tns-c6-1, ...

After implementing jQuery, media queries may not function as intended

My approach involves using media queries to adjust the width and height of specific div elements when the window is resized. Additionally, I utilize jQuery to increase their size on mouseover. However, I have encountered an issue where the execution of jQu ...

Successfully bypassed Captcha by using inspect element

Currently, I have integrated Google Captcha on my login page. However, upon inspecting the elements, I noticed that the Captcha element is visible in the source code. This allows me to delete the element, essentially removing the Captcha checkbox and byp ...

My HTML document is not displaying the JavaScript variable as expected

I have created a JavaScript function with several variables and attempted to test it to display the variables in my HTML document. However, I am facing an issue where the variables do not appear as expected. Specifically, I am trying to insert the variable ...

Is it possible to convert nested CSS into Material-UI's CSS in JS method?

Here is the code for embedding an iframe: <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: abso ...

When a media query is activated, the Flex item mysteriously vanishes from

UPDATE I followed the suggestion in Ezra Siton's answer and changed the media query display to block, which successfully resolved the issue! Within my html code, I have a parent column flexbox containing two children. The second child is itself anoth ...

Tips on aligning a button next to an image

I need help adjusting my code to display a button next to an image on my webpage. I want it to look like an image button in a loop where users can download, but I am struggling with CSS. Can anyone please modify my code to meet my requirements? Thank you i ...

What is the technique for causing this element to move in reverse?

How can I utilize JS to halt the interval and direct the alien to move backwards once it reaches 700px? I am aware that CSS can achieve this, but I prefer a strictly JS approach. I am struggling with stopping the interval as it hits the left position of 70 ...

The background color is failing to respond to the padding of the adjacent image

Check out this page to see the issue. The code snippet surrounded by pre tags has a background-color that is not aligning properly with the image, even though the image has a defined padding of 16 pixels on the right. The text alignment is correct, but the ...

Submitting values in URI through an HTML form

Hey there, I'm really in need of some assistance with this issue that's been driving me crazy... :) So, I'm working on a project using Symfony2 and AngularJS: <form class="navbar-form navbar-right" role="form" action="" method="post"> ...

jQuery wrapAll issue

I have a repeating group of three divs in my code that I need to wrap together. Here's an example from my HTML: <div class="one" /> <div class="two" /> <div class="three" /> <div class="one" /> <div class="two" /> <d ...

The CSS of the Sendgrid template is being overlooked

I have utilized Sendgrid to create the template mentioned below: https://i.sstatic.net/TFQfl.png In order to send this template using my Node server, I have developed the module provided in the code snippet: /* * Created by root on 6/6/16. */ var path ...

Center text vertically within a div using CSS

I need assistance with aligning my text vertically centered and left aligned to the logo in my HTML & CSS code. How can I achieve this with the code provided? Link to Fiddle: http://jsfiddle.net/z6yet59f/ .slogan { background:#eaeaea; float:lef ...

Quickly Alter Background Image when Hovered Over

I am looking to enhance my website by having the background image change on mouseover of an object, with the replacement image only showing for a quick moment before returning to the original. Additionally, I want to incorporate a short sound file into the ...

Update information on the page seamlessly with user-friendly URLs, no need for hashtags or question marks!

I am faced with the common challenge of maintaining a website where I want the header and menu divs to remain constant while the content div changes based on user input through the menu. To achieve this, I used PHP include, which works well, but causes th ...