Bootstrap3 and jQuery are attempting to achieve vertical alignment

I am trying to vertically align Bootstrap col-md* columns. I have two blocks, one with text and one with an image, and I want them to be equal in height with the text block vertically centered. My current solution is not working correctly. Can someone please help me?

HTML

<div class="row">
  <div class="col-md-6">
    <div class="some-text">Some text</div>
  </div>
  <div class="col-md-6">
    <div class="some-img"><img src="..." width="100%"></div>
  </div>
</div>

CSS

.some-text {
    position: absolute;
    top: 50%;
    transform: translate(0,-50%);
}

JS

$('.some-text').each(function() {
  var newH = 0;
  $(this).parent().siblings().each(function() {
    if ($(this).height() > newH)
    {
      newH = $(this).height();
    }
  });
  $(this).parent().css('height', newH);
});

Answer №1

Implementing this layout can be achieved using the power of CSS Flexbox

View Example on Fiddle

@media(min-width: 992px) {
  .content {
    display: flex;
    align-items: center;
  }

  .box img {
    width: 100%;
  }
}
<div class="row content">
  <div class="col-md-6 box">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque omnis quaerat assumenda. Sed nam molestiae eligendi. Ratione labore doloremque exercitationem id, reprehenderit ipsum! Totam et veritatis sapiente! Iusto, molestias debitis!</p>
  </div>

  <div class="col-md-6 box">
    <img src="http://placehold.it/450x250">
  </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

Showing the heading again after a new page starts

Currently, I am using WeasyPrint to generate a document and facing an issue with long sections that may span multiple pages. When a section breaks across pages, the name of the section does not display after the page break as intended. The following examp ...

Upon opening index.html in the browser, the jQuery code fails to execute, as no errors are displayed in the console

I am struggling to make a simple toggleClass() function work in jQuery and I can't seem to figure out why. This is just a beginner exercise for me with jQuery. The code works perfectly when I test it as a snippet or manually in the console, but when I ...

Display a DIV next to the mouse cursor when a span is hovered over

Is there a way to make a DIV element appear at the mouse cursor when a user hovers over a SPAN or another DIV? I attempted to create a function for this purpose, but unfortunately, it does not seem to be working properly even though jQuery is correctly lo ...

Refresh the Datatable with the click of a button

Despite my efforts and multiple attempts at different solutions, I am unable to get this particular issue resolved. It seems that my lack of experience with javascript/UI is hindering me from achieving the desired outcome. The background function in quest ...

What is the most effective method for tracking file upload progress using SSE?

I am currently working on creating a file upload progress feature that is compatible with older browsers using AJAX. In HTML5, there is the xhr.upload.progress event which works well for modern browsers, but I need an alternative for non-XHR2 browsers. I h ...

Is it possible to merge the bind event for two selectors using jQuery?

I have the following: $('#loginLink') .bind('click', accessLinkClick); $('#registerLink') .bind('click', accessLinkClick); function accessLinkClick(e) { e.preventDefault(); $('# ...

What is the process of choosing a language (English/French) within a pop-up?

<body style="text-align:center"> <h2>Popup</h2> <div class="popup" onclick="myFunction()">Interact with me to show/hide the popup! <span class="popuptext" id="myPopup">A Simple Popup!</span> </div> <script& ...

Is animation not functioning for SVG path within a clip path on Firefox?

Creating the HTML for a unique effect: <svg class="svg-defs2" version="1.1" xmlns="http://www.w3.org/2000/svg" height="200" width="640"> <defs> <clipPath id="clipping2"> <!--Adjusting the x-coordinate of start will expand ...

What is the best way to display a SolidJS component on the screen?

I am currently working on a unique menu design for my application. The concept involves rendering a functional component within an element created in the body using createRoot and render methods. https://i.stack.imgur.com/g6Ofv.png export function create ...

JavaScript/CSS memory matching game

Just starting out in the world of programming and attempting to create a memory game. I've designed 5 unique flags using CSS that I want to use in my game, but I'm feeling a bit stuck with where to go next. I understand that I need some function ...

Increase the height of the div element by a specified number of

Is there a way to dynamically expand a div's height using CSS without knowing its initial height? I want to increase the height by a specific amount, such as "x px taller" regardless of its starting size. For example, if the div starts at 100px tall, ...

color of the foreground/background input in a dropdown menu that

I am attempting to modify the foreground or background color utilized by a dash searchable dropdown when text is entered for searching in the list. The input text is currently black, which makes it extremely difficult to read when using a dark theme. Wit ...

Repetitive Jquery Ajax requests

Does anyone know how I can retrieve data from a server in batches of 50 records until all records have been fetched? var isDataAvailable = true; var startingIndex = 1; while (isDataAvailable) { // Fetch the next batch of records $.ajax({ ur ...

Display a preview image at the conclusion of a YouTube video

I am currently working on an iOS device and have a Youtube video thumbnail that, when clicked, disappears and the video automatically plays in fullscreen mode using an iframe. It's working perfectly. Now, I would like to know how I can make the thumb ...

jQuery AJAX callback fails to trigger

I am encountering an AJAX request in my code: $.ajax({ url : "proxy.php", type : "POST", data : xmlData, contentType : "application/x-www-form-urlencoded", processData : false, success : function(data) { // successful response ...

Unable to choose the specific div element within the container that has an inset shadow applied by utilizing Pseudo

It seems that I am encountering an issue when trying to select the div elements within a container that has a defined shadow using pseudo elements ::before OR ::after. Once the shadow is applied, the div elements become unselectable. Please refer to the c ...

Tips for creating custom CSS to target a specific element within a group of similar elements

My cssSelector for locating elements is div.formErrorContent. There are 4 matched elements, but I need to pinpoint the first element. Can anyone assist me with this? I am aware of how to write the xpath code for this: (//div[@class='formErrorContent ...

Content within a Row of a Data Table

Hello! I am just starting to learn JavaScript and jQuery. Can you help me with an issue I am experiencing? Basically, I have a table and I need to identify which tr contains a td with the text "Weekly", "Daily", or "Monthly". Once I locate that specific t ...

What is the most effective method for dimming or disabling a Material-UI div?

Within my application, there are instances where I need to dim and disable the mouse events for certain div elements based on the component's state - such as when it's loading. My initial approach involved creating a helper function that generate ...

Overriding the !important declaration in inline styles within various CSS IDs and classes

I need to find a way to override an inline !important declaration that is nested inside multiple CSS ids and classes. Let's analyze the following situation: <div class="A"> <div class="B"> <div class="C"> < ...