How can I position two spans vertically between two floating divs using CSS?

On the top of my page, I have two divs - one floated to the left and one to the right. I successfully placed text with border between them. Now, I am facing the challenge of stacking two areas of text between these two divs.

You can see an illustration of my issue in this Fiddle: http://jsfiddle.net/TcRxp/

I want the orange box to be positioned under the green box, both center aligned with each other. The "legend" div that is floated to the right used to be at the same level but has shifted down now.

I attempted to add another table to solve the problem, but it didn't work.

I apologize for the messy markup - multiple people have worked on this code over time and none of us are experts.

I have advocated for a designer to join our team, but unfortunately, it has not happened yet.

Thank you,

Paul

Answer №1

UPDATE: Taking @Jeremy B's advice into consideration

Is it necessary to make changes using CSS? When facing situations like this, it's important to pay attention to the order in which HTML elements are placed.

Check out the revised version here: http://jsfiddle.net/TcRxp/8/

I managed to achieve your desired outcome by rearranging the order of the three DIVs and implementing the CSS recommendation from @Jeremy B

In essence, the layout logic is as follows:

  1. Display the float-right content
  2. Display the float-left content
  3. Display the content in the middle (which will now appear to the right of the float-left content).

Answer №2

To begin, ensure that your top span is configured as a block element for proper stacking:

<span class="color status active bold" style="display:block">
Status:</span>

Next, align the middle div to the left by adding the float:left property to #headmiddle in your CSS:

Answer №3

When it comes to combining CSS and tables for layout, achieving the desired results can be quite challenging.

To simplify your HTML structure, consider the following:

<div id="headleft">a small search form can go here</div>
<div id="headmiddle">
    <div class="active"><strong>Status:</strong> Currently Active</div>
    <div class="search">Displaying search results</div>
</div>
<div id="headright">
    <dl>
        <dt>Legend:</dt>
        <dd>Primary status</dd>
        <dd>Secondary status</dd>
    </dl>
</div>

Here is a simplified version of the CSS code:

div { padding: 2px; }
strong { font-weight: bold; }
#headleft, #headmiddle, #headright { float: left; font-size: 0.8em; }
#headmiddle div { border: 1px solid #000; margin-bottom: 3px; }
.search { background: orange; }
.active { background: #8ed200; }
dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

This approach results in more semantically correct HTML that is easier to read and modify in the future. Check out this example on JSFiddle.

Answer №4

Should you want to accomplish this using CSS, you can refer to my alterations: Here is the Fiddle

The enhancements I made are as follows:

#headmiddle span.status { display: block }

This adjustment will result in your spans appearing stacked on top of each other.

Answer №5

After gathering information from various sources, I was able to solve the issue at hand. While Alex Coles' solution was a good starting point, there were still some adjustments to be made, especially in the center alignment. The code snippet I initially utilized can be found in this forum post:

<style type="text/css">
.leftit {
    float: left;
}
.rightit {
    float: right;
}
.centerit {
    width: 30%;
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}
.centerpage {
    width: 80%;
    margin-right: auto;
    margin-left: auto;
}
</style>
</head>
<body>
<div class="centerpage">
<div class="leftit">Hello Left</div>
<div class="rightit">Hello Right</div>
<div class="centerit">Hello Middle</div>
</div>

(View fiddle for above code)

By incorporating the improvements suggested by Alex, I came closer to achieving my objective. However, the width of the center color blocks remained an issue. Referencing this question, I discovered the concept of "max-width," which proved to be the missing piece in the puzzle...or so I thought.

Update: Unfortunately, "max-width" does not work in IE7 quirks mode, which is essential for my project. To address this compatibility issue, I followed the instructions outlined in this webpage to fine-tune my CSS for IE7 quirks mode, IE8, and Firefox.

The finalized code can be viewed in this fiddle link:

.leftit {
    float: left;
    font-size: 0.8em;
}
.rightit {
    float: right;
    font-size: 0.8em;
}
.centerit {
    width:220px;
    margin-right: auto;
    margin-left: auto;
    font-size: 0.8em;
}
#headmiddle div {
    border: 1px solid #000; 
    margin-bottom: 3px;
}
.centerpage {
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}

strong { font-weight: bold; }

.search { background: orange; }
.active { background: #8ed200; }

dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

<div class="centerpage">
    <div class="leftit">a little search form here</div>
    <div class="rightit">
        <dl>
            <dt>Legend:</dt>
            <dd>Status numero uno</dd>
            <dd>Status two</dd>
        </dl>
    </div>
    <div class="centerit" id="headmiddle">
        <div class="active"><strong>Status:</strong>
            Active</div>
        <div class="search">Search results displayed</div>
    </div>
</div>

I appreciate all the valuable insights shared through the answers provided. This experience has been a great learning opportunity for me.

Sincerely, Paul

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

Unable to locate the iframe in the test scenario

I encountered this particular test case: Select Frame id=coach_frame63454108.cf1 Wait Until Element Is Visible ${ap.gui.header.appname} Page Should Contain Element ${ap.gui.header.appname} Page Should Contain Element ${ap.gui.hea ...

Inject a jQuery form submission using .html() into a div element

I am currently working on developing a basic forum where users can view listed topics and have the option to add new ones on-the-fly by clicking a link or image. How can I detect and handle the form submission event for the dynamically added form within an ...

Enhancing the appearance of radio buttons and checkboxes with Bootstrap 4's

Having trouble customizing radio and checkbox styles in Bootstrap 4? I'm trying to achieve something like this: https://i.sstatic.net/Wp6SR.jpg I've successfully changed the background, but I'm struggling to change the style when the radi ...

Dash that serves as a barrier between two words to avoid them from breaking onto

Is there a way to keep a dash ( - ) from breaking a word when it is at the end of a line? Similar to how &nbsp; prevents a line break between two words. The issue I am facing currently is with a dynamic blog post that includes the word X-Ray. Unfortun ...

Express app unable to retrieve dropdown menu data using Node.js BodyParser

I am currently working on creating a pug file for a signup form and I'm encountering an issue with the drop-down menu not functioning. How can I properly include my drop-down menu in the body parser? I initially assumed it would be processed with json ...

[CSS] What's the trick to getting it to smoothly glide to the top?

As a new designer, I have a question about how to make the background color extend to the top without any white space. Can someone please guide me on how to achieve this? <!DOCTYPE html> <html> <head> <style> body{ margin: 0px; p ...

hiding the search box by clicking away from it

Can someone help me with modifying this search box design? @import url(http://weloveiconfonts.com/api/?family=entypo); /* entypo */ [class*="entypo-"]:before { font-family: 'entypo', sans-serif; color: #C0C0C0; } * { margin: 0px; pad ...

Creating a minimalistic floating placeholder in HTML and CSS without the need to define a background color for the placeholder

Hey there, I'm currently working on implementing a floating placeholder for an input field. However, I'm facing an issue where I don't want to set the placeholder with a background color due to the varying input backgrounds and styles in my ...

Displaying incorrect results within the div container

I have been developing a website where a div element on a specific page displays values from a PHP file. Below is the PHP code responsible for rendering the information on the HTML: $output .= ' <div class="row text-left col-md-3 ...

Issue with text-shadow in MacOS Safari unable to display shadow effect

Check out this example of an issue that I'm experiencing https://jsfiddle.net/smdLzsx5/1/ Could someone please investigate and see if they encounter the bug as well? I'd like to know if this problem can be replicated consistently so I can report ...

Tips for updating the class of a button as you switch between slides

My ionic slide box features 4 slides that can be navigated using buttons at the bottom of the screen or by finger-scrolling. The goal is to have the 'active' class added to the button corresponding to the currently active slide. For instance, if ...

The seamless flow of web design

Seeking guidance on creating a responsive web page. I have a functional website that looks great on my 13" MacBook, but encounters distortion at different screen sizes. What steps are necessary to ensure it appears crisp and appealing on any device? Should ...

Using a personalized font in CSS isn't functioning correctly and is displaying Times New Roman instead

I've been struggling to include a custom font in my project, but it's not working as expected. Here's how I added it to the .css file: @font-face { font-family: 'Open Sans'; src: url('/open-sans.eot'); src: u ...

Why aren't my laptop media query CSS styles being applied?

My laptop size media query is not applying the CSS styles I want. I have tried using both min and max width but without success. I am trying to replicate the layout found here: @media screen and (min-width: 960px) and (max-width:1199px){ .grid-co ...

Creating image links in this jQuery Slider plugin: A beginner's guide

Currently, I am attempting to insert links into each photo within the slider. While I have successfully done this on two other websites, I am encountering difficulties due to variations in the code structure. <a href="http://www.google.com I have expe ...

How to create a clickable image in email HTML when the image is set as a background image for

Is there a way to make an image clickable within an HTML email, even if the image is set as a background-image URL? I would typically wrap the DIV in an "a href", but I am uncertain if this method is supported by email clients. The image is set as a back ...

Eliminate any undefined data in the popup map of Javascript

I am working with JSON data that is being displayed in a pop-up on a map. In cases where there is missing data (Visibility), the word undefined appears in the pop-up. Is there a way to remove the undefined text so that it does not show up in the pop-up? ...

ways to display a chosen option in a dropdown menu

Below is the code snippet I am currently using, which involves JQuery and Bootstrap 4: $('.dropdown-menu li').on('click', function() { var selectedValue = $(this).text(); $('.dropdown-select').text(selectedValue); }); & ...

Display HTML content from a JSON array with the help of ng-bind-html

I am looking to display HTML tags stored in a JSON object property within a modal window. Below is an example of the JSON object: { "fileUploadCategories": [ { "id": "Bread", "title": "GrandmaBread", "info": "Some info text.", "restService": "emp ...

Ways to conceal a div using ng-hide

Is there a way to make a div disappear using ng-hide? I have this code and I'm trying to hide the div with id="ApagarNA". The code displays a table with values, but some rows contain N.A in all 4 columns, and I want to hide those specific rows. For e ...