CSS positioning and z-index on card - html

I'm trying to code this card design:
However, I need some help with the positioning and z-index. This is how far I've come:
.card {
width: 450px;
height: 400px;
background: lightblue;
}
.card-hover-state {
position: relative;
width: 100%;
height: 100%;
background-image: linear-gradient(-180deg, #000000 0%, rgba(216,216,216,0.00) 100%);
opacity: 0.6;
}
.card-hover-state-title {
font-size: 15px;
color: #FFFFFF;
max-width: 60%;
}
.card-hover-state-button {
font-size: 11px;
color: #FFFFFF;
}
<div class="card">
<div class="card-hover-state"></div>
<div class="information-container">
<div class="card-hover-state-title">Brace yourself - A fancy Lorem Ipsum Title is comming</div>
<div class="card-hover-state-button">READ MORE</div>
</div>
</div>
However, the text isn't showing. What am I missing?

Use position: relative on .card, then you actually don't need .card-hover-state - you can use a pseudo element instead. Whichever of those you use, you need to add position: absolute; top: 0; left: 0; to position it over the .card background.
Then I would make .information-center a flex parent and use justify-content: space-between to separate its children, and align-items: center to center them vertically, and add position: relative so it will have a position that puts it on top of the absolutely positioned pseudo element (or .card-hover-state) that comes before it.
.card {
width: 450px;
height: 400px;
background: lightblue;
position: relative;
}
.card::before {
content: '';
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(
-180deg,
#000000 0%,
rgba(216, 216, 216, 0.00) 100%
);
opacity: 0.6;
}
.information-container {
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
color: #fff;
}
.card-hover-state-title {
font-size: 15px;
max-width: 60%;
}
.card-hover-state-button {
font-size: 11px;
}
<div class="card">
<div class="information-container">
<div class="card-hover-state-title">Brace yourself - A fancy Lorem Ipsum Title is comming</div>
<div class="card-hover-state-button">READ MORE</div>
</div>
</div>

You can get it by using below approach.
I just added display:table to your .information-container and assigned width:100% to take full parent div width, after that I assigned .card-hover-state-title and .card-hover-state-button
to position:relative so they don't use the parent div's opacity:0.6 and also added display:table-cell and width to get inline both.
I added CSS properties in the following classes:
.information-container {
display: table;
width: 100%;
padding: 20px;
box-sizing: border-box;
}
.card-hover-state-title {
font-size: 15px;
color: #FFFFFF;
width: 50%;
position: relative;
display: table-cell;
}
.card-hover-state-button {
font-size: 11px;
color: #FFFFFF;
position: relative;
display: table-cell;
text-align: right;
vertical-align:middle;
}
.card {
width: 450px;
height: 400px;
background: lightblue;
position: relative;
}
.card-hover-state {
position: absolute;
width: 100%;
height: 100%;
background-image: linear-gradient(-180deg, #000000 0%, rgba(216, 216, 216, 0.00) 100%);
opacity: 0.6;
}
.information-container {
display: table;
width: 100%;
padding: 20px;
box-sizing: border-box;
}
.card-hover-state-title {
font-size: 15px;
color: #FFFFFF;
width: 50%;
position: relative;
display: table-cell;
}
.card-hover-state-button {
font-size: 11px;
color: #FFFFFF;
position: relative;
display: table-cell;
text-align: right;
vertical-align:middle;
}
<div class="card">
<div class="card-hover-state"></div>
<div class="information-container">
<div class="card-hover-state-title">Brace yourself - A fancy Lorem Ipsum Title is comming</div>
<div class="card-hover-state-button">READ MORE</div>
</div>
</div>

Related

How to make the center point of a button appear on center, not on the left of the element?

I'm having problem with making the button appear on the center of screen. The button is supposed to be in the center of the screen, but somehow my css is making in that the center of the button is at the left top corner. Any one know why?
Some more details:
I want it to be like this:
how it should look
But instead it looks like this:
how it looks
Anyone can help?
Here is the code:
html, body {margin: 0; height: 100%; overflow: hidden}
img
{
max-width: 100%;
height: auto;
position: relative;
top: 33%;
}
.obj1
{
width: 20%;
height: 100%;
background: rgb(136, 44, 44);
float:left;
position: relative;
}
.obj2
{
width: 20%;
height: 100%;
background: rgb(255, 255, 255);
position: relative;
float:left;
}
.obj3
{
width: 20%;
height: 100%;
background: rgb(0, 0, 0);
position: relative;
float:left;
}
.obj4
{
width: 20%;
height: 100%;
background: rgb(42, 75, 148);
position: relative;
float:left;
}
.obj5
{
width: 20%;
height: 100%;
background: rgb(72, 114, 48);
position: relative;
float:left;
}
.obj6
{
width: 20%;
height: 100%;
background: rgb(119, 39, 112);
position: relative;
float:left;
}
#show-more
{
background: #1594e5;
color: #fff;
font-family: calibri;
display: block;
width: 140px;
font-size: 24px;
text-transform: uppercase;
padding: 10px;
text-align: center;
margin: 20px auto;
cursor: pointer;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
<body>
<div class="obj1">
</div>
<div class="obj2">
</div>
<div class="obj3"></div>
<div class="obj4"></div>
<div class="obj5"></div>
<a id="show-more">Show More</a>
</body>
Using position absolute and top/left you point to a top-left corner of the element.
Additionally use transform: translate(-50%, -50%); to move the button to top and left of 50% of his height/width

Placing line to to the left and right of h1's

I have this fiddle and what I am trying to do is place the line directly to the left and right of the h1's and scale respectively during a resize of window. So it should look like this.
----This that this
Here we go-----
I have attached the fiddle. I have tried floating the div left and making it absolute but I'm having no luck. I have only left one div which is a red bar and removed the other one. Once I learn to do one I can do the other one. Any help is appreciated.
HTML
<h1 class="mainPageTopText">
Find this that this.
<div class="banner-bottom"></div>
</h1>
<h1 class="mainPageBottomText">
Here we go.
</h1>
</div>
</div>
CSS
.mainPageWriting {
display: flex;
}
.mainPageTopText {
width: 500px;
position: absolute;
margin-right: 400px;
margin-top: 50px;
z-index: 12;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
.banner-bottom {
height: 10px;
width: 100%;
left: 0;
position: absolute;
background-color: red;
z-index: 100;
}
.mainPageBottomText {
position: absolute;
margin-top: 100px;
padding-left: 100px;
z-index: 12;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
.centered {
background: #1D2731;
display: flex;
align-items: left;
justify-content: center;
height: 100%;
}
.banner-bottom {
height: 10px;
width: 100%;
left: 0;
position: absolute;
background-color: red;
z-index: 100;
}
Fiddle
https://jsfiddle.net/angatvir/aujrkpLk/183/
I hope this is the answer you're looking for
based on
----This that this
Here we go-----
and not
----This that this Here we go-----
#special {
}
#first {
display: grid;
grid-template-columns: 1fr auto 1fr;
grid-template-areas: "line text .";
}
#first .line {
grid-area: line;
height: 5px;
align-self: center;
background: red;
}
#first .text {
grid-area: text;
}
#second {
display: grid;
grid-area: text-below;
grid-template-columns: 1fr auto 1fr;
grid-template-areas: ". text line";
}
#second .line {
grid-area: line;
height: 5px;
align-self: center;
background: green;
}
#second .text {
grid-area: text;
}
<h1 id="special">
<span id="first">
<span class="text">This that this</span>
<span class="line"></span>
</span>
<span id="second">
<span class="text">Here we go</span>
<span class="line"></span>
</span>
</h1>
Looks quite simple, you can use psudo elements for this.
.mainPageTopText::before,
.mainPageTopText::after {
content: '----';
display:inline;
}
<h1 class="mainPageTopText">
Find this that this.
Here we go.
</h1>
Use :before and :after and in content set the line you can also style it
.mainPageWriting {
display: flex;
}
.mainPageTopText {
width: 500px;
position: absolute;
margin-right: 400px;
margin-top: 50px;
z-index: 12;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
.mainPageTopText:before{
content:'----';
}
.mainPageBottomText {
position: absolute;
margin-top: 100px;
padding-left: 50px;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
.mainPageBottomText:after{
content:'-----';
}
.centered {
background: #1D2731;
display: flex;
align-items: left;
justify-content: center;
height: 100%;
}
.banner-bottom {
height: 10px;
width: 100%;
left: 0;
position: absolute;
background-color: red;
z-index: 100;
}
<div class="centered">
<div class="mainPageWriting">
<h1 class="mainPageTopText">
Find this that this.
</h1>
<h1 class="mainPageBottomText">
Here we go.
</h1>
</div>
</div>
Update your css and html like this
HTML
<div class="centered">
<div class="mainPageWriting">
<h1 class="mainPageTopText">
<div class="left-line"></div>
Find this that this.
</h1>
<h1 class="mainPageBottomText">
Here we go.
<div class="right-line"></div>
</h1>
</div>
</div>
css
.centered {
background: #1D2731;
display: flex;
align-items: left;
justify-content: center;
height: 100%;
}
.mainPageWriting{
padding:50px 100px;
}
.mainPageTopText {
width: auto;
position: relative;
/* margin-right: 400px; */
margin-top: 50px;
z-index: 12;
text-align: left;
margin: 0px;
font-family: 'Luxim';
padding-left: 50px;
padding-right: 50px;
background: rgba(255, 255, 255, 1.0);
color: black;
}
h1 .left-line {
content: "";
position: absolute;
width: 40px;
height: 2px;
background: #000;
top: 17px;
left: 0px;
}
.mainPageBottomText {
position: relative;
/* margin-top: 100px; */
/* padding-left: 100px; */
text-align: right;
z-index: 12;
margin: 0px;
padding-left: 50px;
padding-right: 50px;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
h1 .right-line {
content: "";
position: absolute;
width: 40px;
height: 2px;
background: #000;
top: 17px;
right: 0px;
}
Try This code Below added some styles. You can make use of the pseudo elements to give the lines. removed banner-bottom div.
.mainPageWriting {
display: flex;
}
.mainPageTopText {
width: 500px;
position: absolute;
margin-right: 400px;
margin-top: 50px;
z-index: 12;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
.mainPageBottomText {
position: absolute;
margin-top: 100px;
padding-left: 100px;
z-index: 12;
font-family: 'Luxim';
background: rgba(255, 255, 255, 1.0);
color: black;
}
/*** add these */
.mainPageTopText::before{
content:"";
position:absolute;
height:0;
border:1px dashed;
width:20%;
top:0;
bottom:0;
left:-25%;
margin:auto;
}
.mainPageBottomText::after{
content:"";
position:absolute;
height:0;
border:1px dashed;
width:20%;
top:0;
bottom:0;
right:-25%;
margin:auto;
}
/** add these **/
.centered {
background: #1D2731;
display: flex;
align-items: left;
justify-content: center;
height: 100%;
}
<div class="centered">
<div class="mainPageWriting">
<h1 class="mainPageTopText">
Find this that this.
</h1>
<h1 class="mainPageBottomText">
Here we go.
</h1>
</div>
</div>

Series of divs, where on each one is stacked a black div with transparency, with a centered text

I have to display on the mobile view for a webpage a list of divs, where each of them has a specific background-image and central h1 where I display the title. Stacked on each of these divs with the background-image, there is a black div with an opacity: 0.5 to make the image darker.
This is the my code:
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
It is correctly working, but the title is kept below the black div. I have tried to modify the z-index of the h1 tag, but I had no luck so far. Do you have an idea on how to solve this issue?
This is a JSFiddle with the complete code. Thanks in advance for your replies!
When one mix elements (siblings) where some have a position other than static, they end up in a higher layer, hence, in your case, the h1 sits behind.
As mentioned, for z-index to work it need a position (other than static), though one rarely need to use z-index, instead make sure all, or none, has a position, so in your case, simply drop z-index and add position: relative
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
If the sole purpose of the hover-square is to darken the square, you could use a pseudo element instead, and save some markup and gain some flexibility
.square-container {
min-height: auto;
background-color: white;
}
.square {
position: relative;
width: 100vmin;
height: 100vmin;
color: white;
}
.square::before { /* added/changed to pseudo */
content: '';
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
For z-index to work you need to create stacking context and the easiest way to do this in this case is to just set position: relative on h1 element.
DEMO
But if you want h1 under navbar then you also need to set higher z-index on navbar so if h1 is 10 then navbar must be 11.
Just use position: relative
DEMO HERE
CSS
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}

Centering Issue inline-block div elements

I am running into an issue where my contact-section-left is not centering in the parent div. This is not a vertical-align: top issue. You can see the border-right white line, that is showing how much the height extends for the contact-section-left div is, but I am it to be the same size as the right side with the image (sorry the example doesn't have the image).
I am not sure if I am going for the wrong approach here or what, but I am wanting it to look like the paint image I made below.
Any ideas?
.total-center {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#contact-section {
width: 100%;
background: #00a16d;
}
#contact-section-wrap {
padding: 2%;
}
#contact-section-left, #contact-section-right {
height: 100%;
display: inline-block;
color: #FFF;
font-size: 1.5em;
padding: 1% 0;
position: relative;
}
#contact-section-left {
width: 60%;
border-right: 1px solid #FFF;
font-style: italic;
}
#contact-section-right {
width: 30%;
text-align: center;
}
#contact-img {
background-image: url("../icons/envelope.png");
background-repeat: no-repeat;
background-size: auto;
margin: 0 auto;
display: block;
width: 128px;
height: 128px;
position: relative;
}
#contact-width {
width: 200%;
font-size: 2em;
}
.total-width {
width: 100%;
}
<div id="contact-section">
<div id="contact-section-wrap">
<div id="contact-section-left">
<div class="total-center total-width">Tell us more about your project.</div>
</div><div id="contact-section-right">
<div id="contact-img"><span class="total-center" id="contact-width">Contact us</span></div>
</div>
</div>
</div>
Your entire code can be simplified as follows. I use a pseudo element for the vertical line in between, and shift the position with order via flexbox.
jsFiddle
#contact-section {
display: flex;
justify-content: space-around;
align-items: center;
color: #FFF;
background: #00a16d;
padding: 1em 2em;
}
#contact-section:before {
content: "";
flex: 0 0 1px;
height: 2em;
background: #fff;
order: 2;
}
#contact-section-left {
font-size: 1.5em;
order: 1;
font-style: italic;
}
#contact-section-right {
background: url("https://i.imgur.com/cLMHUZE.png") center / contain no-repeat;
font-size: 2em;
order: 3;
padding: .5em 0;
}
<div id="contact-section">
<div id="contact-section-left">Tell us more about your project.</div>
<div id="contact-section-right">Contact us</div>
</div>
Assiging display: flex; align-items: center; to the parent of the left/right sections will display them side-by-side and center them vertically. Then if you move the border-right from the left (shorter) element to a border-right of the right (taller) element, the line should look more like you want it.
.total-center {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#contact-section {
width: 100%;
background: #00a16d;
}
#contact-section-wrap {
padding: 2%;
display: flex;
align-items: center;
}
#contact-section-left, #contact-section-right {
height: 100%;
display: inline-block;
color: #FFF;
font-size: 1.5em;
padding: 1% 0;
position: relative;
}
#contact-section-left {
width: 60%;
font-style: italic;
}
#contact-section-right {
width: 30%;
text-align: center;
border-left: 1px solid #FFF;
}
#contact-img {
background-image: url("../icons/envelope.png");
background-repeat: no-repeat;
background-size: auto;
margin: 0 auto;
display: block;
width: 128px;
height: 128px;
position: relative;
}
#contact-width {
width: 200%;
font-size: 2em;
}
.total-width {
width: 100%;
}
<div id="contact-section">
<div id="contact-section-wrap">
<div id="contact-section-left">
<div class="total-center total-width">Tell us more about your project.</div>
</div><div id="contact-section-right">
<div id="contact-img"><span class="total-center" id="contact-width">Contact us</span></div>
</div>
</div>
</div>
Your #contact-section-wrap doesn't have a height. The height: 100%s you are setting aren't really doing anything. They still rely on a parent height to have any idea what they're getting 100% of.
Try setting a height on #contact-section-wrap.

Strikethrough Element Without Going Over or Under Element

I want to draw lines to the left and right of an element up to the edge of their parent element.
I'm not sure how I could describe this otherwise, but maybe a screenshot will do the trick:
As you can see, this is close to perfect, and if I put
overflow: hidden;
on the heading, then its even better, but then I can't see my nice rounded corners (red circled parts in screenshot) because it's then cut-off.
At the moment, as is, this is my HTML:
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
Where "introPage" is the gray part you see.
My CSS for this:
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
}
.test:before,
.test:after {
content: "";
position: relative;
background: #0099FF;
height: 6px;
display: inline-block;
width: 50%;
border-radius: 2px;
}
.test:before {
right: 10px;
margin-left: -50%;
}
.test:after {
left: 10px;
margin-right: -50%;
}
Anyone has a better solution to this?
Thanx in advance!
Here's a quick Fiddle
Sorry , I had to use 2 divs for the blue lines so they would cooperate with the hybrid layout: flexbox for modern browsers and display table for a fallback.
HTML
<div id="IntroPage" class="introPage flexBox">
<div class='line'></div>
<div class="test">
Heading
</div>
<div class='line'></div>
</div>
CSS
body {
background: grey;
}
.introPage {
position: relative;
width: 100%;
max-width: 100vw;
padding-top: 3em;
height: 100%;
background: gray;
display: table-row;
}
.test {
position: relative;
text-align: center;
width: 20%;
min-width: 1.5em;
display: table-cell;
font-variant: small-caps;
font-weight: 700;
font-size: 2em;
line-height: 2.5em;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 20%;
}
.line {
position: relative;
background: #0099FF;
height: .4em;
border-radius: 2px;
display: table-cell;
height: 6px;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 39%;
}
.flexBox {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: center;
align-content: center;
align-items: center;
}
<style>
h2 { width:100%; text-align:center; border-bottom: 1px solid #000; line-height:0.1em; margin:10px 0 20px; }
h2 span { background:#fff; padding:0 10px; }
</style>
<h2><span>THIS IS A TEST</span></h2>
http://codepen.io/chriscoyier/pen/zDGkw
The quick and dirty way would be to set the width of the test before and after elements to a smaller width (Say maybe 40% instead of 50%).
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
}
.test:before,
.test:after {
content: "";
position: relative;
background: #0099FF;
height: 6px;
display: inline-block;
width: 40%;
border-radius: 2px;
}
.test:before {
right: 10px;
}
.test:after {
left: 10px;
}
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
The best case solution would be to re-size the test before and after elements based on the width of the "test" class. I'm not so sure this is possible in css alone and you will likely have to use javascript to resize the width of those elements based on the size of the test element.
The basic outline of this process would be to calculate the width of the text, convert it from pixels to a percentage, then subtract that percentage from 100%, and divide by 2.
I may give this a shot later depending on how much time I have, if anyone wants to pick it up from here feel free to edit the post (community wiki style).
I think I have an answer...works with any page width.
http://codepen.io/anon/pen/ZGxNgB
<div id="IntroPage" class="introPage">
<div class="test">Heading</div>
</div>
.introPage {
position: relative;
max-width: 1000px;
margin: auto;
padding-top: 50px;
height: 100%;
background: gray;
}
.test {
position: relative;
/* overflow: hidden; */
text-align: center;
width:100%;
display:block;
height:30px;
}
.test:before,
.test:after {
content: "";
position: absolute;
background: #0099FF;
height: 6px;
display: inline-block;
width: 40px;
border-radius: 2px;
top:12px;
}
.test:before {
float:right;
right:-40px;
pos
}
.test:after {
float:left;
left:-40px;
}