Using HTML5 and CSS How to place one element in front of another elemen

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
In an HTML5 page I would like to put one element exactly in front of another so that it completely covers the one which is behind.
So in the HTML code I have put in the code window I would like the user to see only the the element which is in front. I would like to achieve this without using absolute positioning.

In WPF application, using XAML I can achieve this very easily. So if have a Grid Cell and put a a button in it and then put another button in the same cell. The second button completely overlaps the first button. I would like to achieve the same kind of thing in HTML5 and would appreciate guidance on how to achieve this.
At the moment on my page red and green button are displayed one above another. I would like the green button to completely overlap red button.

To clarify my question I am putting a XAML example with HTML code.
<div>
    <input type="button" height="90" style="width:400px; background-color: red;" value="I am in behind" />
</div> 
<div>
    <input type="button" height="90" style="width:400px; background-color: green;" value="I am in front" />
</div> 


// In the WPF XAML example Button two will completely overlap button one. 


<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
        </Grid.RowDefinitions>

       
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" ></ColumnDefinition>
                <ColumnDefinition Width="1*" ></ColumnDefinition>
                
            </Grid.ColumnDefinitions>
<Button Name="One" Grid.Row="0" Grid.Column="0" ></Button>
<Button Name="Two" Grid.Row="0" Grid.Column="0" ></Button>
</Grid>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try this code :

<div style="z-index:1; position:absolute;">
    <input type="button" height="90" style="width:400px; background-color: red;" value="I am in behind" />
</div> 
<div style="z-index:2; position:absolute;">
    <input type="button" height="90" style="width:400px; background-color: green;" value="I am in front" />
</div> 

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Why not just hide the first element

.elementId {
   display: none;
}

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Working sample
CSS
<style type="text/css">
.buttons div:first-child {
  display: none;
}
</style>

Open in new window

HTML
<div class="buttons">
  <div>
    <input type="button" height="90" style="width:400px; background-color: red;" value="I am in behind" />
  </div> 
  <div>
    <input type="button" height="90" style="width:400px; background-color: green;" value="I am in front" />
  </div> 
</div>

Open in new window


Working sample here
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Many thanks. I am very grateful. Both solutions were helpful, but as Julina's solution was more thorough with a working a example, I found it more helpful. I was away from my desk hence some delay in accepting the solutions.
Thank you very much again.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I would like to achieve this without using absolute positioning.
Just pointing out the first solution uses absolute positioning

Otherwise you are most welcome.

Author

Commented:
Thank you Julian. I realise that now. The first solution did not fully answer my question. Many thanks again for you kind help.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial