What is the best way to vertically align my content in the center?

Is there a way to vertically center all of the text content, especially focusing on the drop-down button and block, here? Despite my efforts to research and find solutions online, I have not been able to achieve the desired visual results.

I am restricted to using percentages for width and height as variables. Fixed padding or margins did not work as they do not look right when the page is resized.

Any advice on how to accomplish this task would be greatly appreciated. I am new to coding and eager to learn! I might be a bit of a noob, but I am trying my best.

I would like to find a clean solution, and if possible, I would also like to horizontally center it, as I am currently using padding for that purpose, which is not perfect. However, since I will have a maximum width, I can settle for that.

.div1 {
Height: 100%;
Width: 100%;
background-color: D4D4D2;
opacity: 0.75
}
.div1:hover {
background-color: red;
opacity: 0.95
}

    .dropbtn {
    background-color: transparent;
    color: black;
    padding: 10px 14px;
    font-size: 20px;
    text-align:center;
    border: none;
    cursor: pointer;
    position: relative;
    min-width: 310px;
    }

    .dropdown {
    position: relative;
    display: inline-block;
    }

    .dropdown-content {
    display: none;
    position: relative;
    background-color: transparent;
    min-width: 310px;
    }

    .dropdown-content a {
    color: black;
    position: relative;
    padding: 12px 16px;
    text-decoration: none;
    text-align:center;
    display: block;
    }

    .dropdown-content a:hover {background-color: none}
    .dropdown-content a:hover {color: white}
    .dropdown:hover .dropdown-content {
    display: block;
    }

    .dropdown:hover .dropbtn {
    background-color: transparent;
    color: white;
    }
</style>

<div class="div1">
<div class="dropdown">
  <h2>Hoverable Dropdown</h2>
    <p>Move the mouse over the button to open the dropdown menu.</p>

    <div class="dropdown">
    <button class="dropbtn">Dropdown</button>
    <div class="dropdown-content">
    <a href="#">Link 1 link very  long</a>
    <a href="#">Link 2 medium</a>
    <a href="#">Link 3</a>
    </div>
</div>
</div>
</div>

Answer №1

Here is a solution that might meet your needs, although it may require some adjustments. I included a height of 100% to the html & body tags and centered the content vertically using the .center-vertical class. This method is commonly used for vertical centering, so it's good to keep in mind for future reference :)

body,
html {
  height: 100%;
}
.div1 {
  Height: 100%;
  Width: 100%;
  background-color: #D4D4D2;
  opacity: 0.75;
}
.div1:hover {
  background-color: red;
  opacity: 0.95;
}
.dropbtn {
  background-color: transparent;
  color: black;
  padding: 2px 14px;
  font-size: 20px;
  text-align: center;
  border: none;
  cursor: pointer;
  position: relative;
  min-width: 310px;
}
.dropdown {
  display: block;
}
.dropdown-content {
  display: none;
  position: relative;
  background-color: transparent;
  min-width: 310px;
}
.dropdown-content a {
  color: black;
  position: relative;
  padding: 2px 16px;
  text-decoration: none;
  text-align: center;
  display: block;
}
.dropdown-content a:hover {
  background-color: none
}
.dropdown-content a:hover {
  color: white
}
.dropdown:hover .dropdown-content {
  display: block;
}
.dropdown:hover .dropbtn {
  background-color: transparent;
  color: white;
}
.center-vertical {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="div1">
  <div class="dropdown ">
    <h2>Hoverable Dropdown</h2>
    <p>Move the mouse over the button to open the dropdown menu.</p>

    <div class="dropdown center-vertical">
      <button class="dropbtn">Dropdown</button>
      <div class="dropdown-content">
        <a href="#">Link 1 link very  long</a>
        <a href="#">Link 2 medium</a>
        <a href="#">Link 3</a>
      </div>
    </div>
  </div>
</div>

Answer №2

When under time pressure, sometimes trial and error is the best approach :D

Despite my limited understanding and time constraints, I persevered and eventually achieved the desired outcome through trial and error. The code may not be perfect or tidy, but hopefully, it can be useful to someone else in the future. Always learning :D

   .div1 {
 Height: 100%;
 Width: 100%;
 background-color: D4D4D2;
 opacity: 0.75
 }

   .div1:hover {
 background-color: red;
 opacity: 0.95
}

    .center-block {
      top:50%;
      left: 50%;
      transform: translate(-50%,-50%);
      position: absolute;
      }

    .dropbtn {
    background-color: transparent;
    color: black;
    padding: 2px 14px;
    font-size: 20px;
    text-align:center;
    border: none;
    cursor: pointer;
    position: relative;
    min-width: 310px;
    }

    .dropdown {
    position: relative;
    display: inline-block;
    }

    .dropdown-content {
    display: none;
    position: relative;
    background-color: transparent;
    min-width: 310px;
    }
 
    .dropdown-content a {
    color: black;
    position: relative;
    padding: 2px 16px;
    text-decoration: none;
    text-align:center;
    display: block;
    }

    .dropdown-content a:hover {background-color: none}
    .dropdown-content a:hover {color: white}
    .dropdown:hover .dropdown-content {
    display: block;
    }

    .dropdown:hover .dropbtn {
    background-color: transparent;
    color: white;
    }
 <div class="div1">
    <div class="center-block">
    <div class="dropdown">
    <button class="dropbtn">Dropdown</button>
    <div class="dropdown-content">
    <a href="#">Link 1 link very  long</a>
    <a href="#">Link 2 medium</a>
    <a href="#">Link 3</a>
    </div>
    </div>
    </div>
    </div>

Answer №3

div{
  width: 350px;
  height: 150px;
  background-color: green;
  color: white;
  margin-bottom: 10px;
  display: flex;
}
#div1{
  justify-content: center;
}
#div2{
  align-items: center;
}
#div3{
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
<div id="div1">
  Horizontally centered
</div>
<div id="div2">
  Vertically centered
</div>
<div id="div3">
  Both Horizontally & Vertically centered
</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

What is the best way to position images and URLs in div elements using CSS?

Currently, I am on a journey to become a Front End Developer through Udacity. As part of my learning process, I have a project that needs to be submitted. Unfortunately, I encountered some issues that I couldn't resolve on my own. Specifically, I&apos ...

"What is the best way to access and extract data from a nested json file on an

I've been struggling with this issue for weeks, scouring the Internet for a solution without success. How can I extract and display the year name and course name from my .json file? Do I need to link career.id and year.id to display career year cours ...

What is the correct way to utilize the submit() function within this specific form?

I have a small registration form that requires validation before submission. In order to achieve this, I've developed a JavaScript function as shown below: var name = document.getElementById('name').value; var company = document.getElem ...

AngularJS integration with Bootstrap Confirmation jQuery plugin

I am currently struggling to implement the Bootstrap-Confirmation plugin with AngularJS. Despite following instructions from this YouTube tutorial, I cannot seem to get the directive to function correctly. A related query on Stack Overflow references a di ...

Is there a way to create a JavaScript-driven search filter that updates automatically?

My website showcases a lineup of League of Legends champion icons, with one example shown below: <div class = "champion"> <p>Aatrox <img class = "face_left" src = "images/small/Aatrox.png"> <div class = "name" onmouseover="if(cha ...

Using Angular and ASP.NET for image uploading functionality

Trying to source the image from the PC and upload it into the database as "Images/aaa.jpg". I am relatively new to Angular and have attempted an example that did not work for me. I have been stuck on this issue for almost 3 days trying to find a solution. ...

Why is the column width on this Tumblr page constantly flickering?

Seeking assistance with a minor issue on my Tumblr page - the content column seems to be flickering on mouseover, shifting between full width and its original programmed width. Any insight on the CSS causing this? Much appreciated! Check it out here (Dis ...

What is the best way to apply a background-image to a DIV while also implementing a double line border to prevent the image from showing through the border?

Within my DIV, I have applied a background-image and added a double border to the DIV. The image is currently visible in between the lines. Is there a method to resolve this issue and prevent the image from displaying in this manner? ...

What is the best method for incorporating a YouTube embedded video into an image carousel using HTML and CSS?

I'm encountering an issue with my product carousel that includes an image and a video. The image displays correctly, but when I attempt to click on the video to have it appear in the main carousel view, it doesn't function as expected. Here is a ...

Saving information to a hidden div using jStorage

Currently, I am utilizing jStorage for storing data in local storage. When I store the data using console.log() and later retrieve it using $.jStorage.get(), I found that the values are not being assigned to the hidden div. Can someone provide guidance o ...

Deleting images based on their class through a loop

Currently, I am in the process of constructing a carousel using data retrieved from an endpoint. The challenge I face is determining the appropriate image size to request from the server. To address this, I perform some front-end processing to dynamically ...

Setting character limits within textareas based on CSS classes in a dynamic manner

I'm looking to develop a JavaScript function that can set text limits for multiple textareas at once using a class, allowing for flexibility in case specific textareas need to be exempt. However, I'm facing issues with my debuggers - Visual Studi ...

Height of the div dynamically increases upwards

Just a quick question - is there a way to make position: fixed divs at the bottom of an element (such as the body) grow upwards as content is dynamically added? Maybe something like anchor: bottom or increase: up? I'm thinking that using JavaScript m ...

The image fails to load once the AI-generated image is complete and the response process has concluded

After a long hiatus from development, I am diving back in and acknowledging the rustiness of my skills. Any syntax or formatting errors encountered are preemptively apologized for. Currently, I am immersed in a small web app project that involves taking a ...

How can I efficiently verify page references using tools like CSS, JavaScript, and more?

I have Firebug (my team lacks Firefox) and the IE developer toolbar (IE7), but I'm struggling to easily validate if the referenced files in a page are loading. I see Javascript errors, but they don't lead me directly to the exact file in the hier ...

`Arranging Widget Boxes in a Vertical Layout`

I am currently displaying each record with two boxes for Afternoon and Night horizontally: https://i.stack.imgur.com/JMKug.png This is the code structure I am using to achieve this layout: <div id="record_box"> <div class="row" style="paddi ...

Merge floating and absolute positioning techniques

Creating a calendar exhibiting all events in one div requires precise positioning based on the values of top and height. Let me demonstrate this concept. In the image below, both 6am events are aligned vertically. This alignment issue can be resolved by u ...

Executing a Python script within an HTML page using Django and extracting a variable from an input form

I have created a sentiment analysis Python script, but now I want to integrate it with an HTML page. The goal is to take input from the HTML form, process it using the Python script, and then display the results back on the HTML page. Currently, I am usin ...

Is there a way to set it up so that clicking on a small image will automatically switch to the corresponding larger size image?

For instance, I have 4 small images and 1 main image. When I click on a small image, the main image changes to display that selected picture. I was wondering if there is a specific value to place inside target=""? I prefer to only use HTML/CSS as I am no ...

Shift the position of an <img> downwards by 50% of its height while maintaining the flow of the elements

Consider this snippet of code below <div> <img src="http://www.lorempixel/100/200" /> <p> Bla bla bla bla </p> </div> I am trying to figure out how to move the image 50% of its height and also adjust the ...