Aligning a block heading in the middle of the page

I developed a basic flex-based table.

section {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  margin: 0;
  background-color: green;
}

h2, ul {
  width: 50%;
  padding-left: 1.5em;
  padding-right: 1.5em;
}

h2 {
  text-align: right;
  vertical-align: middle;
}
  
h2 {
  background-color: blue;
}

ul {
   background-color: yellow;
}
<section>
  <h2>Heading</h2>

  <ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    <li>baz</li>
    <li>baz</li>
  </ul>
</section>

Initially, the h2 and ul elements do not occupy the full height of the section despite functioning this way in my local project. Why is this discrepancy?

The crux of the issue lies in vertically centering the text within the h2 block to fill the entire section. I intend to modify the background colors of the left and right cells distinctly. Even after setting the vertical-align property, it fails to achieve the desired effect. What could be causing this problem?

Answer №1

To achieve this effect, utilize the display: flex property on h2 and then apply align-items: center, along with justify-content: flex-end for right text alignment. Additionally, consider removing margins to ensure consistent item heights.

section {
  display: flex;
  margin: 0;
  background-color: green;
}
h2,
ul {
  flex: 1;
  padding-left: 1.5em;
  padding-right: 1.5em;
  margin: 0;
}
h2 {
  background-color: blue;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  color: white;
}
ul {
  background-color: yellow;
}
<section>
  <h2>Heading</h2>

  <ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    <li>baz</li>
    <li>baz</li>
  </ul>
</section>

Answer №2

Utilize flex align (-items, -self) to achieve vertical alignment

section {
  align-items: center;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  margin: 0;
  background-color: green;
}

h2, ul {
  width: 50%;
  padding-left: 1.5em;
  padding-right: 1.5em;
}

h2 {
  text-align: right;
  align-self: center;
}
  
h2 {
  background-color: blue;
}

ul {
   background-color: yellow;
}
<section>
  <h2>Heading</h2>

  <ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    <li>baz</li>
    <li>baz</li>
  </ul>
</section>

Answer №3

We should specify the line spacing

Set the line-height to 100px; or any other value you prefer for h2 elements

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

Utilizing v-if and splice on users to select items from v-model

After following a tutorial, I have the code snippet below that I would like to enhance: <div style="margin-top: 10px;"> v-for="task in taskItems" :key="task.id" <q-icon :name="task.icon"/> <div ...

Adaptable website design featuring dynamic data grid

I've spent quite some time searching for a way to achieve the layout displayed in the linked image, but I haven't been successful in finding exactly what I need. My goal is to create a responsive layout that includes a datagrid (ui-grid) within ...

"Can you guide me on how to add a background pattern image to an HTML tooltip arrow

I've been working on a customized jQuery plugin for tooltips. The tooltip box appears when the mouse hovers over a specific element, complete with an arrow pointing towards the base element. My goal is to apply a repeating texture image to the entire ...

Is it possible to incorporate HTML tags within the <template> section of AIML files?

My goal is to create a simple bot using an AIML file. The idea is that I can input a question and the corresponding pattern will be returned from the file. For instance, I have this sample pattern in my AIML file: <category> <pattern>TEST& ...

Assistance needed with dynamically resizing a background image

Is there a way to automatically adjust the size of a background image for an element? For instance, I want my links in HTML to have a background image with slanted borders and rounded corners. Usually, you would set the width of the anchor element to fit t ...

Switch up text using jQuery on css-tricks.com

I am having trouble with the code I found on a page about toggling text using jQuery from this link. Could someone please explain to me the significance of ':visible'? Thank you. Here is the fiddle link, and below is the code that I copied. Vi ...

Generate a series of buttons with generic identifiers that can be easily targeted using getElementById. The IDs will be dynamically assigned

I am looking to dynamically generate unique IDs for a list of buttons, allowing me to easily target specific buttons using getElementById. Here is an example code snippet where each button has the same ID: @foreach (var cat in Model) { <div clas ...

Is there a way to deactivate the <script> tag using CSS specifically for media queries?

When designing a website exclusively for desktop usage, I encountered the issue of it not being viewable on mobile devices. I attempted to address this problem by utilizing the code below: script { display: none; pointer-events: none; } Unfortunat ...

Experiencing difficulties with setting the width of owl carousel

Here is the HTML code snippet: <div class="row"> <div class="col-lg-7 col-sm-6"> <div class="owl-carousel" id="homeCarousel"> <div class="item"> <img src="images/brela.jpg" alt="brela makarska"> </d ...

Color overlay on top of image background

I'm struggling to create a striped colored background with a narrow center single color background on top for showcasing articles and photos. However, I am unable to get the colored background to display correctly. Despite trying different approaches ...

Adding an id to a ul tag using JavaScript

I am trying to dynamically add an ID called "myMenu" using JavaScript to a ul element for a search filter. Unfortunately, I am unable to directly access the ul tag to change it, so I need to do it via JavaScript. As I am new to JavaScript, I am looking t ...

Unable to execute script tag on PHP page loading

When I make an AJAX request to fetch JavaScript wrapped in <script> tags that needs to be inserted on the current page, how can I ensure that the code executes upon insertion? Here's the snippet I'm using to add the code: function display ...

Learn how to toggle the visibility of three div elements arranged horizontally

$(document).ready(function () { $("#toggle").click(function () { if ($(this).data('name') == 'show') { $("#sidebar").animate({ width: '10%' }).hide() $("#map").an ...

What methods are available to test my website across different browsers such as outdated versions of Internet Explorer like IE8?

Currently, I am working on Chrome with Windows 7 OS installed. However, Windows 7 does not support Internet Explorer 8. This is causing issues when trying to view my web pages in the IE8 version and older. How can I resolve this problem? I have attempted ...

Blackberry Browser 4.2 experiencing spacing troubles

Seeking advice for developing a web application specifically for the Blackberry Browser 4.2. Looking to add vertical spacing between a series of links, but struggling due to lack of support for padding and margin in this version. Have tried using height an ...

What is the equivalent of a very deep selector in TailwindCSS?

When working with the v-deep selector to customize the appearance of tiptap, a rich text editor, accessing the .ProseMirror class in SCSS would look like this: editor-content { // ... styles &::v-deep(.ProseMirror) { // ... styles } } ...

CSS - When using both display: flex and position: absolute, the anchor point is shifted

I have a flexbox element with three children. I want the second child to overlap the first child using absolute positioning, like this: Desired Outcome: https://i.stack.imgur.com/i479w.png It's important that the second child appears on top of the ...

Transforming the DOM using jQuery

I am looking to manipulate the DOM using jQuery. This is the initial source code: <td class="name"><a href="position_details.php?x=-109&amp;y=95">21</a> </td> <td class="name"><a href="position_details.php?x=-109& ...

Tips for modifying the height of a bootstrap-vue table and enabling scrolling with a fixed header

I have a div containing a b-table that I need help adjusting the height for. I want to make the table scrollable with a fixed header. Can anyone assist me? Here is my code: Code inside the template: <div class="tbl-barangay"> <b-table stripe ...

Encountering issues with CSS selectors when using Selenium WebDriver

I am encountering an error with the following code: elem = new Array() elem = driver.findElements(By.CssSelector('input')); What could be causing the issue in the code above? If I have an HTML form like this: <form role="form" method="post ...