To style a text box next to text within a paragraph in CSS, simply create an empty box

I have some CSS classes defined as follows:

.p_box {
    position: relative;
}

.p_box span {
    background-color: white;
    padding-right: 10px;
}
.p_box:after {
    content:"";
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    top: 0;
    height: 20px;
    border: 1px solid black;
    z-index: -1;
} 

Unfortunately, the styling is not working as expected. It worked for text formatting but not for creating a box.

I want it to look like an empty box with borders like this:

some text: |---------|
           |---------|

You can view how it currently looks using that styling here.

In my application, both the upper and lower borders are hidden where the text is, but the left border appears incomplete:

some text: -------|
           -------|

Upon further consideration, I believe this happens because it hides part of the box rather than creating a solid border. There may be a better approach to achieve the desired result.

Update

To better illustrate what I'm looking for, refer to this image: https://i.sstatic.net/Oyfc5.png

The empty box should appear like this:

<p class="p_box">Test:</p>
After the word "Test," the empty box should be displayed.

Update2

Using left: fixed px number; does not fully solve the issue, especially when the length of the text varies. For example, in this instance: https://jsfiddle.net/envLpanh/8/

Answer №1

I recently implemented some CSS modifications to achieve the desired effect, which you can view in action on this fiddle

    .p_box {
        position: relative;
    }

    .p_box span {
        background-color: white;
        padding-right: 10px;
        display: table-cell;
        min-width: 200px;
    }
    .p_box:after {
        content:"";
        display: table-cell;
        width: 100%;
        height: 20px;
        border: 1px solid black;
        z-index: -1;
    } 

To control the minimum width of .p_box span, you can utilize the min-width: property or simply remove it altogether. However, removing it may affect the layout structure. Thus, I recommend sticking with using min-width as the more appropriate solution.

Answer №2

Have you found what you're seeking?

.p_box:after {
    position: absolute;
    left: 90px;
}

Answer №3

Are you in search of this?

Check out the demo here

Here is the updated CSS code:

.p_box:after {
content:"";
position: absolute;
bottom: 0;
left: 100px;
right: 0;
top: 0;
height: 20px;
border: 2px solid black;
z-index: -1;

}

Answer №4

Here is my attempt to achieve your desired outcome:

.p_box {
    position: relative;
}

.p_box span {
    background-color: white;
    padding: 5px;            
    padding-right: 10px;     
    line-height: 20px;       
}
p.p_box:after{
   left:110px;      
}
.p_box:after {
    content:"";
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    top: 0;
    height: 20px;
    border: 1px solid black;
    z-index: -1;
}
<body>
    <p class="p_box"><strong><span>Something:</span></strong></p>
</body>

Check it out in the JSFiddle...

View Updated DEMO code


Updated code with JQuery:

$(document).ready(function(){
    var text_block_width = $('.p_box strong span').outerWidth(true);
    debugger;
    console.log(text_block_width);
    $('p.p_box').append('<style>p.p_box:after{left:'+text_block_width+'px}</style>');
})
.p_box {
    position: relative;
    width:100%;
}

.p_box span {
    background-color: white;
    padding: 5px;            
    padding-right: 10px;     
    line-height: 20px;       
}
.p_box:after {
    content:"";
    position: absolute;
    bottom: 0;          
    left: 0;
    right: 0;
    top: 0;
    height: 20px;
    border: 1px solid black;
    z-index: -1;
}
<body>
    <p class="p_box"><strong><span>hsdjfsdjfsdfjsdfjsdfjsdf blablallll:</span></strong></p>
</body>

See the working DEMO code below:

Check out the code

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

Embedding SVG or image code within the </style> tags is a common practice in web development

Can anyone assist me in placing this code within </style>? I am unsure of how to proceed with this. I believe the best approach would be to start by moving most of it into </style>, and then I can decide what should remain there. I would grea ...

Leveraging jQuery Ajax and MySQL to generate dynamic HTML content

I'm in the process of creating a unique photo gallery that utilizes dynamic features. Instead of relying on constant HTML/PHP refreshing for updates, I am incorporating jQuery to handle dynamic MYSQL queries. As a beginner, I've managed to creat ...

What is the best way to prevent the body from scrolling when scrolling on a fixed div without making the body's scroll bar disappear?

Is there a way to prevent the body from scrolling while I scroll on a fixed div? I attempted using overflow:hidden for the body, which stops scrolling but causes the page to shake when the scroll bar disappears. Is there a solution that allows me to keep ...

Is it possible to retrieve the value of a span in a PHP contact form instead of an input?

I am in the process of setting up a contact form, and I have some valuable information saved within certain spans. Specifically, it's related to an ecommerce shopping basket. As the built-in checkout system is proving to be subpar, we are opting for a ...

Error encountered in Flatpickr NextJS+TS application: Uncaught DOMException - accessing CSSStyleSheet.cssRules getter is not permitted for cross-origin stylesheet

After successfully integrating Flatpickr into my project, I encountered an issue when testing in mobile view. When trying to open the calendar by clicking on the input field, I received the following error: Uncaught DOMException: CSSStyleSheet.cssRules get ...

Tips for sending data to CSS in Angular

I have an Angular application where I need to calculate the width of an element and store it in a variable called finalposition. Then, I want to move this element to the left by (finalposition)px when hovering over it. How can I achieve this styling effect ...

Remove the $(window).resize event handler

Setting up a listener for the browser window, I use this line of code: $(window).resize(resizeObjects); However, during certain points in the web application cycle, I need to remove this event from window.resize. Can anyone provide guidance on how to det ...

Tips on hiding the menu toggler in PrimeNg

Struggling with PrimeNg implementation in my Angular project, I am unable to find a simple solution to hide the menu toggler. Allow me to illustrate my current setup in the first image and what I desire in the second. Let's dive in: View First Image ...

What is the best way to showcase my background image using html and css?

I've come across a similar topic addressing my problem, but I am still unable to resolve it. I am attempting to add a background image to my portfolio, but for some reason, it is not working. Can anyone offer any suggestions or ideas? Here is the cod ...

html issue with the footer

Just starting out with HTML and encountering some difficulties with adding a footer. The main issue is that when I add the footer, the 'form' element gets stretched all the way down until it reaches the footer - you can see an example of this her ...

Two liquid level divs within a container with a set height

I hesitated to ask this question at first because it seemed trivial, but after spending over 3 hours searching on stackoverflow and Google, I decided to give it a shot. The issue I'm facing can be found here: http://jsfiddle.net/RVPkm/7/ In the init ...

Dynamic Column Image and Text Display

I am working on a website where I am trying to have an image on the left and a paragraph on the right, but when the screen size is reduced, I need the layout to switch so that the image is at the top and the paragraph is at the bottom. Even though I have ...

What is the best way to use jQuery to position a <div> at the bottom of another <div>?

Is there a way to achieve something similar to this concept? https://i.sstatic.net/xhewF.png The dimensions displayed are just for visualization purposes, but feel free to utilize them. I am interested in moving the smaller red <div> box and attac ...

Tips for keeping a reading item in place while scrolling

To enhance the user experience, I would like to improve readability without affecting the scroll position. Question: Is there a way to fix the scrolling item at a specific position (item with the .active class)? I am looking to maintain a consistent read ...

How to Customize Bootstrap Colors Globally in Angular 4 Using Code

I am interested in developing a dynamic coloring system using Angular. I have set up a service with four predefined strings: success, info, warning, and danger, each assigned default color codes. My goal is to inject this service at the root of my applicat ...

Attempting to determine the smallest and largest dimensions of two values using CSS3

Trying to make sure the image on my phone adjusts its size based on orientation, I attempted using Ionic4. When in landscape mode, I want it to adapt according to height instead of width. Essentially, I need it to use the minimum size and also require the ...

How can you specify the maximum width and height for a TextField in JavaFX using CSS?

Is there a way to set preferred and maximum width and height for a TextField using CSS? ...

After swapping out "div" with "input" and then replacing "input" with

My goal is to create a function where a user can edit text within a div. The idea is to replace the current text with an editable input field, and then convert it back into a div after the user hits enter. I've encountered an issue while trying to im ...

Create a division that will remain visible on the screen and allow scrolling when a certain class is

Having some trouble with a fixed class div that is not continuing to scroll after the class is removed on scroll. I've attempted to fix this issue but the div keeps getting hidden instead of continuing to scroll. If anyone has any advice or can poin ...

Verify the placement within the text box

Are there methods available in JavaScript or any JavaScript framework to determine the position within a textbox? For example, being able to identify that figure 1 is at position 2 and figure 3 is at position 3. Figure 1 Figure 2 ...