How can you add a new div directly after the parent div's content and display it in-line?

I have a banner that stretches to fill the width completely. Inside this main div, there is another smaller div measuring 30px in size positioned at the end. The nested div contains a close icon background image, and upon clicking it, the entire banner gets hidden using Javascript.

I am aiming to place this close-bar div right after the text content within the banner div.

Here is the desired layout:

[this is a banner with text inside of it [X]]

<div id="banner">this is a banner it has text inside of it<div id="close-ban"></div></div>

Answer №1

If you want to position a banner and close button side by side, you can use the following code:

#banner {
  width: 100%;
  float: left;
}
#close-ban {
  float: right;
}
<div id="banner">this is a banner with text inside<div id="close-ban">close</div></div>

Alternatively, you can create a container with 100% width and place the banner and close button divs inside it like this:

#container {
  width: 100%;
}
#banner {
  float: left;
}
#close-ban {
  float: right;
}
<div id="container">
<div id="banner">this is a banner with text inside <div id="close-ban">close</div></div>
</div>

Answer №2

One idea is to change the close-ban from a div element to a span element for inline placement with your other HTML content: http://www.w3schools.com/css/css_inline-block.asp (The span tag functions similarly to an inline-block. Another option would be to include display:inline-block in the CSS for your close-ban)

Just remember that if your text extends the width of the top banner, it may cause the close-ban to move to the next line using the method mentioned above. To prevent this, you could explore positioning the close-ban relative to the screen or the banner itself: http://www.w3schools.com/cssref/pr_class_position.asp

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

When my script is located in the head of the HTML page, I am unable to

My goal is to make my JavaScript code function properly when I insert it into either the head or body elements of an HTML document. Let's look at some examples: First, I insert the script into the body as shown in this example (works correctly): ...

Update text displayed on radio button selection using jQuery

Is there a way to change the label text on a selected radio button from "Set default" to just "default" using jQuery? I think I need to use the class radio-default as the selector, but I'm not very familiar with jQuery. <div class="radio-default ...

How do I make a div's height equal to 100% of the height of its parent

I am trying to make a button stick to the bottom of a card no matter how much content is on it. I used the Bootstrap class mt-auto, which worked well. However, when I set the height of all divs to 100%, the row in the card body overflows from the lg breakp ...

Using Java Swing to apply HTML styling to text strokes

I have come across this code snippet for a Java swing JLabel: "<html>\n" + "<head><style>\n" + "p { color: white;\n" + " text-shadow:\n" + " -1px -1px 0 #000,\n" + " 1px -1px 0 #000,\n" + " -1 ...

Divide the width by half in a CSS grid layout with 3 columns

Is it possible to replicate the layout shown in the image using CSS grid? https://i.sstatic.net/XSoE8.png ...

Upon clicking the link, the JavaScript functionality failed to activate

When I click on a link and try to add a class, it's not working. I want to create a "modal" div. Here is the PHP/HTML code: <?php if(isset($_GET['ido'])) { ?> <div id="modal" class="modal" style="background: blue; width: 1 ...

Interconnected HTML div values impacting one another

Forgive me for the not-so-great title, as I am unsure of what this particular HTML coding method is called. That's why I am asking this question in the first place. I've written some code and I need to know what type of coding practice this is. ...

Dynamically created HTML elements have no events attached to them in Angular and jQuery

Utilizing jQuery, I am dynamically generating new elements within an Angular Form that has been constructed using a Template Driven Forms approach. Despite successfully creating the dynamic elements, they do not seem to be assigned events/callbacks due to ...

Having trouble retrieving the value of the hidden field using ng-model

Hello, I'm currently learning AngularJS and facing some challenges with accessing hidden field values using ng-model. Specifically, I am working on an editing modal where I need to retrieve the ID for each record. Below is my controller code snippet: ...

tool for showcasing data on a webpage with a specific layout

Which chart library is best suited for presenting data in the formats shown in the images below? Can HighCharts handle these formats? Does Google Charts allow for a combination of bubbles and lines? ...

Develop a feature that is designed to reset the password in the user database, only to encounter a failure in the

Currently, I am working on a basic reset function. One of the files on the homepage is a simple html form structured as follows: <html> <body> <div> <form id="reset" action="login/reset.php" method="post"> <fieldset& ...

Ways to eliminate the vertical scroll feature in a kendo chart

I am encountering an issue while loading a kendo chart inside grid-stack.js where I am unable to resize the height properly. Whenever I decrease the height, a vertical scroll appears which should not happen. I have tried setting the height to auto and refr ...

store user settings in local storage

After writing some code with a link that toggles text visibility upon click, I now want to incorporate saving this state in web storage so that it persists upon page reload. As a beginner in JavaScript and HTML, this task has proven challenging for me. Th ...

Seeking help with h2 headings, nested hyperlinks, and clickable images

Attempting to tackle a homework question today. This is the task at hand.... List a variety of foods in the following categories: Sandwiches, Drinks, Desserts. Use h2 tags to create these categories. Under each category, create a nested list that inc ...

I encountered a difficulty in getting my video to play properly on the Safari browser

I am trying to figure out why my video won't play on Safari, even though it works fine on Google Chrome and Edge. Below is a snippet of my HTML code: <video width="640" height="360" controls="true" autoplay loop muted ...

What is the most effective method for customizing non-MUI elements within an MUI framework?

As someone new to React and development in general, I understand that there are various ways to style elements within React itself: importing CSS, using locally scoped CSS modules, implementing CSS-in-JS via libraries like Styled Components or Emotion, ut ...

Struggling to create a BMI Calculator using JS, the result is consistently displaying as 'NaN'

Having trouble creating a BMI Calculator using JavaScript. The issue I'm facing is that the calculation always returns 'NaN'. I've tried using parseInt(), parseFloat(), and Number() but couldn't solve the problem. It seems that the ...

Find the total sum of various spans

As a beginner in programming, I am facing a challenge that I cannot seem to solve. How can I retrieve the values of multiple spans with the same class? This is how the HTML code looks: <ul> <li> <input type="checkbox" value="30 ...

Arrange various Div elements of varying sizes in a predetermined sequence

I need to ensure that a large div maintains the same height as 20 other divs within the same container, when the screen size is between 1024px and 1920px in width. The challenge lies in arranging all these items in a floated layout grid that looks clean a ...

Bootstrap's square-shaped columns

I would like to implement a grid of squares for navigation purposes. By squares, I mean that the colored areas should have equal width and height. Currently, I have achieved this using JavaScript, but I am interested in a CSS-only solution. My project is ...