What is the best way to position an image alongside an h2 heading?

Perhaps the technology stack is not relevant in this case, but I am currently working on a nextjs project with tailwind for styling. I am attempting to place an image next to an h2 tag, but encountering unexpected behavior from the image. It seems as though there is no vertical overlap; it's almost as if the image is occupying one row and the text another. Here is my current setup:

https://i.sstatic.net/GvwVR.png

Below is the code that produced the display above:

 <div className="pb-3 justify-center align-center">
                <Image
                    src={logoNextjs}
                    alt="logo nextjs"
                    width={100}
                    height={100}
                />
                <div>
                    <h2 className="text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl text-center">
                        Sample
                    </h2>
                </div>
                <p className="text-center pb-1 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 xl:px-48 ">
                    Demo
                </p>
            </div>

If I apply the float-left class from tailwind to the Image tag in nextjs, I can achieve some alignment between the image and the h2 tag, albeit with slight vertical overlap:

https://i.sstatic.net/CX6OL.png

However, the issue persists as the image appears too far to the left when I actually want it right beside the text.

My understanding is that the Image component is essentially an optimized version of the img tag, suggesting that plain CSS solutions should suffice.

How can I make sure the image aligns perfectly vertically with the text and sits right next to it?

Answer №1

To achieve a centered layout, you can use flex-box with the following CSS property: justify-content: center:

<div className="flex flex-col pb-3 justify-center align-center">
  <div className="flex justify-center items-center w-full">
    <Image
      className="flex justify-center items-center w-[10rem]"
      src={
        "https://i.pinimg.com/736x/4a/2b/e7/4a2be73b1e2efb44355436c40bf496dd.jpg"
      }
      alt="logo nextjs"
      width={100}
      height={100}
    />
  </div>

  <div>
    <h2 className="text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl text-center">
      Centralized Image
    </h2>
  </div>
  <p className="text-center pb-1 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 xl:px-48 ">
    Demo
  </p>
</div>

Here is the resulting layout:

https://i.sstatic.net/DDnJm.png

Answer №2

Using basic CSS methods can be effective.

Common ways to position elements are:

  • Floating - While outdated, still works
  • CSS tables - Not as popular but functional
  • CSS grid - Can sometimes be more complex than needed

Additionally...

  • Flexbox - Likely the most suitable solution for many scenarios

See a live demonstration below:

header {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 24px;
  height: 124px;
  background-color: rgb(191, 191, 191);
}

img {
  width: 100px;
  height: 100px;
  background-color: rgb(127, 127, 127);
}

h2, p {
  margin: 0;
  text-align: center;
}

h2 {
  margin-bottom: 6px;
}
<header>
  <img src="" alt="logo nextjs" />
  <div>
    <h2>Sample</h2>
    <p>Demo</p>
  </div>  
</header>


Learn more about Flexbox:

Additional Resources:

Answer №3

Your code is almost there, but there are two mistakes that need to be corrected:

  1. The wrapper div is missing the flex class.
  2. The p tag should be inside the h2 within the div.

That's all you need to fix!

<script src="https://cdn.tailwindcss.com"></script>
<div class="pb-3 flex justify-center align-center">
  <img
    src="https://static-00.iconduck.com/assets.00/nextjs-icon-2048x1234-pqycciiu.png"
    alt="logo nextjs"
    width="100"
    height="100"
  />
  <div>
    <h2 class="text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl text-center">
      Sample
      <p class="text-center pb-1 text-lg font-normal text-gray-500 lg:text-xl sm:px-16 xl:px-48 ">
        Demo
      </p>
    </h2>
  </div>
</div>

By using these TailwindCSS classes, you can avoid writing extra CSS code!

Remember, if you want them to be on the sides, use justify-between instead of justify-center.

I recommend focusing on improving your native CSS skills, especially FlexBox as mentioned by @Rounin, as this will help you understand the functionality of these classes better.

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

Having trouble with the vertical-align property in Google Chrome?

<html> <head> <style> table,td {border:1px solid black ;} table {width : 80% ;height:80%;} .top {vertical-align:top}; .center {vertical-align: middle}; .bottom {verti ...

Utilize Bootstrap's custom validation exclusively on fields that are invalid

Exploring Bootstrap features has led me to wonder if there's a straightforward method to change the styling of a field specifically when it is considered invalid. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Display a picture retrieved from a POST request using Angular and a Spring Boot backend

Recently, I've been delving into the world of HTTP requests. My latest task involves uploading an image from the client and sending it to the server using a POST request. When testing this process in Postman, everything works smoothly as I receive th ...

To modify the color of the breadcrumb navigation bar from purple to #d6df23 using PHP

Can someone help me figure out how to change the color of the bar displayed in this link: I need assistance with modifying the breadcrumb navbar. Is there a way to change the color to yellow? I attempted editing the header file, but it didn't produ ...

The HTML element <a> can have both a href attribute and a onclick attribute

I'm currently facing a challenge with my project - I am trying to create a submenu that includes all sub-pages within a single HTML file. However, whenever I attempt to use both href and onclick, the functionality fails to work as intended. The only ...

Expand the object by clicking on it, causing the surrounding objects to move outside the viewport instead of appearing below

I'm facing a challenge with 3 squares inside one container, each occupying 33.33% of the viewport and placed next to each other. When I click on one square, it should expand to full viewport width and reveal hidden content, while the other two remain ...

Obtaining an image in the form of an object

import React from 'react'; import styles from './Home.module.css'; import information from '../../images/information.png' const Home = () => { return ( <div> <div className={styles.info}& ...

Update the default arrow icon in the expand/collapse navigation bar

I need help changing the downward arrow in the code below to a fontawesome icon. I'm unsure about how to: 1. Remove the default arrow on the right of the parent node. 2. Use "+/-" symbols to represent the collapse state. It seems that the onclick c ...

How can you use the :not selector in CSS to target specific elements within a group?

Consider the following set of inputs: <div id="group"> <input id="uno" class="red"></input><br/> <input id="dos" class="red"></input><br/> <input id="tres" class="blue"></input><br/ ...

I am having trouble showing the background image in the div

I created a 960 layout using 3 divs to easily organize child divs with images and text. However, I'm having trouble getting the background image of the first div to display. Can you help me figure out what I'm doing wrong? This is the concept f ...

The upcoming page.tsx export force-dynamic is failing to render dynamically

My dilemma involves a page that I am trying to render dynamically rather than statically. Unfortunately, the commonly suggested solution of simply using: export const dynamic = "force-dynamic"; has not yielded the desired results for me. I have ...

Problem with jQuery offset: SWF position remains unaffected

I attempted to create a code that would position an SWF file on top of an HTML button using jQuery's offset function to determine the coordinates. However, despite my efforts, the SWF does not appear to move as intended. var offset = $("#button").off ...

How can we determine the dimensions of an absolutely positioned element within a relatively positioned element?

One thing I noticed is that when placing an absolutely positioned element within a relatively positioned element, the latter does not automatically inherit the width and height of the former unless manually set. Take this example: <!DOCTYPE html> ...

I am in the process of creating a dropdown menu for a navbar that will appear when the cursor hovers over a specific element, complete with an arrow pointing upwards

In my current project with NextJS and Tailwind CSS, I've successfully created a dropdown menu but I'm facing an issue with positioning the arrow to point to the specific element being hovered on. In a previous version, I tried rotating a div at 4 ...

How to change the file name of an HTML page in a website template?

After downloading a free dashboard website template from the internet, I've been trying to incorporate it into my own website. My main focus now is on creating a navbar that opens the relevant page upon clicking. To do this, I duplicated the html fi ...

The min-height property in CSS appears to be ineffective on Jelly Bean

In my current project developing an Android application, I have incorporated Webviews in certain activities and fragments. However, I encountered a perplexing issue when running the app on Jelly Bean (api 16 and 18) emulator - the css property min-height ...

It is not possible for me to modify the attributes in responsive mode without first generating a new class within the tag

In order to make the attribute work in a responsive manner, I believe I need to create a class within the h2 tag. However, I am unsure where the issue lies. Could it possibly be related to the browser? Check out the image below for reference: this is my p ...

`Jump-start Your Development with jQuery Lightbox Centering`

Recently, I implemented a lightbox effect for images on my website. However, I encountered an issue where the lightbox was not centering properly in the window upon initial click. Instead, the lightbox would appear at the bottom of the page on the first cl ...

I am having difficulty combining 2 HTML pages without the output becoming distorted

Having encountered issues when combining two HTML pages, one for a navbar and the other for a contact form in my Django project. The resultant single page appears distorted as shown in the image below. I attempted to use sections but it does not seem to re ...

Creating responsive images in a navbar with HTML and CSS

I need help with this code snippet: <div> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <div class="d-flex flex-row justify-content-center align-items-center"> <a cla ...