What is the best way to position divs both vertically and horizontally at the center of a container

Here is the HTML markup I'm working with:

<div class="wrapper">
  <div class="block new-block"></div>
  <div class="block used-block"></div>
  <div class="block service-block"></div>
  <div class="block certified-block"></div>
  <div class="block offer-type-block"></div>
</div>

Accompanied by this CSS:

.wrapper {
  width : 800px;
  height : 100px;
  background : #393533;
  margin : auto;
}

.block {
  width : 19%;
  height : 80px;
  background : salmon;
  display : inline-block;
}

I am looking to horizontally and vertically center the 5 inner divs within the wrapper container using CSS. Any suggestions on how I can achieve this? Thank you in advance!

Answer №1

If you want to center the contents of a wrapper using CSS flexbox, you can do it like this:

.container {
  width: 600px;
  height: 200px;
  background-color: #f7f7f7;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  width: 100px;
  height: 100px;
  background-color: #eaeaea;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Answer №2

<!DOCTYPE html>
<html>
<head>
 <title> Unique Sample Code </title>
 <style>
.wrapper {
    width: 800px;
    height: 120px;
    background: #393533;
    margin: auto;
    padding: 20px;
    box-sizing: border-box;
    text-align: center;
}
.block {
  width : 19%;
  height : 80px;
  background : salmon;
  display : inline-block;
}
 </style>
</head>
<body>

<div class="wrapper">
  <div class="block new-block"></div>
  <div class="block used-block"></div>
  <div class="block service-block"></div>
  <div class="block certified-block"></div>
  <div class="block offer-type-block"></div>
</div>



   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <script>

   </script>
</body>
</html>

Answer №3

Give this code a try, it might be able to assist you:

.container {
        width: 800px;
        height: 100px;
        background-color: #393533;
        margin: auto;
        position: relative;
      }

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

Include a personalized picture in Marker Cluster without replacing the existing markers

Here is the jQuery code I am currently using to incorporate map marker clusters: markerCluster = new MarkerClusterer(map); To add individual markers to the clusters, I use markerCluster.addMarker(Marker); (full code block can be found at the end of this ...

Is there a transparent space in a PNG image that covers the the anchors located beneath

I've been working on this design which you can see at My plan is to incorporate various animations and transitions, so I have inserted each layer separately (the island, individual clouds, buttons) as images. Unfortunately, I haven't been able t ...

carousel anomaly

Here is the code snippet for my webpage: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="Dish.TestForm" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm ...

Implementing a two column stacked layout with Susy, similar to the design of Twitter's interface

Having worked extensively with Susy, I recently encountered a unique use case that has left me puzzled. To illustrate my point, let's take Twitter as an example. If we observe their website layout, they have a standard three-column design with a brea ...

Pan motion gesture above HTML components

Is it possible to create a hovering effect over elements in a container using a pan gesture? https://i.sstatic.net/E6G56.gif Here's the HTML code: <div class="container"> <div class="item"></div> <div class="item"></div ...

Include CakePHP named parameters in the URL when selecting from a list

If I have a selection list like the one below: <select name='languages'> <option value='german'>German</option> <option value='english'>English</option> </select> How can I use Jav ...

Tips for inserting text onto an image using a hover effect

I have designed a section on my website where an image darkens upon hovering, and I would like text to appear centered both horizontally and vertically when the image is hovered over. However, despite my efforts, the text does not display on the image as i ...

What could be the reason my black overlay doesn't show up when the button is clicked?

Attempting to craft a pop-up window on my own, I encountered an issue. Upon pressing the button, instead of the anticipated pop-up appearing and darkening the background below it, the entire page freezes with no sign of the pop-up box. If I eliminate the ...

Automation of transmitting HTML data to a database

After dabbling in web development for some time, I've come to realize that transferring data between an HTML form and a database can be quite challenging. My usual solution involves using an AJAX call to a PHP script, as illustrated in the image below ...

Steps to trigger the "Confirm form resubmission" prompt when a user attempts to refresh their browser

I am encountering a situation where users are unable to refresh the page by clicking the browser's refresh button or pressing F5. I would like to simulate 'form resubmission' in order to display a prompt message. Could someone please provid ...

Removing the last 3 elements using jQuery

My goal is to remove the label for Notes, text area of note and the checkbox near the text area, along with the delete button itself when the user clicks the delete button. I attempted to achieve this using the code snippet below, however it only removes t ...

Using AJAX to Capture PHP Error Responses

I have implemented a form where users can upload profile pictures and see live validation feedback without refreshing the page. I am currently using AJAX for this functionality, but it seems that the requests are not reaching the PHP file. Also, after subm ...

Retaining specific items in a collapsed Bootstrap Navbar

I am looking to keep certain items in my navigation bar visible, but also have a collapse toggle when the window becomes too small on the right side. Example: LOGO item item item Sign In Sign Up When collapsed: LOGO ...

Using JQuery to retrieve part of a className results in a null value being returned

I am facing an issue with a piece of code in codesandbox where it returns null when I attempt to retrieve part of the className from a div using JQuery. How can I troubleshoot this and make it work? Check out the Codesandbox Example import React, { Compo ...

Using jQuery to iterate through elements of a PHP array

I've got a PHP array that outputs like this: Array ( [0] => Array ( [title] => Much title [end] => Such end [start] => Very start ) [1] => Array ( [title] ...

I'm looking for a more efficient and graceful way to implement jquery .slideToggle on numerous hidden divs. Any suggestions?

If you would like to see a demonstration of what I am working on, here is a link to a live example: https://jsfiddle.net/yfe7ky3x/ My goal is to create a basic portfolio website that showcases various projects. I want to implement a feature where clicking ...

problem arising from the box shadow and transparent areas of an image

I'm facing an issue while attempting to apply a box-shadow effect on my image. The problem arises due to certain transparent parts of the image causing unexpected results with the box-shadow. To better understand the situation, refer to the code snipp ...

Using jQuery or JavaScript to trigger a specific popup when clicking on an email link

I am looking to accomplish the following: The user will receive an email with a link. When they click the link from the email, it should take them to the homepage of the site where there are links. Clicking on a link should trigger a popup. However, I w ...

Creating a sleek hover effect with a bottom border on your Bootstrap navbar

Having trouble implementing a hover effect with a bottom border in the Bootstrap navbar, similar to this: https://i.sstatic.net/sbqt4.png However, my current result looks like this: https://i.sstatic.net/JXghl.png I want to adjust the bottom border to ...

Is it feasible for a rollover to occur on a layered component?

Is there a way to achieve the following using CSS, jQuery, and PHP? I have a div element with the class button_background that contains a background image. Inside this div is an anchor tag (A tag) with a background that overlays the parent div's back ...