two containers displayed next to each other

Within the html structure below, there are two nested divs with the class "ejector". One of these ejector divs contains the word "female" while the other contains "male." My goal is to have these words displayed not against the colored backgrounds (pink and green respectively), but rather against the white background after the bars end. Specifically, the word "females" should be positioned on the white background right after the green bar ends (while keeping the number 25 on the green bar). In an attempt to achieve this, I applied a float:right and set a margin-right value of -15 to the ejector class, hoping it would move outside the boundary of the nested div (yet remain on the same line) so that the words appear on the white background. However, this approach did not work as expected. You can view the current setup in this fiddle http://jsfiddle.net/mjmitche/kpPbE/10/

Could you provide guidance on how to accomplish my intended outcome?

In addition, I also attempted to eliminate the nested div by positioning it next to the div featuring the desired background color using a float:left. Nevertheless, the text ended up appearing below the intended colored background http://jsfiddle.net/mjmitche/kpPbE/12/

<div class="chart" style="width:800px; margin-left:auto; margin-right:auto">
    <h4 style="width:600px; margin-left:auto; margin-right:auto">Visitors to StackOverflow</h4>

    <div class="pink" style="width: 50px;">25 <div class="ejector">females</div></div>
    <div class="green" style="width: 30px;">15 <div class="ejector">males</div></div>

</div>

CSS

.chart div {
        text-align: right;
        padding: 3px;
        margin: 1px;
        color: #000;
        width: 600px;
      }
      .green {
        font: 15px sans-serif;
        background-color: green;
        text-align: right;
        padding: 3px;
        margin: 1px;
        color: white;
        height: 20px;
        line-height: 20px;
      }
      .pink  {
        font: 15px sans-serif;
        background-color: #f5c5f2;
        text-align: right;
        padding: 3px;
        margin: 1px;
        color: white;
        height: 20px;
        line-height: 20px;
      }

     .ejector{
    float:right;
    margin-right: -15px;
  }

Answer №1

For proper alignment, you will need to utilize either absolute or relative positioning, with the latter option being slightly more complex. To keep things simple, let's stick with using absolute positioning.

Update the CSS for the container divs (.green and .pink) by adding position: relative; so that they serve as the root elements for absolutely positioned items. The revised CSS code for your .ejector class should look like this:

div.ejector{
    position: absolute;
    color: black;
    top: 0; /* aligns the word at the same level as the number */
    left: 100%; /* positions the word 100% from its parent's width */
    margin-left: 10px; /* adjust spacing between words and colored bars */
}

One drawback of absolute positioning is that flow elements placed after the div's will overlap. You can solve this issue by applying a margin-right on the .green and .pink divs, considering the additional space needed - such as the size of each ejector div plus 10px for spacing, which would be 57px & 71px in this scenario.

Check out the demonstration here: http://jsfiddle.net/kpPbE/13/

Answer №2

Give this a shot if it fits your needs

<div class="chart" style="width:800px; margin-left:auto; margin-right:auto">
<h4 style="width:600px; margin-left:auto; margin-right:auto">Visitors to StackOverflow</h4>

<span class="pink" style="width: 50px;">25 </span ><span>females</span><br>
<span class="green" style="width: 30px;">15 </span><span>males</span>

Forget about using the ejector class for now, errors in your code include:

<div class="pink" style="width: 50px;">25 <div class="ejector">females</div></div>

You have placed the females div within the pink background div, causing the pink background style to apply to it. Another issue is that the div element is block level, which pushes the next element to a new line. Consider using a span, which is an inline element instead.

Answer №3

It appears that there are a few issues at hand.

  1. To improve the functionality, consider changing the <div> elements for colors to <span>
  2. Completely remove the unnecessary ejector <div>
  3. Include display: inline-block in the CSS for .pink and .green to assign a width to the <span>.

The reason it is not functioning correctly is due to confining the ejector <div> within the specified width of the pink/green <div>.

Furthermore, depending on your requirements, you can enclose each line within either a <div> or a <p>.

Here is a link to view the changes: http://jsfiddle.net/5hwVH/5/

Below is the revised HTML:

<div class="chart" style="width:800px; margin-left:auto; margin-right:auto">
    <h4 style="width:600px; margin-left:auto; margin-right:auto">Visitors to StackOverflow</h4>

    <div>
        <span class="pink" style="width: 50px;">25</span> females
    </div>
    <div>
        <span class="green" style="width: 30px;">15</span> males
    </div>

</div>

And here is the corresponding CSS:

.chart div {
        text-align: right;
        padding: 3px;
        margin: 1px;
        color: #000;
        width: 600px;
      }
      .green, .pink {
        font: 15px sans-serif;
        text-align: right;
        padding: 3px;
        margin: 1px;
        color: white;
        height: 20px;
        line-height: 20px;
        display: inline-block;
      }
      .green {
        background-color: green;
      }
      .pink {
        background-color: #f5c5f2;
      }

      }

You'll notice that I have consolidated much of the .pink and .green CSS properties. Combining CSS where feasible is advisable as it simplifies maintenance tasks in the long run.

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

Is there a way to customize the animation for a button in material UI?

Trying to implement material UI in React and looking for a button that does not have the standard wave animation effect upon clicking, which you can see demonstrated here. Instead, I am searching for an animation that instantly fills the entire button wit ...

The Safari browser displays the mobile-friendly version of the website

Everything appears correctly when looking at the website in Firefox and Chrome. However, Safari is displaying the mobile version of the site instead. I did not make any CSS changes specifically for desktop screens while working on the responsive design for ...

Is it possible to insert a variable into a span or v-tooltip tag?

I am currently using vue 2 along with vuetify 2, and I seem to be encountering an issue when attempting to display data from a table. Whenever I insert curly braces between the v-tooltip tags, it results in a blank page. Below is an example of the code: &l ...

Unable to customize the color of Bootstrap pagination

I am attempting to customize the color of the pagination in Bootstrap, but no matter what I try, it remains blue. My goal is to have the active pagination links in red and the inactive ones in black. What could be causing this issue? ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

No adjustment in color upon switching themes

I've been struggling to achieve the desired outcome while working on a small coding task. The goal is to modify the color of the screen background, text, and button as illustrated in the uploaded image. I have three code files for this task - index, t ...

Ways to eliminate white space in bootstrap CSS menu bar

My goal is to create a responsive navigation bar using Bootstrap and customize it to fit my design needs. Although I was able to implement the dropdown on mobile-sized windows, I encountered a margin issue that I couldn't remove on the right side. I ...

Using PHP's header function to alter directories

So I am currently diving into PHP and facing a challenge with using headers. My setup involves using xampp for development purposes. I have a basic form where users can log in on "index.php". Upon successful login, the header function kicks in to redirect ...

Creating an HTML element with jQuery using the before() method

Here is the code snippet I am working with: $(document).on("click", "#add", function() { console.log("clicked"); $(this).before('<lable>'+ current +'.</label><input type="text", id="option"><br>'); ...

Run a JQuery function on a specific div element

Hey there, I'm currently on the hunt for a solution regarding a webpage issue. I have a function that increases a text number but what I really need is for this function to start when the section is in view. The function seems to be working just fine ...

Tips for incorporating transition animations into route changes in next.js

On the homepage, there are 2 links that I want to have distinct transition animations for. For example, if I click the link on the left, the animation should start from the left and move towards the right when changing routes. ...

Unable to drag and drop onto every part of a div element that has undergone a CSS transformation

Having trouble implementing a Drag & Drop feature. The droppable div is styled with CSS transformations, but the draggable div doesn't drop correctly on the right part of the box. If you'd like to take a look at the code and try it out yourself, ...

Changing column layouts on mobile using Bootstrap 4

Using Bootstrap's grid system, I have created a layout with a sidebar. The structure is as follows: <div class="row"> <div class="col-md-3"> ... </div> <div class="col-md-9"> <div class="row"> <div ...

unable to retrieve element values using class names

I have created multiple h1 elements with the same class names listed below. <h1 class="h1">One</h1> <h1 class="h1">Two</h1> <h1 class="h1">Three</h1> <h1 class="h1">Four</h1> I have also added a button that ...

Displaying an Instagram image using an HTML image tag

After entering the URL in my browser, I can clearly view the photo on However, when trying to use the same URL within an html img tag, all I see is a broken image on my screen! <img src="https://www.instagram.com/p/CBqFrrCHVM7/media/?size=t" ...

URL Bootstrap Table Formatter - Enhancing Your Table Design

Javascript: function LinkFormatter(value, row, index) { return "<a href='"+row.url+"'>"+value+"</a>"; } HTML: <th data-field="snum" data-sortable="true" data-formatter="LinkFormatter" >LINK</th> <th data-sortable=" ...

CakePHP does not recognize the input type "number" in forms

My goal is to create a form field that only accepts numbers as input. However, when I attempt the following code in CakePHP: <?php echo $this->Form->input('aht',array( 'label' => 'AHT', 'type' = ...

Allow users to manually resize <td>'s within a platform

Looking for a way to allow users to manually resize a textarea and iframe in a system I am developing. Here's an example of what I mean: <body> <table width="100%"> <tr> <!-- I want the user to be able to re ...

Change an image seamlessly without any flickering, and display the new one instantaneously

TL;DR: How can images be swapped smoothly without causing page flicker while indicating loading status? I am facing an issue with swapping between 2 images using buttons. The first image loads fine, but the second image doesn't display until it' ...

Choose only the initial element, regardless of its category

Is there a way to apply a specific style exclusively to the very first element within a container, only if it is an h1, h2, or h3? However, if there are multiple headings of different levels on the same page, only the initial one should receive the designa ...