Attempting to align an image and text next to each other within a container

I've been scouring the web for a solution to my issue, but I can't seem to find one that fits my particular scenario.

Currently, I'm facing an issue where the text and image are stacking on top of each other instead of appearing side by side within the headerBlock div.

#wrap { 
  width: 800px; 
  margin: 0 auto; 
  overflow: hidden;
  float: left;
}

#headerBlock { 
  height: 200px; 
  background:  #776b68; 
}

div.headerText {
  vertical-align: middle;
  text-align: justify;
}

div.headerImg img {
  vertical-align: middle;
}
<div id="wrap">
  <div id="headerBlock">
    <div class="headerText">Some text here</div>
    <div class="headerImg"><img src="/somepicturehere">
    </div>
  </div>

  <!--Ignore these, it's for future use-->
  <div id="leftBlock"></div>

  <div id="rightBlock"></div>

  <div id="footerBlock"></div>

</div>

Answer №1

To achieve the desired effect, set .headerText and .headerImg to have display: inline-block;.

#wrap { 
  width: 800px; 
  margin: 0 auto; 
  overflow: hidden;
  float: left;
}

#headerBlock { 
  height: 200px; 
  background:  #776b68; 
}

div.headerText {
  display: inline-block;
}
div.headerImg {
  display: inline-block;
}
div.headerImg img {
  vertical-align: middle;
}
<div id="wrap">
  <div id="headerBlock">
    <div class="headerText">Some text here</div>
    <div class="headerImg">
      <img src="/somepicturehere">
    </div>
  </div>
  <!--Ignore these, it's for future use-->
  <div id="leftBlock"></div>
  <div id="rightBlock"></div>
  <div id="footerBlock"></div>
</div>

If you want to vertically align them instead, apply display: flex; and align-items: center; on the container:

#wrap { 
  width: 800px; 
  margin: 0 auto; 
  overflow: hidden;
  float: left;
}

#headerBlock { 
  height: 200px; 
  background:  #776b68; 
  display: flex;
  align-items: center;
}

div.headerText {
  display: inline-block;
}
div.headerImg {
  display: inline-block;
}
<div id="wrap">
  <div id="headerBlock">
    <div class="headerText">Some text here</div>
    <div class="headerImg">
      <img src="/somepicturehere">
    </div>
  </div>
  <!--Ignore these, it's for future use-->
  <div id="leftBlock"></div>
  <div id="rightBlock"></div>
  <div id="footerBlock"></div>
</div>

Answer №2

To arrange the child elements side by side in a row, use display: flex on the parent element. You can further adjust the alignment using properties like align-items and justify-content.

#wrap { 
width: 800px; 
margin: 0 auto; 
overflow: hidden;
float: left;
}

#headerBlock { 
height: 200px; 
background:  #776b68; 
display: flex;
align-items: center;
}

div.headerText {
text-align: justify;
}

div.headerImg img {
vertical-align: middle;
}
<div id="wrap">
<div id="headerBlock>
    <div class="headerText">Some text here</div>
    <div class="headerImg"><img src="/somepicturehere">
    </div>
</div>

<!--Ignore these, it's for future use-->
<div id="leftBlock"></div>

<div id="rightBlock"></div>

<div id="footerBlock"></div>

</div>

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

Flying Saucer core fails to load Bootstrap CSS files when generating a PDF

I've spent two days searching for a solution, but I can't seem to find one. I need to create a pdf form that opens in a browser. After much consideration, I chose to utilize the flying saucer library. While my internal CSS loads successfully in H ...

What causes the differences in properties between @click and @paste?

Check out my demo Vue SFC Playground In the Comp component, I am logging the props.id as well. When I click on comp, the id is correct. However, when I hover and paste the id, it remains the same until I move the mouse out for a while. I want to unders ...

Tips for personalizing the WordPress menu options located in the header

After creating a WordPress menu and displaying it in the header with a total of 6 pages or menu items, I now want to split the menu so that 3 items appear on the left side and the other 3 on the right. However, my lack of knowledge in stylesheet styling ...

What is the best way to add styling to my image when hovered over within a link on a responsive website?

I'm having trouble aligning the :hover state with the original footer image links. Here is an edited version of the flicker issue I encountered on Chrome, while nothing appeared on Safari. https://i.sstatic.net/ExdPW.png I apologize for linking to ...

Is it possible to deactivate the CSS hover effect using JavaScript?

There is a parent element with an associated child element. Right now, the child element is hidden and only appears when hovering over the parent. It vanishes when the mouse moves away from the parent due to css settings. The goal is for the child elemen ...

CSS: the max-width property does not constrain an absolute positioned container from overflowing

I've been trying to figure this out for hours, but I can't seem to get it right. My top menu has submenus that include a popup menu. In one of my popups, I need the container to have a maximum width of 170 pixels and all the items inside to wra ...

Implement CSS to layer HTML 5 canvases while including a margin

I have a design conundrum in my app where I am using multiple stacked HTML canvas elements for drawing. My goal is to make the canvases fit perfectly within their containing div while also maintaining a left and bottom margin. This is how my HTML structur ...

Tips for decoding Dropbox Public folder images in HTML

Is there a way to read and display all images from a Dropbox public folder using jQuery loop? I have successfully displayed a single image like this: Simple Image "" But how can I access all images in the Dropbox public folder if their number is unknown ...

What is the precise width?

Here's a question for you: an HTML element has the following styles applied to it - width: 150px, padding: 3px, border: 4px, margin: 7px. What is the actual width of the element? I'm confused by this question because it specifies that the width ...

Enhancing the appearance of the MUI DatePicker

I'm currently using a DatePicker component from Mui Lab and I'm attempting to customize the appearance of the Calendar component by incorporating some border or background color. Although I've tried utilizing the PaperProps prop for DatePick ...

User account details displayed in the website's header for Prestashop users

I am currently using prestashop 8.1.5 with the Megafox theme installed. The issue I am facing is in the header section where user information is displayed as a drop down menu, but unfortunately, the username is not visible when a user is logged in. I have ...

Ways to align a form in the middle of a Bootstrap 4 jumbotron

<div class="special-center"> <h1 class="unique-header">Special Text</h1> <p class="callout-text">Unique content goes here.</p> <hr class="line-break"> <p>################</p> <form class="st ...

Tips for displaying multiple XML datasets on an HTML webpage

I came across an example on W3schools that I'm trying to replicate: http://www.w3schools.com/xml/tryit.asp?filename=tryxml_app_first However, the example only displays one CD. My goal is to showcase all the data (in this case CDs) in the XML file. H ...

I encountered a limitation in utilizing opacity with my background-image

I have been struggling to apply opacity to my background image while still being able to see and write text on it. I have tried various methods but either the text is visible without the ability to write on it, or only half the screen is covered with the b ...

Arranging React Bootstrap columns in a way that the column on the right appears above the column on the left

Is there a way to use React Bootstrap to create a layout with two columns side by side, and have the column on the right stack on top of the column on the left when the screen size is reduced? Check out the code snippet below: import "./styles.css&qu ...

Prevent any interaction on a webpage with the help of JavaScript

Just dipping my toes into the world of JavaScript. I've created an app using Outsystems technology that triggers a pop-up for users to input information. After they fill out the form, there's a 'Save' button they can click. The problem ...

Is there a way to efficiently pull specific time-related data from XML API forecasts using PHP and present them in HTML tables?

Currently, I'm facing a bit of confusion on how to obtain a forecast for the upcoming 5 days of a specific city using OpenWeather's forecast api. The challenge lies in the fact that the forecasts are provided in 3-hour intervals, but I only requi ...

Creating a Tic Tac Toe game using Javascript程序

As a beginner in programming, I am working on an assignment to create a Tic Tac Toe program using HTML and JavaScript. I found some code related to Tic Tac Toe on a website, but I am having trouble understanding how it works. Here is the code snippet: if ...

I am wondering how to use the value assigned to a variable's textContent as an argument for player input in my JavaScript code

i am currently developing a JavaScript project to create a user interface for my rock, paper, scissors game. Currently, the game only runs in the console and prompts the player for input. So far, I have implemented three buttons (one each for rock, paper, ...

What is the best way to insert a vertical line between two 960.gs containers?

Incorporating the 960.gs grid system into my design, I am looking for the optimal method to include a slim separating vertical line between two boxes that allows for customization of width and color. One approach I have considered involves creating div cl ...