The issue of misaligned display of HTML and CSS rectangles

Greetings! I've got a website that contains some informative text for users. To enhance its visibility, I'm looking to add a white rectangle as a background behind the text.

Although I can successfully draw the rectangle, it appears over the text instead of behind it.

Below is the CSS code I'm using to create the rectangle:

#rect1 { 
position: absolute; 
width: 100px; 
height: 100px; 
background: #ffffff; 
} 

And here's where I include it in the HTML main page:

<body> 
<div class="container_24"> 
<header> 
<h1>Omicrome</h1> 
...
</div>
      
<div id="rect1"></div> 
<div class="main clearfix">
...
      </div>
      <div class="grid_6">
      </div> 

The

<div id "rect1"></div>

portion specifies how to create the rectangle within the html code. Do you have any suggestions on how I can position the rectangle behind the text as intended?

Answer №1

One way to enhance the appearance is by including

z-index: -1;

within the styling of #rect1:

#rect1{
  position: absolute;
  width: 100px;
  height: 100px;
  background: #ffffff;
  z-index: -1;
}

View this in action on Fiddle.

(Additionally, consider adjusting the dimensions of the rectangle for a better visual effect.)

Answer №2

In the case that absolute positioning is necessary, adjusting the z-index to -1 can be a solution. However, an alternative approach could involve styling the existing div or span with a different background and border instead of introducing an additional div element.

Answer №3

<html>
    <head>
        <style>
             #rect1{
                position: absolute;
                width: 100px;
                height: 100px;
                background: #eee;
                z-index:-1;
                 }
        </style>
    </head>
       <body>

   <div class = "container_24">
   <header>
   <h1>Omicrome</h1>
   <nav>
    <ul>
      <li><a href="# " class = selected>Home</a></li>
      <li><a href="articles.html">Articles</a></li>
      <li><a href="software.html">Projects</a ></li>
      <li><a href="about.html">About</a></li>
    </ul>
  </nav>

  <div class = "banner grid_18" href="about.html">
   <a href="logic.html"  class="button" id ="new">View Article</a>
    <h2>

    </h2>
  </div>
  <div class=" grid_8 callout"></div>
   <a href="gallery.html"  class="button" id ="spacesoft">View Gallery</a>
</header>

<div id = "rect1"></div>


<div class = "main clearfix">
<div class ="grid_9">
<h3>About The Site</h3>
<p>Here at Omicrome we are always coming up with new and innovative ideas for the future and for the present. These ideas are researched and expanded upon to make them a reality. View our ideas in the article section and see them come to life in the project section. Our many project's range from software to hardware, Huge build's to small and fun one day project's. We are all about space and technology...
   <p><a href="about.html"  class="button">Find Out More</a></p>
   </div>
   <div class ="grid_9">
   <h3>Our Content</h3>
   <p>We post a variety of ideas and project's based around space and     technology. Some of them include a hand held cheap computer, software to teach people about space and snapshot's of stellar and interstellar objects visible from our telescope. We ask questions like "Why dont we have a robotic Mars  colony?" and find an answer or solution. </p><p>
</p><p>  </p>

  <p><a href="about.html" class="button">Find Out More</a></p>
  </div>
  <div class ="grid_6">


  </div>
</html>

That is the code provided. Additionally, feel free to utilize bootstrap classes to format your div text separately if desired.

The background-color remains as #eee for visibility purposes. Feel free to customize it accordingly.

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

The incorrect CSS styling was applied to the custom input background with the attribute "text"

Is there any way to remove the white border and background that appears around a form input field with type "text"? Looking for some help on this issue! .my-form-login input[type="text"] { background-image: url('../images/text-input.png') ...

Manually browse through images in photoswipe by clicking on them to move to the previous or next ones

Utilizing photoswipe within my mobile app has been a seamless experience. Instead of utilizing the full screen view, we are displaying images within a target div. A new requirement was introduced to hide the built-in toolbar and incorporate arrow buttons ...

Adjust image source based on media query (CSS or JavaScript)

Is there a way to update the image src based on a media query with a maximum width of 515px? I'm considering using JavaScript, jQuery, or even AngularJS if there is an event equivalent to a CSS media query that can achieve this. I want to have differ ...

What is the method to deactivate multiple links using jQuery?

Below is the HTML code: <a title="Login" data-href="/MyAccount/Access/Login" data-title="Admin" data-entity="n/a" id="loginLink" class="nav-button dialogLink"><b>Login</b></a> <a title="Register" data-href="/MyAccou ...

Altering the innerHTML of a <p> tag with a smooth transition

I am a beginner in the world of web design. I am currently working on a project where I need to continuously change the text inside a <p> element with a transition effect similar to a slideshow. Below is the code I have so far: <p id="qu">S ...

What is the best way to extract information from a dynamically generated input field using vanilla JavaScript?

Creating a dynamic input field inside the 'education-field' div is the main goal here. The user can add more fields by clicking the "Add more" button, and the data from these input fields is crucial for form validation purposes. Users can input t ...

Tips for displaying the html content saved in the database onto an ejs webpage

This task seems simple, but I'm struggling to solve it. In my Node.js/Express webapp, I have the Quill.js editor installed. It stores my description in MySQL DB like this: <p><strong>This is the quill data. How are we doing dev?</stron ...

Creating resizable SVG elements using HTML or CSS for dynamic width and height

Is there a way to give my SVG element dynamic width and height in order to display the entire SVG image? For example, take a look at this CodePen link. <svg width="250" height="250" viewBox="0 0 250 250"> Alternatively, .svg { width : 250px; ...

The data-ng-bind directive cannot be used with the <option> tag

Currently, I am in the process of learning angular and encountered a problem that has me stuck. Upon researching on AngularJS : Why ng-bind is better than {{}} in angular?, I found out that both {{}} and ng-bind are said to produce the same result. However ...

The placement of all buttons must be at the bottom of every card

I'm looking for a way to align the buttons Get LenoxBot and Dashboard at the bottom of each card, as they currently have varying heights. https://i.sstatic.net/qVXZI.png This is the code I'm currently using: <div class="card mt-3 serverscar ...

How can I make a row div taller?

Looking at the livelink and code provided below, I have successfully created a responsive grid layout. The last adjustment that needs to be made is to change some of the squares from square shape to rectangular. The top two squares in the second column an ...

The CSS sibling combinator is not functioning as expected

Imagine having this HTML structure, <div id="banner-message"> <p>Hello World</p> <button>Hover to change color</button> </div> My goal is to target the p element from the button. Why is the following CSS not workin ...

The JQuery function .html() fails to include all the HTML content retrieved from a JSON response

I've created some HTML using PHP's json_encode(), and it looks like this: ob_start(); if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); get_template_part( 'template-par ...

Using CSS to design a table-like structure with rows that span multiple columns

I am trying to generate a table dynamically using CSS and a JSON array. For example: In the code snippet provided, I have assigned a class of 'spannedrow' to span certain cells in the table, although the class is not defined yet. This is just ...

Is there a way to clear inline css styles added by jquery?

I've recently made changes to my responsive menu, but I'm facing an issue where the menu disappears upon resizing. When my window size is reduced to below 1100px and I open the responsive menu, everything works as expected. However, upon closing ...

How can I detect a DOM element mutation based on a CSS selector, and if this is possible, how can it be accomplished?

Imagine there's a website with a specific HTML element. It seems that this element has the same class during the DOMContentLoaded event as it does during the load event. However, after the load event, this class (and possibly the ID and other HTML att ...

Looking for a vertical scrollbar solution for your idangerous vertical swiper?

Incorporating idangerous vertical swiper with two slides in a mobile app through jquery mobile has proven challenging. Specifically, I am struggling to implement a vertical scroll bar in the second slide due to its fixed height and the potential collision ...

Is it possible to trigger the @click event in Vuejs when utilizing v-html to render HTML content?

Here is an interesting HTML scenario: errorText: '<a class="button" @click.prevent="enableExceedable(issue.mapping)">Enable exceedable limits</a>'; I am trying to insert this string into the following div: <div v-if="hasIssue != ...

Prevent certain individuals from selecting a checkbox within HTML forms

Currently, I have a form where users need to fill in their information. However, I want certain parts of the form to be preventable based on the level of access the user has. For example, I have successfully implemented a code snippet onfocus='blur() ...

Adjust CSS style height dynamically

I am currently creating a leaderboard for users: <div class="user"> <ul id="jogadores" *ngFor="let u of sortedUsers$; let i=index;" (click)="routeToUser(u.id)"> <li class="user"> <img class="gravatar" src="https://www.gra ...