What is the best way to align two square blocks in the center of a page

I am working on a webpage where I need to display the status of two websites - whether they are currently up and running or not. For sites that are up, I want the block to have a light green background, and for those that are down, a light red one. Additionally, the name of the site should be centered inside each block.

Here is what I have attempted so far:

    body {
      font-family: sans-serif;
    }
    #container {
      width: 800px;
      height: 600px;
      margin: 0 auto;
      border: 1px solid #ccc;
    }
    #smallcontainer {
      width: 208px;
      height: 100px;
      margin: 200px auto auto;
    }
    .status {
      height: 100px;
      width: 100px;
      background: #efefef;
      float: left;
      margin-left: 2px;
      border: 1px solid #ccc;
    }
<div id="container">
  <div id="smallcontainer">
    <div class="status"></div>
    <div class="status"></div>
  </div>
</div>

While it functions (check full screen output), I believe there could be a more efficient way to achieve this with CSS. My current code feels like a workaround. How can I neatly center text both vertically and horizontally within each block?

Furthermore, is there a method to ensure that this layout is responsive across all desktop screen sizes? Would using percentage for width and height instead of pixels be a better approach?

Answer №1

If you're looking to create a flexible layout, consider using flexbox. Check browser support here

Here's the HTML structure:

<div id="container">
    <div class="status"></div>
    <div class="status"></div>
</div>

And the corresponding CSS:

#container {
    width: 800px;
    height: 600px;
    margin: 0 auto;
    border: 1px solid #ccc;
    display: flex;
    align-items: center;
    justify-content: center;
}
.status {
    height: 100px;
    width: 100px;
    background: #efefef;
    margin-left: 2px;
    border: 1px solid #ccc;
}

For a live example, check out this JSFiddle link.

Answer №2

Check out this Fiddle for a demonstration of text alignment both vertically and horizontally centered within a div.

body {
        font-family: sans-serif;
    }
    #container {
        width: 800px;
        height: 600px;
        margin: 0 auto;
        border: 1px solid #ccc;
    }
    #smallcontainer {
        width: 208px;
        height: 100px;
        text-align: center;
        margin: 200px auto auto;
    }
    .status {
        height: 100px;
        width: 100px;
        background: #efefef;
        float: left;
        margin-left: 2px;
        border: 1px solid #ccc;
        line-height: 100px;
    }

Answer №3

Check out this jsfiddle link

body {
      font-family: sans-serif;
    }
    #container {
      width: 100%;
      height: 400px;

      margin: 0 auto;
      border: 1px solid #ccc;
    position:relative;
    }
    #smallcontainer {
      width: 208px;
      height: 100px;
      position:absolute;
      left:50%;
      top:50%;
      margin-left:-100px;
      margin-top:-50px;
    }
    .status {
        height: 100px;
        width: 100px;
        background: #efefef;
        float: left;
        margin-left: 2px;
        border: 1px solid #ccc;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-box-pack: center;
        -webkit-box-align: center;

        display: -moz-box;
        -moz-box-orient: vertical;
        -moz-box-pack: center;
        -moz-box-align: center;

        display: box;
        box-orient: vertical;
        box-pack: center;
        box-align: center; 
        text-align:center;
    }

For more information on "display:flexbox", visit https://developer.mozilla.org/en-US/docs/Web/CSS/display

Answer №4

My approach to this would be:

HTML structure:

<div id="main-container">
    <div id="sub-container">
        <div class="status">
            <div class="border">
                <div class="text-content">Text Here</div>
            </div>
        </div>
        <div class="status">
            <div class="border">
                <div class="text-content">More Text Here</div>
            </div>
        </div>
    </div>
</div>

CSS styling:

* {
    box-sizing: border-box;
}
body {
    font-family: sans-serif;
}
#main-container {
    width: 95%;
    height: 400px;
    margin: 0 auto;
    border: 1px solid #ccc;
    position: relative;
}
#sub-container {
    width: 208px;
    height: 100px;
    margin: 0 auto;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
.status {
    height: 100%;
    width: 50%;
    float: left;
    text-align: center;
    padding: 2px;
}
.border {
    background: #efefef;
    border: 1px solid #ccc;
    width: 100%;
    height: 100%;
    position: relative;
}
.text-content {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

Check out the implementation in this fiddle link: http://jsfiddle.net/bootsified/kf7Lbq24/

Answer №5

To center the desired divs precisely, you can apply negative margins to each one. Keep in mind that the width and height must be specified in pixels for this method to work.

body {
  font-family: sans-serif;
}
#container {
  width: 800px;
  height: 600px;
  border: 1px solid #ccc;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -400px;
  margin-top: -300px;
}
#smallcontainer {
  width: 208px;
  height: 100px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -104px;
  margin-top: -50px;
}
.status {
  height: 100px;
  width: 100px;
  background: #efefef;
  float: left;
  margin-left: 2px;
  border: 1px solid #ccc;
}
<div id="container">
  <div id="smallcontainer">
    <div class="status"></div>
    <div class="status"></div>
  </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

I possess a solitary div element that requires dynamic replication

I have a single container and an unspecified number of rows of data. I want to display this data on HTML cards that are generated dynamically based on the number of rows. For example, if there are 10 rows of data, I need to create 10 card elements with ea ...

After closing, remove the #about section from the URL

One of the buttons on my webpage triggers an overlay using JavaScript that directs to a URL like the following: However, when the close button is clicked, the overlay is removed but the "/#about" part of the URL remains. What is the most effective method ...

What are some ways to optimize the use of the jquery.mCustomScrollbar.js script?

I have a myriad of inquiries and would greatly appreciate your assistance in addressing them. Despite spending countless hours attempting to solve the issue independently, I have been unsuccessful. One question that plagues me is why, when using Google Ch ...

The utilization of CSS variables within intricate properties

Imagine having a CSS property called box-shadow: box-shadow: 5px 5px 0 #ccc; What if there is a need to create a variable from the color #ccc? While in SCSS, you could accomplish this with code like: $grey: #ccc; .element { box-shadow: 5px 5px 0 $gre ...

How can I pass a parameter to a selector for an Ajax call when onClick event

Utilize a JQUERY AJAX call within a $(document).ready(function()..) function that iterates over MongoDB documents and dynamically adds HTML Div elements to the page using a .get Each Div includes a <button type='button' id='deleteItem&ap ...

Is it possible to style a nested ID with CSS?

Imagine a scenario where you are working within an HTML file that you cannot modify, but you have the ability to only edit the CSS through a stylesheet. Is it possible to target an ID within another ID in the same way you can do with classes? #id1 #id2 {s ...

Issues with CKEditor's failure to save content in utf-8 character encoding

Let me explain my situation concisely. I'm encountering an issue with my PHP project that I can't seem to resolve. The problem arises when I try to update the content using CKEditor on my website. Below is the configuration and instance of CKEdi ...

Adjusting data points on a radar chart.js through user interaction

I have a radar map generated using chart.js, along with some input fields where users can enter values that I want to reflect in the graph. I am exploring ways to update the chart dynamically as the user types in the values. One option is to have the char ...

Tips for creating a line break within a Bootstrap progress bar when the text exceeds the width of a div

By default, the progress bar displays text inside the portion that has progress. To ensure the text does not wrap prematurely, use white-space: nowrap; How can the issue of long text within a div be resolved without using JavaScript? Is there a way to ma ...

Update the Bootstrap carousel data-interval to stop on the click event of a specific component

I recently implemented a bootstrap carousel on my website featuring one video and two images in the slides. A specific client request was to have the carousel auto slide for 3000ms, but stop sliding when the play button on the video is clicked. I attempted ...

Bootstrap grid. Instead of moving to the next row, scroll horizontally when the content overflows

Here is the code snippet: <div class="row shochatgroup"> <div *ngFor="let message of messages" class="col-auto"> <button *ngIf="message.state.attributes.shochatpaid > 0 && message.state.at ...

Is there a way to create rectangles with a designated percentage of blue color using CSS or Bootstrap 3?

There's an image on my laptop that perfectly illustrates what I mean, but unfortunately, I'm not sure how to share it with you. In essence, I am looking to create a rectangle filled with a specific percentage of the color blue. Could you assist m ...

What is the best way to implement responsive input fields and buttons on my website?

I am in the process of creating my first website, and I am inspired by the Contact page design on this site(). Everything looks great until the screen size drops below 1000px - at that point, my button shifts to the right side and leaves some empty space o ...

Dynamic Mat-select-trigger that automatically adjusts its size to fit the content

Currently, I am experimenting with an Angular Mat-Select that allows multiple selections. To display the selected values in the value field, I have implemented a custom Mat-Select-Trigger. My goal is to enable automatic resizing of the value field (similar ...

My div element isn't responding to border radius adjustments on the top left and right corners

I'm trying to achieve curved corners at the top left and top right of my div, but the border-top-left-radius and border-top-right-radius properties don't seem to work for me. Here is the HTML code: <div id="trape"></div> And the C ...

Removing HTML tags from Angular template bindings

My data list sometimes includes HTML elements <li *ngFor="let result of results"> <span [innerHTML]="result.question.title"> </span> </li> The issue with using innerHTML is that the HTML is parsed and rendered, affecting ...

How to customize the color of radio buttons in JFXRadioButton using CSS

I am facing an issue with customizing the styling of JFXRadioButton component from this library : jfoenix.com I want to modify the color of the circle, but the default class is not working. Are there any suggestions or solutions available? (The colors me ...

iOS Safari does not support CSS transforms when applied to parent elements

As a workaround, I decided to create a mockup of the scenario since developing a testable version wasn't straightforward. However, here is the gist of the issue: @keyframes mainFadeIn { 0% { opacity: 0; transform: translateY(-3rem); } ...

Delay in dropping while using React Beautiful DnD

While incorporating react-beautiful-dnd into my project, I faced an issue where the dragged item is initially positioned incorrectly in one of the lists. There is a brief delay before it jumps to its correct position. You can see the problem in action by ...

How can I show information on the same page as a link by simply clicking on it?

My goal is to create a functionality where clicking on a link will display specific information. Currently, all the links and their corresponding information are displayed at once. I want to change this so that the links are displayed first, and the inform ...