What is the best way to place an <a> tag and a <p> tag side by side in an HTML

I have a question on organizing this HTML code:

<ol class="results-list">
    <% @results.sort_by { rand }.each do |result| %>
        <li class="<%= 'checked-out' if result.catalog_item.library_status == 'Checked out' %>">

            <div class="search-result">
                <%= link_to 'View', mobile_library_submission_checkout_path(result, search_term: params[:q], theme_id: @search_theme ? @search_theme.id : nil), method: :get, class: 'btn red view' %>
                <p class="title"><%= result.title %></p>

                <span class="meta">
                    <p class="display-name"><%= result.display_name %></p>
                    <% if result.theme %>
                        <p><%= result.theme.name %></p>
                    <% end %>
                    <p><%= result.location_string %></p>
                    <p class="tag-list"><%= result.tag_list.join(" / ") %></p>
                </span>
            </div>
        </li>
    <% end %>
</ol>

My issue is with aligning the link_to button and the p tag containing result.title. I've attempted to use inline display but haven't been successful. How can I achieve this?

.results-list {
    display: block;
    font-family: $reckless-medium;
    max-width: 400px;
    margin: 1em auto 0;
    counter-reset: item;
    list-style: none;

    .checked-out {
        color: $light-text;

        a {
            color: $light-text;

            .title { color: $light-text; }
        }

        .icon-caret-right-grey { background: none; }
    }

    .search-result {
      width: 40%;

      .view {
        display: inline-block;
        padding: 2% 14% 2% 14%;
        margin-left: 1%;
        margin-right: 2%;
        color: $white;
      }

      a.view {
        display: inline-block
      }

      p.title {
        display: inline-block
      }
    }

The current layout is shown in these images:

https://i.sstatic.net/8SotC.png

and

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

However, my goal is to position the view button next to the title text on the right side. There should be sufficient space for the view button on the right side of the title. How can I achieve this layout?

Answer №1

When using inline-block elements, they have the ability to be displayed on a single line but will naturally expand based on their content size within the container.

To ensure that inline-block elements remain on a single line, you can set a specific max-width for both elements so that they fit on one line. Keep in mind that if white-space characters are present between the elements like

<div></div> <div></div>
, setting both to 50% width may push them onto a new line.

.container {
  display: block;
}

.a {
  display: inline-block;
  max-width: 45%;
  background-color: #800;
}

.b {
  display: inline-block;
  max-width: 45%;
  background-color: #080;
}
<div class="container">
  <div class="a">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
  <div class="b">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</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

unable to reset contact form functionality

I need to implement a Contact Us form on my website. The form should clear the text fields after submission and display a Thank You message. Below is the HTML code: <div class="form"> <div id="sendmessage">Your message has been sent. Thank yo ...

Unable to navigate a simulated scrollbar

As someone who is new to web development, I am embarking on the journey of building a UI Grid that can effectively display a large amount of data. My goal is to implement a scrollbar that allows for horizontal scrolling across approximately 1,000,000 data ...

The button is not centered within the grid container

I need assistance centering the button within a Grid layout using Material-UI <Grid container style={{backgroundColor:'green'}} alignItems="center" justify="center" > <Grid alignItems="center" justify=&q ...

What is the best way to dynamically insert an image into an <li> element?

I am looking to enhance my menu by adding an image below each li item as a separator. How can I achieve this using CSS? I want the images to automatically appear whenever a new li tag is added to the ul. Here is the HTML code: <ul class="menu"> ...

Embedding complex JSON information into an HTML dropdown menu

Here is a JSON string that I am working with: { "electronics": { "cameras": [ "sony", "nikon", "canon" ], "TV": "none", "laptop": [ "hp", "apple", "sony" ] }, "home": { "furniture": [ ...

Stopping videos in the JQuery Cycle Plugin triggers the stop event

Currently, I have a simple cycle set up with YouTube videos using the following code: <script type="text/javascript"> $(document).ready(function () { $('#cycle').cycle({ fx: 'fade', sp ...

Display the modal in Angular 8 only after receiving the response from submitting the form

I am encountering an issue where a pop-up is being displayed immediately upon clicking the submit button in Angular 8, before receiving a response. I would like the modal to only appear after obtaining the response. Can someone assist me with achieving thi ...

Ways to adjust the text size in jqGrid?

The current theme is ui-lightness. How can I adjust the font size within the grid? Your suggestions are appreciated. Many thanks. ...

What is the best way to integrate my custom JavaScript code into my WordPress theme, specifically Understrap?

I am looking to enhance my website with a sticky navbar positioned directly under the header, and I want it to stick to the top of the page as users scroll down. Additionally, I want the header to disappear smoothly as the user scrolls towards the navbar. ...

The confusion between <g:if> and <g:set> features in Grails is causing some issues

I am working with a gsp template that includes a variable. If this variable is true, I want to add an extra button; if it's false, nothing should happen. What could be causing my code to not work correctly? <g:set var="removeButton">{{ removeB ...

Monitoring page reload with JavaScript

Here is an example of tabbed content: <div class="tab"> <button class="tablinks" onclick="openCity(event, 'NewYork')" id="defaultOpen">New York</button> <button class="tablinks" onclick="openCity(event, 'LosAngeles& ...

Could you explain the distinction between Node.bind() and Template Binding?

Currently, I am exploring the documentation for Google Polymer and have come across two types of data binding - Node.bind() and Template Binding. Can someone please explain the distinction between Node.bind() and Template Binding to me? ...

The alignment of the image background is malfunctioning on the iPad 3 device

Hey there, I need some assistance resolving an issue with a header background image on iPad3. The problem is that the image is not displaying correctly on iPad3 browsers, even though it works fine on desktop browsers. Below you can find my CSS and HTML cod ...

Share the name of the Quasar component with the Vue 3 render function

I'm struggling to dynamically create a Vue 3 app with different components specified by name, such as "div", "button", or Quasar's "q-btn". When I try to pass the latter to Vue's render function Vue.h, I encounter difficulties. <html> ...

Issue with jQuery 'on' event not triggering following 'load' event

I am facing an issue on a page where similar events occur but when new content is loaded halfway through, most of the jQuery functionalities stop working. The scenario involves answering questions in a 'game' format using AJAX calls. Once all que ...

JavaScript code using the Jquery library to retrieve the following article from 'cached memory'

I am aware of the easier CSS options for my question, but I am determined to learn JS (Jquery), so please bear with me. My plan: Menu-items are connected to articles within my content division. Initially, only the first article (#intro) is displayed. All ...

The canvas map has an unseen boundary on the right, causing all other div sections to be

When I set the width of my google map canvas to 80%, the map itself fills that percentage but leaves a wide space to the right of the canvas, taking up the remaining 20% of the screen. This creates an issue because my div cannot be placed in that space. ...

I am interested in incorporating a vertical scrollbar into a gridview in ASP.NET

I've successfully bound data to a grid view in my ASP.NET application. I've implemented pagination, but instead of that, I'd like to incorporate a scroll bar to prevent lengthy paging. Below is the code for my grid view: <div class="box ...

Customizing nvd3 chart colors with CSS: A step-by-step guide

Currently, I am faced with a challenge in SugarCRM as I try to modify the colors of nvd3 charts using CSS. The pie chart colors are structured differently compared to other nvd3 charts, making it difficult for me to figure out how to customize them through ...

Retrieve the child DIV element within its sibling without using an identifier

I have a setup similar to the following: <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">Tiger128 (v2)</h3> </div> <div class="panel-body"> <inp ...