Z-index doesn't work with flex elements? [duplicate] - css

This question already has answers here:
Flexbox, z-index & position: static: Why isn't it working?
(1 answer)
Understanding z-index stacking order
(1 answer)
Closed 5 years ago.
I'm trying to have two columns, one being a menu which can expand and overlap the other column. But I used a flex element to wrap these columns and my menu expands behind the other element, even with a greater z-index.
The render is something like this:
.main {
font-family: 'Open Sans', Helvetica, sans-serif;
display: flex;
background-color: #99baef;
}
nav {
height: 100%;
width: 8em;
background-color: black;
z-index: 1;
}
.navbox {
box-sizing: border-box;
background-color: black;
color: white;
height: 4em;
width: 100%;
text-align: center;
line-height: 4em;
border-top: 1px dotted #99baef;
transition: all 1s;
}
.navbox:hover {
width: 130%;
border-top: none;
background-color: #4a77c4;
color: black;
}
.container {
padding: 1em;
}
a {text-decoration: inherit;}
<div class="main">
<div class="maincolumn">
<nav>
<a href="">
<div class="navbox">
Nav 1
</div>
</a>
<a href="">
<div class="navbox">
Nav 2
</div>
</a>
</nav>
</div>
<div class="maincolumn">
<div class="container">
<h2>Title</h2>
<p>This is a text.</p>
</div>
</div>
</div>
See? I want my menu to overlap the rest of my page when expanding. How can I do that?

The z-index property only effects elements that have a position value other than static (the default) or are flex items (direct children of display: flex or display: inline-flex).
There are two options to make the z-index work in your case:
Set the z-index to the 1st .maincolumn, which is a flexbox item:
.maincolumn:first-child {
z-index: 1;
}
.main {
font-family: 'Open Sans', Helvetica, sans-serif;
display: flex;
background-color: #99baef;
}
.maincolumn:first-child {
z-index: 1;
}
nav {
height: 100%;
width: 8em;
background-color: black;
}
.navbox {
box-sizing: border-box;
background-color: black;
color: white;
height: 4em;
width: 100%;
text-align: center;
line-height: 4em;
border-top: 1px dotted #99baef;
transition: all 1s;
}
.navbox:hover {
width: 130%;
border-top: none;
background-color: #4a77c4;
color: black;
}
.container {
padding: 1em;
}
a {text-decoration: inherit;}
<div class="main">
<div class="maincolumn">
<nav>
<a href="">
<div class="navbox">
Nav 1
</div>
</a>
<a href="">
<div class="navbox">
Nav 2
</div>
</a>
</nav>
</div>
<div class="maincolumn">
<div class="container">
<h2>Titre</h2>
<p>This is a text.</p>
</div>
</div>
</div>
or
Set position: relative to nav:
nav {
position: relative;
height: 100%;
width: 8em;
background-color: black;
z-index: 1;
}
.main {
font-family: 'Open Sans', Helvetica, sans-serif;
display: flex;
background-color: #99baef;
}
nav {
position: relative;
height: 100%;
width: 8em;
background-color: black;
z-index: 1;
}
.navbox {
box-sizing: border-box;
background-color: black;
color: white;
height: 4em;
width: 100%;
text-align: center;
line-height: 4em;
border-top: 1px dotted #99baef;
transition: all 1s;
}
.navbox:hover {
width: 130%;
border-top: none;
background-color: #4a77c4;
color: black;
}
.container {
padding: 1em;
}
a {text-decoration: inherit;}
<div class="main">
<div class="maincolumn">
<nav>
<a href="">
<div class="navbox">
Nav 1
</div>
</a>
<a href="">
<div class="navbox">
Nav 2
</div>
</a>
</nav>
</div>
<div class="maincolumn">
<div class="container">
<h2>Titre</h2>
<p>This is a text.</p>
</div>
</div>
</div>

Flex items are only direct children of flex-container.
Flex items respect z-index order, but you are applying z-index not to flex-items but to their descendants.
From w3c flexbox spec:
Flex items paint exactly the same as inline blocks CSS21, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static.
So for this to work you should apply greater z-index to your first flex item. Demo:
.main {
font-family: 'Open Sans', Helvetica, sans-serif;
display: flex;
background-color: #99baef;
}
.maincolumn:first-child {
z-index: 1;
}
nav {
height: 100%;
width: 8em;
background-color: black;
}
.navbox {
box-sizing: border-box;
background-color: black;
color: white;
height: 4em;
width: 100%;
text-align: center;
line-height: 4em;
border-top: 1px dotted #99baef;
transition: all 1s;
}
.navbox:hover {
width: 130%;
border-top: none;
background-color: #4a77c4;
color: black;
}
.container {
padding: 1em;
}
a {text-decoration: inherit;}
<div class="main">
<div class="maincolumn">
<nav>
<a href="">
<div class="navbox">
Nav 1
</div>
</a>
<a href="">
<div class="navbox">
Nav 2
</div>
</a>
</nav>
</div>
<div class="maincolumn">
<div class="container">
<h2>Title</h2>
<p>This is a text.</p>
</div>
</div>
</div>

Related

CSS aling items in the same row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to aling items in the same row.
I would to obtain the button in the center and the legend in the right part ( near the end of the page)
.legendContainer {
// padding: 10px
// display: flex;
align-content: flex-end;
justify-content: flex-end;
// margin-left: 10px;
}
.legend {
list-style: none;
}
.legend li {
float: left;
margin-right: 10px;
}
.legend span {
border: 1px solid #ccc;
float: left;
width: 12px;
height: 12px;
margin: 2px;
margin-right: 5px;
}
.legendTitle {
font-weight: bold;
}
.legend .KO {
background-color: #FFA8A8;
}
.legend .aOK {
background-color: #B6FFCE;
}
.legend .send {
background-color: #F6FFA4;
}
<div class="row justify-content-center ">
<div class="col-md-4"></div>
<div class="col-md-4" style="padding-left: 70px;">
<button>Offers</button>
<button>Opportunity</button>
</div>
<div class="col-md-4" style="padding-left: 140px">
<div class="legendContainer" *ngIf="tableOffer">
<ul class="legend">
<div class="legendTitle">Legend</div>
<li><span class="KO"></span> KO</li><br>
<li><span class="Send"></span>Send</li><br>
<li><span class="OK"></span> OK</li>
</ul>
</div>
</div>
</div>
I have tried using padding but on mobile device it isn't look good.
Something like this?
Removed the big style paddings: style="padding-left: 70px;", style="padding-left: 140px" from the divs. div.row first child and .legendcontainer with width: fit-content; to fit their content and margin-left: auto, (.ms-auto) so they move to the end. Changed the .col of the buttons div to col-8 (60% big) and the legend container to col-4 (40% big). Bootstrap max col size (and default if its not defined) is 12 (8+4).
.legendContainer {
width: fit-content;
margin-left: auto;
}
.legend {
list-style: none;
margin: 0;
}
.legend li {
float: left;
}
.legend span {
border: 1px solid #ccc;
float: left;
width: 12px;
height: 12px;
margin: 2px;
margin-right: 5px;
}
.legendTitle {
font-weight: bold;
}
.legend .KO {
background-color: #FFA8A8;
}
.legend .aOK {
background-color: #B6FFCE;
}
.legend .send {
background-color: #F6FFA4;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row flex-row justify-content-center w-100 align-items-center">
<div class="col-8 ms-auto p-0" style="width:fit-content;">
<button>Offers</button>
<button>Opportunity</button>
</div>
<div class="col-4">
<div class="legendContainer" *ngIf="tableOffer">
<ul class="legend">
<div class="legendTitle">Legend</div>
<li><span class="KO"></span> KO</li><br>
<li><span class="Send"></span>Send</li><br>
<li><span class="OK"></span> OK</li>
</ul>
</div>
</div>
</div>
Buttons in the center. And the legend on the right.
.btns {
text-align: center;
}
.legendContainer {
/*padding: 10px*/
display: flex;
align-content: center;
justify-content: flex-end;;
/*margin-left: 10px;*/
xbackground: gray;
}
.legendContainer ul {
margin-top: 2px;
}
.legend {
list-style: none;
}
.legend li {
float: left;
margin-right: 10px;
}
.legend span {
border: 1px solid #ccc;
float: left;
width: 12px;
height: 12px;
margin: 2px;
margin-right: 5px;
}
.legendTitle {
font-weight: bold;
}
.legend .KO {
background-color: #FFA8A8;
}
.legend .aOK {
background-color: #B6FFCE;
}
.legend .send {
background-color: #F6FFA4;
}
<div class="row justify-content-center ">
<div class="col-md-4"></div>
<div class="col-md-4 btns" style="">
<button>Offers</button>
<button>Opportunity</button>
</div>
<div class="col-md-4" style="padding-left: 140px">
<div class="legendContainer" *ngIf="tableOffer">
<ul class="legend">
<div class="legendTitle">Legend</div>
<li><span class="KO"></span> KO</li><br>
<li><span class="Send"></span>Send</li><br>
<li><span class="OK"></span> OK</li>
</ul>
</div>
</div>
</div>
legend to display: inline;, or float the li left or right. Additionally, you can also use display:flex; on the ul
.legendContainer {
}
.legend {
display: flex;
justify-content:space-around;
list-style-type:none;
float: left;
}
.legend li {
/* float: left; */
margin-right: 10px;
}
.legend span {
border: 1px solid #ccc;
float: left;
width: 12px;
height: 12px;
margin: 2px;
margin-right: 5px;
}
.legendTitle {
font-weight: bold;
}
.legend .KO {
background-color: #FFA8A8;
}
.legend .aOK {
background-color: #B6FFCE;
}
.legend .send {
background-color: #F6FFA4;
}
<div class="row justify-content-center ">
<div class="col-md-4"></div>
<div class="col-md-4" style="padding-left: 70px;">
<button>Offers</button>
<button>Opportunity</button>
</div>
<div class="col-md-4" style="padding-left: 140px">
<div class="legendContainer" *ngIf="tableOffer">
<ul class="legend">
<div class="legendTitle">Legend</div>
<li><span class="KO"></span> KO</li><br>
<li><span class="Send"></span>Send</li><br>
<li><span class="OK"></span> OK</li>
</ul>
</div>
</div>
</div>

Text and images won't center on my page

I am making my first webpage and I have the elements I want- I can't seem to get them to center on the page. I have tried margin: auto, text-align: center, and lots of toher things. Nothing has worked.
I want the header, an image , and a button at the end of my page, to be a central spine down my page. Any suggestions would be appreciated.
Thanks
Here is my code:
HTML:
<div class="container">
<h1 class="text-center"> My tribute to Ami Coxill Moore</h1>
</div>
<!-- picture of ami here -->
<div class="container">
<div class="row">
<div class="col-xs-4 col-xs-offset-4"></div>
<img class="img-resize img-center img-zero image-border img-responsive" src="https://i.imgur.com/Lc5nBon.jpg" alt="Ami laughing at her 26th Birthday">
</div>
</div>
CSS
body {margin-top: 100px;
background-color: #ff69b4;
font-family: "gerogia", serif;
color: #0000ff; }
h1 {
display: inline-block;
background-color: yellow;
font-size: 300%;
border: solid;
border-radius: 25px;
align: center;
padding-left: 40px;
padding-right: 40px;
float: center;
max width: 100%;
text-align: center;
marrgin: 0 auto;
float: center;}
div {
padding-bottom: 20px;
padding-right: 50px;
padding-left: 50px;}
.img-responsive {
margin: 0 auto;}
.img-resize {
max-width: 400px;
max-height: auto;}
you need to add the container text-align
<div class="container" style="text-align: center;">
the div that wraps your element is the responsible to align the element.
Define a class and call it for example .text-center and put inside it text-align: center,
once you want to center a text just give this class to the container element (parent element).
Hope this solve your issue.
body {margin-top: 100px;
background-color: #ff69b4;
font-family: "gerogia", serif;
color: #0000ff; }
.text-center {
text-align: center;
}
h1 {
display: inline-block;
background-color: yellow;
font-size: 300%;
border: solid;
border-radius: 25px;
padding-left: 40px;
padding-right: 40px;
max width: 100%;}
div {
padding-bottom: 20px;
padding-right: 50px;
padding-left: 50px;}
.img-resize {
max-width: 400px;
max-height: auto;}
<div class="container text-center">
<h1> My tribute to Ami Coxill Moore</h1>
</div>
<!-- picture of ami here -->
<div class="container text-center">
<div class="row">
<div class="col-xs-4 col-xs-offset-4"></div>
<img class="img-resize img-center img-zero image-border img-responsive" src="https://i.imgur.com/Lc5nBon.jpg" alt="Ami laughing at her 26th Birthday">
</div>
</div>
There are some mistake in your code, for example, you must use of max-width: 100%; no max width: 100%; or text-align: center; no align: center; value for float is left or right so float:center is wrong, and other mistakes.
see my code:
body {
margin-top: 100px;
background-color: #ff69b4;
font-family: "gerogia", serif;
color: #0000ff;
}
h1 {
background-color: yellow;
font-size: 300%;
border: 1px solid;
border-radius: 25px;
text-align: center;
padding-left: 40px;
padding-right: 40px;
max-width: 100%;
text-align: center;
margin: 0 auto;
}
div {
padding-bottom: 20px;
padding-right: 50px;
padding-left: 50px;
}
.row {
text-align: center;
}
.row img {
display: inline-block;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
<h1 class="text-center"> My tribute to Ami Coxill Moore</h1>
</div>
<div class="container">
<div class="row">
<a href="#add-fastrack"><img class="img-resize img-center img-zero image-border img-responsive" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRDAYrQr9qgT2W00EV_CoCahFki3Vw4lSMNt81k9FCSTXoKT8TY2w" alt="Ami laughing at her 26th Birthday">
</a>
</div>
</div>

aligning multiple div, img and text in CSS - problems

I'm trying to align several DIVS, Images and text and I can't figure this out..
Here's what I'm trying to achieve:
Heres' my HTML
<div class="section5">
<div class="outer">
<div class="container1">
<img src="icon.png" width="85">
<div class="title1">Text</div>
<div class="subtitle1">Text</div>
</div>
<div class="container2">
<img src="iphone.png" width="375">
<div class="subtitle2">Text</div>
</div>
</div>
</div>
Here is my CSS:
.section5{ height: 525px; background-color: #5e6172; text-align: center; position: relative;}
.outer{ width: 80%; background-color: #45da45; height: 100%; margin: 0 auto; position: relative;}
.title1{color: #ffffff; font-size: 2.6em; font-family: h35; }
.subtitle1{color: #ffffff; font-size: 1.5em; font-family: h35; margin-top: 0.25em; }
.subtitle2{color: #ffffff; font-size: 1.5em; font-family: h35; margin-top: 0.25em; }
.container1{display: block; background-color: #ccc; }
.container2{display: block; background-color: #fffc1e; }
Here is the JSFIDDLE:
http://jsfiddle.net/mib92/hogwohf8/
My current problems:
1) My text at the bottom needs to be on the right side of the image.. center like in my example image.
2) the bottom of my bottom picture must be align to the bottom of container2 AND the bottom of section5
3) While doing this, the container 1 must remain in the vertical middle of the remaining space of the section5.
Thank you
I hops it's help you.
.section5 {
height: 525px;
background-color: #5e6172;
text-align: center;
position: relative;
}
.outer {
width: 80%;
background-color: #45da45;
height: 100%;
margin: 0 auto;
position: relative;
}
.title1 {
color: #ffffff;
font-size: 2.6em;
font-family: h35;
}
.subtitle1 {
color: #ffffff;
font-size: 1.5em;
font-family: h35;
margin-top: 0.25em;
}
.subtitle2 {
color: #ffffff;
font-size: 1.5em;
font-family: h35;
margin-top: 0.25em;
}
.container1 {
display: block;
background-color: #ccc;
}
.container2 {
display: block;
background-color: #fffc1e;
}
.right-sied {
display: inline-block;
vertical-align: top;
width: 41%;
}
.left-sied {
display: inline-block;
width: 49%;
}
.left-sied img {
max-width: 100%;
}
<div class="section5">
<div class="outer">
<div class="container1">
<img src="icon.png" width="85">
<div class="title1">Text</div>
<div class="subtitle1">Text</div>
</div>
<div class="container2">
<div class="left-sied">
<img src="iphone.png" width="375">
</div>
<div class="right-sied">
<div class="subtitle2">Text</div>
</div>
</div>
</div>
</div>
Live Demo
check this:
.section5 {
height: 525px;
background-color: #5e6172;
text-align: center;
position: relative;
}
.outer {
width: 80%;
background-color: #45da45;
height: 100%;
margin: 0 auto;
position: relative;
}
.title1 {
color: #ffffff;
font-size: 2.6em;
font-family: h35;
}
.subtitle1 {
color: #ffffff;
font-size: 1.5em;
font-family: h35;
margin-top: 0.25em;
}
.subtitle2 {
color: #ffffff;
font-size: 1.5em;
font-family: h35;
margin-top: 0.25em;
}
.container1 {
display: block;
background-color: #ccc;
}
.container2 {
display: block;
background-color: #fffc1e;
}
.container1 .wrapper {
display: inline-block;
float: right;
}
.container2 img {
margin: 0 auto;
}
<div class="section5">
<div class="outer">
<div class="container1">
<img src="http://images.all-free-download.com/images/graphiclarge/daisy_pollen_flower_220533.jpg" width="85">
<div class="wrapper">
<div class="title1">Text</div>
<div class="subtitle1">Text</div>
</div>
</div>
<div class="container2">
<div class="subtitle2">Text</div>
<img src="http://images.all-free-download.com/images/graphiclarge/daisy_pollen_flower_220533.jpg" width="375">
</div>
</div>
</div>

How to make image buttons stay in place when re sizing web browser

This is my image-button for my website. http://puu.sh/cK7Sf/6309c39cdb.jpg When I re-size my browser it goes over here http://puu.sh/cK7VU/f17dafcc41.jpg
Here is my code
HTML
<div class="Nav">
<div id="buttons">
<div id="home_button"></div>
CSS
#home_button {
background-image: url("home.png");
background-repeat:no-repeat;
background-size: 100%;
width: 150px;
height: 60px;
position: absolute;
top: 196px;
left: 502px;
z-index: 10;
}
I am new to web developing so please don't hate :)
I've created an example to show you what a basic nav bar styling looks like.
Demo on dabblet
HTML:
<body>
<!--Navigation-Bar-Container-->
<div class="nav-container">
<!--Navigation-Bar-Item-Wrapper-->
<div class="nav">
<!--Items-->
<div id="menu-item-1" class="menu-item">Item 1</div>
<div id="menu-item-2" class="menu-item">Item 2</div>
<div id="menu-item-3" class="menu-item">Item 3</div>
<div id="menu-item-4" class="menu-item">Item 4</div>
<div id="menu-item-5" class="menu-item">Item 5</div>
<hr />
</div>
</div>
<!--Content-->
</body>
CSS:
body {
background: url(http://s25.postimg.org/b6q25p4p7/black_thread.png) repeat black;
}
hr {
color: #777777;
}
.nav-container {
top: 20px;
position: relative;
width: 100%;
height: 40px;
text-align: center;
}
.nav {
width: 100%;
height: 40px;
maring: 0 auto;
text-align: center;
}
.menu-item {
display: inline-block;
width: 50px;
padding-left: 30px;
padding-right: 30px;
padding-top: 10px;
padding-bottom: 10px;
color: #777777;
}
.menu-item:hover {
background-color: black;
cursor: pointer;
border-radius: 2px;
}
Here is what I would do, some adjustment needed, not sure why the button is absolutely positioned, or what the background image is for, but here goes:
HTML:
<ul class='menu'>
<li>Link 1</li>
<li>Link 2</li>
</ul>
CSS:
.menu {
list-style-type: none;
}
.menu li {
display: inline-block;
border: 0.2em solid black;
border-radius: 10%;
padding: .5em;
background: linear-gradient(lightblue, blue);
}
.menu li a {
text-decoration: none;
color: black;
}

positioning the text in the center of li tag

I have created three blocks using the html code as shown below. The "a" tag has the min-height css property because of which it will be displayed as block. I am not able to place the text inside the span tag in the center of "li" tag. Although each text inside the tag is of different length, I want to display them in the center.
<div class="container">
<div class="col-md-12">
<ul id="tabs" class="nav-tabs">
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Data</span>
</a>
</li>
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Charts</span>
</a>
</li>
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Reports</span>
</a>
</li>
</div>
</div>
CSS:
.container ul {
list-style: none outside none;
}
li {
display: list-item;
}
.col-md-4 {
width: 33%;
}
.nav-block {
background-color: #FFA500;
display: block;
min-height: 70px;
position: relative;
}
.container .nav-block .header {
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
left: 20%;
padding: 5px;
position: relative;
text-align: center;
top: 21%;
}
Please let me know where I am going wrong
just changed the css to center the texts, check in the fiddle
.nav-block {
background-color: #FFA500;
display: block;
min-height: 70px;
position: relative;
text-align: center;
}
.container .nav-block .header {
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
padding: 5px;
position: relative;
text-align: center;
top: 21%;
}
You should set the text-align:center; on the li{}
And delete the position:relative; on .container .nav-block .header {
DEMO
With editting my sugestions you have a shorter css, because you can delete the unaccecary things. Like here.
.container .nav-block .header {
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
padding: 5px;
}
DEMO 2 (same as the other but less css)
Try this below css.
.nav-block {
background-color: #FFA500;
display: block;
min-height: 70px;
position: relative;
text-align: center; /*New Edit*/
}
.container .nav-block .header {
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
/*left: 20%; - Remove*/
padding: 5px;
position: relative;
text-align: center;
top: 21%;
}
just add text-align:center; to .nav-tabs or .nav-block
You aren't closing your <ul> tag.
I hope this is what you want ->
DEMO
HTML
<div class="container">
<div class="col-md-12">
<ul id="tabs" class="nav-tabs">
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Data</span>
</a>
</li>
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Charts</span>
</a>
</li>
<li class="col-md-4 nav-list">
<a href="#" class="nav-block">
<span class="header">Reports</span>
</a>
</li>
<!--closing the ul tag-->
</ul>
</div>
</div>
CSS
.container ul {
list-style: none outside none;
}
li {
display: list-item;
}
.col-md-4 {
width: 33%;
}
.nav-block {
background-color: #FFA500;
display: block;
min-height: 70px;
position: relative;
text-align:center; /*aligning the text center*/
line-height:60px; /*this gives vertical alignment to your element*/
}
.container .nav-block .header {
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
left: 0; /*removed the defined 20% to center you text inside anchor <a> tags*/
padding: 5px;
position: relative;
text-align: center;
top: 21%;
}

Resources