Position child div to the left of parent div? - html

I have an image that has a width of 50% and a div with text that is 65% because I want the heading inside the div to overlap the image. But, the problem is that I don't want the other content inside the div to overlap the image - just the heading. I want the text and the button to be next to the image not on top of it.
But I can't seem to figure it out.
Here's a Codepen: https://codepen.io/tayanderson/pen/EbJZxy
.project {
height: 75vh;
margin: 15em 0;
position: relative;
.project-info {
z-index: 100;
display: flex;
flex-direction: column;
justify-content: center;
position: absolute;
width: 65%;
height: 100%;
right: 0;
h2 {
text-transform: initial;
color: #eee;
}
p {
font-weight: 300;
color: #ccc;
margin: 40px 0;
}
.project-desc {
width: 70%;
.button {display:inline-block;}
}
}
.project-img {
background-size: cover;
background-position: center;
height: 100%;
-webkit-filter: grayscale(100%) brightness(50%);
filter: grayscale(100%) brightness(50%);
width: 51.3%;
}
a {text-decoration:none;
color: #fff;}
#media only screen and (max-width: 768px) {
.project-info, .project-img {
width: 100%;
}
.project-info {
left: 0;
padding: 0 20px;
.project-desc>*{
width: 100% !important;
float: left !important;
}
p {display: none;}
}
}
}

You can try something like that using flexbox.
https://codepen.io/dakata911/pen/BmEpeB
.article {
width: 60%;
margin: 0 auto;
display: flex;
}
.article__image,
.article__text {
width: 50%;
}
.article__image {
background: url('http://www.country1067.com/wp-content/uploads/sites/28/2017/07/logoImage_4.jpg') center center no-repeat;
background-size: cover
}
.article__text {
padding: 12px;
background: #c3c3c3;
}
.article__text h2 {
margin-left: -96px;
color: #FFF;
font-size: 40px;
}

why donĀ“t you make the two divs to 50% and give padding to project-info?
You need this attribute for do correctly the padding inside the div
*{
box-sizing:border-box;
}
Here you have the result
https://codepen.io/marcosefrem/pen/BmERmY

I think I understand what you are describing. Instead of using the percentages to position the items use position: relative instead and float the image (remember to put it first in the flow).
Check this out:
https://codepen.io/alexplummer/pen/eeoRmN
.project-info {
display: inline-block;
width: calc(50% - 10px);
height: 100%;
padding-left: 10px;
...
}
.project-img {
width: 50%;
float: left;
...
}

Related

Split Website into 3 responsive triangles

Hey I am searching for a solution to split my front page of my website into 3 responsive triangles. All theses triangles should have an image inside and some text but it doesn't work that well...
All ready tried to work with 'border' or 'transform'. Also used 'vw' and 'vh' instead of percentage.
CSS for the first "main" triangle:
.triangle {
width: 100vw;
height: 100vh;
overflow: hidden;
}
.triangle:before {
background-size: cover;
content:"";
display:block;
width: 100%;
height: 100%;
background-image: url("http://lorempixel.com/output/city-q-g-1920-1080-10.jpg");
transform: rotate(-45deg);
transform-origin:0 0;
}
The triangle should get the whole width of the display resolution and height too.
* {
box-sizing: border-box;
}
div {
display: inline-block;
vertical-align: top;
border: .5vw solid black;
text-align: center;
padding: .2vw;
width: 33%;
color: white;
margin: 0;
}
#left {
background-color: green;
}
#middle {
background-color: orange;
}
#right {
position: absolute;
right: .2vw;
background-color: blue;
}
Please see this Fiddle

Trying to get the images to fit in box and Boxes to be responsive

I am trying to get this code to work I am about 90% of the way there, but am having difficulty scaling and making flexbox responsive.
What I want to know is:
1) When I change the width to auto on line 20 (also 44, and 68) why does the image disappear? I want image to fit in box and the text overlay to be over image.
2) How do I get the image box to be responsive? When in Mobile I want them stacked and then to flex as they get to desktop so they are in row. So column to row. Why is that not working?
Any help to get images to fit in box and the text overlay to cover image and for it to flex and be responsive would be helpful. I have been stuck on this for quite a while and not sure why this is not working.
Code Pen link
HTML
<div class="opener-wrapper">
<div id="opener1" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lorem Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
<div id="opener2" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lore Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
<div id="opener3" class="opener flex-center">
<div class="opener-msg">
<h1 class="opener-title">Lorem Ipsum Title</h1>
<p class="opener-subtitle">Lorem Ipsum Text that doesnt matter</p>
</div>
</div>
</div>
CSS
.opener-wrapper {
display: flex;
flex-wrap: wrap;
}
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener1::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener2::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener2::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener3::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 100%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener3::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
.flex-center {
/* display: flex;
flex-direction: column;
justify-content: center;
align-content: center; */
}
.opener-msg {
color: #fff;
text-shadow: #343a40 2px 2px;
min-width: 100%;
min-height: 12em;
position: relative;
margin: 3% 0;
text-transform: uppercase;
}
.opener-msg::before {
content: "";
display: block;
position: absolute;
margin-left: 0;
min-width: 100%;
min-height: 12em;
z-index: -1;
opacity: 0.4;
background-color: #343a40;
}
.opener-title,
.opener-subtitle {
width: 100%;
display: block;
text-align: center;
}
.opener-title {
margin: 3% 0;
}
1) When I change the width to auto on line 20 (also 44, and 68) why does the image disappear? I want image to fit in box and the text overlay to be over image.
This box is set to position: absolute. Absolutely positioned elements are sized relative to their parent container (or next relatively positioned parent). They require a width and height.
What if you set the background-image on #opener1, and set the color overlay on #opener1::after?
2) How do I get the image box to be responsive? When in Mobile I want them stacked and then to flex as they get to desktop so they are in row. So column to row. Why is that not working?
You need to set the flex-direction to row on the flex container. There is currently no flex-direction set on .opener-wrapper. This will allow the boxes to flow horizontally. Also, you have min-width set to 100%. This will ensure that your boxes take up 100% of the width -- which is not what you want. Remove this line, and add max-width: 33.33%;.
You may want to take a look at Chris Coyier's A Complete Guide to Flexbox if you haven't yet!
1) width: auto; sets the width of the element to the browser default width. In this case, the browser initializes the :before pseudo-element with a width of 0px which can be seen in the computed styles in this imgur image
2) You will need #media rules to resize the elements depending on how the page is being viewed. Currently all of your openers have a width and min-width of 100%. This makes them use 100% of the space that they occupy. If you want elements to display on the same row, you have to divide that 100% by how many elements you want to display on each row. For the case of these three elements, I did the following.
.opener {
width: 33%;
height: 100%;
min-width: 33%;
min-height: 100%;
}
To follow this up, I added a #media section to the CSS to change the width of the elements to 100% when the display is smaller than 600px.
#media only screen and (max-width: 600px) {
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
width: 100%;
min-width: 100%;
}
#opener2::before {
width: 100%;
min-width: 100%;
}
#opener3::before {
width: 100%;
min-width: 100%;
}
}
All together, your CSS should be similar to this:
.opener-wrapper {
display: flex;
flex-wrap: wrap;
}
.opener {
width: 33%;
height: 100%;
min-width: 33%;
min-height: 100%;
}
#opener1::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener1::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener2::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener2::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
#opener3::before {
background-image: url('https://source.unsplash.com/random');
background-size: cover;
background-position: center;
content: "";
display: block;
position: absolute;
width: 33%;
height: 12em;
z-index: -2;
opacity: 0.7;
}
#opener3::after {
background-color: #314F59;
content: "";
display: block;
position: absolute;
width: auto;
height: 100%;
z-index: -1;
opacity: .35;
}
.flex-center {
/* display: flex;
flex-direction: column;
justify-content: center;
align-content: center; */
}
.opener-msg {
color: #fff;
text-shadow: #343a40 2px 2px;
min-width: 100%;
min-height: 12em;
position: relative;
margin: 3% 0;
text-transform: uppercase;
}
.opener-msg::before {
content: "";
display: block;
position: absolute;
margin-left: 0;
min-width: 100%;
min-height: 12em;
z-index: -1;
opacity: 0.4;
background-color: #343a40;
}
.opener-title,
.opener-subtitle {
width: 100%;
display: block;
text-align: center;
}
.opener-title {
margin: 3% 0;
}
#media only screen and (max-width: 600px) {
.opener {
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
}
#opener1::before {
width: 100%;
min-width: 100%;
}
#opener2::before {
width: 100%;
min-width: 100%;
}
#opener3::before {
width: 100%;
min-width: 100%;
}
}

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;
}

Trouble adding div with 100% height, and keeping existing content vertically aligned

The below code works as intended (full width background image, with horizontally and vertically aligned text overlapping it), but I want to make a couple of changes. Instead of having a background image for #banner, I want to switch it to a color, and add a div before .teaser that is width: 1366px, height: 100% of parent, and includes a background image set to cover. I still need .teaser vertically aligned though. I've been struggling with this for awhile, so any help would be very much appreciated. http://jsfiddle.net/exczzyje/
HTML
<div id="banner">
<div id="valign">
<div class="teaser">
<h1>Title</h1>
<p>Tagline</p>
</div>
</div>
</div>
CSS
html, body {
width: 100%;
height: 100%;
}
#banner {
width: 100%;
height: 67%;
height: calc(67% - 123px);
background: #000 url('images/banner.jpg') 50% 0 no-repeat;
background-size: cover;
margin: 0 auto;
overflow: hidden;
padding: 0;
position: relative;
}
#banner[id] {
display: table;
position: static;
}
#banner #valign {
width: 100%;
position: absolute;
text-align: center;
top: 50%;
}
#banner #valign[id] {
display: table-cell;
position: static;
vertical-align: middle;
}
#banner .teaser {
width: 960px;
margin: -12px auto 0 auto;
padding: 21px 0;
position: relative;
top: -50%;
}
#banner .teaser h1 {
color: #fff;
font-size: 45px;
}
#banner .teaser h1:after {
content: "";
display: block;
width: 42px;
margin: 18px auto;
border-bottom: 3px solid #bfbfbf;
}
#banner .teaser p {
color: #808080;
font-size: 24px;
margin: 0;
text-align: center;
}
Are you looking for something like this:
See JSFiddle
CSS:
* {
margin: 0; padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
#banner {
width: 100%;
height: 100%;
background-color: black;
padding: 0;
}
#banner #valign {
width: 100%;
height: 100%;
}
#newDiv {
height: 100%;
width: 1366px;
display: table;
margin: 0 auto;
background: #000 url('http://www.psdgraphics.com/file/colorful-triangles-background.jpg') 50% 0 no-repeat;
background-size: cover;
}
#banner .teaser {
display: table-cell;
width: 960px;
margin: 0 auto;
padding: 21px 0;
text-align: center;
vertical-align: middle;
}
#banner .teaser h1 {
color: #fff;
font-size: 45px;
}
#banner .teaser h1:after {
content: "";
display: block;
width: 42px;
margin: 18px auto;
border-bottom: 3px solid #bfbfbf;
}
#banner .teaser p {
color: #808080;
font-size: 24px;
margin: 0;
}
Just add a container around the outside instead.
Updated HTML
<div id="container">
<div id="banner">
<div id="valign">
<div class="teaser">
<h1>Title</h1>
<p>Tagline</p>
</div>
</div>
</div>
</div>
Updated CSS
#container {
width: 100%;
height: 67%;
height: calc(67% - 123px);
background: #000;
margin: 0 auto;
}
#banner {
width: 1366px;
height: 100%;
background: #000 url('images/banner.jpg') 50% 0 no-repeat;
background-size: cover;
margin: 0 auto;
overflow: hidden;
padding: 0;
position: relative;
}

Issue to center a div horizontally

I'm trying to center my div .down-arrow horizontally but without success. The DIV is absolute positioned but margin:0px auto; does not seem to be working. What is the issue? thanks
http://jsfiddle.net/7UNrP/
HTML:
<header>
<div class="down-arrow">arrow</div>
</header>
CSS:
header {
position: relative;
width: 100%;
height: 600px;
min-height: 300px;
margin-right: auto;
margin-left: auto;
background-image: url('http://lorempixel.com/output/nature-q-c-1020-711-1.jpg');
background-size: cover;
background-position: center center;
background-color: rgb(222, 222, 222);
}
.down-arrow {
position: absolute;
margin:0px auto;
bottom: 20px;
display: inline;
padding: 10px;
color: #FFF;
border: 0;
font-weight: 400;
font-size: 12px;
background: red;
-webkit-animation: icon 1.2s infinite;
}
#-webkit-keyframes icon {
from {
opacity: 1;
bottom: 20px;
}
to {
opacity: 0;
bottom: 10px;
}
}
The problem is that you haven't told the arrow div where to be except bottom:20px so it defaults to left:0;
JSfiddle Demo
You need to add this to your arrow CSS
left:50%; /*push the div halfway over*/
-webkit-transform:translateX(-50%); /* bring it back by half its own width */
transform:translateX(-50%);
You might want to refer to this post which had much the same issue. I go into further detail regarding this solution.
Reference Question
The issue is due to using margin:0 auto with the display:inline and position: absolute. You can easily center it by applying text-align:center to the header as your inner content has an inline layout.
Example
You could use
text-align:center;
in header css like this
header {
position: relative;
width: 100%;
height: 600px;
min-height: 300px;
margin-right: auto;
margin-left: auto;
background-image: url('http://lorempixel.com/output/nature-q-c-1020-711-1.jpg');
background-size: cover;
background-position: center center;
background-color: rgb(222, 222, 222);
text-align: center;
}
EDIT:
This aligns the .down-arrow div in the center horizontally and keep it 20 pixel away from the bottom side of the its container
.down-arrow {
position: absolute;
margin-left: 50%;
/* bottom: 20px; */
/* display: inline; */
padding: 10px;
color: #FFF;
border: 0;
font-weight: 400;
font-size: 12px;
background: red;
-webkit-animation: icon 1.2s infinite;
}