Adjusted positioning of element, taking into account the pseudo-element preceding it

I am facing a frustrating problem with the html layout of a form. Due to restrictions within a large framework, I am unable to make significant changes to the structure. However, I need to reposition a button to a more suitable location. While I have made some progress, I am not completely satisfied with the current solution. Maybe you could provide me with some fresh ideas on this matter. To illustrate my approach, here is a simplified version:

The layout consists of two container divs: top and bottom. The top container displays a fixed-width button on the left side, which may vary in width depending on its label translation. Meanwhile, the bottom container contains various elements, including a second button at the top. Although it functions properly, its appearance is not ideal. My goal is to visually move it into the top container since there is a logical connection between the two buttons. Unfortunately, directly placing it inside the top container is not an option currently. Therefore, I have utilized a fixed position for the second button, but adjusting its horizontal placement poses a challenge. Calculating the distance from the first button to prevent overlap while considering translations results in an unsightly gap between the two buttons.

In an attempt to improve the layout, I explored using a pseudo-element (::before) on the second button. Utilizing the translated label of the first button stored as a property, I attempted to replicate it in the CSS to create a before pseudo-element matching the length of the first button. The code example below demonstrates this technique.

My main struggle lies in positioning the pseudo element beneath the first button in the top container. This method was intended to indirectly align the second button accordingly, but unfortunately, it seems impractical. As someone new to coding and styling, I thought seeking guidance here might lead to a viable solution...


Below is a greatly simplified code snippet showcasing my approach:

Please refer to the provided JSFiddle link to interact with the code:

HTML:

<div id="top-container">
    <button>multilingual button text</button>
</div>

<div id="bottom-container">
    <h2>Some title opening the bottom container
        <span class="into-top-container">
            <button id="place-me" reference-text="multilingual button text">button to be placed</button>
        </span>
    </h2>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
</div>

CSS:

body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
div {
    margin: 0;
    padding: 5px;
}
button {
    margin: 0;
    padding: 5px;
    white-space: nowrap;
}


div#top-container {
    width: 100%;
    border: 1px solid green;
}

div#bottom-container {
    width: 100%;
    border: 1px solid blue;
}

#place-me {
    position: fixed;
    top: 0;
    left: 400px;
    margin: 5px;
    background: yellow;
}

#place-me::before {
    z-index: 0;
    /*visibility: hidden;*/
    position: absolute;
    content: attr(reference-text);
    margin: 0 5px;
    padding: 0;
    background: gold;
    right: 100%;
}

Notes:

  • In the given code, the second button is positioned using left: 400px;. I aim to modify this value but setting it to left: 0 does not yield the desired outcome...

  • The visibility CSS rule for the pseudo-element is currently disabled for demonstration purposes

  • It should be noted that the second button is not nested within the top container but logically aligns below the title of the bottom container. The objective is to visually shift it upward into the top container, which is close to achieving the desired result except for the horizontal alignment...


For further clarity, here is a screenshot upon request:

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

This image extracted from the above JSFiddle includes a red ellipse highlighting the pair of elements I intend to relocate and a directional arrow indicating the preferable destination. The aspiration is to precisely position the second button adjacent to the first one without specifying a fixed horizontal coordinate. Hence, the pseudo-element serves as a placeholder. Once accomplished, I will conceal the pseudo-element and ensure proper alignment based on the length of the translated text.

The ultimate goal is illustrated in the following depiction:

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

Answer №1

After encountering a regression in our code, I delved deeper into the issue and discovered a logical and efficient solution. Taking some time to analyze the problem led me to a cleaner approach:

To demonstrate, I utilized the same simplified code structure.

You can view the modified jsfiddle here, which is based on the original question.

In terms of HTML, the only noticeable change was moving the reference-text from the button to the container for reasons explained below:

CSS adjustments were made as follows:

* {
    font-size: 12px;
    font-weight: normal;
    font-family: Arial;
}

body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    font-size: 12px;
    font-weight: normal;
}
span,
div {
    margin: 0;
    padding: 5px;
}
button {
    margin: 0;
    padding: 5px;
    white-space: nowrap;
}


div#top-container {
    width: 100%;
    border: 1px solid green;
}

div#bottom-container {
    width: 100%;
    border: 1px solid blue;
}

span.into-top-container {
    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;
    border: 1px solid transparent;
}

span.into-top-container::before {
    visibility: hidden;
    content: attr(reference-text);
    position: relative;
    margin-right: 5px;
    padding: 5px;
    border: 2px solid;
    background: gold;
}

#place-me {
    background: yellow;
    pointer-events: all;
}

The key strategy shift involved positioning the container housing the button rather than the button itself (as seen with

<span class="into-top-container">
). This allowed for the pseudo before element, now anchored to the container instead of the button, to occupy space without interfering with the button directly.

By placing the container over the original multilingual button, the button became unclickable. To address this, I set a css property of pointer-events: none for the container and restored it back to pointer-events: all for the placed button. This ensured that events (such as clicks) were ignored by the container and passed through to the underlying original button.

To maintain consistency, I ensured that the font styling within the pseudo element matched that of the original multilingual button. Since font styling dictates the actual width of the button, it was essential for the pseudo element's width to align accordingly. In the given example, I achieved this by setting uniform font styles for all elements initially (* {...} in the CSS snippet). Alternatively, this could be specified directly within the rules for the pseudo element itself. I opted for the simpler approach to uphold code clarity.

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

Challenges with Z-index and layering dynamics

I'm having trouble getting z-index to work. I've searched online but can't find a solution. How can I make the .navigation class have a lower z-index than the .mobilenav and .mobilenavclosed classes so that they appear above everything else ...

Trouble with CSS styling for focused input fields

Attempting to style a form by Patriot with CSS, I encountered an issue. input#card-month:focus ~ label.card-label.label-cardExpiryYear { font-size: 20px !important; /* This works fine */ } input#card-month:focus ~ label.card-label.label-cardExpiryMont ...

Masonry is limited to displaying a single column at a time

Struggling to make masonry work for the first time, as it is only displaying as a single column. Being more of a designer than a developer, it's possible that I'm overlooking something. Here is the code snippet I'm using: <body> ...

Adjusting Media Queries according to the browser window's zoom level

Is there a way to detect the browser width dynamically? For instance, can I adjust the CSS styling based on zoom adjustments like ctrl + or ctrl -? By "box," I am referring to a perfectly square shape. So, when the browser width is 100%, I want a layout wi ...

Function execution within jQuery ajax success callback only occurs once, not twice

On my webpage, I have a feature where I input card names and their details are retrieved using AJAX. To simplify the code and replicate an issue, I've removed irrelevant parts and tested this snippet which still triggers the error. The strange issue ...

The picture does not occupy the entire page

Here is a simplified version of my HTML and CSS: @keyframes animatedBackground { from { background-position: 0 0; } to { background-position: 100% 0; } } *{margin:0;padding:0;}/*added as requested*/ .background-image { background-im ...

Tips for selecting elements within the overflow container that are hidden:scroll

Is it feasible to fetch all elements located within the "hidden area" of a parent element that has an overflow:scroll property? A parent container, <div>, has a style of overflow:scroll;height:200px. Within this container is a table. Take a look at ...

How can CSS be applied to form elements in jQuery when the class attribute is already in use?

I have a question regarding form validation using Jquery. The form elements in my code have the class attribute set as "required". Now, I want to style these input fields specifically without affecting all input elements. I have defined a CSS class like t ...

The images were uploaded, but unfortunately they are not adapting to different screen sizes

I recently created a website that is optimized for tablet screens and included images. However, I noticed an unwanted gap between the first column and third column that needs to be fixed. My plan is to adjust the layout using Bootstrap code or another meth ...

The bottom border color in Tailwind is currently not in use

Apologies if this question has been asked before, kindly point me in the right direction. Today marks my first day venturing into the world of React JS. I am currently following a course on YouTube (https://www.youtube.com/watch?v=b0_Y_eU_SXI). Everything ...

Positioning text too far to the left causes it to become disorganized and difficult

I am looking to achieve the following result: LEFT THING, not fixed here comes a text that spans 100% width of this "column" width (content itself but my issue is that when it gets too long, this column drops below determines that) the left ...

CSS Begin the background repeat from a specific origin

I am trying to achieve a specific background effect starting from the line shown in the screenshot below: I attempted to implement it using the following CSS code: background : ('path/to/image') 0 650px repeat-y However, the light part of the ...

Issues may arise when attempting to parse AJAX responses using Javascript/JQuery AJAX

There are many posts on this topic, but I am having trouble finding the specific information I need. Here is where I am struggling: I have an AJAX request for an ID and an integer (which will be used as a margin to apply on the DOM later). This is the re ...

Emphasize a portion of a text / Apply formatting to a variable

I am facing an issue where I need to format only the date in a specific string. I have managed to extract the date and store it in a variable after isolating it from the original message. <div class="foo"><p> Click here now and get by January ...

What is the best way to anchor floating labels in React while incorporating Tailwind CSS?

I am currently working on developing a registration form using React and Tailwind CSS. As part of this project, I have implemented a feature where the label slides up when an input form is focused. Everything seems to be working fine up to this point. Howe ...

Utilize JavaScript code with AJAX content without repeated inclusion

My index.php file allows users to input day, month, year, and more. Using ajax, I dynamically display the results. $(document).ready(function() { $('#searchBtn').click(function() { $.ajax({ url: &a ...

PHP URL Encoding - variable in a hyperlink

Within my .php page, I have a link that directs to Information.php. <li><a href="http://localhost/Information.php?AI=<?php echo $_GET['AssignedI'] ?>">Stats</a></li> The 'GET' method retrieves the variabl ...

Enhancing the appearance of my website's shared content on Facebook: A guide

When I share links from well-known news sites like TNW and Engadget, the preview looks great with a thumbnail, bold title, and summary. But when I try to share one of my website's articles, it doesn't look as appealing. Is there a way to customiz ...

The art of positioning in menu - absolute versus relative

Struggling with positioning absolute divs is a common challenge for many of us. In my case, it's horizontal sub-menus with the following CSS: ul.children{ display:none; position:absolute; } ul.children li{ position:relative; height:60px; float:none; ...

Leveraging Javascript within MVC3, either on a master page or child page

I'm currently developing a web application utilizing MVC3 and I am curious about the best practices for incorporating JavaScript. Specifically, I am uncertain about whether to include JavaScript in the master page as well as in individual child pages. ...