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

Asynchronous AJAX request using JQuery loader with async:false setting

As a newbie to the world of jQuery, I find myself lost in solving my current issue. Despite checking various solutions to similar problems, none seem to work for me. Working on MAC OS X using Safari with JQuery 2.1.1, I must ensure that my platform is comp ...

Scroll the content of a div to the bottom using JavaScript

I'm facing a situation with my code: function scrollme(){ dh=document.body.scrollHeight ch=document.body.clientHeight if(dh>ch){ moveme=dh-ch window.scrollTo(0,moveme) } } However, I am looking for a way to make it scroll only within a specific d ...

The Markup() function in Flask is used to generate markup content that is not enclosed within

Before I explain my issue, let me go ahead and share the code with you. Using Jinja in HTML <p>{{ item }}</p> 'Flask' item = Markup('<ul><li>list item 1</li><li>list item 2</li></ul>') ...

Clicking the delete button in Firebase to remove an item

I am in the process of developing a simple CRUD application and have opted for Firebase as my backend solution. While I have successfully implemented the create and read functionalities, I've hit a roadblock with the delete operation. When attempti ...

The issue with displaying long text in HTML select options in Internet Explorer 9

Having trouble with an HTML select element in IE9 when in compatibility view with IE7 standards. <select ...> <option title="test1">test1</option> <option title="test2">test2</option> <option title="longer text.. ...

Why am I unable to choose an element by its Id?

My goal is to pass an ID as an argument to a function in the following manner: HTML <button type="button" class="like btn" onclick="like('<%=postid%>')"> <svg id ='<%=likei ...

Upon clicking the 'Add Image' button, TINYMCE dynamically incorporates an input

I am in search of creative solutions to address an issue I'm facing. Currently, I am utilizing TINYMCE to incorporate text into my webpage. However, I would like to enhance this functionality by having a feature that allows me to add an image along w ...

Move the text to the following line if a horizontal scroll bar is visible using JavaScript/JQuery and CSS styling

I have a section with multiple div elements...how can I ensure that when there is horizontal scrolling in the section, the hidden divs shift to the next line? HTML <section id="a"> <div class="num"> <div class="num"> <div class="num" ...

Bringing in LESS variables to Rails 3

In my assets folder, I have a global.less file where I have defined various site-wide variables. Currently, in order to use these variables in other less files, I have to add this line at the beginning of each file: @import 'global'; While thi ...

Is Jquery Mobile's Table lacking responsiveness?

I have implemented a basic table from the jQuery Mobile website on my page. Take a look at the HTML code below: <div data-role="page" id="mainPage"> <div data-role="content> <table data-role="table" id="my-table" da ...

The banner may be cropped when viewing on lower resolutions such as mobile devices or when zoomed in

I've encountered a similar issue on a popular website, www.godaddy.com. When zooming in or accessing the site from a mobile device, the right side of the banner gets cut off. However, when viewed on a full-sized computer screen, there are no issues. M ...

Similar to the :has() in jQuery, the equivalent in CSS

Consider the code snippet below: <div class="section"> <div class="row">...</div> <div class="row"> <!-- bottom margin here needs to be 0 --> <div class="section"> &l ...

The absence of color attribute in CSS serves as a safeguard against overriding

Issue Update: Upon pushing the application to the development server, ASP includes injected styles that are overriding the necessary custom styles. One specific example is a wrapper div with an 'a' tag that overrides all styled 'a' tags ...

Adding complex JSON format to an HTML table involves formatting the data correctly and then using

Utilizing AJAX, I fetched a JSON response and am now looking to map the JSON data into an HTML table structured like this: { "records": [{ "type_id": 000001, "type_desc": "AAAAAA", "type_createby": "Adam" }, { "type ...

Animating changes on a webpage using CSS transitions in conjunction with the

Can the border color of a simple line div be changed from top to bottom using GreenSock? .line { position: relative; border: 1px solid #c3ced8; width: 0; height: 50px; display: block; content: ''; z-index: 1; top: -19px; marg ...

Setting the z-index for a JavaScript plugin

I need help with embedding the LinkedIn plugin into a website that has stacked images using z-index for layout. I have tried assigning the plugin to a CSS class with a z-index and position properties, but it hasn't worked as expected. Can anyone sugge ...

Ways to Adjust the Earth's Position in WebGL Earth

I have been working with this WebGL Earth component for my project, but I am facing difficulty in adjusting the position of the planet on the canvas. My intention was to place the planet at the bottom of the page, but I haven't been able to achieve th ...

Tips for adjusting UI with Media queries based on screen dimensions

Looking for a better solution I currently have 5 sets of items. Button Text Dropdown Pagination Icon For larger screen sizes, specifically @media (min-width: 990px) {} I would like all items to be in a single row, like so: [button][Text][Dropdown][Pagi ...

The selected image should change its border color, while clicking on another image within the same div should deselect the previous image

I could really use some assistance! I've been working on Angular8 and I came across an image that shows how all the div elements are being selected when clicking on an image. Instead of just removing the border effect from the previous image, it only ...

Encountered an issue retrieving two rows nested within a parent row using Bootstrap 4 and VueJs

Currently, I am working on a VueJs and Bootstrap4 component where my aim is to achieve a design similar to the one shown using the available bootstrap classes. After going through the documentation, I came across the customized classes h-75 and h-25 provi ...