Incorrect Alignment of Centered Boxes with CSS

I'm having trouble creating centered boxes with CSS. Currently, I have the following code: http://jsfiddle.net/LW7mN/2/

.trex-clear-all{
    display: inline-block;
    width: 99%;
    height: 17px;
    font-size: 12px !important;
    text-align: -webkit-right !important;
    text-align: right;
}
#trex_showonhover{
    visibility:hidden;
    background-color:#FFFFFF;
    border:1px solid #999999;
    cursor:default;
    position:absolute;
    text-align:center;
    width: 280px;
    z-index:100;
    -moz-border-radius: 15px;
    border-radius: 15px;
    padding: 10px 10px 10px;
}
#trex_close_div{
    top:6px;
    right:6px;
    position:absolute;
}    
.trex_showdiv{
    width:310px;
    float: right;
}

.trex-text-title{
    font-family: 'Segoe UI_', 'Open Sans', Verdana, Arial, Helvetica, sans-serif !important;
    font-size: 11px !important;
    font-weight:700 !important;
    padding: 2px !important;
}


.trex-new-widget-container {
    font: 10pt normal Arial, sans-serif;
    height: auto;
    margin: 10px auto 0 auto;
    text-align: center;
    width: 100%;
}


.trex-new-widget-container .trex-box {
    border: 1px solid #000;
    cursor: pointer;
    height: 180px;
    width: 240px;
    float: left;
    margin: 3px;
    position: relative;
    overflow: hidden;
    -webkit-box-shadow: 2px 1px 1px 1px #ccc;
    -moz-box-shadow: 2px 1px 1px 1px #ccc;
    box-shadow: 2px 1px 1px 1px #ccc;
}


.trex-new-widget-container .trex-box img {
    position: absolute;
    left: 0;
    width:100%;
    height: 100%;
    -webkit-transition: all 300ms ease-out;
    -moz-transition: all 300ms ease-out;
    -o-transition: all 300ms ease-out;
    -ms-transition: all 300ms ease-out; 
    transition: all 300ms ease-out;
}


.trex-new-widget-container .trex-box .trex-caption {
    background-color: rgba(240,240,240,0.6);
    position: absolute;
    color: #000 !important;
    z-index: 1000;
    -webkit-transition: all 400ms ease-out;
    -moz-transition: all 400ms ease-out;
    -o-transition: all 400ms ease-out;
    -ms-transition: all 400ms ease-out; 
    transition: all 400ms ease-in;
    left: 0;
}




.trex-new-widget-container .trex-box .trex-full-caption {
    width: 240px;
    height: 50px;   
    top: 130px;
    text-align: center;
    padding-top: 0px;
}


.trex-new-widget-container .trex-box:hover .trex-full-caption {
    -moz-transform: translateY(-60px);
    -o-transform: translateY(-60px);
    -webkit-transform: translateY(-60px);
    transform: translateY(-60px);
    opacity: 1;
    background-color: rgba(0,0,0,0.9);
    color: #fff !important;
}

When you adjust the page width (resize browser, or change resolution), the number of boxes per row changes. This is fine, but I want the boxes to be aligned in the middle, not on the left as they currently are.

I hope this clarifies the issue. Here is a simple sketch for better visualization:

Thank you

Answer №1

.trex-widget-wrapper .trex-container {
    border: 1px solid #000;
    box-shadow: 2px 1px 1px 1px #ccc;
    cursor: pointer;
    display: inline-block;
    /*float: left;*/
    height: 180px;
    margin: 3px;
    overflow: hidden;
    position: relative;
    width: 240px;
}

Answer №2

To achieve the desired layout, utilize the display property rather than float:

.new-widget-container .box {
    border: 1px solid #000;
    box-shadow: 2px 1px 1px 1px #ccc;
    cursor: pointer;
    display: inline-block;
    height: 180px;
    margin: 3px;
    overflow: hidden;
    position: relative;
    width: 240px;
}

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

How can I ensure my game or website fits perfectly on the screen without any need

I have recently developed a fun Erase-A-Man game that is optimized for mobile devices. However, I am facing an issue where users need to scroll to view all the letters on their screens. My goal is to make the game fit perfectly on all phone screen sizes so ...

Modifying alignment of buttons in React components

Transforming the typical Tic Tac Toe game from the React tutorial, with an added one-player feature. Oddly, button alignment shifts out of place once a value is inserted; causing the button to drop by about 50px until the row is filled with values, after ...

The system encountered a TypeError: Unable to access the 'nativeElement' property as it is undefined

Hello, I am a beginner in Angular 2 and currently working on an image cropping plugin. My goal is to display the image on a canvas element. Below is my HTML code: <div class="row"> <div class="col-sm-6"> <canvas id="layout" width="40 ...

What is the best way to extract a JSON value and use it across multiple functions?

Currently, everything seems to be working fine. However, I'm unsure about the correct way to call the json data. I attempted using a direct link but it didn't work. Do I need to modify the "data" parameters? I'm confused because I saw that ...

What is causing my table to refuse to center?

I'm having trouble keeping my table of images contained within the content area of my page. Additionally, I can't seem to get my footer to stay at the bottom of the page. Take a look at this image for reference: body { margin: 0px; padding ...

Repeating the process of duplicating with jQuery and inserting after each clone multiple times

Attempting to showcase a dynamic form for my business partner. The aim is to add select elements when the button is clicked, but currently encountering an issue where it duplicates the template twice instead of just once. Experimented with different code ...

Tips for avoiding flickering in a background image when it is being changed

Utilizing JavaScript, I am setting a repeated background image from a canvas to a div in the following way: var img_canvas = document.createElement('canvas'); img_canvas.width = 16; img_canvas.height = 16; img_canvas.getContext('2d' ...

Implementing a video background within a dynamic two-column layout, accompanied by text within a separate column

While viewing on a large screen with Bootstrap 5, everything looks good: https://i.sstatic.net/Vm6bH.png However, when I resize the screen, the text in the first column overflows onto the sections below instead of adjusting properly. https://i.sstatic.n ...

Achieving perfect alignment of an iframe on a webpage

Having an issue with aligning the iframe on my website. I have two buttons set up as onclick events that connect to internal pages displaying PHP data in tables within the iframe. Despite trying various CSS styles and positioning methods, I can't seem ...

styled components are having issues with background gradients and opacity not functioning properly

Hello, I am currently working with styled components and have the following global style code: const GlobalStyle = createGlobalStyle` html{ font-family: roboto; background: linear-gradient(45deg,rgba(137,255,255,0.5),rgba(161,252,143, 0 ...

Change the colors of a dynamic row in an HTML table using various options

I have successfully implemented a dynamic table using HTML and JavaScript. However, instead of applying alternate row colors, I want to assign a unique color to each row. How can I achieve this? <!DOCTYPE HTML> <html> <head> ...

Transforming a radio button into a checkbox while successfully saving data to a database (toggling between checked and unchecked)

I have limited experience in web development, but I recently created a webpage that allows users to input data into an SQL database. While my code is functional, I believe there's room for optimization. I pieced it together from various online resourc ...

Images are not displayed when utilizing font-awesome on a MVC 5 website

I'm having an issue where adding font-awesome to the style bundle is causing images not to display correctly. Instead of showing the right icon, I am getting a transparent box. My style bundle configuration looks like this: bundles.Add(new StyleBund ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

Navigate files on a Windows server remotely using a Linux Apache web server by clicking on a browser link

After setting up an apache webserver on Linux Debian and successfully creating an intranet, I encountered a challenge. The intranet is designed to display tables with data from sql queries using php and mysql. However, I wanted to include a hyperlink withi ...

The use of Material-UI collapse animation in a table results in the insertion of div elements, triggering a validateDOMNesting warning

Having a data-filled table, I am looking to implement smooth transitions when adding or deleting an item. This is a ReactJS project utilizing Material-UI. The desired effect is similar to the one demonstrated in their example. While they use a List compon ...

The jQuery function for $(window).scroll is not functioning as expected

My challenge is getting the scroll to reveal my scrollTop value I've been working with this code: $(document).ready(function(){ console.log('Hello!'); $(window).scroll(function(){ console.log('Scrolling...'); var wScroll = ...

Ways to specify the encoding for a text iframe embedded in a webpage

Can anyone help me with document embedding in HTML? In my current project, I am directly embedding my update logs as text files into the webpage, along with a selection menu to view all the updates. However, I ran into a minor issue where the Firefox cons ...

Enable hyperlink to open in a new browser tab after user clicks on button embedded inside an iFrame

<head> </head> <div> <iframe src="https://....."></iframe> </div> https://i.sstatic.net/dk21i.png I'm looking for a way to open the link in a new tab when clicking on this button. Currently, the page loads wi ...

tips for displaying a label and text side by side on a single line

Is there a way to keep my label and text on the same line by adjusting the CSS? I've tried using float based on suggestions from other posts, but they still end up on different lines. .indentColumn { width: 71px; padding-top: 5%; } .labelColumn ...