Combining sticky user input and button side by side using CSS

I'm having trouble getting a user input and button to appear side by side and stay sticky at the top of the screen as you scroll. I've attempted using the float: left method and display:inline-block, but haven't had any success. Here is the code snippet:

<div id="user-input-and-button">
    <input id="user-input" />     
    <button id="send-button" onClick={writeUserData}>send</button> }
</div>

Here's what I've tried:

#user-input{
  width: 70%;
float:left;
height:auto;
padding: 12px 20px;
border: 1px solid #2c2c2c;
border-radius: 40px;
font-size: calc(5px + 4vmin);
position:fixed;

}


#send-button{
float:left;
width: 7%;
height:auto;
  min-height: 30px;
  min-width: 30px;
  position:fixed;

}

And also:

#user-input{
  width: 70%;
height:auto;
padding: 12px 20px;
border: 1px solid #2c2c2c;
border-radius: 40px;
font-size: calc(5px + 4vmin);
position:fixed;
display:inline-block;
}


#send-button{
width: 7%;
height:auto;
  min-height: 30px;
  min-width: 30px;
  position:fixed;
  display:inline-block;
}

Answer №1

.group{
position:fixed;
top:0;
background:gray;
padding:10px;
width:100%;
}
.mt{
margin-top:50px;
}
<div id="user-input-and-button" class="group">
      <input id="user-input" />     
     <button id="send-button" onClick={writeUserData}>send</button>
</div>
<div class="mt">
<p>
Lorem Ipsum is not random text, it has roots in classical Latin literature from 45 BC, making it over 2000 years old. Lorem Ipsum originates from the works of Cicero, a treatise on ethics dating back to 45 BC.
</p>
<p>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in classical Latin literature and dates back over 2000 years. Richard McClintock, a Latin professor, discovered the source of Lorem Ipsum in the writings of Cicero from 45 BC.
</p>
<p>
Lorem Ipsum is derived from a piece of classical Latin literature, specifically from the work of Cicero dated back to 45 BC. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a passage in section 1.10.32.
</p>
<p>
The origin of Lorem Ipsum can be traced back to classical Latin literature from 45 BC. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a passage by Cicero.
</p>
</div>

Execute the code snippet above

Answer №2

To arrange them next to each other, you can utilize the display: flex property. And for affixing them in place, employ position: sticky.

body {
  height: 200vh;
  margin: 0;
}

#user-input-and-button {
  display: flex;
  position: sticky;
  top: 0;
}

#user-input {
  padding: 12px 20px;
  border: 1px solid #2c2c2c;
  border-radius: 40px;
  font-size: calc(5px + 4vmin);
}

#send-button {
  min-height: 30px;
  min-width: 30px;
}
<div id="user-input-and-button">
  <input id="user-input"/>
  <button id="send-button"/>
</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

React component failing to update upon rerender

I've encountered an issue with my Flux setup where the component doesn't rerender when adding a new Todo, although it does when deleting or changing the checkbox. I find this behavior confusing and wonder what might be causing it. The list itself ...

"Accessing and updating the current navigation state using jQuery/ajax when clicked

Currently, I'm working on a website project where I am using a jquery/ajax script to dynamically pull in content from another page with a fade effect when clicking on a tab in the navigation menu. You can check out the effect on the page here. Only th ...

What method can I use to modify an existing decorator directive in AngularJS to automatically select all checkboxes?

After implementing a decorator directive to create a checkbox list following the solution provided in a previous answer on Stack Overflow, where can I find further guidance on adding a main checkbox that toggles the selection of all checkboxes? Is it poss ...

Set up dynamic restrictions for the length of an HTML paragraph with PHP

I am attempting to dynamically set the limit of content retrieved from a database in PHP. Specifically, I need to trim the input from the database before assigning it to an HTML paragraph. I attempted to use the substr function but was unsuccessful. Can y ...

Is it possible to enhance controllers in Sails.js through extension methods?

There are times when I need to execute a certain action on every page of my web application or make a specific method available to all controllers. Previously, in object-oriented MVC frameworks, I would have my controllers extend a base controller and de ...

(JS) utilizing an external .js function by using the <script> tag

Looking to execute the function cookiefix() from main.js, which is linked at the bottom of my HTML file. echo '<body>'; if(!isset($_COOKIE['clicked'])) { if(!isset($_COOKIE['count'])) { echo '<script type="text/ ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

Greetings Universe in angular.js

Having trouble creating a Hello World page in angular.js. When I try to display {{helloMessage}}, it shows up instead of Hello World. I'm not sure where the issue lies. Within the folder are two files: angular.min.js and HelloWorld.html. In HelloWorl ...

Utilizing ES6 Proxy leads to the occurrence of an error message stating "this is not a function" whenever a function call

As I ventured into the depths of ES6 Proxies, attempting to be a crafty developer, I found myself entangled in their complexities. My goal was to intercept any get or set operation on a property from a class I had written and ensure that they were stored e ...

Design: A stationary text box positioned on the left side alongside a selection of scrollable MDL cards on the right

I'm currently experiencing a challenge with adjusting the text on two specific pages. On one side, there is a box with text, while on the other are two MDL cards displaying dialogues. The trouble arises when I try to set a fixed position for the text ...

Sending a Thunk to the store using Typescript

Within my primary store.ts file, the following code is present: const store = createStore( rootReducer, composeWithDevTools(applyMiddleware(thunk)) ); store.dispatch(fetchUser()); Upon initial rendering, an action is dispatched to fetchUser in ord ...

Creating user groups with MongoDB and Node.js

Recently I started working with MongoDB and I'm looking to implement a feature using mongoose and nodejs. Specifically, I want to group participants based on the room description. Here is the schema: //schema for rooms.js //ROOMS const schema = mon ...

Revamp responsiveness in bootstrap 3 for printing with @media queries

My goal is to disable the responsive features of bootstrap when the event javascript:print() is triggered. This way, I want my webpage to maintain the column grid layout that I have defined, regardless of screen size. One solution suggested in this answer ...

Resolve Bootstrap columns with varying heights

My Bootstrap row contains a variable number of columns determined by a CMS, sometimes exceeding the space available on one line. I am looking for a way to neatly display all the columns with equal heights. <div class='row'> <div cl ...

Is it feasible for draggable divs to create a trail of lines as they are dragged?

Looking to create an interactive online coach tactic board where you can easily rearrange player positions and demonstrate specific tactics by dragging divs representing players. Utilizing Touch Punch for smooth dragging functionality on PCs, tablets, and ...

Is there a way to transform a string property into a custom type in a TypeScript array?

I am faced with a situation where I have an interface that is extending a MongoDB document, along with some sample data that is also extending that interface. Below is an outline of the interface: export default interface IModel extends Document { _id: Obj ...

Vue.js has encountered a situation where the maximum call stack size has been exceeded

I have implemented a method called cartTotal that calculates the total price of my products along with any discounts applied, and I am trying to obtain the final value by subtracting the discount from the total. cartTotal() { var total = 0; var di ...

Error with NextJS and styled-components in the bundle file

I recently integrated styled-components into my React project. However, when I bundled the react application using rollupjs and tried to use this bundle with NextJS, I encountered an error in NextJS: ReferenceError: window is not defined Upon inspecting t ...

Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself: center: function() { var top=Math.max($window.height() - $modal.outerHeight(),0) / 2; var left=Math.max($window.width() - $modal.outerWidth(),0) / 2; $modal.css({ ...

Combining various types of media stylesheets with a print stylesheet

I'm struggling with a school assignment that is asking me to compare two different ways of linking a print stylesheet with another stylesheet in HTML. The assignment requires me to explain when each technique would be appropriate: <link rel="style ...