Guide on aligning two boxes in the center with HTML, CSS, and Bootstrap

Is there a way to perfectly center two buttons or clickable boxes, creating a layout similar to the image shown below?

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

This is what has been attempted so far:

CSS file

.container_1 .panel_1 {
    position: absolute;
    top: 35%;
    left: 45%;
    transform: translateX(-50%) translateY(-50%);
}

.container_2 .panel_2 {
    position: absolute;
    top: 35%;
    left: 55%;
    transform: translateX(-50%) translateY(-50%);
}

HTML file

{% block content %}



    <div class="container_1">
        <div class="panel_1">
            <form class="form-horizontal" action="{% url 'left' %}" method="get">
                <input type="submit" value="left" class="btn btn-lg btn-primary">
            </form>

        </div>
    </div>

    <div class="container_2">
        <div class="panel_2">
            <form class="form-horizontal" action="{% url 'right' %}" method="get">
                <input type="submit" value="right" class="btn btn-lg btn-primary">
            </form>
        </div>
    </div>


{% endblock %}

Any suggestions on how to adjust the code to achieve the layout depicted in the image above?

Answer №1

Perhaps this solution could be beneficial for your needs? I have introduced custom containers to house the styles I created in order to prevent any interference with Bootstrap classes, which are not meant to be altered

.main-container {
  height: 100vh;
}

.container_1,
.container_2 {
  min-height: 100%;
}

.full-btn {
  height: 50vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
  <div class="main-container">
    <div class="row h-100 justify-content-around align-items-center">
      <div class="col-3 h-50">
        <div class="container_1">
          <div class="panel_1">
            <form class="form-horizontal" action="{% url 'left' %}" method="get">
              <input type="submit" value="left" class="btn btn-lg btn-primary btn-block full-btn">
            </form>

          </div>
        </div>
      </div>
      <div class="col-3 h-50">
        <div class="container_2">
          <div class="panel_2">
            <form class="form-horizontal" action="{% url 'right' %}" method="get">
              <input type="submit" value="right" class="btn btn-lg btn-primary btn-block full-btn">
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №2

It appears that by mentioning Bootstrap in your title, you are interested in utilizing Bootstrap 4's flex box utility classes for a straightforward solution.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1P" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT79abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz01" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="container-fluid">
  <div class="row justify-content-center align-items-center h-100">
    <div class="col-4">
      <div class="bg-dark text-center">
        
        <form action="#null" method="get" class="mb-0">
        <button type="submit" class="btn btn-lg btn-primary">A</button>
        </form>
        
      </div>
    </div>
    
    <div class="col-4">
      <div class="bg-dark text-center">
        
        <form action="#null" method="get" class="mb-0">
        <button type="submit" class="btn btn-lg btn-primary">B</button>
        </form>
        
      </div>
    </div>
    
  </div>
</div>

The key classes used here are justify-content-center and align-items-center. The former centers child elements horizontally within the container, while the latter does the same vertically.

The h-100 class ensures the row takes up 100% of the page's height. To have it occupy space only after the first 35% of the page, a custom class like this might be useful:

.my-custom-class {
  margin-top: 35%;
}

If you want the button to fill 100% of the width, consider using the .btn-block class: Bootstrap 4.x Documentation: Button Sizes

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

Modify database field according to user selection through dropdown menu

My website allows the admin to assign a job to a user, and that job automatically gets the status "waiting to be accepted." I've added a dropdown box on the job page for users to either accept or decline the job. However, I'm uncertain about the ...

Is there a reason why it's not feasible to merge child/descendant and grouping selectors together?

Understanding the rules of constructing selectors that consist of more than one selector can be quite challenging. It seems that sometimes it is possible to create such selectors, which may contain instances of other selectors made up of multiple parts. H ...

I'm looking for assistance in setting up a static sidebar within a two column layout. Any takers

Seeking assistance to fix the issue of making the right sidebar static when user begins scrolling. I am relatively new to responsive web design. I have attempted using the "position:fixed" property, but uncertain about its compatibility with responsive d ...

Circular JQuery carousel tracking

I am struggling to figure out how to create a looping effect for the images inside the slider. I would like the track to seamlessly loop around from both sides after they are hidden. Here is the link to the current jsfiddle: http://jsfiddle.net/V2x6s/3/ B ...

Guide on deactivating the HTML drawer layout on a website with Selenium using Java

I am currently working on automating a website and I have encountered a challenge with a drawer Menu Panel. My goal is to open the menu, verify certain elements within it, and then close or hide it. However, I am facing difficulty in closing/hiding this d ...

Background images are not appearing in Rails production environment

There seems to be an issue with my images not showing up in the production environment (link here). Here is a snippet of my code: _header.html.erb <%if action_name == 'index' %> <div class="main_header_bg" id="main_head ...

Radio buttons failing to transfer any data

I am experiencing an issue with setting values for radio buttons and using the $_POST variable to read the value, but the value being set is empty. <form style="text-align:left;margin-left:80px;" class="form-inline signup" role="form" method="post" act ...

What is the best way to retrieve the value of an element enclosed within a tag?

<div class="player__AAtt"> <div> <play-js data-account="1234567890" data-id="32667_32797"> </play-js> </div> </div> Is there a way to extract the values of data attr ...

Achieving full page height with div, iframe, and footer components

Feeling a bit stuck with this problem and hoping for some help. I did some searching on SO but couldn't find a solution that fits my needs. Below is a snippet of the markup I'm working with: <div id="wrapper"> <div id="content"> ...

What is the best way to create a column that adjusts its width based on the row width in a table?

Here is the code snippet that I am working with: return ( <Box sx={{ height: 400 }}> <DataGrid columns={columns} rows={rows} /> </Box> ) Currently, my table appears like this: https://i.sstatic.net/KWXwL.png I am aiming for the ...

enclosing the pre tag within a table cell

Despite using the latest Twitter Bootstrap, I am facing some issues with pre tags inside table cells. When a line is too long, it should create a horizontal scroll, but unfortunately, that does not happen. Check out this example without using the pre tag. ...

What steps should I take to utilize an external servlet that is already in place?

Our form has 2 input fields available for users to enter destinations for flight planning purposes. We are looking to integrate an external servlet that offers autocompletion functionality on the fields. For example, when a user types "LO", the servlet wi ...

What is the best way to re-render a component immediately following an update in React?

As I attempt to change the color of a bar to green and then back to black, it seems like the latter color update is taking precedence in my code. const [color, setColor] = useState("black") const bubbleSort = async () => { const sleep = ms => ...

What is the best way to align an HTML DIV with a logo on the left side?

I am trying to position a div called #top_menu in the center of the page. <div id="top_menu">This is the menu and it is centered on the page</div> Here is my CSS code: #top_menu { margin: 0 auto; width: 600px; ... } Next to the top menu, I ...

Looking to verify a disabled select element and adjust the opacity of that element when it is disabled

$_product = $this->getProduct(); $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); ?> <?php if ($_product->isSaleable() && count($_attributes)):?> <dl> <?php foreach($_attrib ...

Booststrap offers a variety of options for adjusting the top

Is it possible to add a margin top to the class property of the div? If so, how can this be achieved? <div class="mb-md-3 ml-md-3 col-lg-8" > <!-- <div class="mb-md-3 ml-md-3 col-lg-8" > --> ...

Adjust the background hue of the Row in the Table

Is there a way to update the background color of a Table Row? I attempted using "BackgroundColor" but didn't see any changes Any suggestions on how to fix this issue? <TableRow style={{ backgroundColor: "#FF0000" }}> <TableCell& ...

Combine several elements in a single jQuery scrollreveal function

I am currently working on a webpage that utilizes the jQuery library plugin scrollreveal to gradually reveal different html elements when the page loads. The functionality of the code is working correctly at the moment, but I have noticed that there is a d ...

Chrome does not recognize the 'position: fixed' style attribute

Encountered an issue in Chrome where the position:fixed property does not behave as expected. While this code works fine in Firefox and Safari, it seems to have a glitch in Chrome (69.0.3497.100). When scrolling, the blue div moves and reveals the green ba ...

Is it feasible to select the last child of an input without having to specify the submit

Here is a portion of the HTML code I am currently testing: <form action="#" method="post> <input name="username" type="text" placeholder="Username" /><br /> <input name="password" type="password" placeholder="Password" />&l ...