Mix up your elements by having some inline and others block, all based on their neighboring elements

In my project, I am working on showcasing a list of 'input' and 'textarea' elements within a flex box arrangement. Each element needs to have a width set to 45% of the flex box. One challenging issue that arises is the following: I am seeking assistance in ensuring that if two items of the same type are present, they should be displayed next to each other on the same line; whereas if they are of different types, they should be displayed on separate lines.

For instance: Two 'input' items side by side will appear on the same line https://i.sstatic.net/vU4PQ.png



When an 'input' and 'textarea' are placed adjacent to each other, they will be displayed on separate lines

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

Answer №1

Delve into the world of CSS selectors as they unlock the answer to your query.

Through CSS selectors, you have the power to:

  • Select all descendants: using a space
  • Select direct children: using the > symbol
  • Select an adjacent sibling immediately after the first: using the + symbol
  • Select an adjacent sibling NOT immediately after the first: using the ~ symbol

Dive deeper into this concept by visiting: Understanding ‘+’, ‘>’ and ‘~’ symbols in CSS Selector

input + input {
  display: inline;
}

 input + input + label {
   display: flex;
 }
 
 label + input {
   display: flex;
 }
<div class="container">
<input placeholder="input1" />
<input placeholder="input2" />

<label>label1</label>
<input placeholder="input3" />

</div>

Answer №2

Feel free to utilize the following code snippet

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <title>Hello, world!</title>
  <style type="text/css">
    body {
      margin: 0;
    }
  </style>
</head>

<body>
  <div class="container">
    <form>
      <div class="row">
        <div class="form-group col-md-6 col-sm-6 col-xs-12">
          <label for="formGroupExampleInput">EAN *</label>
          <input type="text" class="form-control" id="formGroupExampleInput" placeholder="">
        </div>
        <div class="form-group col-md-6 col-sm-6 col-xs-12">
          <label for="formGroupExampleInput2">Family</label>
          <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="F&B">
        </div>
        <div class="form-group col-md-12 col-sm-12 col-xs-12">
          <label for="formGroupExampleInpu3">Standing product reference</label>
          <input type="text" class="form-control" id="formGroupExampleInput3" placeholder="">
        </div>
        <div class="form-group col-md-12 col-sm-12 col-xs-12">
          <label for="exampleFormControlTextarea1">Details</label>
          <textarea class="form-control" id="exampleFormControlTextarea1" rows="4"></textarea>
        </div>
      </div>
    </form>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

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

Choosing attributes-specific tags in BeautifulSoup using Python

import os from bs4 import BeautifulSoup do = dir_with_original_files = 'C:\Users\ADMIN\Desktop\\new_folder' dm = dir_with_modified_files = 'C:\Users\ADMIN\Desktop\\new_folder\\test ...

How can I resolve the issue of "Errors detected in your package-lock.json" in NGRX?

I encountered some problems while trying to set up my Angular project in an NX workspace with NGRX and running it through Jenkins. After running npm install, I started getting errors. Has anyone else experienced errors like these after executing: nx updat ...

Apply the Bootstrap container-fluid class exclusively to extra small devices

Is there a way to utilize the entire width on extra small and small devices (using container-fluid), while only using the container class for other devices? What would be the most effective method for implementing this setup? I attempted using jasnys bo ...

Keep the element anchored to the bottom of the page as the user scrolls

Currently, I'm working on including a div at the bottom of my webpage using this code snippet: http://jsfiddle.net/gyExR/ (huge shoutout to this user) Does anyone know how I can keep this element fixed at the bottom of the page even when scrolling? ...

What is the process for inserting images into a vertical timeline format?

I am currently working on building a timeline and incorporating images into it. I came across a solution on W3 that I'm trying to implement, but I'm facing some challenges. I've been experimenting with the code but I feel like there are part ...

Error message: The Modelviewer is unable to load the local file, displaying the message "Unauthorized to access local resource."

For my WebAR project, I am utilizing Google's ModelViewer. In my vue.js project, I have a component that loads the model using the <model-viewer> attribute. My goal is to set the src attribute of the model-viewer to the absolute path of the .gl ...

Is it possible to open one collapse item and simultaneously close all the others using Bootstrap 5?

Can someone explain how to properly use the Bootstrap 5 collapses component? I want it so that when one collapse item opens, all others close. The documentation mentions setting a parent attribute, but I'm not entirely sure how that works. Here's ...

Implementing text overlays on images within a Bootstrap 4 navigation bar

I am struggling to grasp the flex structure in Bootstrap. I'm attempting to overlay text on an image within the navbar, but it seems the container of the navbar is causing issues. Perhaps my containers are not set up correctly. Any assistance would be ...

Tips for centering a video vertically within a cell

I need assistance with aligning a video in the center of a fixed height cell while hiding the overflow at the top and bottom. Here is what I have so far: <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height ...

Utilizing Jquery to locate the current URL for the purpose of changing or adjusting the class

Looking for a way to use jQuery to dynamically update the margin properties of a specific class based on the current page – whether it's the homepage, admin page, or a common page. The targeted class in question is "breadcrumb." .breadcrumb{margin- ...

What is the best method to showcase the value in HTML using either Angular or JavaScript when the return is true or false?

Just a heads up: The code snippet below is all about logic, so no parameters have been defined. However, they are actually defined and actively used in the same javascript file that I'm diving into. My venture into javascript/angularjs (not quite sur ...

What could be causing this particular issue when using the Firestore get() method? The error message states: "ERROR TypeError: snaps

In my current Angular project, I am utilizing Firebase Firestore database and have implemented the following method to execute a query: findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> { console.log("findArtistBidsAppliedByCurrent ...

Can the footer be adjusted to be full width only?

Is there a way to stretch only the footer on a blogger simple template to full width without altering other elements? I have looked into similar topics, but they mainly focus on WordPress or involve changing multiple elements, which is not suitable for my ...

Retrieving the first five rows from a table with data entries

My task involves working with a table named mytbl, which contains 100 rows. I need to extract only the first five rows when a user clicks on "Show Top Five." Although I attempted the following code, the alert message returned 'empty': var $upda ...

Leverage a JSON variable within jQuery to showcase additional JSON variables

This code snippet is a work in progress, and there is room for improvement. The goal here is to have the content of thumb2 displayed in the #imageLoad container when a user clicks on a thumbnail inside the #placeholder DIV. Keep in mind that both Thumb and ...

Creating an animated diagonal line using TweenMax/EaselJS can add a dynamic element to your designs

I am looking to create a constellation by connecting stars with animated lines. I have attached an image to show the desired end result. The stars and lines are generated using HTML5 canvas and easelJS. Below is the code snippet I have been working on, wh ...

Reading data using the HTML5 FileReader in JavaScript allows for the extraction of information into a

Currently facing a challenge with HTML FileReader. My struggle lies in extracting the data from the FileReader, as most examples I come across utilize the data directly from the built-in function of FileReader. My goal is to retrieve the data from the file ...

Whenever I try to run ionic serve, an error pops up saying: "typescript: node_modules/@types/node/worker_threads.d.ts, line: 8 '=' expected."

My Ionic Info: cli packages: (/usr/local/lib/node_modules) @ionic/cli-utils : 1.9.0 ionic (Ionic CLI) : 3.9.0 Global Packages: Cordova CLI : 9.0.0 (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8f839e88839a8dc180858eacd5c2 ...

"Looking to design a custom UI component complete with animations? Here's how to bring your

I attempted to implement the card component, gradient background, and animation but fell short of achieving the desired outcome as seen in the gif. How can I enhance the smoothness of the animation, replicate the exact gradient, and ensure the card stands ...

Facing a challenge with implementing cascading dropdown lists in asp.net using C#

Hello everyone, I am currently working on implementing a cascading dropdown list in Razor Pages using PostgreSQL. The first dropdown displays the location, and the second dropdown shows the room corresponding to the selected location. However, I have reach ...