Ways to create distance between 2 select elements with CSS?

Having trouble with the spacing between two select fields on a form? Specifically, you want the start time, start time meridian, end time, and end time meridian all in a single line. Any suggestions or solutions to this issue would be greatly appreciated!

Here is the HTML code snippet:

<div class="lable_time">
    <div class="stime">
        <select value="Start-Time">
            <option selected value="null">Start-Time*</option>
            <option value="1:00">1:00</option>
            <option value="2:00">2:00</option>
        </select>
    </div>

    <div class="stime_meridian">
        <select value="AM-PM">
            <option selected value="null">AM</option>
            <option value="PM">PM</option>
        </select>
    </div>

    <div class="time">
        <select value="End-Time">
            <option selected value="null">End-Time*</option>
            <option value="1:00">1:00</option>
            <option value="2:00">2:00</option>
        </select>
    </div>

    <div class="time_meridian">
        <select value="AM-PM">
            <option selected value="null">AM</option>
            <option value="PM">PM</option>
        </select>
    </div>
</div>

And here is the corresponding CSS code:

.lable_time select{
    padding: 15px 30px;
    margin: 0;
    width: 50%;
    font-family: 'Oxygen', sans-serif;
    color: #5585C8;
    background: white;
    border: 1px solid #2744B8;
    cursor: pointer;
    font-size: 18px;
    transition: all 0.5s ease-out;
    -webkit-transition: all 0.5s ease-out;
    -moz-transition: all 0.5s ease-out;
    -ms-transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    outline: none;
    -webkit-appearance: none;
    border-width: 0;
}

.stime{
    float: left;
    margin: 30px 0 0 0;
    width: 350px;
}
.stime_meridian{
    float: left;
    margin: 30px 10px 0 -170px;
    width: 220px;
    border-spacing: 30px 10px;
}
.time{
    float: left;
    margin: 30px 0 0 -170px;
    width: 350px;
}
.time_meridian{
    float: left;
    margin: 30px 0 0 -170px;
    width: 220px;
}

Answer №1

To ensure that the items are always in a single line, regardless of viewport width, you can apply the CSS property display: flex to the parent container.

.lable_time select {
  padding: 15px 30px;
  margin: 0px 0px 0px 0px;
  width: 50%;
  font-family: 'Oxygen', sans-serif;
  color: #5585C8;
  background: white;
  border: 1px solid #2744B8;
  cursor: pointer;
  font-size: 18px;
  transition: all 0.5s ease-out;
  -webkit-transition: all 0.5s ease-out;
  -moz-transition: all 0.5s ease-out;
  -ms-transition: all 0.5s ease-out;
  -o-transition: all 0.5s ease-out;
  outline: none;
  -webkit-appearance: none;
  border-width: 0px;
}

.stime {
  margin: 30px 0px 0px 0px;
  width: 350px;
}

.stime_meridian {
  margin: 30px 10px 0px -170px;
  width: 220px;
  border-spacing: 30px 10px;
}

.time {
  margin: 30px 0px 0px -170px;
  width: 350px;
}

.time_meridian {
  margin: 30px 0px 0px -170px;
  width: 220px;
}
.lable_time {
  display: flex;
}
<div class="lable_time">
  <div class="stime">
    <select value="Start-Time">
                        <option selected  value="null">Start-Time*</option>
                        <option value="1:00">1:00</option>
                        <option value="2:00">2:00</option>

                    </select>

  </div>

  <div class="stime_meridian">
    <select value="AM-PM">
                        <option selected  value="null">AM</option>
                        <option value="PM">PM</option>
                    </select>
  </div>


  <div class="time">
    <select value="End-Time">
                        <option selected  value="null">End-Time*</option>
                        <option value="1:00">1:00</option>
                        <option value="2:00">2:00</option>

                    </select>

  </div>

  <div class="time_meridian">
    <select value="AM-PM">
                        <option selected  value="null">AM</option>
                        <option value="PM">PM</option>
                    </select>
  </div>

</div>

Answer №2

Take a look at the link provided below for the updated code:

.lable_time div{
  display:inline-block;
  width:auto;
}
.lable_time select{

    padding: 15px 30px;
    margin: 0px 0px 0px 0px;

    font-family: 'Oxygen', sans-serif;
    color:#5585C8;
     background: white;
     border: 1px solid #2744B8;
     cursor: pointer;
     font-size: 18px;
     transition: all 0.5s ease-out;
     -webkit-transition: all 0.5s ease-out;
     -moz-transition: all 0.5s ease-out;
     -ms-transition: all 0.5s ease-out;
     -o-transition: all 0.5s ease-out;
     outline:none;
     -webkit-appearance: none;
     border-width:0px;

}

.stime{    }

.stime_meridian{    }

.time{    }

.time_meridian{    }
<div class="lable_time">
             <div class="stime">                  
                    <select  value="Start-Time">
                        <option selected  value="null">Start-Time*</option>
                        <option value="1:00">1:00</option>
                        <option value="2:00">2:00</option>

                    </select>

             </div>

             <div class="stime_meridian">
                    <select  value="AM-PM">
                        <option selected  value="null">AM</option>
                        <option value="PM">PM</option>
                    </select>
             </div>


              <div class="time">                  
                    <select  value="End-Time">
                        <option selected  value="null">End-Time*</option>
                        <option value="1:00">1:00</option>
                        <option value="2:00">2:00</option>

                    </select>

             </div>

             <div class="time_meridian">
                    <select  value="AM-PM">
                        <option selected  value="null">AM</option>
                        <option value="PM">PM</option>
                    </select>
             </div>

        </div>

Hope you find this information useful.

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

Click to dynamically toggle classes with jQuery

I am trying to apply a class called 'select' when I click on a paragraph tag. However, the code I have written doesn't seem to be working. Can someone please provide some suggestions? Here is the code: <style type="text/css"> #el ...

Generate a dynamic animation by combining two images using jQuery

My attempt to animate two images is not working. I received some help on Stack Overflow but still facing issues with my CSS and HTML code. This is the code I am using: $(document).ready(function() { $(".animar").click(function() { $("#img4" ...

select2 with customizable multi-column layout

Looking to create a multi-column select2 with a dynamic number of columns by specifying the number of columns as a data attribute in the select tag, or utilizing another method. <select data-cols="2,6,4"> <!--note data-cols--> < ...

Anchor tags are not visible to Jquery

My issue is with integrating JQUERY and PHP. In my external PHP file, I have the following echo statement - `echo ("<a href='#' id='bb'>hello</a>"); Similarly, in my external js file, I have this JQUERY code - $(' ...

Ways to center an image within a div using Bootstrap 4

I am currently using Bootstrap version v4.0.0-beta.3 and facing a challenge with aligning an image in the absolute center of a fixed-height div tag. While I have successfully aligned the image horizontally using the mx-auto and d-block classes, I am strugg ...

Words of wisdom shared on social media

How can I share text from my HTML page on Twitter? This is the code snippet from my HTML page - function change() { quotes = ["Naam toh suna hi hoga", "Mogambo Khush Hua", "Kitne aadmi the?"]; auth = ["Raj", "Mogambo", "Gabbar"]; min = 0; max = ...

Struggle with the background div menu

I am trying to create a menu with a background color, but when I use "background" or "background-color" on the div that contains my menu, I can't see any color. It's frustrating because I know it should be working.. Here is the HTML : <head ...

Ways to distort this shape using CSS?

I've been attempting to replicate this specific block layout, but I can't seem to get it right according to the design. Can anyone provide guidance on how to achieve it? Here is the desired block: https://i.stack.imgur.com/ud0HM.jpg Here's ...

Press the button in an HTML document to activate JavaScript

What could be the issue with my code? I have a button in HTML <a class="btn btn-warning btn-mini publish-btn" href="#" data-rowid="@computer.id" data-toggle="modal" data-target="#myModal">Outdated</a> and my modal <fieldset style="text-al ...

Book of Tales displaying Emotion Styling upon initial load only

I'm currently working with the styled api provided by emotion in order to create a custom styled button. const StyledButton = styled(Button)` background-color: ${theme.palette.grey['800']}; width: 50; height: 50; &:hover { ba ...

Changes influencing independent div elements

My goal is to design a front-end screen with images labeled steps 1-4 lined up next to each other. The concept is that when the user hovers over one of these steps, the corresponding image should fade in below them in a separate div. Upon removing the curs ...

My HTML file seems to be ignoring the styles in my CSS file. What could be causing this issue?

I've started incorporating bootstrap 5 into my html page. Below is the code snippet: <link rel="stylesheet" src="/main_styles.css"> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" cla ...

Issue with input width percentage not functioning as expected

Is there a special method to specify the width of an input field in CSS without it extending beyond the container? I want my input field to be 98% of the container's width, but when I set it to that, it goes off the screen. This is the HTML code I am ...

Creating a split screen layout using Bootstrap

I've been working hard to create a split-screen layout using Bootstrap. After using CSS to achieve it, I realized that it's not responsive for mobile users. Therefore, I'm reworking it with Bootstrap but facing issues with resizing the image ...

Scrollbar visibility issue

Recently, I came across a curious behavior regarding browser scrollbars. To see it in action, check out this link. HTML: <div class='container'> <div class='fix' /> </div> CSS: body { margin: 0; } .container ...

The scroll bar of the Bootstrap modal keeps playing peekaboo every time a new modal is triggered

After trying numerous solutions without success, I am posting this question regarding an issue with the scrolling behavior of Bootstrap modals. Here is the scenario: I have 2 Modals: Modal-1 and Modal-2. When a user is inputting information in Modal-1 ...

Updating URL links with PHP and DOM manipulation

I am attempting to transform all the links within an HTML variable into random ones using PHP. However, I am encountering an issue that is preventing the links from being properly changed. Below is the code snippet I am working with: <?php $jobTempla ...

What are some effective ways to optimize my Javascript and CSS files for a smaller size?

For those dealing with a high volume of Javascript and CSS files, what strategies are recommended for minimizing file sizes? ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...

How come I don't have to specify the relative path in my HTML file to include a JavaScript file when using Express JS?

I am currently in the process of building my very first project from scratch. As I was setting everything up, I ran into an issue with using relative paths to reference my javascript file in my index.html file. Strangely enough, simply referencing the scri ...