Multiple z-index elements - css

I' am doing a project and I have a little issue with z-index property.
Here is my code:
(HTML)
<div class="outer_obw">
<div class="obw1">
<div class="box" id="blue_box">
<div id="inn_blue" class="inner_box"><p>Box1</p></div>
</div>
</div>
<div class="main_box_content">
<div class="back_box">
<div class="main_box">
<p id="texts">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
<div class="obw3">
<div class="box" id="green_box">
<div id="inn_green" class="inner_box"><p>Box2</p></div>
</div>
</div>
</div>
</div>
(CSS)
.outer_obw {
width: 78.5%;
margin: 0 auto;
}
.obw1 {
min-height: 80px;
}
.obw3 {
min-height: 80px;
margin-top: -40px;
}
.box {
width: 25.25%;
min-height: 80px;
cursor:pointer;
position: relative;
}
.inner_box {
height: 68px;
margin: -10.5px 6px;
text-align: center;
position: relative;
}
#inn_blue {
background-color: #fff;
z-index: 5;
}
#inn_green {
background-color: #fff;
z-index: 5;
}
#blue_box {
background-color: blue;
float: left;
z-index: 1;
}
#green_box {
background-color: green;
float: right;
}
.main_box_content {
display: table;
width: 78.5%;
position: absolute;
margin-top: -40px;
}
.back_box {
display: table;
background-color: blue;
width: 65%;
margin: 0 17%;
position: relative;
z-index: 3;
}
.main_box {
display: table;
background-color: #f1f1f1;
margin: 6px;
padding: 0.5% 3%;
position: relative;
z-index: 10;
}
Here is all code and visualization.
I intended to achieve such an effect:
All what I need to do is insert inn_blue and inn_green (white fields of box1 and box2) between main_box (gray field with a text) and back_box (red background of the main box.
I don't know what I'm doing wrong. Z-index of the main_box should be greater than z-index of inn_blue/inn_green and z-index of the inn_blue/inn_green should be greater then back_box.
And so it is in my code, but the effect is not what I expected...
So the question is what am I doing wrong?

There are so many layers of complexity in your example. Instead, let's use the natural layers to our advantage and position with absolute and minimal markup.
The basics
Start off with a wrapper to contain your three boxes:
<div class="wrapper">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
The wrapper will be position: relative and its three children will be positioned with position: absolute and top / right / bottom / left. In order to allow a flexible size that resizes in proportion, we can make use of the viewport width (vw) unit for both the width and the height. Each child div is given a percentage height.
.wrapper {
position: relative;
background: #EEE;
height: 60vw;
width: 80vw;
}
.wrapper div {
position: absolute;
height: 25%;
width: 20%;
}
.wrapper .one {
top: 16px;
left: 16px;
background: blue;
}
.wrapper .two {
top: 50%;
left: 50%;
margin: -23% 0 0 -31%;
height: 60%;
width: 62%;
background: red;
}
.wrapper .three {
bottom: 16px;
right: 16px;
background: green;
}
<div class="wrapper">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
That gives us this:
Layer the main box
Now we want the red square to overlap both the blue and green squares. All we have to do is move the red <div> below them in the markup. The last element in the markup will overlap the elements before it naturally.
<div class="wrapper">
<div class="one"></div>
<div class="three"></div>
<div class="two"></div><!-- move it one line down -->
</div>
.wrapper {
position: relative;
background: #EEE;
height: 60vw;
width: 80vw;
}
.wrapper div {
position: absolute;
height: 25%;
width: 20%;
}
.wrapper .one {
top: 16px;
left: 16px;
background: blue;
}
.wrapper .two {
top: 50%;
left: 50%;
margin: -23% 0 0 -31%;
height: 60%;
width: 62%;
background: red;
}
.wrapper .three {
bottom: 16px;
right: 16px;
background: green;
}
<div class="wrapper">
<div class="one"></div>
<div class="three"></div>
<div class="two"></div><!-- move it one line down -->
</div>
Now we have the correct layers:
Add the border layers
In order to reduce complexity, we can create the box borders with :before pseudo elements. These will create the extra elements we need to create the overlapping borders.
Give each child div a :before element and background color like below:
.wrapper div:before {
content: '';
position: absolute;
top: -6px;
right: -6px;
bottom: -6px;
left: -6px;
z-index: -1;
}
.one:before {
background: blue;
}
.two:before {
background: red;
}
.three:before {
background: green;
}
The -1 z-index will ensure that they are overlapped by the div backgrounds and the -6px position on all sides pulls them outside by 6px to give us a 6px border.
The final product
We add z-index: 1 to the wrapper, so that it wont overlap our border pseudo elements. box-sizing: border-box is used so that the padding is incorporated into the widths and heights.
Example 1
Limitation of this example: We cannot use overflow to hide excessive text on the main box, as it will cut off our border or cause a scroll bar to always be present..
.wrapper {
position: relative;
background: #EEE;
height: 60vw;
width: 80vw;
max-width: 772px;
max-height: 579px;
min-width: 390px;
min-height: 292px;
z-index: 1;
}
.wrapper div {
position: absolute;
box-sizing: border-box;
background: #FFF;
height: 25%;
width: 20%;
text-align: center;
padding: 10px;
}
.wrapper div:before {
content: '';
position: absolute;
top: -6px;
right: -6px;
bottom: -6px;
left: -6px;
z-index: -1;
}
.one:before {
background: blue;
}
.two:before {
background: red;
}
.three:before {
background: green;
}
.wrapper .one {
top: 16px;
left: 16px;
}
.wrapper .two {
top: 50%;
left: 50%;
margin: -23% 0 0 -31%;
height: 60%;
width: 62%;
background: #EEE;
text-align: left;
}
.wrapper .three {
bottom: 16px;
right: 16px;
}
<div class="wrapper">
<div class="one">Box1</div>
<div class="three">Box3</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
</div>
Example 2
Slightly less elegant, the main box border is positioned relative to the wrapper itself, we can use overflow in this example to cut off or scroll excessive text.
.wrapper {
position: relative;
background: #EEE;
height: 60vw;
width: 80vw;
max-width: 772px;
max-height: 579px;
min-width: 390px;
min-height: 292px;
z-index: 1;
}
.wrapper div {
position: absolute;
box-sizing: border-box;
background: #FFF;
height: 25%;
width: 20%;
text-align: center;
padding: 10px;
}
.wrapper:after {
content: '';
position: absolute;
margin: -23% 0 0 -31%;
top: calc(50% - 6px);
left: calc(50% - 6px);
height: calc(60% + 12px);
width: calc(62% + 12px);
background: #F00;
z-index: -1;
}
.wrapper div:before {
content: '';
position: absolute;
top: -6px;
right: -6px;
bottom: -6px;
left: -6px;
z-index: -1;
}
.one:before {
background: blue;
}
.three:before {
background: green;
}
.wrapper .one {
top: 16px;
left: 16px;
}
.wrapper .two {
top: 50%;
left: 50%;
margin: -23% 0 0 -31%;
height: 60%;
width: 62%;
background: #EEE;
text-align: left;
}
.wrapper .three {
bottom: 16px;
right: 16px;
}
<div class="wrapper">
<div class="one">Box1</div>
<div class="three">Box3</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
</div>

By streamlining the markup and css a bit and only z-indexing the necessaries (namely .top-box-border and .mid-box-inner) I believe I have what you're looking for:
[class*="border"] {
width: 100px;
height: 100px;
border: 10px solid green;
position: relative;
}
[class*="inner"] {
width: 100%;
height: 100%;
background-color: #999;
position: relative;
text-align: center;
box-sizing: border-box;
padding: 1ex 1em;
}
.mid-box-border {
width: 200px;
height: 200px;
border-color: blue;
margin-top: -40px;
margin-left: 75px;
}
.mid-box-inner {
background-color: #ccc;
text-align: left;
z-index: 20;
}
.bot-box-border {
margin-top: -40px;
margin-left: 255px;
}
.top-box-border {
z-index: 10;
}
<div class="top-box-border">
<div class="top-box-inner">Box 1</div>
</div>
<div class="mid-box-border">
<div class="mid-box-inner">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Hic accusantium dicta sint a cum eveniet, id! Corrupti sit reprehenderit ad veniam ratione mollitia molestiae, sapiente quasi id esse, incidunt eligendi.</div>
</div>
<div class="bot-box-border">
<div class="bot-box-inner">Box 2</div>
</div>

Set position: initial; for .back_box

Just make position relative for each box and position absolute for the background(border) and content holder to make them out of their workflow, then first element will take higher z-index and the elements in the back will take lower, look at this one https://jsfiddle.net/s3y94x1w/
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.cont {
width: 100%;
}
.blue-box {
width: 20%;
height: 150px;
position: relative;
margin: 10px 0px 0px 8%;
cursor: pointer
}
.blue-box .background {
background-color: blue;
height: 100%;
width: 100%;
position: absolute;
}
.blue-box .content {
background-color: #fff;
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
z-index: 2
}
.red-box {
width: 40%;
height: 150px;
position: relative;
margin-left: 19%;
margin-top: -70px;
}
.red-box .background {
background-color: red;
height: 100%;
width: 100%;
position: absolute;
}
.red-box .content {
background-color: #eee;
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
z-index: 3;
overflow: hidden
}
.green-box {
width: 20%;
height: 150px;
position: relative;
margin-left: 50%;
margin-top: -70px;
cursor: pointer
}
.green-box .background {
background-color: green;
height: 100%;
width: 100%;
position: absolute;
z-index: -1
}
.green-box .content {
background-color: #fff;
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}
<div class="cont">
<div class="blue-box">
<div class="background"></div>
<div class="content">Box1</div>
</div>
<div class="red-box">
<div class="background"></div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
</div>
<div class="green-box">
<div class="background"></div>
<div class="content">Box2</div>
</div>
</div>

Related

What is causing this html5 video glitch?

I have an html5 video tag with a div that has a text caption overlayed and when you scroll in chrome, the text div disappears as it gets near the top or bottom of the screen fold. Sometimes it's as its nearing the top of the screen, sometimes the bottom. I think it depends where the video is on the screen when it's refreshed.
Anyone know what is causing this? I think this is pretty barebones markup.
codepen: https://codepen.io/sharpdesigner/pen/KRgQZZ
screen capture of the issue: https://imgur.com/a/Hn8DWkP
html
<section id="video-landing" class="hide-mobile">
<div class="container">
<div class="row">
<div class="col-md-6 video-caption">
<h1>Lorem ipsum dolor sit</h1>
<p class="big">Quisque varius erat et</p>
<p>Vivamus aliquam lectus est. Nulla vitae fringilla felis. Aenean id elit sit amet sem mattis bibendum eu a felis. Interdum et malesuada fames ac ante ipsum primis in faucibus.</p>
</div>
</div>
</div> <!-- /container -->
<video muted autoplay="autoplay" loop="loop" id="bgvid">
<source src="http://www.mysportresume.com/phone.mp4" type="video/mp4" />
</video>
</section><!-- #section -->
css
body {
height: 2000px;
margin-top: 500px;
}
.container {
width: 100%;
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
section#video-landing, section#video-landing-mobile {
background: url('../images/video-bg.jpg') no-repeat 0 -175px;
background-size: cover;
}
section#video-landing .row, section#video-landing-mobile .row {
margin: 0;
position: relative;
}
.video-caption {
position: absolute;
max-width: 550px;
top: 165px;
top: 8.5vw;
left: 0;
z-index: 99;
}
.video-caption h1, .video-caption p {
color: #000;
text-align: left;
}
.video-caption h1 {
font-size: 53px;
font-weight: 600;
}
.video-caption p.big {
margin: 0 0 30px;
max-width: 400px;
}
.video-caption p {
font-size: 17px;
}
p.big {
color: #000;
font-size: 27px !important;
font-weight: 300;
line-height: 1.5;
max-width: 850px;
margin: 0 auto;
}
video {
padding-top: 50%;
position: absolute;
top: 50%;
left: 0;
-webkit-transform: translateY(-50%);
transform: translateY(-55%);
width: 100%;
}
#video-landing, #video-landing-mobile {
width: 100%;
padding-bottom: 30%;
position: relative;
overflow: hidden;
color: white;
}
#video-landing .container, #video-landing-mobile .container {
max-width: 1140px;
}
#video-landing video, #video-landing-mobile video {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
}

Responsive Design with Foundation 6

First post using Stackflow, and super excited to be starting off on my web development career, I am using Foundation 6 to build out my first website and was hoping someone could point out some key aspects of my code that would allow it to become more responsive.. Any help would be greatly appreciated as I am new to all of this!
Thanks!
/* Left Header Block */
.header-left-block {
background-image: url('../img/abstract_header_left.jpg');
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.brand-title {
font-size: 75px;
position: relative;
color: #fff;
padding: 0px;
margin: auto;
padding-top: 260px;
font-family: poppins-Extralight;
}
/* Right Header Block */
.header-right-block {
background-color: #FCDA02;
background-repeat: no-repeat;
height: 100vh;
z-index: -2;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.header-right-content {
}
.slash {
font-size: 125px;
color: #fff;
padding: 400px 0px;
}
.sub-header {
color: #fff;
font-size: 30px;
font-family: poppins-bold;
}
.search-text {
font-family: poppins-regular;
color: #fff;
display: inline-block;
padding: 25px 10px;
}
.top-btns {
height: auto;
display: inline-block;
position: absolute;
right: 0;
top: 0;
padding: 5px 30px;
}
/* Section Two */
.section-two {
background-color:;
height: 1000px;
width: 100%;
position: relative;
box-sizing: border-box;
padding: 50px;
margin-bottom: 100px;
}
.section-two:after {
position: absolute;
width: 100%;
height: 100%;
content: '';
background: #00ACC2;
top: 0;
bottom: 0;
right: 0;
left: 0;
transform-origin:;
transform: skewY(-30deg);
margin-top: -8px;
z-index: -1;
}
.section-two-container {
padding-bottom: 400px;
align-content: flex-start;
}
.number {
font-size: 20vw;
color: #fff;
font-family: poppins-mediumitalic;
}
.item-text {
text-align: left;
font-size: 2vh;
color: #fff;
padding: 2px;
}
.sub-header-one {
color: #fff;
font-size: 5vh;
font-family: poppins-bold;
text-align: left;
}
.squiggel {
z-index: 1;
}
/* Section Three */
.section-three {
background-color:;
height: 1200px;
width: 100%;
position: relative;
box-sizing: border-box;
padding: 50px;
margin-top: 0px;
}
.section-three:after {
position: absolute;
width: 100%;
height: 100%;
content: '';
background: #83C200;
top: 0;
bottom: 0;
right: 0;
left: 0;
transform-origin: top right;
transform: skewY(30deg);
z-index: -1;
}
.section-three-content {
margin-top: -1500px;
}
.iphone {
padding-left: 100px;
}
/* Section Four */
.section-four {
background-color:#111;
height: 1200px;
width: 100%;
position: relative;
box-sizing: border-box;
padding: 50px;
margin-top: -800px;
}
/* Menu */
/* Media Queries */
#media only screen and (max-width: 630px) {
.section-two-container {
margin-top: -200px;
}
.section-three-content {
margin-top: -100px;
}
.section-four {
margin-top: -225px;
}
.sub-header-one {
font-size: 4vh;
}
.number {
}
}
/* Font */
#poppins-bold {
font-family: poppins-bold;
src: url(../fonts/Poppins-Bold.ttf);
}
#poppins-regular {
font-family: poppins-regular;
src: url(../fonts/Poppins-Regular.ttf);
}
#poppins-Extralight {
font-family: poppins-extralight;
src: url(../fonts/Poppins-ExtraLight.ttf);
}
#poppins-MediumItalic {
font-family: poppins-mediumitalic;
src: url(../fonts/Poppins-MediumItalic.ttf);
}
/* Mouse Scroll */
p {
margin-left: -55px;
}
#-webkit-keyframes ani-mouse {
0% {
opacity: 1;
top: 29%;
}
15% {
opacity: 1;
top: 50%;
}
50% {
opacity: 0;
top: 50%;
}
100% {
opacity: 0;
top: 29%;
}
}
#-moz-keyframes ani-mouse {
0% {
opacity: 1;
top: 29%;
}
15% {
opacity: 1;
top: 50%;
}
50% {
opacity: 0;
top: 50%;
}
100% {
opacity: 0;
top: 29%;
}
}
#keyframes ani-mouse {
0% {
opacity: 1;
top: 29%;
}
15% {
opacity: 1;
top: 50%;
}
50% {
opacity: 0;
top: 50%;
}
100% {
opacity: 0;
top: 29%;
}
}
.scroll-btn {
display: inline-block;
position: relative;
left: 0;
right: 0;
text-align: center;
margin-bottom: 50px;
z-index: 100;
}
.scroll-btn > * {
display: inline-block;
line-height: 18px;
font-size: 13px;
font-weight: normal;
color: #7f8c8d;
color: #ffffff;
font-family: "proxima-nova", "Helvetica Neue", Helvetica, Arial, sans-serif;
letter-spacing: 2px;
}
.scroll-btn > *:hover,
.scroll-btn > *:focus,
.scroll-btn > *.active {
color: #ffffff;
}
.scroll-btn > *:hover,
.scroll-btn > *:focus,
.scroll-btn > *:active,
.scroll-btn > *.active {
opacity: 0.8;
filter: alpha(opacity=80);
}
.scroll-btn .mouse {
position: relative;
display: block;
width: 35px;
height: 55px;
margin: 0 auto 20px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
border: 3px solid white;
border-radius: 23px;
}
.scroll-btn .mouse > * {
position: absolute;
display: block;
top: 29%;
left: 50%;
width: 8px;
height: 8px;
margin: -4px 0 0 -4px;
background: white;
border-radius: 50%;
-webkit-animation: ani-mouse 2.5s linear infinite;
-moz-animation: ani-mouse 2.5s linear infinite;
animation: ani-mouse 2.5s linear infinite;
section class="grid-x align-middle section-two">
<section class="small-12 medium-5 section-two-container">
<div class="grid-x grid-container align-middle">
<div class="small-12 hide-for-small-only squiggel"><img src="img/red-squiggel.png" style=" height: 350px;"></div>
</div>
</section>
<section class="small-12 medium-7 section-two-container">
<div class="grid-x align-top grid-container ">
<div class="small-4 medium-4 large-5 flex-child-auto"><h1 class="number"> 1 </h1></div>
<div class="small-8 medium-8 large-7 flex-child-auto">
<h2 class="sub-header-one">website design and development</h2>
<p class="item-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</div>
</div>
</section>
</section>
<!-- End Section Two -->
<section class="grid-x align-middle section-three">
<div class="section-three-content small-12 medium-7">
<div class="grid-x grid-container grid-padding-x align-center">
<div class="small-4 medium-4 large-5 "><h1 class="number"> 2 </h1></div>
<div class="small-8 medium-8 large-7 ">
<h2 class="sub-header-one">search engine optimization</h2>
<p class="item-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</div>
</div>
</div>
<div class="section-three-content grid-container small-12 medium-5 iphone"><img src="img/iphone-x.png"></div>
</section>
<section class="grid-x align-middle section-four">
</section>

Containers and divs in CSS

Good day,
I'm not new to CSS or DIVs but have been out of touch for long enough. Trying to work out with Containers and nested DIVs but not getting much success in doing what I'm trying to do. Have already searched enough on SO and Goog but, hopefully you can help me.
Here's the representation of what I wish to achieve.
Screenshot of the layout
And here is what I have done so far ..
.dealsglobal {
width: 45%;
display: inline-block;
float: left;
margin-right:20px;
height: auto;
overflow: auto;
}
.dealsglobal h2 {
margin-left: 10px;
}
.dealsindia {
width: 45%;
height: auto;
overflow: auto;
}
.dealsindia h2 {
margin-left: 10px;
}
.dealsleft {
margin: 0px 25px 10px 0px;
padding: 5px;
border: 1px solid #ccc;
height: auto;
position: relative;
width: 40%;
float: left;
display: inline-block;
overflow: auto;
}
.dealsleft img {
border: 1px solid #ddd;
background: #fff;
border-radius: 4px;
margin-left: auto;
margin-right: auto;
width: 100px;
}
.dealsright {
margin-bottom: 10px;
padding: 5px;
border: 1px solid #ccc;
height: auto;
position: relative;
width: 40%;
display: inline-block;
overflow: auto;
}
<div class="dealsglobal">
<h2>Global Coupons</h2>
<div class="dealsleft">
<img src="thumb-heavengifts.jpg" alt="HeavenGifts.com Web Store" align="middle" />
</div>
<div class="dealsright">
Test
</div>
</div>
<div class="dealsindia">
<h2>Indian Coupons</h2>
<div class="dealsleft">
Test
</div>
<div class="dealsright">
Test
</div>
</div>
The image just wont center, no matter what I do. I believe, somewhere I have set the property to align left or something. Can anyone please point me to the correct direction? Thanks for your help!
I changed your code a little bit, but you'll get idea :
Here is html :
<div class="container">
<div class="left">
<h2 class="inner-text">title</h2>
<div class="image">
<img src="http://via.placeholder.com/350x150" />
</div>
<h5 class="inner-text">description</h5>
</div>
<div class="right">
<h2 class="inner-text">title</h2>
<div class="image">
<img src="http://via.placeholder.com/350x150" />
</div>
<h5 class="inner-text">description</h5>
</div>
<div style="clear: both" />
</div>
And here is the css:
.container{
width: 98%;
border: 2px solid #f00;
padding: 10px;
}
.left {
width: 47%;
float: left;
margin: 0 1% 0 1%;
border: 1px solid green;
}
.right {
width: 47%;
float: left;
margin: 0 1% 0 1%;
border: 1px solid green;
}
.inner-text{
text-align:center;
}
.image{
padding: 20px;
}
.image img{
max-width: 100%;
}
Check here jsfiddle.
You missed adding this css property:
.dealsleft {
text-align:center;
}
You can center elements inside a div by using text-align:center; for all the text elements like: h1, p, h2, a etc. To center an img or a div inside another div you can use margin: 0 auto; the auto is the important part for centering the left and right margins.
More info here
i've updated your code snippet.
.dealsglobal {
width: 45%;
display: inline-block;
float: left;
margin-right:20px;
height: auto;
overflow: auto;
}
.dealsglobal h2 {
margin-left: 10px;
}
.dealsindia {
width: 45%;
height: auto;
overflow: auto;
}
.dealsindia h2 {
margin-left: 10px;
}
.dealsleft {
margin: 0px 25px 10px 0px;
padding: 5px;
border: 1px solid #ccc;
height: auto;
position: relative;
width: 40%;
float: left;
display: inline-block;
overflow: auto;
text-align: center;
}
.dealsleft img {
border: 1px solid #ddd;
background: #fff;
border-radius: 4px;
margin: 5px auto;
width: 100px;
}
.dealsright {
margin-bottom: 10px;
padding: 5px;
border: 1px solid #ccc;
height: auto;
position: relative;
width: 40%;
display: inline-block;
overflow: auto;
text-align: center;
}
<div class="dealsglobal">
<h2>Global Coupons</h2>
<div class="dealsleft">
<img src="thumb-heavengifts.jpg" alt="HeavenGifts.com Web Store" align="middle" />
</div>
<div class="dealsright">
Test
</div>
</div>
<div class="dealsindia">
<h2>Indian Coupons</h2>
<div class="dealsleft">
Test
</div>
<div class="dealsright">
Test
</div>
</div>
I have added background colors for visualization purposes.
* {
box-sizing: border-box;
}
#container {
width: 100%;
padding: 10px;
background-color: red;
display: flex;
justify-content: space-between;
}
.subcontainer-outer {
width: 49%;
background-color: green;
}
.subcontainer {
width: 100%;
background-color: blue;
display: flex;
justify-content: space-between;
}
.content {
width: 45%;
text-align: center;
background-color: yellow;
}
<h3>Container</h3>
<div id="container">
<div class="subcontainer-outer">
<h4>Sub container 1</h4>
<div class="subcontainer">
<div class="content">
<img src="http://placehold.it/100" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class="content">
<img src="http://placehold.it/100" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
</div>
<div class="subcontainer-outer">
<h4>Sub container 2</h4>
<div class="subcontainer">
<div class="content">
<img src="http://placehold.it/100" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class="content">
<img src="http://placehold.it/100" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
</div>
</div>
You can get it by using flex below an example as your requirement.
.dealsglobal {
width: 48%;
margin-right: 10px;
height: auto;
float: left;
}
.flex {
display: flex;
justify-content: flex-start;
align-items: center;
flex-flow: row nowrap;
border: 1px solid red;
padding: 10px;
}
.dealsglobal h2 {
margin-left: 10px;
}
.dealsindia h2 {
margin-left: 10px;
}
.dealsleft {
margin: 0px 25px 10px 0px;
padding: 5px;
border: 1px solid #ccc;
flex: 1 0 0;
align-self: flex-start;
}
.dealsleft img {
border: 1px solid #ddd;
background: #fff;
border-radius: 4px;
margin-left: auto;
margin-right: auto;
width: 100%;
}
.dealsright {
margin-bottom: 10px;
padding: 5px;
border: 1px solid #ccc;
overflow: auto;
flex: 1 0 0;
align-self: flex-start;
}
<div class="dealsglobal">
<h2>Global Coupons</h2>
<div class="flex">
<div class="dealsleft">
<img src="http://lorempixel.com/400/200/sports/" alt="HeavenGifts.com Web Store" align="middle" />
</div>
<div class="dealsright">
Test
</div>
</div>
</div>
<div class="dealsglobal">
<h2>Global Coupons</h2>
<div class="flex">
<div class="dealsleft">
<img src="http://lorempixel.com/400/200/sports/" alt="HeavenGifts.com Web Store" align="middle" />
</div>
<div class="dealsright">
Test
</div>
</div>
</div>
Here is working fiddle
fiddle
You are probably looking for any of these 2:
parentSelector {
display: block; /* can also be an inline-block, with set width */
text-align: center;
}
childSelector {
width: 60%; /* generic. Change it */
display: block;
margin: 0 auto;
}
... or:
parentSelector {
display: flex;
flex-direction: column;
align-items: center;
}
childSelector {
max-width: 60%; /* optional. Change it.
* You don't need to set width here and,
* it's OK not to limit max-width, either
*/
}

How to align a css triangle vertically to a responsive rectangle

i created a responsive rectangle and a responsive triangle with css only.
How can i align the triangle so it is always vertically centered on the right side of the box?
I´d like to do it just with css but if it is not possible i appreciate any hint for alternative options.
HTML:
<div class="container">
<div class="box green rightbox">
<div class="innerbox">Lorem ipsum....</div>
</div>
<div class="mother-triangle">
<div class="triangle-right green"></div>
</div>
</div>
CSS:
.container {
max-width: 1200px;
margin-right: auto;
margin-left: auto;
z-index: 1;
position: relative;
}
.box {
height: auto!important;
min-height: 300px;
}
.box.green {
background-color: rgba(51, 223, 9, 0.75)!important;
}
.innerbox {
padding: 12px;
}
.triangle-right {
width: 0;
height: 0;
padding-top: 10%;
padding-bottom: 10%;
padding-left: 10%;
overflow: hidden;
position: relative;
margin-top: auto;
margin-bottom: auto;
display: inline-block;
vertical-align: middle;
}
.triangle-right:after {
content: "";
display: block;
width: 0;
height: 0;
margin-top: -500px;
margin-left: -500px;
border-top: 500px solid transparent;
border-bottom: 500px solid transparent;
border-left: 500px solid rgba(51, 223, 9, 0.75);
}
.rightbox {
width: 90%;
float: left;
}
JSFiddel demo:
You can use absolute positioning to do that.
Just change position: relative to position: absolute in .triangle-right.
Add top: 50% to move the triangle to the vertical middle.
Updated fiddle
.container {
max-width: 1200px;
margin-right: auto;
margin-left: auto;
z-index: 1;
position: relative;
}
.box {
height: auto!important;
min-height: 300px;
}
.box.green {
background-color: rgba(51, 223, 9, 0.75)!important;
}
.innerbox {
padding: 12px;
}
.triangle-right {
width: 0;
height: 0;
padding-top: 10%;
padding-bottom: 10%;
padding-left: 10%;
overflow: hidden;
position: absolute;
margin-top: auto;
margin-bottom: auto;
display: inline-block;
vertical-align: middle;
top: 50%;
}
.triangle-right:after {
position: absolute;
content: "";
display: block;
width: 0;
height: 0;
margin-top: -500px;
margin-left: -500px;
border-top: 500px solid transparent;
border-bottom: 500px solid transparent;
border-left: 500px solid rgba(51, 223, 9, 0.75);
}
.rightbox {
width: 90%;
float: left;
}
<!-----container start---->
<div class="container">
<div class="box green rightbox">
<div class="innerbox">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
<div class="mother-triangle">
<div class="triangle-right green"></div>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
<!-----container end ---->

Vertically center a heading tag and a heading pseudo selector's content

Another vertical alignment question...
Given the following,
HTML:
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris. Vivamus imperdiet congue iaculis.</h2>
</div>
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris.</h2>
</div>
CSS:
.thing{
background: #eee;
margin: 0 auto 10px;
width: 625px
}
h2{
display: inline-block;
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
vertical-align: middle;
}
.thing:before{
background: red;
content: '';
display: inline-block;
height: 40px;
position: absolute;
vertical-align: middle;
width: 40px;
}
What do I need to do in order to have the text and the red box vertically centered? This should work for headings of any height.
Fiddle
You just need to remove the position:Absolute that breaks the inline-block behavior. Instead use some trick to fight the space between inline-block elements and be sure the two elements can be aside on the containers width. Try this:
.thing{
font-size:0; /*ADD*/
}
h2{
/*margin: 0 0 0 40px; REMOVE*/
width:585px; /*ADD*/
}
.thing:before{
/*position: absolute; REMOVE*/
}
Check this Demo Fiddle
I guess your question has two answers.
Here's solution one: (Make the red box equal the height of the .thing content.
.thing{
background: #eee;
margin: 0 auto 10px;
position: relative; // Make .thing:before relate it's position to this
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
}
.thing:before{
background: red;
content: '';
height: 100%; // Height will equal .thing height
position: absolute;
width: 40px;
}
Demo 1
Second solution: (Make text center vertically)
.thing{
background: #eee;
margin: 0 auto 10px;
min-height: 40px;
position: relative;
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.thing:before{
background: red;
content: '';
height: 40px;
left: 0;
position: absolute;
top: 0;
width: 40px;
}
Demo 2

Resources