Aligning content in the middle vertically with Bootstrap 3

While I understand that this question has likely been asked numerous times before, the solutions provided thus far do not seem to apply to my specific situation. This leads me to believe that there may be an additional factor at play in my content.

<div class="row" style="margin-left:6px; margin-right:6px;" ng-repeat="product in products">
  <div class="col-xs-6 col-sm-6 col-lg-6">
    <a>
      <h4>{{product.name}}</h4>
      <h5>{{product.description}}</h5>
    </a>
  </div>

  <div class="col-xs-6 col-sm-6 col-lg-6">
    <span class="pull-right">
      remove&nbsp;
      <i class="glyphicon glyphicon-remove pull-right"></i>
    </span>
  </div>
</div>

In attempting to vertically center the "remove [x]" icon in the second column with respect to the first column, I have encountered a challenge. Currently, the icon appears to be centered at the top rather than vertically in the middle. My initial attempt to resolve this issue involved the following adjustment:

<div class="col-xs-6 col-sm-6 col-lg-6" style="float:none; display:table-cell; vertical-align:center">
    <span class="pull-right">
      remove&nbsp;
      <i class="glyphicon glyphicon-remove pull-right"></i>
    </span>
  </div>

Regrettably, this approach did not yield the desired outcome. It seems I may have overlooked a crucial detail or made an error in implementation. Any insights or guidance on this matter would be greatly appreciated.

Answer №1

To start, my suggestion is to include an additional class to the columns you intend to manipulate. While it may not have an impact in your basic example, as you add more elements, you will appreciate the benefits of organizing elements based on their reusable class.

Therefore, the initial step involves a minor adjustment to your HTML. Though not entirely necessary since you could easily target (for instance) .col-xs-6, adopting a general approach by adding a class to that row and removing the inline style would be advisable.

<div class="row" style="margin-left:6px; margin-right:6px;" ng-repeat="product in products">
  <div class="col-xs-6 col-sm-6 col-lg-6 midvert">
    <a>
      <h4>{{product.name}}</h4>
      <h5>{{product.description}}</h5>
    </a>
  </div>

  <div class="col-xs-6 col-sm-6 col-lg-6 midvert">
    <span class="pull-right remove">
      remove&nbsp;
      <i class="glyphicon glyphicon-remove"></i>
    </span>
  </div>
</div>

By implementing the above adjustments, we can now selectively target Bootstrap elements without interfering with the flow of similar re-usable classes in subsequent uses. To vertically align them, simply assign a vertical-align property like so:

.midvert {
    display:inline-block;
    vertical-align:middle
  }

Note how the vertical alignment is applied to the CONTAINING BLOCK ELEMENT, rather than the element itself. However, this alone will not produce the desired effect, as our .remove element requires a defined height, which we rectify by including the following:

.remove {
    line-height: 4; 
  }

While the choice of using single numbers for line-height values varies (lending options like px, rem, em, %), I personally opt for single numbers. The reasoning behind this preference is outlined here, but feel free to utilize what suits you best. You'll observe that a number between 4 and 4.5 provides an ideal fit when resizing the window.

I've created a modified Bootply to illustrate the issue.

Answer №2

To achieve the desired outcome, consider incorporating a line-height property into your "remove" link.

For instance, if you apply the following class to your link:

.remove {
  line-height: 40px; 
  vertical-align: middle;
}

Your link will now be properly aligned.

Example Demo

Answer №3

Check out this JSFiddle I put together for you. Hopefully it meets your needs.

Take a look at the CSS tab where I included display:table-cell; for the col-sm-6 CSS class as well.

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

Tips for creating Viewport dimensions in Tailwind CSS

As I delve into the depths of the official Tailwind CSS documentation, I come across a valuable nugget of information: Embrace w-screen to effortlessly extend an element to occupy the full expanse of the viewport. Initially, w-screen proves handy as I b ...

Is there a way to remove the grey overlay when clicking on an <a> tag?

I want to develop a Hybrid app. On a webpage, I have buttons displayed in the following format: <a href="#"> <div style = "height: 50px; width: 50px; background-color: #00ff00; margin: 32px"></div> </a> Whenever I tap and hold ...

Expanding Your Django Template Abilities

Dealing with a fairly simple issue here. I've got a web page template that includes some common CSS styles at the top. <html> {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}"> .... *n ...

Dynamic Divider for Side-by-Side Menu - with a unique spin

I recently came across a question about creating responsive separators for horizontal lists on Stack Overflow While attempting to implement this, I encountered some challenges. document.onkeydown = function(event) { var actionBox = document.getElementB ...

What methods are available in JavaScript for determining the width of a section and implementing proper spacing for a menu?

Is it possible to use Javascript to dynamically adjust the size of each <li> element within the menuArea so that they fill the entire space correctly? // Could we achieve this with the following code... var menuSpace = Math.floor((menuAreaWidth ...

How to customize CSS based on the specific Django app you are using

I am new to Django and currently customizing the admin section. I want to change the CSS based on the app I am working with. Is this possible? I've noticed that the system uses the CSS from the first static folder it finds. Is there a workaround for t ...

When using React <p> and Tailwind, the text overflow occurs in either the X or Y axis, despite specifying Y specifically

I am building a React frontend using TailwindCSS 2.2, and I have these two texts inside a <p> dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd ...

The Django static CSS won't load properly on the webpage

Despite browsing various posts related to my issue, I have been struggling to resolve the problem at hand (for instance, following this informative post: Django static files (css) not working). The challenge lies in getting my static CSS file to load prope ...

Is there a way to achieve a double background color effect in CSS while ensuring that the text is centered within the second background color element?

How can I achieve a layout similar to the one shown in this image: ul menu li tags Should I use two tags for each element? Here is an example: <ul class="menu"> <div class="outside"><li class="inside"><a href="#">Firefox</a ...

The renderToString function in Material UI's sx property doesn't seem to have

Every time I apply sx to a component that is rendered as a string and then displayed using dangerouslySetInnerHtml, the styles within the sx prop do not work. Here is an example showcasing the issue: Codesandbox: https://codesandbox.io/p/sandbox/wonderfu ...

Comparison between Filament Group's loadCSS and AJAX technologies

The loadCSS library developed by Filament Group is widely recognized as the standard for asynchronously loading CSS. Even Google recommends its use. However, instead of using this library, some suggest utilizing ajax to achieve the same result. For example ...

resetting the page's scroll position using a hamburger icon

After adding a hamburger icon to my web project, I noticed that whenever I click on the icon, it scrolls back to the top of the page. I tried adjusting the margin and padding properties, but it still behaves the same way. Even after removing the animation ...

Place a list with scrolling and overflow features side by side

Having trouble getting my headers to scroll with overflow auto and white-space nowrap. Can't figure out why it's not working. I want to create hyperlinks within headers to link to specific parts of the website. I have the code for hyperlinking s ...

Progress Bar Rating System in Bootstrap 4

Trying to achieve a specific layout using Bootstrap 4: https://i.sstatic.net/Ym6Ov.jpg Disregarding "See All" and "4,327 Ratings" Progress so far has led to: https://i.sstatic.net/hPSRn.png The numerical ratings can be replaced with font icons. Provi ...

Semantic UI Footer allows you to easily create a stylish

Greetings! I'm new to the semantic-ui framework and I'm encountering an issue with the footer. My goal is to have the footer always stay at the bottom of the page without being fixed. Here's a snippet of my simple HTML code: <!DOCTYPE h ...

Removing Shadow, Border, and Outline from Bootstrap v5.0.2 Navbar

I have been struggling to get rid of the shadow/outline/border on my navigation bar. Despite trying various CSS tweaks, I have not been successful. Below is the specific HTML code: <nav class="navbar navbar-expand-sm" id="site-navigation ...

Can CSS be used to eliminate shadows from a particular image?

Is it possible to eliminate the shadow from a specific image that is displayed multiple times on various pages of a website? This particular image should not have any shadow. Link to Image The image can be found on pages like this one: Page Link Below ...

What is the best way to make translateX transition function in Firefox?

I am encountering an issue with my code working in Chrome but not Firefox. Can anyone offer suggestions on how I can address this problem? The intended functionality is for the door to open when hovered over and for the landscape image to scroll across. Ho ...

Possible conflict between CSS and JavaScript on Magento website

Problem Description: Visit this link for more details I am facing an issue with the product upload process when using certain attributes. It appears to be related to a positioning problem. Despite trying various solutions, such as removing the position at ...

Ways to shift text upwards while scrolling in a downward direction?

I'm a beginner with jQuery and I could use some assistance. My goal is to have the text move upwards and a static box slowly disappear as you scroll down the website. Something similar to what you see here: p, body { margin: 0; padding: 0; } b ...