What is the best way to make content align at the center of this div

Check out the jsfiddle link

I need help centering content within a div that has the class main-testimonial-block

I've been successfully using the following CSS code to achieve this:

position: absolute;
  left: 50%;
  transform: translate(-50%);

The issue I'm facing is that when I apply this trick, the two boxes inside it end up with a line break. I only want the line break to occur when there's not enough space on the screen, such as on mobile devices.

.main-testimonial-block {}

.snip1359 {
  font-family: 'Roboto', Arial, sans-serif;
  position: relative;
  float: left;
  overflow: hidden;
  margin: 10px 1%;
  min-width: 200px;
  max-width: 405px;
  width: 100%;
  color: #ffffff;
  text-align: left;
  line-height: 1.4em;
  background-color: #1e1e1e;
  padding-top: 120px;
}

(snip1359 styles continue...)

</div>

Answer №1

Implementing this is quite simple with the use of CSS3 Display Flexbox, allowing for easy vertical and horizontal centering of Figures. For detailed guidance on using CSS3 Flexbox, refer to this helpful Flexbox guide

To achieve this, adjust the main-testimonial-block to function as display: flex and manage its child Elements. You can specify a max-height or height for your Figures to prevent them from overflowing with a black Background, such as 110px

A Answer for your Question has been provided below:

.main-testimonial-block {
   display: flex;
   align-items: center; /* vertically centered */
   justify-content: center; /* horizontally centered */

   /* If you desire your Figures to be centrally positioned across the screen,
      set a height in this class,
      where your figures can be aligned within it.
      e.g. height: 100vh for Fullscreen. */
   height: 100vh;
}

Refer to the snippet below for a functional example and alternative centering method.

How to center your content (using modern flexbox methods):

.main-testimonial-block {
   display: flex;
   align-items: center; 
   justify-content: center;
   height: 100vh;
}

.snip1359 {
  font-family: 'Roboto', Arial, sans-serif;
  position: relative;
  float: left;
  overflow: hidden;
  margin: 10px 1%;
  min-width: 200px;
  max-width: 405px;
  width: 100%;
  color: #ffffff;
  text-align: left;
  line-height: 1.4em;
  background-color: #1e1e1e;
  padding-top: 120px;
}

.snip1359 * {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
/* Remaining CSS code truncated for brevity */
<div class="main-testimonial-block">

  <figure class="snip1359">
    <figcaption>
      <blockquote>Test message, works!</blockquote>
    </figcaption>
    <h3>Kamal Chhirang<span>BCA III</span></h3>
  </figure>
  </style>
  <figure class="snip1359">
    <figcaption>
      <blockquote>asfsfs</blockquote>
    </figcaption>
    <h3>test test<span>testtttt</span></h3>
  </figure>
</div>

Centering Elements (without css)

To center content without CSS, first apply text-align: center to your .main-testimonial-block to center the content. Then reset the float on all figures with the class .snip1359 and set them as display: inline-block to act like block Elements while adjusting their width accordingly.

The limitation of this method is that only horizontal centering is possible. Ensure to align the text on child Elements appropriately. Below is the CSS for this technique.

.main-testimonial-block {
   display: block;
   text-align: center;
}

.snip1359 {
   float: none;
   display: inline-block;   
}

Suggestions

  • .snip1359 are floated but not cleared, leading to potential Layout issues. Clear floating elements to avoid this.
  • Consider optimizing your CSS for managing figure content heights, paddings, and margins dynamically, although this may require further exploration beyond the current topic.
  • For mobile responsiveness, bear in mind that Users may access the site on smaller Devices.

Answer №2

To align your content in the center, simply include margin: 0 auto; in both the container and width properties.

.main-testimonial-block {
  margin: 0 auto; 
  min-width: 200px;
  max-width: 405px;
  width: 100%;
}

Additionally, there seems to be an unnecessary </style> tag in your HTML code that should be removed.

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

Refreshing the page after executing a MySQL UPDATE statement

There seems to be a problem with the page refreshing faster than the query execution. I have an UPDATE query to update my database with values in my fields, triggered by pushing a button. However, upon clicking the button (which refreshes the website), th ...

Require that double-width unicode symbols are displayed as double-width in Markdown or CSS

I am seeking a solution to manage the visual width of double-width unicode characters like the LARGE ORANGE SQUARE ...

The JSP page is not showing the table content

Working on my project in Spring MVC, I am facing an issue with displaying content inside the table that lists data from the controller. To ensure that the lists are being accessed from the database, I have tested it using jUnit in my Dao file. The problem ...

Steps for Implementing a "Load More" Button on an HTML JSON Page

I'm currently engaged in a project that involves fetching product information from a JSON file and displaying it on an HTML page using JavaScript. While I've successfully implemented the fetch function, I now want to add a "Load More" function/bu ...

incorporating a personalized HTMLElement using Typescript

Hey there! I'm fairly new to using Angular and could use some help. I'm trying to insert a custom HTML element onto a page when a button is clicked, but I'm struggling to figure it out. Here are my HTML and TypeScript files: TypeScript Fil ...

Bootstrap-4 grid system is effectively maintaining its responsiveness without collapsing

I'm struggling to display one image per row on smaller screens using the sm breakpoint. This is my first attempt at using bootstrap 4. I followed their grid system guidelines which stated that responsive breaks occur when specifying a size for each c ...

Pricing determined by location on a website created with HTML

We are looking to customize our HTML5/CSS3 website by displaying different pricing tables based on the location of each visitor. Our goal is to have a fixed price for users from Singapore while showing a different price for visitors from other parts of th ...

The brightness of the image decreases following an animation on the HTML5 canvas

Question How can I restore the original brightness of an image after animation? Description In this scenario, I have created an image using strips and animated them to simulate a waving effect (inspired by a water wave simulation concept). However, aft ...

Display temporary image until real image loads within ng-repeat angularjs

I am looking to display a placeholder image while the actual image is being loaded. I have tried using the ng-image-appear plugin. The issue I'm facing is that the placeholder image does not appear for img elements inside ng-repeat, although it works ...

Adjust the height of a div according to the width using CSS in a progressive manner

I'm looking to create a fluid container that adjusts its height gradually based on the width of the window (as the div's width is set to 100%). Similar to the behavior achieved with the aspect-ratio CSS rule, I want the height to smoothly increas ...

Tips for utilizing a dropdown menu in Angular

<div> <label> Categories </label> <select> <option *ngFor = "let category of categories" [value] = "category" (click) = "onCategorySelected( category.name )"> ...

What is the best way to integrate my company's global styles CDN for development purposes into a Vue cli project using Webpack?

After attempting to import through the entry file (main.js)... import Vue from 'vue' import App from '@/App' import router from '@/router/router' import store from '@/store/store' import BootstrapVue from 'boot ...

Issue with CSS/JS resolved: figured out a way to create a page/menu that overlaps with their individual scrollbars

I am encountering an issue with a scrollbar that is related to a fullscreen menu appearing on a page and causing the page scrollbar to reset due to a display: none property. The images below provide a visual representation of the problem: Below is the Ty ...

Show a pop-up window when a button is clicked, just before the page redirects

Is there a way to display a popup message before redirecting to another page when clicking on a button? Here's the scenario: <a href="addorder.php?id=<? echo $row01['id']; ?>" ><button id="myButton" class="btn btn-primary btn ...

Why isn't the footer extending to the edges despite setting margins and other properties?

Feeling a bit lost here. My footer isn't extending to the edges of the window like it should, or so I thought. .footer { height: 200px; bottom: 0px; margin: 0px; left: 0px; right: 0px; padding: 0px; width: 100%; background-color: #E ...

Tips for securing my div in place when interacting with an expanding panel

I am attempting to create a visualization similar to this: https://i.sstatic.net/Wxdqr.png Instead of using the built-in card and card-column classes in Bootstrap, I noticed that when I expand a panel, all my <div>s start moving around haphazardly: ...

Utilize Angular components in TypeScript to effectively manage errors within forms

I am currently developing a form in Angular/Typescript with more than 10 fields, and I want to streamline error management without redundancy in my HTML code. Here is an example of the form structure : <form [formGroup]="myForm"> <label&g ...

What causes the variance in style between the two div elements?

As I delve into the world of styled-components, I decided to create this component: import React from 'react' import styled from 'styled-components' const Row = styled.div` ` const Column = styled.div` flex: 0 0 50%; max-widt ...

Prevent the click event on an Angular component

I am currently utilizing the component found on this link: I have not been able to locate a function or property that disables the component state, making it impossible to select dates on the calendar. Due to this limitation, I am interested in learning ...

Adjust the position of the div so that it remains visible as you scroll

Is it possible to position an element within a container that contains a wide table with overflow property so that the element remains visible while scrolling horizontally through the table data? <div id = "mainContainer" style = "width:300px; overflow ...