Having difficulty positioning text within a div with a fixed height

I'm facing a challenge in aligning text at the center of a div. My goal is to horizontally center it, but position it slightly higher than the vertical center. The div has a fixed height of 45%. How can I achieve this layout?

.galf {
width : 100%;
height : 45.3%;
}
.galf a{

}

 <div class = "galf">
 <a>Hello</a>
 </div>

Answer №1

If you're looking to center align the text within a div horizontally, without affecting vertical alignment, here's a solution for you:

To horizontally center align the text, you can use the following HTML and CSS code:

HTML Code:-

<div class = "galf">
  <a>Hello</a>
</div>

CSS Code:-
.galf {
  width : 100%;
  height : 45.3%;
  border: 1px solid;
  text-align:center;
}

You can view the demo on JSFiddle by clicking on the following link:

https://jsfiddle.net/y59o0rjc/

If you also want to vertically center align the text within the div, you just need to make a small modification in the CSS as shown below:

.galf {
  width : 100%;
  height : 45.3%;
  border: 1px solid;
  text-align:center;
  display: table;
}

.galf a {
  display: table-cell;
  vertical-align: middle;
}

Answer №2

give this a shot:

<html>
<head>
    <style>
        .galf {
        width : 100%;
        height : 45.3%;
        text-align: center;
        }
    </style>
</head>
<body>
    <div class = "galf">
        <a>Hey there</a>
    </div>
</body>

Answer №3

Flexbox is the way to go for centering your text both vertically and horizontally.

.container {
  height: 400px;
}
.galf {
  width: 100%;
  height: 45.3%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: red;
  background: black
}

.item {
    height: 30%;
    color: black;
    background: red;
    width: 30%;
    padding: 3%;
    border: 1px solid black;
    text-align: center;
}

.item-container
{
  display:flex;
  
}
<div class="container">
  <div class="galf">
    <a>Hello</a> 
  </div>
  <div class="item-container">
  <div class="item">
    First Div
  </div>
  <div class="item">
    Second Div
  </div>
  <div class="item">
    Third Div
  </div>
  </div>
</div>

Answer №4

If you want to horizontally align it:

.container {
width : 100%;
height : 60%;
text-align: center;
}

 <div class = "container">
 <a>Greetings</a>
 </div>

If your goal is to center it vertically

.container {
width : 100%;
height : 60%;
line-height: 60%;
}

 <div class = "container">
 <a>Greetings</a>
 </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

Red X appearing in IE9 for HTML5 Video

It's strange that my video works on some browsers but not others, even though I'm using mp4, ogv, and webm formats. Even weirder is that some videos work while others don't. To illustrate the issue, I've created a jsfiddle for you to c ...

I am having trouble getting my divs to show up side by side in one line

I am struggling to get the second div (right-column) to move next to the first div (left-column) in order to create 2 columns of links. I would prefer not to create a separate stylesheet for this small page. <div class="container" style="height: 700px; ...

Validating date of birth searches using AngularJS

When searching for a date of birth in the frontend using AngularJS, the request is being sent with a different date of birth to the backend in Spring. I am sending the date of birth in the following format: Sun Aug 15 1999 00:00:00 GMT+0530 (India Standa ...

Modifying CSS dynamically is not possible by using either .addClass() or .css() methods

I'm working with a table that has rows categorized by different classes based on their color. When a button is clicked, I want the row's background color to change instantly. I've attempted using both the .addClass() and .css() methods witho ...

No DIV elements are permitted within the tab content

I implemented a simple jQuery tab code that I found online to display dynamic content: This is the code snippet that I inserted into the HEAD section of my HTML page: <script src="js/jquery-1-9-1.js"></script> <script type="text/javascri ...

Safari browser is experiencing issues with CSS positioning

I am currently working on a website where I am using CSS to position a specific element on the page. The element is absolutely positioned and contained within a relatively positioned parent element. While it displays correctly in Firefox and IE, there seem ...

Issue with Jquery - navigation menu scrolling

I'm struggling to understand why the second gallery isn't scrolling like the first one Check it out here: Here's the jQuery code that's responsible for making it work: $(function(){ var state = 0; var maxState = 7; var winWidth = $(&a ...

Issue with CSS Dropdown Menu Functionality on Internet Explorer versions 7, 8, and 9

My CSS dropdown menu works perfectly in Mozilla, Chrome, and Safari. However, it's a bit finicky in IE10 (still works) and doesn't work at all in IE9. I've tried to troubleshoot the issue, as there are websites with CSS dropdown menus that ...

Easily move a group of HTML elements at the same time with a simple

Exploring HTML5 Drag and Drop with Multiple Elements When it comes to dragging and dropping multiple elements in HTML5, there seems to be a limitation with the default draggable attribute. This attribute allows only one element to be dragged at a time, ma ...

Unlock the innerHTML of a DIV by clicking a button with ng-click in Angular JS

I am curious about something. There is a <div> and a <button> in my code. I am looking to access the inner markup of the <div> within the ng-click function without relying on jQuery. Can you give me some guidance? <div id = text-entry ...

The color of the Angular md-switch .md-thumb is not showing up properly when switching the toggle

In Safari, the md-switch displays a yellow section on the md-thumb when toggled. However, other browsers handle the switch without any issues. The md-bar appears fine, but the md-thumb is yellow. I have tried setting everything to the blue color I am using ...

Issue with the navbar toggler not displaying the list items

When the screen is minimized, the toggle button appears. However, clicking it does not reveal the contents of the navbar on a small screen. I have tried loading jQuery before the bootstrap JS file as suggested by many, but it still doesn't work. Can s ...

Organize three image links side by side using HTML layout

Hello there! I'm currently working on a website project and I'm struggling with arranging some image links in a specific layout. The grey rectangles you see represent where the images should be placed, all of them are uniform in size (275 x 186) ...

Can you explain the mechanics behind the animation of the upvote button on steemit.com?

Behold the upvote button of steemit.com: <span class="Icon chevron-up-circle" style="display: inline-block; width: 1.12rem; height: 1.12rem;"> <svg enable-background="new 0 0 33 33" version="1.1" viewBox="0 0 33 33" xml:space="preserve" xmlns=" ...

Overflow of dropdown menus in HTML CSS

Hello, I am new to both html and stack overflow. Please forgive me if this question has already been asked, as I couldn't find anything (but maybe I didn't search enough?). I have tried using overflow and clear properties, but I am struggling to ...

What is the best way to create titles with a background?

My goal is to have a title overlay an image with specific width and the text displayed in blocks. To better illustrate, here's an example: I prefer to achieve this effect using CSS; however, I am open to utilizing Javascript if needed. ...

Adjustable diagonal line within a container using CSS styling

Seeking assistance with a rectangular div that can have its diagonal length adjusted using JavaScript. I am looking to add a diagonal line to label the diagonal length when it is increased or decreased using the +/- buttons. I require the line to resize a ...

Simple filling out of a dropdown menu form

Currently attempting to populate a drop-down select menu from a table named "Cars" using the information in the "VIN" column. However, for some reason I am not seeing any data displayed. <form> <label>VIN:</label> <select name ...

CSS - vertical and horizontal lines vary in thickness depending on the design

I am working on creating a grid that is automatically generated in my Angular 2 application. Currently, it consists of 1px wide divs as vertical lines and 1px high divs as horizontal lines. However, there seems to be an issue with inconsistent thickness be ...

Customizing WordPress using CSS overrides in the Pinboard theme

I'm currently customizing a WordPress theme for my website (pinboard theme) and so far, it's been an amazing experience. One of the features of this theme is a built-in slider/carousel. However, I am looking to change the title of the slider from ...