Horizontally and vertically center span and image - html

I'm stuck with this problem that takes me forever, I am making a portfolio website and I want to display the projects that I have done. I'm messing around with positions and centering but I can't seem to get it right.
What I want is to have the image and text vertically and horizontally centered, with the text overlapping the image.
I have made a JSFidlle here: https://jsfiddle.net/vw7ftzy8/1/
What I eventually want is this: https://jsfiddle.net/vw7ftzy8/4/ (but responsive)
.projectTest {
color: white;
padding: 10px 40px;
background-color: rgba(0, 0, 0, 0.4);
font-weight: 600;
font-style: italic;
font-size: 56px;
position: absolute;
z-index: 999;
}
.projectsImage1 {
height: 200px;
width: auto;
position: absolute;
}
.projectsImage1Ghost {
visibility: hidden;
position: relative;
}
.text-center {
position: relative;
text-align: center;
background-color:yellow;
}
<div class="text-center">
<span class="projectTest">A test project</span>
<img class="projectsImage1" src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c545.png">
<div class="projectsImage1 projectsImage1Ghost"></div>
</div>

Assuming you do not need to support old versions of IE, I would use a flexbox for class .text-center
Browser support for flexbox here
More information on flexbox here
.projectTest {
color: white;
padding: 10px 40px;
background-color: rgba(0, 0, 0, 0.4);
font-weight: 600;
font-style: italic;
font-size: 56px;
position: absolute;
z-index: 999;
}
.projectsImage1 {
height: 200px;
width: auto;
position: absolute;
}
.projectsImage1Ghost {
visibility: hidden;
position: relative;
}
.text-center {
position: relative;
background-color: yellow;
display: flex;
align-items: center;
justify-content: center;
}
<div class="text-center">
<span class="projectTest">A test project</span>
<img class="projectsImage1" src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c545.png">
<div class="projectsImage1 projectsImage1Ghost"></div>
</div>

If the height of all the images is known (I'm seeing that you have used 200px as a height), you can use CSS transform using the translate function (IE9+). This property when is applied in percents, take into account the size of the element instead of the size of its parent:
.text-center {
background-color: yellow;
height: 200px;
position: relative;
text-align: center;
width: 100%;
}
.projectTest {
background-color: rgba(0, 0, 0, 0.4);
color: white;
font-weight: 600;
font-style: italic;
font-size: 56px;
left: 50%;
padding: 10px 40px;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
white-space: nowrap;
z-index: 999;
}
.projectsImage1 {
height: 100%;
left: 50%;
position: absolute;
transform: translateX(-50%);
width: auto;
}
<div class="text-center">
<span class="projectTest">A test project</span>
<img class="projectsImage1" src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c545.png">
</div>

Related

Sass style is not applying to content

my SASS style is not being applied when I nest them. I don't understand why, they work perfectly when not nested. I've never encountered this issue before and it seems strange that it would not apply the styling this one time. It also works when I remove the :nth-of-type(1) portion. I find this a bit odd, since I specifically need to divide it with for this one "shop-sec" and others. Thank you.
.shop-sec:nth-of-type(1) {
height: 500px auto;
background-color: white;
margin-top: 75px;
display: grid;
position: relative;
grid-template-columns: .75fr 1.25fr 1.25fr .75fr;
grid-gap: 15px;
.shop-sec-content {
width: 100%;
height: 100%;
}
.shop-sec-content img {
width: 100%;
height: auto;
}
.shop-sec-content:nth-of-type(1) {
position: relative;
}
.shop-sec-content:nth-of-type(1) .shop-sec-text {
position: absolute;
right: 0;
padding-right: 50px;
transform: rotate(-90deg);
top: 42.5%;
font-size: 20px;
font-weight: bold;
}
.shop-sec-content:nth-of-type(4) {
position: relative;
}
.shop-sec-content:nth-of-type(4) .shop-sec-text {
position: absolute;
left: 0;
padding-left: 50px;
top: 42.5%;
font-size: 20px;
font-weight: bold;
}
.shop-sec-content:nth-of-type(2) {
position: relative;
}
.shop-sec-content:nth-of-type(2) .shop-sec-content-text {
position: absolute;
top: 50%;
margin-left: 50px;
font-size: 16px;
padding: 5px 20px;
color: white;
background-color: rgba(0, 0, 0, 0.507);
text-transform: uppercase;
letter-spacing: 5px;
}
.shop-sec-content:nth-of-type(2) .shop-sec-content-text span {
font-size: 24px;
font-weight: bold;
letter-spacing: 0px;
}
}
<div class="shop-sec">
<div class="shop-sec-content">
<div class="shop-sec-text">HELLO</div>
</div>
<div class="shop-sec-content">
<div class="shop-sec-content-text">NEW!<br><span>HELLO</span></div>
<img src="">
</div>
<div class="shop-sec-content">
<div class="shop-sec-content-text"></div>
<img src="">
</div>
<div class="shop-sec-content">
<div class="shop-sec-text">01</div>
</div>
</div>

CSS clip-path replacement for IE

Does anyone know of CSS property that I can use to achieve the same result as clip-path to make rounded shapes around a div? My site needs to be compatible with the latest version of IE, but I checked on www.caniuse.com and clip-path is not supported on IE 11.
This is what I am trying to do:
My current code works as you can see in this codepen: https://codepen.io/CodingGilbert/pen/BqwoGm?editors=1100
The problem is, that this code won’t work in IE, how can I solve this? Surely there must be another CSS property which does the same.
.card {
width: 80%;
height: 16.5rem;
border-radius: 5px;
background-color: #fff;
text-align: center;
padding: 0 1.5rem;
margin:10rem auto;
}
.card__inner-wrapper {
height: 55%;
position: relative;
margin: 0 auto;
display: flex;
justify-content: center; }
.card__img {
height: 100%;
position: absolute;
bottom: 50%;
clip-path: circle(50% at 50% 50%);
background-color: #fff;
border: 0.8rem solid #fff; }
.card__text-content {
position: absolute;
top: 6rem; }
.card__heading {
font-size: 1.8rem;
font-weight: 500;
color: #5fc0c3;
margin-bottom: 1.5rem; }
In this case, border-radius: 50%; on the .card__img will give you the same result, and it's compatible with IE9 and above.
Demo:
body {
background-color: gray;
}
.card {
width: 80%;
height: 16.5rem;
border-radius: 5px;
background-color: #fff;
text-align: center;
padding: 0 1.5rem;
margin: 10rem auto;
}
.card__inner-wrapper {
height: 55%;
position: relative;
margin: 0 auto;
display: flex;
justify-content: center;
}
.card__img {
height: 100%;
position: absolute;
bottom: 50%;
border-radius: 50%; /* instead of clip-path */
background-color: #fff;
border: 0.8rem solid #fff;
}
.card__text-content {
position: absolute;
top: 6rem;
}
.card__heading {
font-size: 1.8rem;
font-weight: 500;
color: #5fc0c3;
margin-bottom: 1.5rem;
}
<div class="card">
<div class="card__inner-wrapper">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Light_bulb_icon_red.svg/2000px-Light_bulb_icon_red.svg.png" alt="Bulb icon" class="card__img">
<div class="card__text-content">
<h4 class="card__heading">We help charities</h4>
<p>Share knowledge and working practice to make the best technology choices.</p>
</div>
</div>
</div>
you could use inline SVG to clip an image as it has great browser support - http://caniuse.com/#search=inline%20svg

How to organize my HTML and CSS better?

I am in the learning of coding so I'm very new to this. But I'm pretty sure I could set up my code a much better way than i have done, could anyone tell me what I can do better? Reason for asking is to be better on it.
HTML Code:
<div class="infobox">
<h3>Pinstripe Suit Jacket</h3>
<div class="suggested">
<div class="sug-text">
<h4 class="opskinsug">OPSkins Suggested:</h4>
<h4 class="survivorsug">Survivor's Suggested:</h4>
</div>
<div class="sug-price">
<h4 class="opssug">$0.26</h4>
<h4 class="survsug">$0.27</h4>
</div>
</div>
<div class="cheap" id="pinstripe-jacket">
<p> 44</p>
</div>
<img src="images/pinstripesuitjacket.png" width="300" >
</div>
CSS Code:
.infobox {
background-color: rgba(0, 0, 0, 0.3);
position: relative;
text-align: center;
border-style: solid;
border-color: orange;
border-width: 1px;
border-radius: 25px;
width: 250px;
height: 300px;
display: inline-block;
margin-left: 20px;
}
.infobox a:link {
color: #fff;
text-decoration: none;
}
.infobox a:visited {
color: #fff;
}
.infobox a:hover {
color: black;
}
.infobox h3 {
background-color: orange;
}
.infobox img {
position: relative;
top: -232px;
left: 15px;
z-index: 0;
}
.opskinsug {
font-size: 13px;
text-align: center;
width: 100px;
position: relative;
left: -20px;
z-index: 1;
display: inline-block;
}
.survivorsug {
position: relative;
text-align: center;
left: 20px;
font-size: 13px;
width: 100px;
z-index: 1;
display: inline-block;
}
.opssug {
position: relative;
text-align: center;
width: 100px;
left: -20px;
font-size: 20px;
color: #fff;
top: -40px;
z-index: 1;
display: inline-block;
}
.survsug {
text-align: center;
position: relative;
width: 100px;
font-size: 20px;
left: 20px;
top: -40px;
color: #fff;
z-index: 1;
display: inline-block;
}
.suggested {
background-color: rgba(255, 165, 0, 0.4);
height: 70px;
position: relative;
z-index: 1;
}
.sug-text {
position: relative;
top: -5px;
}
.sug-price {
position: relative;
top: -8px;
}
.cheap {
position: relative;
z-index: 1;
font-size: 52px;
display: inline-block;
left: -70px;
color: #fff;
}
on my screen its looking like this:
preview
Here's an updated HTML. I've replaced the inner <div>s with a definition list <dl>, which isn't entirely meant for this use, but since you're having 1 on 1 relationship with heading and price, it can be used for this. I also removed the <p> inside the last <div>, since that is not a paragraph in any way, and you can position it inside the <div> (no need for a wrapper, really). I didn't do the CSS, but it should be fairly simple to reformat it to fit the new HTML.
<div class="infobox">
<h3>Pinstripe Suit Jacket</h3>
<div class="suggested">
<dl>
<dt>OPSkins Suggested:</dt>
<dd>$0.26</dd>
</dl>
<dl>
<dt>Survivor's Suggested:</dt>
<dd>$0.27</dd>
</dl>
</div>
<div class="cheap" id="pinstripe-jacket">
44
</div>
<img src="images/pinstripesuitjacket.png" width="300">
</div>

Stacking Elements/Classes with CSS

I'm trying to create the image in the link with only html and css. There are a number of elements that would need to "stack" on top of one another.
I am having a difficult time understanding inheritance, nesting, etc. Here's the code I've written so far:
.heart {
position: relative;
margin-top: 20px;
background-color: #000000;
opacity: .8;
width: 65px;
height: 30px;
border-radius: 15px;
display: inline;
}
.box {
margin: 75px auto;
position: relative;
height: 490px;
width: 700px;
background-color: #18a0ff;
box-shadow: 1px 15px 50px 2px;
display: flex;
}
.thumbnail_image {
position: absolute;
float: left;
display: inline-block;
top: 0px;
left: 0px;
}
.text_container {
top: 60px;
left: 200px;
right: 100px;
width: 400px;
height: 338px;
position: relative;
display: flex;
}
h1 {
font-color: #ffffff !important;
text-transform: uppercase;
font-size: 60px;
font-family: Montserrat;
font-weight: 700;
line-height: 1.1;
text-align: left;
}
<div class="box">
<div class="heart">
</div>
<div class="thumbnail_image">
<img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457298445/Sheldon_Pic_l3cprk.jpg">
</div>
<div class="text_container">
<h1>Don't You think that if I were wrong, I'd know it?</h1>
</div>
</div>
My concern is how to properly place the heart dialog, the text container, and the image overlay. I seem to be misunderstanding proper inheritance syntax or structure.
Use position:absolute; on heart dialog, text container, and image overlay elements and then position them correctly with the left and right properties.
Absolute positioning and z-index are the key words involved in stacking images with HTML and CSS.
I went ahead and mocked up your image with some html/css to give you an idea of implementation.
Z-index is not relevant in this particular example since you only require one layer above the base, which is automatically given to you with absolute positioning, however if you had multiple layers you would need to set the z-index to a number value where lower numbered z-indexes appear at the bottom and higher z-indexes appear at the top.
Here's my code, hope it helps:
body {
background-color: grey;
}
.container {
position:fixed;
height: 500px;
width: 700px;
background-image: url(http://i.stack.imgur.com/MS8X8.png);
background-position: 46% 52%;
background-size: 150%
}
.hearts {
position: absolute;
background-color: rgba(149, 165, 166,.5);
color: white;
right: 40px;
top: 15px;
padding: 15px 25px 15px 25px;
border-radius: 15px
}
.blue {
width: 550px;
height: 500px;
background-color: rgb(102,173,255);
float: right;
}
h1, h5 {
position: absolute;
font-family: sans-serif;
color: white;
text-transform: uppercase;
}
#quote {
left: 200px;
top: 30px;
font-size: 60px;
}
#attr {
left: 200px;
top: 450px;
}
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "main.css">
</head>
<body>
<div class = "container">
<div class = "hearts">423</div>
<div class = "blue">
<h1 id = "quote">don't you <br> think that <br> if i were </br>wrong,<br> i'd know it?</h1>
<h5 id = "attr">-Sheldon Cooper</h5>
</div>
</div>
</body>
</html>
Understanding the stacking order
In your case, the natural stacking order will do the job; this is nicely explained over on the MDN. The main thing to understand is that elements will overlap those that come before them in the markup. This is better explained with a simple example:
div {
position: absolute;
background: red;
height: 100px;
width: 100px;
top: 0;
left: 0;
}
.two {
background: blue;
top: 10px;
left: 20px;
}
.three {
background: green;
top: 20px;
left: 40px;
}
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
With that out of the way...
Let's make these!
Feel free to jump to the complete example at the end of this answer!
Want to use some pedantic semantics?
A <blockquote> element to wrap everything together in a semantic container.
A <nav> element to contain the back and forward navigation
A <cite> element that contains the name of the person quoted
Our markup now looks like this:
<blockquote>
<p>Don't You think that if I were wrong, I'd know it?</p>
<cite>Sheldon Cooper</cite>
<a href="#" class="love-counter">
<3 123
</a>
<nav>
Previous
Next
</nav>
</blockquote>
The CSS
Main background image and color
These can be placed as a background on the blockquote itself. You can use background-size to ensure that the image always has the same dimensions. (It will obviously distort images which have an incorrect size)
blockquote {
background: #18a0ff url(image-url) no-repeat;
background-size: 170px 490px;
}
Add the transparent grey background and quotation character
This can be added with a absolutely positioned before pseudo-element of blockquote. The element is stretched out with left / right / bottom along with a width that matches the image. The transparent grey overlay and transparent text is provided by rgba color.
blockquote:before {
content: '\201C';
position: absolute;
left: 0;
top: 0;
bottom: 0;
padding-top: 30px;
font-size: 2.4em;
text-align: center;
background: rgba(0,0,0,0.7);
width: 170px;
color: rgba(255,255,255,0.3);
}
Align the main quote text along with its citation
In order to incorporate smaller quotes, it could be more visually pleasing to vertically center the main text. We can use the display: flex property along with justify-content to easily achieve this; the flex-direction: column property stacks the main quote over the top of the citation. The blockquote is also given left and right padding to appropriately position it horizontally.
blockquote {
display: flex;
justify-content: center;
flex-direction: column;
padding: 0 140px 0 200px;
}
Position the back / forward navigation and love counter
These are easily located with position: absolute along with the appropriate left / right / bottom / top properties. They will look something like this:
.love-counter {
position: absolute;
right: 20px;
top: 20px;
}
nav {
position: absolute;
left: 0px;
bottom: 20px;
}
Complete example
Compatibility: IE 11+ and all modern browsers.
You might consider a javascript method to shrink the font size for larger quotes.
#import url(https://fonts.googleapis.com/css?family=Passion+One:400,700);
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
blockquote {
background: #18a0ff url(http://i.stack.imgur.com/e3nDc.jpg) no-repeat;
background-size: 170px 490px;
height: 490px;
color: #FFF;
font-family: 'Passion One', cursive;
font-size: 4.2em;
position: relative;
display: flex;
justify-content: center;
flex-direction: column;
padding: 0 140px 0 200px;
font-weight: 400;
line-height: 1;
width: 650px;
text-transform: uppercase;
}
blockquote p {
margin: 0;
margin-top: 0.75em;
}
cite {
font-size: 0.25em;
font-weight: 400;
margin-top: 2em;
}
cite:before {
content: '\2014 '
}
blockquote:before {
content: '\201C';
font-size: 2.4em;
padding-top: 30px;
text-align: center;
background: rgba(0, 0, 0, 0.7);
width: 170px;
color: rgba(255, 255, 255, 0.3);
position: absolute;
left: 0;
top: 0;
bottom: 0;
}
.love-counter {
color: #FFF;
text-decoration: none;
font-size: 0.2em;
position: absolute;
right: 20px;
top: 20px;
font-family: helvetica;
font-weight: bold;
background: rgba(0, 0, 0, 0.2);
padding: 0 10px;
border-radius: 10px;
height: 30px;
line-height: 30px;
min-width: 60px
}
nav {
position: absolute;
left: 0px;
bottom: 20px;
font-size: 0;
width: 170px;
text-align: center;
}
nav a:before,
nav a:after {
font-size: 36px;
width: 50%;
display: inline-block;
color: #FFF;
}
nav a:first-child:before {
content: '<';
}
nav a:last-child:after {
content: '>';
}
.x-large {
background-image: url(http://i.stack.imgur.com/qWm5m.jpg);
}
.x-large p {
font-size: 0.62em;
}
<blockquote>
<p>Don't You think that if I were wrong, I'd know it?</p>
<cite>Sheldon Cooper</cite>
<3 123
<nav>
Previous
Next
</nav>
</blockquote>
<h2>Larger quote</h2>
<blockquote class="x-large">
<p>Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.</p>
<cite>Albert Einstein</cite>
<3 123
<nav>
Previous
Next
</nav>
</blockquote>
html,
body,
box,
thumbnail_image,
overlay,
h1,
h3,
h6,
p,
body {
width: 100%;
padding-bottom: 25px;
}
input {
font-family: "Roboto";
position: absolute;
top;
25.5px;
font-weight: 700;
font-size: 14px;
color: #fff;
background-color: transparent;
text-align: right;
border-width: 0;
width: 100%;
margin: 0 0 .1em 0;
}
.heart_button {
position: absolute;
top: 25.5px;
right: 55px;
}
heart_button:hover,
heart_button:active,
heart_button:focus {
color: #dd0239;
}
.heart_background {
position: absolute;
top: 20px;
right: 20px;
background-color: #000000;
opacity: .1;
width: 65px;
height: 30px;
border-radius: 15px;
}
.box {
margin: 30px auto;
position: relative;
height: 490px;
width: 700px;
background-color: #18a0ff;
box-shadow: 1px 15px 50px 2px;
}
.quote_image {
position: absolute;
opacity: .1;
top: 62px;
left: 51px;
}
.image_overlay {
background-color: #282a37;
width: 170px;
height: 490px;
position: absolute;
float: left;
}
.thumbnail_image {
position: absolute;
float: left;
opacity: .12;
display: inline-block;
top: 0px;
left: 0px;
}
.text_container {
left: 200px;
width: 400px;
height: 338px;
position: absolute;
}
h1 {
color: #fff;
text-transform: uppercase;
font-size: 60px;
font-family: Montserrat;
font-weight: 700;
line-height: 1.1;
text-align: left;
}
.author_name {
position: absolute;
left: 206px;
bottom: 0px;
}
h3 {
font-family: Open Sans;
font-weight: 700;
letter-spacing: 1px;
text-transform: uppercase;
font-size: 14px;
text-align: left;
color: #fff;
}
p {
font-family: "Roboto";
font-weight: 700;
font-size: 14px;
color: #fff;
text-align: center;
}
h6 {
font-family: Open Sans;
font-weight: light;
font-size: 22px;
letter-spacing: 2px;
text-transform: uppercase;
text-align: center;
}
html {
background: linear-gradient(209deg, #E5ECEF 40%, #BBC2C5 100%) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#footer {
clear: both;
}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,800' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="box">
<div class="heart_button">
<img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457311522/little_heart_jle1j3.png">
</div>
<div class="heart_background">
</div>
<div class="image_overlay">
</div>
<div class="thumbnail_image">
<img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457298445/Sheldon_Pic_l3cprk.jpg">
</div>
<div class="text_container">
<h1>Don't You think that if I were wrong, I'd know it?</h1>
</div>
<div class="author_name">
<h3> - Sheldon Cooper </h3>
</div>
<div class="quote_image">
<img src="http://res.cloudinary.com/dp32vpqfu/image/upload/v1457314397/quotations_image_wfwimc.png">
</div>
</div>
</body>
<footer>
<div>
<h6>A Project by Charles Bateman</h6>
</div>
</footer>

How to vertically center div in fluid container?

I would like to be able to vertically center a div containing meta data such as Title and Author within a container that has a fluid width. In the example below, I would like the .meta div to be vertically centered within the article which is fluid width.
I tried following this article (http://css-tricks.com/centering-in-the-unknown/) but it doesn't work.
HTML
<div class="container">
<h3>Test</h3>
<article>
<div class="meta">
<div class="title"></div>
<div class="author"></div>
<img src="" />
</div>
</article>
</div>
CSS (using LESS)
.container {
h3 {
margin: -5px 0 0 0;
padding: 32px 0px 16px 0;
.freight-sans-pro;
font-size: 1.375em;
line-height: 1em;
font-weight: 200;
}
article {
max-height: 375px;
overflow: hidden;
position: relative;
z-index: 900;
margin-bottom: 2px;
background-color: #color-black;
line-height: 0em;
a {
max-height: 375px;
display: block;
img { opacity: .5; .opacity-transition; }
}
.meta {
width: 100%;
position: absolute;
bottom: 40%;
z-index: 500;
padding: 0px 10%;
color: #color-white;
font-size: 20px;
text-align: center;
.title {
margin: 0;
font-size: 2em;
line-height: 1em;
font-weight: 700;
text-shadow: 0px 2px 20px rgba(0, 0, 0, 0.7);
padding-bottom: 8px;
text-decoration: none;
display: block;
}
.author {
margin: 0;
font-size: .8em;
line-height: .75em;
font-style: italic;
text-transform: uppercase;
text-shadow: 0px 2px 20px rgba(0, 0, 0, 0.7);
text-decoration: none;
display: block;
}
}
}
}
Tried your Less but got an error in codepen.io so could not debug.
You could try something simple like this:
.container {
border: 2px solid blue;
width: 180px;
height: 120px;
overflow: hidden;
display: table-cell;
vertical-align: middle;
}
Check it working here
You might find this article useful as well: http://logconsole.com/vertical-align-center-image/
It's about vertically centring image, but in most cases you could just change image with a div.
Try this on what you want to be centered. I altered #domkoscielak's code so that it work.
.meta {
width: 180px;
height: 120px;
top: 50%;
margin-top: -60px; /*half of height*/
position: absolute;
}