Tips for showing an inline <span> within <div> elements

Is there a way to showcase inline spans within a div element while ensuring they are displayed equally in size and space, based on the user's screen? And how can I ensure that these spans are positioned behind other div elements?

body { 

width: auto;
height: auto;
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
overflow: scroll;

    .whole {
        width: 100%;
        height: 400px;
        display: inline-block;
        margin: 0px 0px 0px 240px;
        z-index: -1;
        position: relative;
    }

    #main { 
        background-color: #212121;
        width: 50%;
        height: 400px;
        display: block;

    }

    #second-main { 
        background-color: #424242;
        width: 50%;
        height: 400px;
        display: block;
    }

    #third-main { 
        background-color: #616161;
        width: 50%;
        height: 400px;
        display: block;
    }

    #fourth-main { 
        background-color: #757575;
        width: 50%;
        height: 400px;
        display: block;
    }
    <div class = "whole">
                <span id="main">
                    <p></p>
                </span>

                <span id="second-main">
                    <p></p>
                </span>

                <span id="third-main">
                    <p></p>
                </span>

                <span id="fourth-main">
                    <p></p>
                </span>

                <span id="fifth-main">
                    <p></p>
                </span>
    </div>

Answer №1

<span> is inline by default, while <div> is block by default. However, in your CSS, you are overriding these default styles with display: block (in #main, etc). If you want them to resize as the screen does and behave inline, a more effective approach overall would be to use flexbox:

HTML:

<div class="whole">
  <span id="main">
      <p></p>
  </span>
  <span id="second-main">
      <p></p>
  </span>
  <span id="third-main">
      <p></p>
  </span>
  <span id="fourth-main">
      <p></p>
  </span>
  <span id="fifth-main">
      <p></p>
  </span>
</div>

CSS:

body {
  margin: 0;
  padding: 0;
  border: 0;
  font: inherit;
  font-size: 100%;
  vertical-align: baseline;
  overflow: scroll;
}

.whole {
  width: 100%;
  height: 400px;
  -ms-display: flexbox;
  display: flex;
  margin: 0px 0px 0px 240px;
}

.whole > span {
  -ms-flex: 1;
  flex: 1;
  height: 100%;
}

#main { 
  background-color: #212121;
}

#second-main { 
  background-color: #424242;
}

#third-main { 
  background-color: #616161;
}

#fourth-main { 
  background-color: #757575;
}

(you were also missing a closing CSS bracket for body, and your font-size: 100% was being overwritten in the very next line by a shorthand font rule)

Here is a working fiddle: https://jsfiddle.net/tceqx58x/

Answer №2

In order to have all elements fit into one line, the width must be divisible by the total number of items. For example, if you want to fit 5 span elements in one line, each span should have a width of 20%. (Note that when using percentages, margins can push elements beyond the specified percentage)

It is recommended to use display: inline-block; for the span elements and avoid repeating the style assignment multiple times.

Answer №3

To ensure all spans in the entire class fit properly, consider using display: inline-block instead of display: block and decreasing the width to 20%.

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

Exploring the depths of HTML with the Agility Pack

Looking to extract specific elements from my HTML code: <div id="mailbox" class="div-w div-m-0"> <h2 class="h-line">InBox</h2> <div id="mailbox-table"> <table id="maillist"> <tr> ...

"Adding dots" in a slideshow using HTML, CSS, and JS

I'm currently working on a slideshow that includes navigation dots at the bottom. Although the slideshow is functioning properly, I am encountering an issue where only one dot appears instead of the intended three in a row. Despite my research and att ...

Utilizing Java to Store HTML Content in MySQL

Currently, I am immersed in a project that requires storing webpages in a database. To accomplish this task, I am utilizing crawler4j for crawling, along with Proxool and MySQL Java Connector to establish connections with my database. During testing, an e ...

Angular dropdown button positioned to the left side

I'm encountering a slight issue with a dropdown button while working on making my website mobile-friendly. My goal is to have the button drop down on the left-hand side of it. Below is the snippet of my html code: <!-- A div element for the button ...

Tips for retaining the value of a variable in JavaScript

I am a page that continuously redirects to itself, featuring next day and previous day arrows. Upon loading, the page always displays the new day. The days are stored in a localStorage as follows: for(var i = 0; i < status.length; i++) { local ...

I keep encountering an error stating that parameter 1 for 'FormData' is not of type 'HTMLFormElement'. I am struggling to resolve this issue on my own. Can someone please assist me with fixing this problem?

Here is the snippet of code that I am struggling with: const authForm = useRef(); const handleSubmit = (e) => { e.preventDefault(); //formData let form = new FormData(authForm.current); console.log(form) } This code snippet shows how I added a ...

Ways to align my image to the left and text to the right using Bootstrap

body{ height: 100%; font-size: 1rem ; } .navbar-brand{ font-family: 'Syne Mono', monospace; color: black; size: 0.5rem; } header { width: 100%; height: 100vh; background: rgb(237,104,104); background: linear-gradient(90d ...

Having trouble getting my JavaScript code to function properly on Firefox browser

I have created a script where the cursor automatically moves to the next form field once it reaches its maximum length, in this case 1. Here is the JavaScript code: window.onload=function(){ var container = document.getElementsByClassName("container")[0] ...

Is there a way to disable certain CSS features in Chrome, like CSS grid, for testing purposes?

I'm exploring alternative layouts for my app in case CSS grid is not supported. Is there a way to disable features like CSS grid in Chrome or Canary? ...

Embarking on the journey of creating mobile web applications - where should you begin?

How can I begin learning to create mobile web apps? I have a keen interest in the design aspects, particularly the effects and animations that give them a native appearance. I suspect there are differences in CSS, HTML, and possibly JavaScript compared to ...

Having trouble with the dropdown feature on AngularJs?

Encountering an issue while trying to display the dropdown list. Upon inspecting in Chrome, it seems like the data is loading correctly but when clicked, the dropdown menu does not show up. The data is fetched from the BooksController and all options are ...

TranslateY animation glitching

I need help with expanding a box to 100% height after click, then collapsing and sticking to the bottom. I created a function in Vue framework to handle the animation, but it's not working smoothly. How can I make it less buggy? Check out the demo her ...

Reload a forum in a div without refreshing the entire page

I'm interested in finding out if it's possible for a div to reload similar to how a window does. I have a forum set up, but I don't want the entire page to reload after each form submission. Instead, I'm wondering if only the elements w ...

The main-panel div's width increase is causing the pages to extend beyond the window's width limit

I recently downloaded a Reactbootstrap theme and managed to integrate it with NextJS successfully. However, I am facing an issue where my <div className="main-panel"> in the Layout.js is extending slightly beyond the window. It seems like t ...

Adding and removing classes in JQuery based on a specific div ID

Is there a way to use addClass() and removeClass() on divs with different class names, identified by their unique ids? Any suggestions on how to make this functionality work? Here is the JavaScript code snippet: $(document).ready(function() { $("#bu ...

Tips for activating hover effect on child components by hovering over the parent container?

At the moment, I am tackling an issue with CSS in a nextJS web application. The problem lies in a parent div element that contains multiple child elements. My goal is for hovering over the parent div to trigger changes on the child elements as well. Here& ...

Using jQuery to target a specific item from a retrieved list of elements

I'm currently working on a photo gallery feature that is reminiscent of Instagram or Facebook user photos. My goal is to enable users to view details about each image (such as the date) in a box that appears over the image when they hover over it. E ...

Maximizing the potential of Angular forms through native FormData

Currently, I am revisiting an older project that still uses traditional methods for form submission. The HTML includes a form element with defined method and action. My goal is to submit the form in the old-fashioned way using the action attribute to post ...

"Including Span icons, glyphs, or graphs within a table cell in AngularJS based on a specified condition or numerical

I'm attempting to incorporate span icons/glyph-icons using Angular within a td before displaying the country. I've utilized the code below to achieve this, but I'm looking for a more dynamic and elegant solution. Your assistance is greatly a ...

The file uploader on the HTML page only allows for PNG files to be uploaded

Currently, I am working on an application where I am facing a challenge related to file uploads. Specifically, I have an input field like this: <input id="full_demo" type="hidden" name="test[image]"> I am looking for a way to restrict the upload of ...