I am trying to get an element to appear on top of its parent's sibling but it only appears underneath. I have tried changing the z-index and playing around with floats but can't find a working solution. I want to keep the Stuff span inside its parent span as it is related to it and works well if CSS is disabled.
He is what I have so far http://jsfiddle.net/P3qwx/
HTML
<div class="grid">
<span>
<h4>1</h4>
</span>
<span>
<h4>2</h4>
<span>Stuff</span>
</span>
<span>
<h4>3</h4>
</span>
<span>
<h4>4</h4>
</span>
</div>
CSS
.grid > span {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
z-index: 5;
}
.grid > span > span {
background-color: #ff00ff;
display: inline-block;
width: 250px;
z-index: 10;
}
This is what I get (FF30)
This is what I want
You can try this:
Demo
.grid > span > span {
background-color: #ff00ff;
display: inline-block;
width: 250px;
z-index: 10;
position:absolute;
}
Pradeep Pansari's answer is all good but I would like to explain a little bit more thus provide another solution to your question.
First of all, your z-index code doesn't work at all. z-index only has an effect if an element is positioned.
Now, let's add the position. Your css is now
.grid > span {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
z-index: 5;
position:relative;
}
.grid > span > span {
background-color: #ff00ff;
display: inline-block;
width: 250px;
z-index: 10;
position:absolute;
}
This is the result http://jsfiddle.net/P3qwx/4/
What's happening here? Why is the purple block is still hidden under the third and fourth yellow blocks?
This is because for each yellow block, there is a stacking context created
So long story short, your purple block and its z-index only takes effect under the second yellow block, it has no power whatsoever under the third and fourth one because of different stacking context. Here's the hierarchy
Yellow block 1 (z-index 5)
Yellow block 2 (z-index 5)
Purple block (z-index 10)
Yellow block 3 (z-index 5)
Yellow block 4 (z-index 5)
Once we got to this point, fixing is simple, either removing the z-index and setting the position to absolute and let the default stacking rule takes care of business
Demo
.grid > span {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
}
.grid > span > span {
background-color: #ff00ff;
display: inline-block;
width: 250px;
position:absolute;
}
Or (I suppose you don't want this but just for for completeness sake..)
Demo
HTML
<div class="grid">
<span class="span1">
<h4>1</h4>
</span>
<span class="span2">
<h4>2</h4>
<span class="span5">Stuff</span>
</span>
<span class="span3">
<h4>3</h4>
</span>
<span class="span4">
<h4>4</h4>
</span>
</div>
CSS
.span1 {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
position:relative;
z-index: 1;
}
.span2 {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
position:relative;
z-index: 5;
}
.span3 {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
position:relative;
z-index: 3;
}
.span4 {
background-color: #ffff00;
display: inline-block;
width: 100px;
height: 100px;
vertical-align: top;
position:relative;
z-index: 4;
}
.span5 {
background-color: #ff00ff;
display: inline-block;
width: 250px;
position:absolute;
z-index:1;
}
Related
This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Why is this inline-block element pushed downward?
(8 answers)
Closed 11 months ago.
I have a React component which is divided into 3 parts to hold 3 different data values - date, title and amount.
The layout looks good and aligned, but when I add a value in the first section (red), it will adjust my CSS is a very strange way which I can not figure out why.
First image shows the component itself, second image shows the component with HTML content inside it.
Expense.js
<div className="expense">
<div className="date">
<h6>DEMO!</h6>
</div>
<div className="title">
</div>
<div className="amount">
</div>
Expense.css
.expense {
border: 1px darkslategrey solid;
height: 100px;
display: flow-root;
}
.date {
display: inline-block;
background-color: darkred;
width: 20%;
height: 100%;
}
.title {
display: inline-block;
background-color: darkorange;
width: 60%;
height: 100%;
}
.amount {
display: inline-block;
background-color: darkgreen;
width: 20%;
height: 100%;
}
Add vertical-align: top to the three (inline-block) components. The default value for this is baseline, which is what you see in your second image.
Actually you don't need to add it three times, but can do it like this:
.expense > div {
vertical-align: top;
}
Full code (converted to plain HTML/CSS):
.expense {
border: 1px darkslategrey solid;
height: 100px;
display: flow-root;
}
.expense>div {
vertical-align: top;
}
.date {
display: inline-block;
background-color: darkred;
width: 20%;
height: 100%;
}
.title {
display: inline-block;
background-color: darkorange;
width: 60%;
height: 100%;
}
.amount {
display: inline-block;
background-color: darkgreen;
width: 20%;
height: 100%;
}
<div class="expense">
<div class="date">
<h6>DEMO!</h6>
</div><div class="title">
</div><div class="amount">
</div>
</div>
This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 4 years ago.
I have two spans in a div positioned next to each other. But it gets misaligned the moment I add overflow: hidden to one of the span.
Why does this happen?
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
Try This:-
Use vertical-align: top;
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
vertical-align: top;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
Alright so the way that I fixed it for you was using verticle-align:bottom.
The reason this happens is just because css has problems. It has improved greatly, but there are still issues with it. This is a great example of one.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
<style>
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
vertical-align: bottom;
}
</style>
</body>
By default display: inline-block set vertical-align: bottom; so you need to set vertical-align: top for span.
Note: Overflow only work with position, so here you check in snippet it's working fine.
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
For more specific:
vertical-align has about 5 values:
- top
- bottom
- center
- baseline
- inherit
If your elements have different heights, you will see differents.
- top: all elements will be aligned to top
- bottom: all elements will be aligned to bottom
- center: all elements will be aligned to center
- baseline (default): Depends on font-size, line-height, they will be aligned.
- inherit: just simple inherit from parent.
And an extra information: If your markup does not provide any spaces between elements, it will works fine, but since they're inline-block, a single new-line or space will breaks them into two words (kind of) and so... between them, there're spaces. No matter how you set width for both (eg. 50% for two) they will still breaks into two or more lines because spaces.
I have a div containing an input and an a element:
.wrapper {
width: 300px;
display: inline-block;
}
.custom-input {
display: block;
width: 100%;
}
.button {
width: 20px;
height: 20px;
background-color: #ccc;
display: block;
}
<div class="wrapper">
<input class="custom-input" type="text" />
<a class="button"></a>
</div>
Here's a jsfiddle.
I want my input and my button inline. The input with button always has 100% width of the wrapper. In some cases, I want to remove the button. The input then has 100% width of the wrapper div.
It is only inline when I use inline-flex for the wrapper. But I want it to be able to run on old browsers (IE 8-9), and I want my input and my element to always have 100% width of wrapper.
How can I do it?
Using width: 100% on your input will make it take all the horizontal space available, pushing the button to the line below.
This should work :
.wrapper{
width: 300px;
display: inline-block;
}
.custom-input{
display: inline-block;
}
.button{
width: 20px;
height: 20px;
background-color: #ccc;
display: inline-block;
}
Here's the updated jsfiddle : http://jsfiddle.net/k6yhtf92/3/
try to use display: inline-block instead display:block
updated css
.wrapper{
width: 300px;
display: block;
}
.custom-input{
display: inline-block;
}
.button{
width: 20px;
height: 20px;
background-color: #ccc;
display: inline-block;
}
I have one div which contains an ul tag and a span tag to creat my justified horizontal navbar.
The second span is an hidden span because i wanna the text of the first span is justified.
So, the problem i have now that, the parent div'height is larger than his children's height.
HTML:
<div id="div1">
<ul id="span1">
<li>ABC</li>
<li>DEF</li>
<li>GHI</li>
<li>JKL</li>
</ul>
<span id="span2"></span>
</div>
CSS:
#div1 {
background-color: red;
margin: 0;
padding: 0;
text-align: justify;
height: 15px;
}
#div1 ul#span1 {
display: inline;
background-color: blue;
}
#div1 ul#span1 li {
display: inline-block;
}
#div1 #span2 {
display: inline-block;
width: 100%;
height: 0px;
background-color: green;
}
JSFIDDLE
Any suggestions? Thanks!
Another question: I want to add a separate circle between two words like that:
How can i do? Thanks!!!
EDIT: For my first question, thanks for #Leth0_, i've just changed height = 15px and it worked. Please give me some suggestions for my second question! THANKS!!
You could try manually setting the height, try adding...
height:20px to the parent.
Try this JSFIDDLE
#div1 {
background-color: red;
margin: 0;
padding: 0;
text-align: justify;
}
#div1 #span1 {
display: inline-block;
width:100%;
background-color: blue;
}
#div1 #span2 {
width: 100%;
height: 0px;
background-color: green;
}
I have following HTML for a heading. The .left and .right are empty spans. I have specific width for the .left and but the .text width is not always same. I want to set the background for the .left (fixed width) and the .right. The .right should get all the remaining space in the parent element (h1). How that can be done?
<h1>
<span class="left"></span>
<span class="text">Text</span>
<span class="right"></span>
</h1>
I'm trying following CSS which does not work:
.left{
background: yellow;
width: 30px;
height: 20px;
display: inline-block;
}
.right{
display: inline-block;
background: blue;
}
Here's the JSFiddle link:
http://jsfiddle.net/jMR8u/
Here's what I'm trying to get:
The idea is to set a background image in h1 except the .text span and the problem is that I can not set the background for the .text, otherwise it would be easier.
This version will stretch to fit the contents of .text and should be cross-browser.
You can fake the blue (right) background by making it a border of .text:
.text { border-right: 1000px solid; }
Then, shift .right to the left by 1000px:
.right { margin-left: -1000px; }
Give a width to .left, make each element inline-block, hide the extra blue border on the right, and make sure .text and .right do not wrap to a new line:
.left { width: 200px; }
.left, .text, .right { display: inline-block; }
h1 { overflow: hidden; white-space: nowrap; }
And give it color!
body { background: green; }
.left { background: red; }
.text { border-color: blue; }
Here is a JSFiddle demonstration:
if i interpret your image correct .. this is the answer http://jsfiddle.net/jMR8u/4/
h1{
border: 1px solid red;
position: relative;
}
.left{
background: yellow;
width: 30px;
height: 20px;
display: inline-block;
}
.right{
display: inline-block;
background: blue;
position: absolute;
z-index: 99;
height: 20px;
width: 100%;
}
.text {
height: 20px;
width: 150px;
display: inline-block;
position: relative;
z-index; 101;
}
ok, then use layers .. with z-index and positioning
You could use flexbox (but use the new syntax). Sadly, it only works on Chrome and Opera for now, so this has limited usefulness:
h1 { display: -webkit-flex; display: flex; }
.left { width: 30px; }
.right { flex: 1; -webkit-flex: 1; } /* This makes it fluid. */
.left { background: yellow; }
.right { background: blue; }
Here is a JSFiddle demonstration: http://jsfiddle.net/FN7vQ/
if you can set width to the .text span and h1 element.
body{
background:green;
}
h1{
border: 1px solid red;
display:table;
width:100%;
}
.left{
background: yellow;
width: 30px;
display: table-cell;
}
.right{
display: table-cell;
background: blue;
}
.text {
display:table-cell;
width: 150px;
}
If I understood your requirement correctly. you should change your markup a little bit as below
h1 {
background: #660000;
padding-left: 30px;
line-height: 1.1;
}
h1 span {
background: #fff;
padding: 0 3px;
color: #600;
}
<h1>
<span>
Lorem, ipsum dolor. you are doing great
</span>
</h1>
and CSS goes here below