align both horizontally and vertically

I am looking to center a text and card inside a div both vertically and horizontally. Can someone help me achieve this layout similar to the image provided below? Much appreciated!

.percentile-card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.3s;
  width: 30%;
  height: 30%;
  float: left;
}

.percentile-card:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
<div class="border">
  <h2 style="font-size: 3em;">Where Do i Stand Overall ?</h2>
  <div class="percentile-card text-center">
    <h3>You did better Than</h3>
    <h3><b>60%</b></h3>
  </div>
</div>

https://i.sstatic.net/f1bXZ.png

Answer №1

After reviewing all the answers, I found them to be invalid. Therefore, I decided to post my own answer.

.root_class {
  display: flex;
  justify-content: center;
  align-items: center;
  width: fit-content;
  margin: 0 auto;
}

@media (max-width: 576px) {
  .root_class {
    flex-direction: column;
  }
}

.text {
  font-size: 2rem;
  color: #bbb;
  margin: 0;
  margin-right: 1rem;
  text-align: center;
}

.percentile-card {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  transition: 0.3s;
  margin: 1rem;
}

.percentile-card p {
  margin: 0;
}

.percentile-card p:first-child {
  color: #bbb;
}

.percentile-card:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<div class="border root_class">
  <p class="text">Where Do i Stand Overall ?</p>
  <div class="percentile-card text-center">
    <p>You did better Than</p>
    <p><b>60%</b></p>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d5cad5d5c0d78bcfd6e5948b94938b95">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

Answer №2

To position it in the center of the page, you can eliminate the float property from percentile-card and replace it with a centered margin.

For vertical alignment, using view height can be an effective solution.

You may find this W3 link helpful in understanding margins better. Additionally, check out these demos.

If you want to customize the view height independent of any preceding element's constraints, exploring different positions might be beneficial.

Sample code:

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style>
    .percentile-card{
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
      transition: 0.3s;
      width: 800px;
      height: 30%;
      margin: 0 auto;
      padding:10px;
    }

    .percentile-card:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }

    .border{
        margin-top: 40vh;
        text-align: center;
    }
</style>
</head>
<body>

<div class="border">
    <div class="percentile-card" style="display: flex;">
        <div style="flex-basis: 70%">
            <h2 style="font-size: 3em;">Where do I stand overall ?</h2>
        </div>
        <div class="percentile-card" style="flex-basis: 30%">
            <h3 >You did better Than</h3>
            <h3><b>60%</b></h3>
        </div>
        
    </div>
</div>

</body>
</html>

For the border with card inside variant:

Sample code:

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .percentile-card{
          box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
          transition: 0.3s;
          width: 800px;
          height: 30%;
          
        }

        .percentile-card:hover {
          box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
        }

        .border{
            margin-top: 40vh;
            text-align: center;
        }
    </style>
</head>
<body>

    <div class="border">
        <div class="" style="display: flex; border:1px solid black;margin: 0 auto; padding:10px;width: 800px">
            <div style="flex-basis: 70%">
                <h2 style="font-size: 3em;">Where do I stand overall ?</h2>
            </div>
            <div class="percentile-card" style="flex-basis: 30%">
                <h3 >You did better Than</h3>
                <h3><b>60%</b></h3>
            </div>
            
        </div>
    </div>

</body>
</html>

Answer №3

I managed to solve it!

.percentile-card{
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
  transition: 0.3s;
  width: 30%;
  height: 30%;
  float: left;
}

.percentile-card:hover {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.Center{
position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
  <div class="border">
        <h2 style="font-size: 3em;">Where Do I Stand Overall?</h2>
        <div class="percentile-card text-center Center">
          <h3>You Performed Better Than</h3>
          <h3><b>60%</b></h3>
        </div>
      </div>

Answer №4

👉🏻 Discover the Art of Centering Elements

Explore the best techniques to master and implement centering in CSS based on your specific requirements.

If you refer to the code provided above, you can enhance the styling of your elements by adding the following CSS to your border class:

.border {
  border: 1px solid red;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center
}

Take a look at this functional code snippet:

.border {
  border: 1px solid red;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center
}
.percentile-card{
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
  transition: 0.3s;
  width: 30%;
  height: 30%;
  float: left;
}

.percentile-card:hover {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
<div class="border">
   <h2 style="font-size: 3em;">Where Do I Stand Overall?</h2>
   <div class="percentile-card text-center">
     <h3>You Did Better Than</h3>
     <h3><b>60%</b></h3>
   </div>
 </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

Dragging a React <div> using useRef

As a beginner in React, I decided to challenge myself by recreating a classic game like 'space invader'. It seemed like a fun and simple project for someone at my skill level. My goal is to have a spaceship at the bottom of the screen that can s ...

Firing the `onchange` event without requiring a click in Firefox using Jquery 1.4

Something peculiar is happening - in Firefox, when a select dropdown is left without any item selected, the onchange event triggers on blur. This occurs even if the focus is simply shifted to another element on the page. The select statement I am using is ...

Change the appearance of a div based on the presence of a certain class within a child div using jQuery styling techniques

I'm trying to set a default padding of 15px for div.content, but if it contains a child div.products-list, I want the padding to be removed. I've looked into solutions using jquery, but nothing seems to work. Here's how my layout is structur ...

Positioning the fourth object aligned to the right side within a map function in React using Bootstrap col-md-4

There are 9 objects in an array, and I want to display them with 3 items in one row. The first row looks fine, but the first item of the second row is moving to the right side. <div className='row'> { services? services.map((d, i) => ...

Having trouble locating a text element with Python's Selenium Web-Driver

I am having trouble retrieving the seller information for a product, specifically the text associated with it. It seems like there may be some unusual behavior due to the fact that the text is also a link. Can anyone offer assistance? Here is the Python c ...

Is there a way to repeatedly apply addClass / removeClass functions multiple times?

I am having an issue where I want to display a loading icon using CSS on my page when a user sends data via ajax, and then remove this loading icon once the ajax call is complete. The strange thing is that the page successfully shows the icon the first tim ...

Animate the child element of this selector using jQuery

When using the this selector to animate an image within the li and a elements, I encountered a problem. Here is the Jquery code snippet: jQuery(document).ready(function () { jQuery(".jcarousel-skin-tango li").mouseenter(function () { ...

Modify the content of the bootstrap modal-body to become scrollable

Bootstrap is a familiar tool for me, but I'm fairly new to flexbox css. In my bootstrap modal, I typically have a header, body, and footer. I'm trying to modify the model-body class so that if the content exceeds a certain size, it becomes scrol ...

is it possible to create cells within the <td> tag?

My table contains the following data: No Name Product Type Num of Units 1 ADA B112 3 Pcs 2 ADA B253 1 Pcs 3 ADA K23 6 Pcs 4 DUZK l1 10 Pcs 5 DUZK l5 10 Pcs 6 Naro NX 1 Pcs The SQL query I'm using is: $query = "SELECT *, GROUP_C ...

Tips for CSS: Preventing onhover animation from resetting with each hover

I've created an on-hover CSS animation that smoothly transitions between images. However, I encountered a lagging issue when the user quickly hovers over SECTION ONE and SECTION TWO before the animation ends, causing the animation to restart and lag. ...

Determining the identity of an HTML element using its surrounding values

I have a webpage with a table displaying various services and their corresponding download buttons. Here is an example of the content: nov. service 1 [ click to download pdf ] nov. service 5 [ click to download pdf ] nov. service 3 [ ...

Positioning Div at the Bottom of a Interactive Flip Card Using Bootstrap 4

My current project features a creative flip image card created using bootstrap and js. On the back of the card, there is a title, a main text body, and a few small additional pieces of information. My goal is to have these three small bits of information a ...

Troubleshoot: Unable to send or receive messages in Socket.IO Chat

I recently tried following a tutorial on socket.io chat, which can be found here. Although the tutorial video showed everything working perfectly, I have encountered issues with my implementation. It seems like the messages are not being sent or received ...

browse through the percentage of the display

Is there a way to use jQuery to determine when the user has scrolled 50% of the screen? (For example, scrolling 50px) After detecting this scrolling milestone, how can we smoothly animate the page to #second, or effectively move to the top at 100%? (There ...

What is the best way to integrate Bootstrap with NuxtJS?

Can anyone provide guidance on incorporating Bootstrap into a nuxt.js project without relying on a CDN? I have attempted to include the necessary bootstrap files in the nuxt.config.js, but encountered difficulties. Specifically, I am also looking to incl ...

incorporating a rollover menu into the images displayed on the webpage

I have a set of 3 images displayed inline, each enclosed in a span tag with the class imgAvatar. Positioned above them are 3 identical hidden images (delete buttons) stored within spans with the class imgAvatarSelector. The CSS for the delete buttons is a ...

HTML - Using Tables to Add and Edit Rows

Take a look at this demo on JSFiddle for my project: Demo jsfiddle I'm currently in the process of creating a table with various functionalities. The function "sortTable" is responsible for sorting the table and it's functioning as expected. T ...

To create a complete layout without relying on fixed positioning, ensure that the header, container, and footer collectively

Is there a method to ensure the header, container, and footer encompass the entire layout without using fixed positioning in CSS? Check out this HTML fiddle for reference. <div class="wrapper"> <header> <img src="https://www.ecobin.co ...

Developing a custom JavaScript function to iterate through a group of html elements

In my mission to create a function that loops through a set of HTML elements and gathers their values, I aim to then utilize those values to produce an HTML textarea. Thus far, I've established a series of HTML input boxes and a JavaScript function f ...

Ways to remedy the white line produced by CSS transform when hovered over?

Has anyone discovered a fix for the white line or border that appears when an element is transformed with CSS scale on hover? I've tried various solutions such as: transform: translateZ(0), -webkit-backface-visibility: hidden, transform-style: preser ...