A full-width div containing a child span is unable to be aligned to the right

After reviewing this fiddle, I'm struggling to get my menu aligned correctly at the right edge of the overall "card". Despite trying various methods like margins, right: 0, and float, nothing seems to work as expected. In some cases, I even end up losing the background color of the parent div (as with the float method which collapses the parent div).

Below is the current HTML and CSS from the fiddle:

<div class="server">
    <div class="name">Server01</div>
    <div class="menu">
        <span class="items">
            <span>Menu01</span>
            <span>Menu02</span>
            <span>Menu03</span>
        </span>
    </div>
    <div class="content">
    </div>
</div>

.server {
    position: relative;
    width: 400px;
    height: 200px;
    margin: 10px;
    background-color: #686868;
}

.name {
    font-size: large;
    position: relative;
    top: 0px; left: 0px; right: 0px;
    padding-left: 10px;
    background-color: cornflowerblue;
    color: white;
    cursor: default;
}

.menu {
    font-size: small;
    position: relative;
    width: 100%;
    background-color: cornflowerblue;
    color: white;
}
.menu .items span {
    margin-left: 10px;
}
.menu .items {
    display: inline-block;
    position: relative;
    right: 0px;
    text-align: right;
    cursor: pointer;
}

Answer №1

If your <span /> element has a width of 100%, it is necessary to align the text to the right instead of floating the box. When the box is floated, it requires space to move into, but with a 100% width there is no room for movement. If you prefer to maintain the background color, consider wrapping the element in another element and applying the background color to the new parent element.

Check out the revised JSFiddle here: http://jsfiddle.net/UNxyw/1/

The text-align:right property was only added to the .menu element.

Answer №2

Having a text-align property on a span without specifying the width will not work as expected. EDIT:

If you apply the styling to the .menu class instead, it will function correctly.

.menu {
    font-size: small;
    position: relative;
    width: 100%;
    background-color: cornflowerblue;
    color: white;
       text-align: right;
}

.menu .items {
    position: relative;
    right: 0px;

    cursor: pointer;
}

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

Can you please tell me the term used to describe when a background adjusts and follows the movement of a mouse cursor?

Just wrapped up freeCodeCamp's HTML & CSS Responsive Web Design course and noticed a cool feature in some portfolios that I'd like to learn. However, I'm not sure what it's called. Here are some examples: The video game Escape From Ta ...

Using a URL in the fill property is a simple process that involves specifying

Here is the snippet of the code I'm working with: <?xml version="1.0" standalone="no"?> <html> <head> <style> .someClass { fill:"url(#Pattern)"; } </style> ...

Interactive Thumbnail Selection for HTML5 Video

Having trouble with creating thumbnails? I managed to solve the cross-domain issue using an html2canvas PHP proxy. No error messages in the Console, but the thumbnails are unfortunately not showing up - they appear transparent or white. Here is a snippet ...

How to Customize the Size and Color of secureTextEntry Inputs in React Native

When it comes to styling a password input like the one below: <TextInput name="Password" type="password" mode="outline" secureTextEntry={true} style={styles.inputStyle} autoCapitalize="none" autoFocus={true} /> I also ap ...

Can the input field offer guidance on its own?

While browsing, I came across an HTML5 checkout form that grabbed my attention. You can view the source here I decided to make some changes to the form and discovered that entering an incorrect email address triggers helpful tips. Strangely, upon closer ...

Can multiple shadow hosts utilize a common shadow DOM?

As I navigate the world of Web Components, I find myself still grappling with a full understanding of its capabilities. One thing that stands out to me is the numerous advantages it offers. A burning question in my mind is whether it's feasible to sh ...

Tips for creating a dropdown that autocompletes similar to the one found in Google Chrome

Looking to create a unique custom drop-down autocomplete suggestion list using HTML and CSS, similar to Chrome's design (shown below). https://i.sstatic.net/Kia2N.png In my attempts to achieve this, I utilized an absolutely positioned table beneath ...

ImageMapster for perfect alignment

I'm struggling with centering a div that contains an image using imagemapster. When I remove the JS code, the div centers perfectly fine, indicating that the issue lies with the image mapster implementation. It's a simple setup: <div class=" ...

Issue encountered while transferring files between a web platform and an API

Currently, I am working on two separate projects. The first project involves a website where users can upload files, and the second project is an API designed for file uploads due to the limitations of the old website. I am utilizing Laravel (6) to develo ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

How can links in HTML be eliminated if a certain condition is not fulfilled?

If a specific condition is met, I would like to eliminate the "a href" tag. For example, when the term "None" is present, I prefer it not to be linked. html: {% for item in responses %} <tr> <td ...

"Submitting an HTML form and displaying the response without redirecting to a

I created a basic portlet for Liferay that includes a form for inputting parameters to send to an external webservice. However, when I submit the form, I am redirected to the URL of the webservice. Is there a method to prevent this redirection and instead ...

Spinning Circle with css hover effect

I have recently developed a simple website: Within this website, there is an interesting feature where a circle rotates above an image on hover. However, despite my best efforts, I couldn't make it work as intended. Here's the code snippet I use ...

How can you set an input field to be initially read-only and then allow editing upon clicking a button using Vue.js?

//I have two divs that need to be set as readonly initially. When an edit button is clicked, I want to remove the readonly attribute and make them editable. <div> <input type="text" placeholder="<a href="/cdn-cgi/l/email-protection ...

align the horizontal navigation bar

I am facing an issue with my horizontal navigation bar. I want to include a search bar within the navigation bar, but it is causing misalignment. Here is the link to the code: https://jsfiddle.net/r6ntxg2f/ If you look at the menu, you will notice that i ...

Box without a border wrapping around it

I can't seem to figure out why the border I applied to a child div is not completely enclosing the element. I created two boxes using div tags and added a border to one of them. #box { height: 500px; width: 500px; background-c ...

Tips for moving text within a Canvas using a button to trigger the function that displays the text in a Javascript environment

Within my HTML, there is a canvas element with the id="myCanvas". When a button is clicked, it triggers this particular function: function writeCanvas(){ var can = document.getElementById("myCanvas"); var ctx = can.getContext("2d"); va ...

Data bindings encapsulated within nested curly braces

I am currently utilizing Angular2 in my application. Within my html file, I have a *ngFor loop set up like so: <div *ngFor="let element of array"> {{element.id}} </div> Next, I have an array containing objects structured as follows: some ...

Fine-tuning the page design

I'm working on a registration page using Bootstrap-5 for the layout. Is there a way to place all elements of the second row (starting with %MP1) on the same row, including the last field that is currently on the third row? See image below: https://i. ...

Challenges with text in a Bootstrap 5 responsive carousel

I'm currently in the process of developing a new website and I am utilizing bootstrap 5. One issue I have encountered is that the text and button within the carousel in the header section do not appear to be compatible. To provide better clarity, I wi ...