What is the best way to align text in the center of a div?

I just created this dropdown menu, but I am encountering an issue.

Whenever I resize the div or h4 element, the text ends up at the top. Even after trying to solve it with text-align: center;, the problem persists.

Here is a visual representation of what I am aiming for:

See Imgur image

Below is the CSS code used for styling:


#alue_search {
  box-sizing: border-box;
  background-image: url('https://cbot.me/images/alue_icon.png');
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  color: #353535;
  border-radius: 5px 5px 0px 0px;
}

#alue_search:focus {
  outline: none;
}

.dropdown {
  display: inline-block;
  position: relative;
  background-color: #f6f6f6;
  min-width: 230px;
  z-index: 1;
  border-radius: 5px;
  border: 3px solid #ddd;
}

.dropdown-content {
  position: relative;
  z-index: 1;
}

.dropdown-content h4 {
  height: 35px;
  background-color: #fff;
  border: none;
  border-radius: 5px;
  color: #353535;
  transition: all 0.3s ease-in-out;
  display: none;
}

.dropdown-content h4:hover {
  background-color: #ddd;
  cursor: pointer;
}

.dropdown-content div {
  height: auto;
}

.show {display:block;}

If you require the HTML part as well, here it is:

<div class="dropdown">
      <div id="myDropdown" class="dropdown-content">
        <input type="text" placeholder="Search area" id="alue_search" onkeyup="filterFunction()">
        <div onclick="select('Helsinki')"><h4>Helsinki</h4></div>
        <div onclick="select('Jyväskylä')"><h4>Jyväskylä</h4></div>
        <div onclick="select('Kuopio')"><h4>Kuopio</h4></div>
      </div>
</div>

Answer №1

The simplest way to achieve this is by using the line-height property on your h4 element to vertically center the text.

.dropdown-content h4 {
    line-height: 35px;  // Centering vertically
    text-align: center; // Centering horizontally

    // Additional styling goes here
}

https://i.stack.imgur.com/yCOcM.png

Answer №2

To make it both vertically and horizontally centered, use the following CSS:

.dropdown-content h4 {
  height: 35px;
  ...
  text-align:center;
  line-height:35px;
}

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

Unable to logout with ExpressJS passport-local

After pasting the passport-local app into my own, I noticed something interesting: I managed to successfully log in users, but for some reason, I can't seem to get them logged out. app.get('/logout', function(req, res){ req.logout(); r ...

Guide on adjusting padding for jQuery Flot graph for improved styling

I am utilizing jQuery Flot to generate a basic graph. I do not want the axes to display on either side, but this is causing the points to be slightly cut off. Any suggestions on how to resolve this issue? $.plot('#'+id, [ { data: data.values ...

Having trouble getting PostCSS nesting to work with CSS variables in Tailwind CSS and Next.js?

I've been attempting to utilize PostCSS nesting alongside CSS variables, but unfortunately the CSS variables are not being converted at all. Instead, I am seeing Invalid property value in the DOM for CSS Variables. The tailwind.css file I have inclu ...

Do you need assistance with downloading files and disconnecting from clients?

When looking at the code snippet below: async function (req, res, next) { const fd = await fs.open("myfile.txt") fs.createReadStream(null, { fd, autoClose: false }) .on('error', next) .on('end', () => fs.close(fd)) . ...

Transitioning from using a jQuery get request to utilizing Vue.js

Looking to transition from JQuery-based js to Vue as a newcomer to JavaScript. Seeking guidance on making a get request and displaying the retrieved data. What approach would you recommend for this task? Here's the HTML snippet: <div> <di ...

Removing other objects with Mongoose after an update

I'm facing an issue with my update query in mongoose. I can't figure out why other objects are getting deleted when I only intend to update one specific object. The code is functioning correctly in terms of updating, but it's causing all the ...

Tips for defining types for specific CSS properties in TypeScript, such as variables

Perhaps there are already solutions out there, and I appreciate it if you can share a link to an existing thread. Nevertheless... In React, when I use the style prop, I receive good autocompletion and validation features like this example: What am I look ...

What is the function of the 'this' keyword within a Vue component?

Can someone explain how the keyword 'this' is able to access tasks that are not within the same object? anyobject = { template: '#task-list', props: { tasks: {default: []} }, data() { return { newTask: '&apos ...

Please request user input in order to generate a multiplication chart

Is there a way to ensure that the program works properly so that when the user inputs a value, it is included in the multiplication table? <html> <head> <title> Multiplication Table </title> <style> body{ font-family: aria ...

Tips for preventing the use of eval when invoking various functions

Here's a snippet of code I've been using that involves the use of eval. I opted for this approach as it seemed like the most straightforward way to trigger various factory functions, each responsible for different web service calls. I'm awa ...

Modifying Data in Another Component in VueJS

I have a classic Vue Component structured like the following: Vue.component('bar', { template: `<div class="bar"></div>`, data () { return { blocks: [ ] ...

Is it possible to make a form field inactive based on another input?

I need help with disabling certain form fields until other fields are filled in. You can check out an example of what I'm trying to achieve here: https://jsfiddle.net/fk8wLvbp/ <!-- Bootstrap docs: https://getbootstrap.com/docs --> <div ...

Issue with Component: Data is not being injected into controller from ui-router resolve, resulting in undefined data

Encountering an issue with resolve when using a component and ui-router: the data returned after resolving the promise is displaying as "undefined" in the controller. Service: class userService { constructor ($http, ConfigService, authService) { th ...

Angular.js: Navigating through HTML content

I am attempting to display a list of HTML data using angular.js and would like to implement ngInfiniteScroll. Here is the template I created: update: <div class="scroll" id="antTalkList" talk-type="total" view-type="total" infinite-scroll=' ...

Unraveling a date field from JSON with AngularJS filtering

Extracting data from JSON file shows /Date(1435837792000+0000)/ How can I format the date to appear as Oct 29, 2010 9:10:23 AM? ...

Is it possible to utilize a controller within a different controller?

As I work on developing an application, I am faced with the challenge of using controller functions within another controller function. Is it possible to achieve this or not? Use-case: My goal is to verify in the User collection (using mongoDB) if a user ...

Incorporating custom CSS and HTML into the link is essential for enhancing the appearance of KnpMenu

I'm working with the KnpMenuBundle and I need to customize a link that has the route 'uri'=>'#'. How can I achieve this? The desired link should be styled like this: <a href="#" class="js-sub-menu-toggle"> <s ...

Retrieve the id of the clicked hyperlink and then send it to JQuery

<a class = "link" href="#" id = "one"> <div class="hidden_content" id = "secret_one" style = "display: none;"> <p>This information is confidential</p> </div> <a class = "link" href="#" id = "two" style = "display: non ...

What is the process through which React form elements receive the event parameter?

I'm currently working on a form component that looks like this: import React from 'react'; class CommentBox extends React.Component { constructor(props) { super(props); this.state = { value: '' } this.han ...

How can I achieve the quickest image loading speed with JavaScript?

If I have a large ecommerce website with 15,000 image elements that need to be added to the HTML, what is the best approach using JavaScript to optimize efficiency and enhance user experience? ...