What is the best way to center my text vertically in each line?

I'm encountering an issue with aligning my text vertically in two lines within a <p> tag. I am trying to position the second line at the bottom of my <div>. I experimented with using vertical-align: bottom and text-bottom along with the top and bottom properties in my <p>, but unfortunately, it did not produce the desired result.

.header {
  grid-area: header;
  background-color: red;
  display: grid;
  grid-template-areas: 'headerLeft rest headerRight';
  grid-template-columns: 10% 1fr 30%;
  padding: 5px;
}

.header p {
  margin: 0px;
}

.headerRight {
  grid-area: headerRight;
}

.headerRight p {
  float: right;
}
<div class="layout">
  <div class="header">
    <div class="headerRight">
      <p style="margin: 0px 0px 0px 1px;"><img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" /></p>
      <p><img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" /></p>
      <p style="margin: 0px 5px 0px 0px">Ola</p>
      <br />
      <p style="margin: 0px 5px 0px 0px">Ola</p>
    </div>
  </div>
</div>

Answer №1

Here are some suggestions for updating your HTML with minor changes; detailed explanations can be found within the code comments:

.header {
  grid-area: header;
  background-color: red;
  display: grid;
  grid-template-areas: 'headerLeft rest headerRight';
  grid-template-columns: 10% 1fr 30%;
  padding: 5px;
}

.header p {
  margin: 0px;
}

.headerRight {
  grid-area: headerRight;
  
  /* CSS Grid layout is utilized here: */
  display: grid;
  /* Define grid gap gutters: */
  grid-gap: 0 5px;
  /* Named areas of the grid are specified: */
  grid-template-areas:
      ". paragraphAreaOne imageOne imageTwo"
      ". . imageOne imageTwo"
      ". paragraphAreaTwo imageOne imageTwo";
  
  /* Columns sizing defined; first column spans one fractional unit (1fr), 
     other columns set to min-content to fit their content: */
  grid-template-columns: 1fr repeat(3, min-content);
  /* Three rows sized based on content: */
  grid-template-rows: repeat(3, min-content);
}

/* Elements positioned into appropriate grid areas: */
.headerRight p:first-of-type {
  grid-area: paragraphAreaOne;
}

.headerRight p:nth-of-type(2) {
  grid-area: paragraphAreaTwo;
}

.headerRight img:first-of-type {
  grid-area: imageOne;
}

.headerRight img:nth-of-type(2) {
  grid-area: imageTwo;
}
<div class="layout">
  <div class="header">
    <div class="headerRight">
      <img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" />
      <img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" />
      <p>Ola</p>
      <p>Ola</p>
    </div>
  </div>
</div>

Check out the JS Fiddle demo here.

For more information:

Answer №2

When working with grid, you have the option to use align-items:end on the header to align your elements to the bottom. For more information, check out this link.

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

What is the process for extracting HTML code from a website and converting it into a string for use in VB?

Storing the page source in the variable HTML is known to me. Dim Client As New WebClient URL = Console.Readline HTML = Client.DownloadString(New Uri(URL)) However, it has been observed that not all elements from a website are saved using this method. For ...

Certain iFrame instances are unable to display specific cross-domain pages

I'm currently working on a project to create a website that can embed external cross-domain sites, essentially like building a browser within a browser. I've been experimenting with using iFrames for this purpose: While it works for some pages, ...

What could be causing Media-Query to fail when using the max-width media feature?

I recently added a max-width media feature to my code, but for some reason, the colors aren't changing when the width is less than 300px. I've been trying to inspect elements on my laptop to troubleshoot the issue. Additionally, I've made su ...

Having trouble with Vue.js implementation of Bootstrap tab navigation?

I am currently working on a vue.js application that consists of 2 routed components. I recently attempted to integrate a bootstrap tab navigation into the first component, but unfortunately, the tab contents are not being properly displayed. <templat ...

Is there a way to add a border around a div that extends beyond the div, similar to what

I'm attempting to create a border around this section, as seen in the image. However, I'm unsure of how to move it a few pixels outside of the main box. Here is what I've tried so far, but it's not quite achieving the desired result. H ...

Changing Images with Button Click in Javascript

I am facing an issue with my buttons that should swap images once clicked. The first two buttons work perfectly, but for the third and fourth buttons, the images do not disappear when clicking another button. Below is the current code in the document head ...

Creating responsive images with Bootstrap 5

1 I am currently using Bootstrap 5 to develop a website and encountering an issue with getting images to move above text in the feature and mission sections on mobile or tablet screens. Previously, I was able to achieve this effect using the following cod ...

Is there a method to categorize distinct "continuing" numbered list items in order to show that they are interconnected?

There is HTML code that I am currently dealing with, and it looks like this: <ol> <li>...</li> </ol> ... content <ol start="2"> <li>...</li> </ol> ... more content <ol start="3"> <li&g ...

Tips for successfully implementing a basic JQuery code in Internet Explorer

Having trouble getting this to work on my website, especially in Internet Explorer. Check it out here: http://jsfiddle.net/b43hj/24/ Any tips on how to make it compatible with all browsers? Thank you edit - I've only used the code from the jsfidd ...

What is the best way to incorporate a background image in a Bootstrap tooltip?

I'm having trouble displaying an element with a background-image property within a Bootstrap tooltip. The image is not showing up as expected. <div class="big-panel"> <a href="#" data-bs-toggle="tooltip" data ...

Having trouble locating the code for adding a hover effect to the FontAwesome icon

In regards to my portfolio located here: My Portfolio When hovering over the "Marketing Automation" section, a small fa fa-tablet icon appears to the left which turns blue when hovered over. I would like for this icon to remain white, rather than changing ...

The tag that is mandatory is not functioning properly in the Bootstrap form

I am currently working on integrating a Bootstrap form into my Flask website, and I am facing an issue with making certain fields mandatory before submission. I have tried using the "required" attribute in the input tags, but for some reason, it is not wor ...

Searching for mobile WiFi preferences through a web browserHere are the steps to

Is there a method to access mobile connectivity settings through a website? I am interested in determining the connection type (3G\4G\WiFi) and if it is WiFi, identifying the security method being used. Is this achievable? If so, how? Edit: If ...

Utilize grids to ensure consistency in the width of every <li> element across the browser

Is there a way to make the ul element span the entire width of the webpage regardless of window size? http://jsfiddle.net/32hp1w5p/ ul { grid-column: 1/13; list-style-type: none; display: table; margin: 0 auto; } li { float: left; border: ...

Unable to align the row in the center

I'm having trouble centering a row with 4 columns. It seems off when I look at the picture below, specifically at the vertical border under the "most read" blue square. html file: <div class="block-section"> <div class="sm-section-wrapper" ...

Is the HTML encoded character displaying strangely due to spacing issues?

I'm having trouble creating a link with a right chevron that has a larger font size. The issue I'm facing is that the right chevron appears to have excessive top margin, causing a significant gap between it and the line above. Additionally, I wa ...

What is the best way to align text alongside icons using ng-bootstrap in Angular 8?

I have a set of four icons positioned next to each other, but I want them to be evenly spaced apart. I tried using the justify-content-between class, but it didn't work. How can I achieve this? I'm creating a Progressive Web App (PWA) for mobile ...

I am looking for guidance on how to convert a FlexDashboard file into an HTML format. Can anyone provide instructions

I recently created a FlexDashboard for someone else and they've requested it in HTML format to incorporate it into their web system like WordPress. Since I don't have access to their WordPress system, I need to provide them with an HTML file inst ...

Responsive Bootstrap 4 navigation bar featuring collapsible menu and various elements displayed on the right side

I have been struggling for quite some time to create a mobile menu using Bootstrap 4. My goal is to display the 3 brand names and the toggle button on the same row in the mobile menu. However, when I click the toggle button, the 3 brands end up below the c ...

DIV collapsing in reverse order from the bottom to the top

Looking for some help on how to create two links that, when clicked, will scroll a hidden <div> up to fill the full height of its parent container. I've been having trouble with the element using the full height of the <BODY> instead. ...