What is the best way to perfectly center my CSS menu in a fluid style layout?

Can you assist me with this issue?

I am trying to center my CSS menu precisely in the middle of the user's screen, both vertically and horizontally. Since users have varying screen resolutions, I need a fluid example to achieve this.

Below is the HTML markup and CSS code in question:

<!DOCTYPE html>
    
    <html>
    
    <head>
    
    <style type="text/css">
    * {
    font-family: Verdana,Arial,sans-serif;
    font-size: 9pt;
    }
    body {
    background: rgb(105,105,105);
    }
    </style>
    
    <style type="text/css">
    .tabs {
      position: relative;   
      min-height: 292px; /* This part sucks */
      clear: both;
      margin: 25px 0;
    color:#7c7766;
    }
    .tab {
      float: left;
    }
    .tab label {
      background: rgb(105,105,105);
      padding: 10px; 
      border: 0px solid #787265; 
      margin-left: -1px; 
      position: relative;
      left: 1px;
      color: #FFF;
    }
    
    .tab label:hover {
      background: #d7d3c3; 
      padding: 10px; 
      border: 0px solid #787265; 
      margin-left: -1px; 
      position: relative;
      left: 1px; 
      cursor: pointer;
    }
    
    .tab [type=radio] {
      display: none;   
    }
    .content {
      position: absolute;
      top: 25px;
      left: 0;
      background: #f5f1e6;
      right: 0;
      bottom: 0;
      padding: 20px;
      border: 0px solid #787265; 
    }
    [type=radio]:checked ~ label {
      background: #f5f1e6;
      border-bottom: 0px solid white;
      z-index: 2;
      color: #000;
    }
    [type=radio]:checked ~ label ~ .content {
      z-index: 1;
    }
    </style>
    
    <body>
    
    <div class="tabs">
        
       <div class="tab">
           <input type="radio" id="tab-1" name="tab-group-1" checked>
           <label for="tab-1">Tab One</label>
           
           <div class="content">
               stuff 1
           </div> 
       </div>
        
       <div class="tab">
           <input type="radio" id="tab-2" name="tab-group-1">
           <label for="tab-2">Tab Two</label>
           
           <div class="content">
               stuff 2
           </div> 
       </div>
        
        <div class="tab">
           <input type="radio" id="tab-3" name="tab-group-1">
           <label for="tab-3">Tab Three</label>
         
           <div class="content">
               stuff 3
           </div> 
       </div>
        
    </div>
    
    </body>
    
    </html>

Answer №1

To center an element using CSS, you can use the following code:

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

Another option is to wrap the element in a parent container to avoid changing existing markup:

<div class="wrapper">
    <div class="tabs">
        <!-- Your content here -->
    </div>
</div>


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

Answer №2

Here is a suggestion for centering content by adding width...

 <div class="content" style="width:700px;margin-top:20%;margin-left:auto;margin-right:auto;">
  ...
  </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

Angular17+ introduces a new feature called the Dynamic Tag Class, providing

Why isn't my navigation bar updating when the page changes? Here is my app.component.html code: <html> <app-navbar></app-navbar> <router-outlet></router-outlet> </html> This is where my navbar.component.html s ...

What is the best way to make my input tags move downwards when resizing the screen?

I need help with a grid layout. My goal is to have the input tags (marked in blue) shift below when resizing the browser or viewing on a mobile or tablet device, while keeping the other three tags on the same line. I attempted to position the small box to ...

Is your navigation bar failing to extend to the entire width of the page

Click Here for the Host Folder If you follow the link and then click on "all.html," you will come across a page with an incomplete navbar. The li elements in the list appear to be stacking oddly, causing "Member Resources" and others to show up in a row b ...

Position an image on the top left corner of a div's border

I have a situation where I'm trying to overlay a small square transparent image on the top left border of a div that has a 1px border. The image is 48px by 48px in size and I want it to give the illusion that it's going underneath the top and lef ...

Creating a user authentication portal

Looking for advice on creating a basic front end HTML login. I'm new to coding and would appreciate any suggestions! Additionally, how can I link the login system to my database? ...

The Bootstrap card is elegantly framed by a crisp white border surrounding the

After referencing a tutorial, I implemented the following code: <div class="card" style="width:400px"> <img class="card-img-top" src="image.png" alt="Card image"> <div class="card-body"> <h4 class="card-title">John Doe</ ...

What is the complete pathway in CSS?

Consider this scenario: within my CSS file, there is a line of code used to display an image - specifically, a label icon located in the sidebar. background-image:url(../assets/images/icons/arrow_state_grey_expanded.png); However, due to its relative pat ...

Effective Strategies for Preserving Form Input Values during Validation Failure in Spring MVC

I am currently working on validating user input, and I want the user's input fields to be retained in case of any validation errors. This is how I have set up my input fields: <form:input path="firstName" class="text short" id="firstName" value=" ...

Ensure that the HTML input element only accepts positive values, including decimal numbers

I need to build an input field that only accepts positive numbers, including decimal values. Leading zeros are not allowed, and users should not be able to paste invalid values like -14.5 into the field. Here is my current implementation: <input type= ...

Centered Navigation Bar Aligned with Header

I'm having trouble aligning my header vertically with the text in my nav-bar using Bootstrap 4.6. The nav-bar items are right-aligned, so the text isn't centered in the middle of the page like shown in picture 3. .jumbotron { background: #3 ...

Is there a way to extract information from a <span> element with restricted access?

I'm currently utilizing bs4 and urllib2 to extract data from a website. Check out the webpage here. The goal is to retrieve the remaining digits of the telephone number 3610...... but beforehand, it's necessary to click on a button to reveal th ...

How can I dynamically change the orderBy parameter based on conditions in an Angular application?

I need to dynamically change the orderBy parameter in a table row based on the result of a method. Here is an example of my current tr setup - <tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error&apo ...

How to retrieve values from checkboxes generated dynamically in php using jquery

This is a unique question and not related to event binding or any suggested duplicates. Hello, I am facing an issue while trying to fetch the value of a checkbox to send it in an ajax request to a PHP page. The checkboxes are dynamically created using PHP ...

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

A method using CSS to automatically resize an image in a teaser box as text content increases, ensuring compatibility across various web

In the process of constructing a teaser element using HTML, I plan to integrate an image and a title. This is what I have in mind: <div> <img src="./image.jpg" /> <h1 contenteditable>Something</h1> </div> Wh ...

Difficulty with the responsiveness of a website due to issues with the bootstrap grid system

I've been working with a Bootstrap grid that you can find at . While it works perfectly on large desktop screens, everything is a bit messy on mobile devices. The "NEW" text specifically is not visible on mobile and doesn't seem to be responsive. ...

When a link is clicked, it quickly blinks or flashes on the screen

<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2"> <%= link_to 'FARDIN KHANJANI', root_path, class: 'navbar-brand logo' %> </div> Upon viewing the gif I included, you may notice that a flash oc ...

Create a box with borders and divisions using HTML

Hello, I am trying to create a box in HTML with a slanted divider line inside. How can I achieve this design? I am aiming to replicate the layout shown in this image: Link to Image I have tried implementing the code below, but the alignment is not matchi ...

Tips on using jQuery to horizontally align an element in the viewport

Although this question has been raised before, my situation is rather unique. I am currently constructing an iframe for future integration into a slideshow component on the website. The challenge lies in the fact that I have a dynamically sized flexbox th ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...