Align the button vertically at the center of the div

I am currently working on the front page layout which you can see in the following link: https://i.sstatic.net/CBkFE.png.

My goal is to adjust the position of the create, update, and delete buttons so that they align perfectly with the middle point between "Project: [7] Example Project" and "7 items". However, despite creating a custom class called mybtn and using vertical-align:center property in the CSS code, I have not been able to achieve the desired effect. Any suggestions or tips would be greatly appreciated!

<div>
    <div>
      <h2>Project: [{{items.length}}] Example Project</h2>
        <p>{{items.length}} items
            <button (click)='onDelete()' type="button" class="btn float-right btn-space mybtn">delete</button>
            <button (click)='onUpdate()' type="button" class="btn float-right btn-space mybtn">update</button>
            <button (click)='onCreate()' type="button" class="btn float-right btn-space mybtn">create</button>
        </p>
    </div>

  <table class="table table-bordered table-striped margin">
      <thead>
            <tr>
                <th>select</th>
                <th>No.</th>
                <th>Company</th>
                <th>CCO No.</th>
                <th>Budget Code</th>
                <th>Description</th>
                <th>Award Date</th>
                <th>Sent Date</th>
                <th>Cost Status</th>
                <th>Committed Amount</th>
            </tr>
        </thead>
        <tbody>
          <tr *ngFor="let item of items">
            <td><input type="checkbox" (change)="checkbox(item, $event)" [(ngModel)]="item.flag"></td> 
            <td>{{item.No}}</td>
            <td>{{item.Company}}</td>
            <td>{{item.CCO_No}}</td>
            <td>{{item.Budget_Code}}</td>
            <td>{{item.Description}}</td>
            <td>{{item.Award_Date}}</td>
            <td>{{item.Sent_Date}}</td>
            <td>{{item.Cost_Status}}</td>
            <td>{{item.Committed_Amount}}</td>
          </tr>
        </tbody>
    </table>
  

Here is the relevant snippet from the CSS file:

.btn-space {
      margin-right: 5px;
      margin-bottom: 10 cm;
      margin-top: 10 cm;
  
    }
  
    .table td, .table th, .table input {
      text-align: center;   
   }
  
   .mybtn {
    vertical-align:center; 
   }
  

Answer №1

To achieve the desired layout, you should wrap the h2 in a div with a left float property. Similarly, enclose the buttons in a div with a right float property and add some padding to this div. Lastly, wrap the items counter paragraph in another div and apply clear both on it.

In order to modify your header div:

<div>
    <h2>Project: [{{items.length}}] Example Project </h2>
    <p>{{items.length}} items
        <button (click)='onDelete()' type="button" class="btn float-right btn-space mybtn">delete</button>
        <button (click)='onUpdate()' type="button" class="btn float-right btn-space mybtn">update</button>
        <button (click)='onCreate()' type="button" class="btn float-right btn-space mybtn">create</button>
    </p>
</div>

Transform it into:

<div>
    <div style="float:left;">
        <h2>Project: [{{items.length}}] Example Project </h2>
    </div>
    <div style="float:right; padding:20px;">
        <button (click)='onDelete()' type="button" class="btn float-right btn-space mybtn">delete</button>
        <button (click)='onUpdate()' type="button" class="btn float-right btn-space mybtn">update</button>
        <button (click)='onCreate()' type="button" class="btn float-right btn-space mybtn">create</button>
    </div>
    <div style="clear:both;">
        <p>{{items.length}} items</p>
    </div>
</div>

This adjustment should give you the desired outcome. I hope this information is helpful.

Answer №2

To resolve this issue quickly, you can simply implement the following code:

.mybtn {
  position: relative;
  top: -10px;
}

Using the property vertical-align will not address this problem.

Answer №3

By selecting the bootstrap-4 tag for your question, it seems like you are interested in a Bootstrap 4 solution that doesn't rely on css hacks.

If that's the case, implementing the solution is straightforward and doesn't involve any custom css:

Begin by creating a new row within the main column and placing two columns inside. Your headline and paragraph should go in the first inner column, while the buttons should be placed in the second.

To align the buttons to the right, simply add the classes

d-flex align-items-center justify-content-end
to the buttons column. That's all!

In order to give the buttons some spacing, I replaced your custom css with the standard Bootstrap 4 class mr-1 (margin-right 1 unit).

Lastly, to center the text within the table, I included the class text-center. The table-responsive class ensures that the table layout remains intact on smaller screens.

Below is the complete code snippet which you can run for demonstration:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container">
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col">
                    <h2>Project: [7] Example Project </h2>
                    <p>7 items</p>
                </div>
                <div class="col d-flex align-items-center justify-content-end">
                    <button (click)='onDelete()' type="button" class="btn mr-1">delete</button>
                    <button (click)='onUpdate()' type="button" class="btn mr-1">update</button>
                    <button (click)='onCreate()' type="button" class="btn">create</button>
                </div>
            </div>

            <table class="table text-center table-responsive table-bordered table-striped margin">
                <thead>
                    <tr>
                        <th>select</th>
                        <th>No.</th>
                        <th>Company</th>
                        <th>CCO No.</th>
                        <th>Budget Code</th>
                        <th>Description</th>
                        <th>Award Date</th>
                        <th>Sent Date</th>
                        <th>Cost Status</th>
                        <th>Committed Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let item of items">
                        <td><input type="checkbox" (change)="checkbox(item, $event)" [(ngModel)]="item.flag"></td>
                        <td>No</td>
                        <td>Company</td>
                        <td>CCO</td>
                        <td>Budget</td>
                        <td>Description</td>
                        <td>Award_Date</td>
                        <td>Sent_Date</td>
                        <td>500</td>
                        <td>1000</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

Answer №4

To style the 'mybtn class' in css, opt for middle rather than center:

.mybtn {
  vertical-align:middle; 
}

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

Gatsby causing issues with Material UI v5 server side rendering CSS arrangement

I shared my problem on this GitHub issue too: https://github.com/mui-org/material-ui/issues/25312 Currently, I'm working with the Gatsby example provided in Material UI v5: https://github.com/mui-org/material-ui/tree/next/examples/gatsby After imple ...

choose the initial element within a class

Seeking a way to target the first element within each group of the same class? Consider the following scenario: <div class="group"> <div class="element"></div> <div class="element"></div> <div class="element"&g ...

The image filter plugin is functioning properly in one project, however, it is not working in

After successfully using a plugin from w3schools to filter elements in one project, I encountered an issue when attempting to implement it in another project. Here is the link to the problematic code pen: https://codepen.io/zakero/pen/mZYBPz If anyone ca ...

The property min-width is effective when used with table cells (td), however, it does not produce the

I originally used a table layout for my tabs, but now I need to switch to flex due to new style requirements. However, the min-width concept that worked with tables is not functioning properly with div. In the example below, when you resize the page, the ...

Using the ID selector to create a media query

My website is live, and I'm working on making it mobile-friendly. Most of the site works well on mobile, but I'm having trouble centering a jquery lightbox function using media queries. I'm not sure if my syntax is wrong or if there's a ...

Ways to connect to a minimized hidden tabindex component using the hashtag symbol

I have created a glossary with terms in columns and hidden texts using plain css/html. Each entry is structured like this: A term followed by a hidden explanation, revealed on focus without the use of Java or JS. Instead of using :hover, I utilize :focus ...

Codeigniter form submission resulting in controller receiving incorrect ID from the view

I am new to codeigniter and PHP, and I am trying to delete a record from the database and directory. When I submit the form, it deletes the last item from the table instead of the one I select in the view. Here is my code: view.php <?php foreach ( ...

The background image is not being properly applied by React's makeStyles

Even after attempting various methods to load the image for the backgroundImage property, it still does not appear on the page. Interestingly, loading external images (such as those from Google) works just fine. I experimented with the following: backgro ...

Difficulty in displaying modal using jQuery (bootstrap) programmatically

I can't seem to get my bootstrap modal to display manually using my jQuery code <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="container"> <div ...

Is the homepage the only page rendering correctly?

After working on an MVC project for over 6 months, I consistently faced a problem with HTML and CSS. The homepage always rendered correctly, as shown here: http://prntscr.com/tucp1 However, all other pages had strange white spaces between words, like in t ...

Using CSS to overlay text on an image on a webpage

Can anyone help me with this puzzling scenario? <TD><a href="index-1.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image20','','images/m21.jpg',1)">Home</a><img src="images/m2.jpg" nam ...

What steps should be followed to construct a window identical to the one depicted in the attached image using HTML and CSS?

Check out this link to see the window style I'm trying to recreate using HTML, CSS, and Javascript. No Jquery needed. Thank you in advance. ...

choose image for select box background

Similar Question: Background Image for Select (dropdown) does not work in Chrome I am trying to update the background image of a select box to https://www.google.com/images/nav_logo101.png Currently, I am using the following code: <form> & ...

Bootstrap rows are in need of additional padding

Could someone kindly explain what's incorrect with this? .container { margin-top: 10px; } <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="panel panel-primary"> <d ...

The presence of CSS rollovers on a scrollable div is causing overflow problems

Currently, I am developing an interface that involves a list of items inside a scrollable div. Some of these items have rollover menus that extend beyond the boundaries of the div when hovered over. It is crucial for the website to be compatible with disab ...

Unfortunately, the jquery Calendar plugin is malfunctioning

After much consideration, I decided to implement this calendar plugin on my website Plugin Unfortunately, it isn't working as expected. I am encountering the following error: $(...).pignoseCalendar is not a function at HTMLDocument. (Index ...

Enhance a disabled button by incorporating a ripple effect

I've got a button that toggles on and off depending on the required form fields. When it's enabled, clicking it activates a ripple effect. Now I'd like to have the ripple effect even when the button is disabled. I attempted something simila ...

Has the default underline in Bootstrap 5 been altered?

When working with Bootstrap 4, I noticed that it sets the default text-decoration to be none. However, in Bootstrap 5, just adding a regular anchor tag results in both blue text and an underline being displayed. I was trying to achieve only showing the u ...

Issue with applying style to programmatically inserted Button in JQueryMobile

Is there a way to reapply jQueryMobile's css to a button that is inserted programmatically inside a ListView? When I hard-code the button, the styling applies correctly, but when adding it dynamically, the style is lost. For reference, this is the co ...

The divs are being filled with Bootstrap's separater and list-group components

Check out my pen: http://codepen.io/helloworld/pen/yyoJGR I'm wondering about why the white separator and list grouping are flowing through the divs or the parent row in this code? <div class="container columnStyle"> <div class="row"> ...