Ways to vertically align an image and text using CSS

Hello! I am in the process of developing a website and encountering some challenges with aligning an image and text vertically. The image seems to be occupying 100% of the space on the website, despite my efforts. Here is the relevant code snippet:

body {
  width: 100%;
  max-width: 960px;
  margin: 0;
}

div.content {
  width: 100vw;
  display: flex;
}

div.column1 {
  width: 15%;
  background-color: #F7F7F7;
  overflow: hidden;
  height: 100vh;
}

div.column2 {
  width: 70%;
  overflow: hidden;
}

.banner {
  width: 100vw;
  height: 10vh;
}

.container2 {
  display: flex;
}
<div class="content">
  <div class="column1">
  </div>
  <div class="column2">
    <div class="container2">
      <div class="lobby">
        <img src="img/lobby.jpg" alt="" /> </div>
      <div class="content">
        <p>lorem50gsdgsdsgdgsgdfgdfgdfgdfgfdggsd</p>
      </div>
    </div>
  </div>
  <div class="column1">
  </div>
</div>

The layout of the website consists of 3 columns, with content placed in the middle column. Despite using display flex, the vertical alignment does not seem to be functioning as expected. Any insights into why this may be happening? Thank you for your assistance!

Answer №1

To vertically center the children, make sure to apply align-items:center on the flex parent element. For more information on using flex-container, you can visit this link. If you need general information about flexbox, check out this resource.

For horizontal alignment, consider adding justify-content:center to your styling.

Answer №2

By utilizing the display: flex property on the content div, all you need to do is include align-items:center and your text will be perfectly centered vertically:

body {
  width: 100%;
  max-width: 960px;
  margin: 0;
}

div.content {
  width: 100vw;
  display: flex;
  align-items:center;
}

div.column1 {
  width: 15%;
  background-color: #F7F7F7;
  overflow: hidden;
  height: 100vh;
}

div.column2 {
  width: 70%;
  overflow: hidden;
}

.banner {
  width: 100vw;
  height: 10vh;
}

.container2 {
  display: flex;
}
<div class="content">
  <div class="column1">
  </div>
  <div class="column2">
    <div class="container2">
      <div class="lobby">
        <img src="img/lobby.jpg" alt="" /> </div>
      <div class="content">
        <p>lorem50gsdgsdsgdgsgdfgdfgdfgdfgfdggsd</p>
      </div>
    </div>
  </div>
  <div class="column1">
  </div>
</div>

Answer №3

If you want to get things working, let's brainstorm together, shall we? Let me explain what's going on here: To start off, if you have a parent div, its children should be stacked on top of each other

  1. Step one: Make the div have flex: 1, check out this website to learn more about flex
  2. Next, arrange the items side by side using display: flex
  3. Align the items in the container with justify-content: center and align-items:center
  4. If you want to center the div on your page, try using: margin: 0 auto
  5. And now for the magic touch: flex-direction: column, explore more in the documentation at flex-direction

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

Highlighter for HTML - Prism

I have been using Prism for styling CSS and it's working well: <pre><code class="language-css">p { color: red }</code></pre> However, I am facing issues with highlighting HTML code: <pre><code class="language-html"& ...

Creating a web responsive grid from scratch: Best practices

Currently, I am engaged in developing a timesheet/billing system for a client's project. One of the main tasks on my plate is to create an effective grid-layout that accurately displays data in its designated positions. We have not yet determined whic ...

Card image floating to the right using Bootstrap

I have a set of three horizontal cards, not in a card deck as it's not responsive, and I want to align images in the bottom right corner of each. Bootstrap 4 comes with a class for card-img-left which works perfectly. I've attempted using float-r ...

How can I simulate mouse position in a UIWebView?

Can the mouse position (x,y) for HTML-5 elements on a UIWebView be programmatically set using stringByExecutingJavascript in order to trigger hover, mouse over, and other interactions? ...

Ways to create a div that automatically increases in height to fit its text when the browser size decreases

In the following code snippet, there is a top bar with a lengthy text: <div class="topbar"> <div style="float:left;height:36px;line-height:36px;text-transform: none;"> long text.... </div> </div> Within the topbar ...

The hidden inner div fades away within the confines of a trapezoid-shaped container

I've been attempting to position a div within another trapezoid-shaped div. However, the inner div remains invisible no matter what I try. Z-indexes have also been tested. .trapezoid { border-color: transparent transparent rgba(255,0,0, ...

Continuously I am being informed that the value is null

Check out this code snippet: var Ribbon = /** @class */ (function () { function Ribbon(svg) { // Code here... } Ribbon.prototype.init = function() { // Initialization code here... }; Ribbon.prototype.updateR ...

Transform the radio selection into physical buttons with the Ruby on Rails Simple Form Gem

I've been struggling to achieve the desired layout when using the automatic wrappers generated by the Simple Form gem. I am working with Ruby on Rails and the Simple Form Gem, trying to create radio buttons with a specific design: https://i.sstatic.n ...

A concise way to write an else if statement in Javascript and jQuery

Is there a way to make this code more concise? It works perfectly fine, but it's too lengthy. Basically, the code involves two dropdown lists where the user selects options, and based on their selection, values appear in two textboxes. The catch is th ...

Having trouble converting my form data into an array for table display

My website has a form for entering dummy patient information, with a table to display the submitted data. However, I'm facing an issue where the data is not being stored in the array when the user clicks the "Add Patient" button. Upon checking the arr ...

Is it possible to integrate Laravel as the backend for an already existing HTML website template?

I recently designed an HTML template for a salon listing directory site, and while it looks great, I realize I also need a backend solution to manage listings, add advertisements, and more. Can I seamlessly integrate a Laravel-based admin panel into my c ...

Height Overflow Problem in Three-Column Design

html,body{ height: 100%; } #app{ height: 100%; border: 2px solid black; } .row{ display: flex; height: 100%; } .col-3 { width: 20%; background: red; } .col-6 { width: 60%; background-color:blue; } #large-item{ height: 100%; width: 100%; backgro ...

Trouble with closing windows in the in-app browser on Android devices

Is there a method to successfully close the in-app browser? Despite window.close working on iOS devices, it is not effective on Android. I have experimented with alternatives like window.top.close and window.open("", "_self") window.close, but none have ...

tips for selecting various API requests based on the selected drop down menu choice

Hey there! I'm looking to enhance my login page by adding a feature that allows users to select from a dropdown menu with different options. Each option will be linked to a specific API, and based on the API response, the user's ability to log in ...

Adjusting the document root in an Apache site's directory

I have been working on my portfolio website and I want to showcase some past projects. There is one particular website that I developed in the past which is no longer live, but I have an archived version of it. I would like to be able to view this old site ...

Extracting null data from web scraping using Python and Beautiful Soup

I'm currently facing challenges with extracting the correct values from a website that provides prices for Silver, Gold, Palladium, and Platinum. You can find the website here. Below is the HTML structure of the website: <div id="header-tab ...

What is the best way to retrieve the URL from a specific HTML tag?

I am attempting to extract all URLs that have id='revSAR' from the provided HTML tag using a Python regex: <a id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp ...

Utilize Chrome Extension to Add or Remove HTML Elements

In the process of developing a Chrome extension, I encountered a specific web page where there is a table with commented out information. I am seeking a way to eliminate the comment syntax in order to display the hidden information within the table. What ...

What is the best method for removing a class with JavaScript?

I have a situation where I need to remove the "inactive" class from a div when it is clicked. I have tried various solutions, but none seem to work. Below is an example of my HTML code with multiple divs: <ul class="job-tile"> <li><div ...

Adjust the height of the svg

My page contains an SVG that is impacting the height of its container and causing other elements to move. Is there a way to adjust the height of the SVG? Currently, the SVG is a square but I would prefer it to be a rectangle as the height is too large. &l ...