Is it possible to add the similar background ( using pseudo element or whatever ) to the left element instead of right? Couldnt achive this using pseudo elements because they constantly appear at the start of containing element so I only could achieve this effect for the right column .
* {
box-sizing: border-box;
margin: 0;
}
.wrapper {
padding: 20px 0;
position: relative;
overflow: hidden;
}
.container {
padding: 0 15px;
max-width: 1170px;
margin-right: auto;
margin-left: auto;
}
.row {
display: flex;
flex-wrap: wrap;
margin-left: -15px;
margin-right: -15px;
}
.col-left,
.col-right {
padding: 0 15px;
flex-grow: 1;
text-align: center;
height: 500px;
}
.col-left {
width: 70%;
}
.col-right {
width: 30%;
}
.col-right::after {
content: '';
display: block;
position: absolute;
z-index: -1;
top: 0;
height: 100%;
width: 50%;
margin-left: -15px;
background-color: lightgray;
}
<div class="wrapper">
<div class="container">
<div class="row">
<div class="col-left"><h1>Left</h1></div>
<div class="col-right"><h1>Right</h1></div>
</div>
</div>
</div>
You can make the pseudo element relative to their column and use a big width to have overflow:
* {
box-sizing: border-box;
margin: 0;
}
.wrapper {
padding: 20px 0;
position: relative;
overflow: hidden;
}
.container {
padding: 0 15px;
max-width: 1170px;
margin-right: auto;
margin-left: auto;
}
.row {
display: flex;
flex-wrap: wrap;
margin-left: -15px;
margin-right: -15px;
}
.col-left,
.col-right {
padding: 0 15px;
flex-grow: 1;
text-align: center;
height: 500px;
position:relative;
}
.col-left {
width: 70%;
}
.col-right {
width: 30%;
}
.col-right::after {
content: '';
position: absolute;
z-index: -1;
left: 0;
top:-20px;
bottom:0;
width: 100vw;
background-color: lightgray;
}
.col-left::after {
content: '';
position: absolute;
z-index: -1;
top: -20px;
right:0;
bottom:0;
width: 100vw;
background-color: green;
}
<div class="wrapper">
<div class="container">
<div class="row">
<div class="col-left"><h1>Left</h1></div>
<div class="col-right"><h1>Right</h1></div>
</div>
</div>
</div>
Explanation:
Currently the columns width is related to wrapper as wrapper has the style definition position:relative. We want the width of the columns to be related to the div with class "row". So if you give position:relative to .row then "col-left" as well as "col-right" will have width as you expected.
What to do:
To get the expected result add the line position:relative to row class definition.
.row {
display: flex;
flex-wrap: wrap;
margin-left: -15px;
margin-right: -15px;
position:relative;
}
Now to get the effect for left hand side add the code as below
.col-left::before {
content: '';
display: block;
position: absolute;
z-index: -1;
top: 0;
height: 100%;
width: 70%;
margin-left: -15px;
background-color: lightgray;
}
Related
I am trying to create a progression section, where i have a text, image and progression.
I am able to achieve this but the problem here is that i want the image section and text should be vertical middle align to the parent div.
Is there a way i can use flex instead of relative and absolute.
.progress-bar-container {
background-color: #33cc33;
width: 100%;
position: fixed;
bottom: 0;
z-index: 1;
height: 40px;
}
img {
width: 20px;
height: 20px;
}
.progress-info-wrapper {
position: absolute;
}
.text-wrapper {
color: #263238;
margin-left: 8px;
}
.progress {
height: 40px;
width: 11%;
background-color: #99ff99;
}
<div class="progress-bar-container">
<div class="progress-info-wrapper">
<img src="https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U"
alt="test-img"><span class="text-wrapper">Add more items to get offer</span></div>
<div class="progress"></div>
</div>
You mean something like that? Add some flex properties to your .progress-info-wrapper class.
.progress-info-wrapper {
position: absolute;
display:flex;
align-items: center;
height: 100%;
}
.progress-bar-container {
background-color: #33cc33;
width: 100%;
position: fixed;
bottom: 0;
z-index: 1;
height: 40px;
}
.progress-info-wrapper {
position: absolute;
display:flex;
align-items: center;
height: 100%;
}
img {
width: 20px;
height: 20px;
}
.text-wrapper {
color: #263238;
margin-left: 8px;
}
.progress {
height: 40px;
width: 11%;
background-color: #99ff99;
}
<div class="progress-bar-container">
<div class="progress-info-wrapper">
<img src="https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U"
alt="test-img"><span class="text-wrapper">Add more items to get offer</span></div>
<div class="progress"></div>
</div>
To vertically align your image and text, you should use flexbox in progress-info-wrapper
.progress-bar-container {
background-color: #33cc33;
width: 100%;
position: fixed;
bottom: 0;
z-index: 1;
height: 40px;
}
img {
width: 20px;
height: 20px;
}
.progress-info-wrapper {
position: absolute;
display: flex;
align-items: center;
height: 100%;
}
.text-wrapper {
color: #263238;
margin-left: 8px;
}
.progress {
height: 40px;
width: 11%;
background-color: #99ff99;
}
<div class="progress-bar-container">
<div class="progress-info-wrapper">
<img src="https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U"
alt="test-img"><span class="text-wrapper">Add more items to get offer</span></div>
<div class="progress"></div>
</div>
Flex would help you with layout deciding how the items will need to be positioned next to each other but not over each other.
In that case position:absolute still fits better. To center the element you need the magic of margin: auto but you should give your element an height using fit-content.
Here's your demo with the .progress-info-wrapper css rules changed as:
.progress-info-wrapper {
position: absolute;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: fit-content;
}
.progress-bar-container {
background-color: #33cc33;
width: 100%;
position: fixed;
bottom: 0;
z-index: 1;
height: 40px;
}
img {
width: 20px;
height: 20px;
}
.progress-info-wrapper {
position: absolute;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: fit-content;
}
.text-wrapper {
color: #263238;
margin-left: 8px;
}
.progress {
height: 40px;
width: 11%;
background-color: #99ff99;
}
<div class="progress-bar-container">
<div class="progress-info-wrapper">
<img
src="https://i.picsum.photos/id/237/200/300.jpg?hmac=TmmQSbShHz9CdQm0NkEjx1Dyh_Y984R9LpNrpvH2D_U"
alt="test-img">
<span class="text-wrapper">Add more items to get offer</span>
</div>
<div class="progress"></div>
</div>
The button will not stay with the image when I adjust the size of the browser. I tried the position:absolutein the img div and the responsive didn't work well with the position property. Obviously the float:left doesn't work either as written in CSS.
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group img {
z-index: 2;
text-align: center;
margin: 0 auto;
border: 1px solid red;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
float: left;
position: relative;
z-index: 1;
margin-top: 200px;
margin-left: 330px;
top: 40px;
}
<section class="section6">
<button>REQUEST AN INTERPRETER</button>
<div class="img-group"><img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters"></div>
<div class="bg-bar"></div>
</section>
See on JSFIDDLE of what I did.
You're using fixed sizing units and this is not how you make responsive pages.
If you want the button to stay in the middle, you have to position it absolutely inside the relative div.
Something like this:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.relative {
position: relative;
padding: 10px;
background: #0fc0fc;
animation: reduce 2s ease-in-out infinite;
height: 50px;
}
button.centered {
position: absolute;
left: 50%;
/* Kind of makes the anchor point of the element to be in the horizontal center */
transform: translateX(-50%);
}
#keyframes reduce {
0%,
100% {
width: 100%;
}
50% {
width: 50%;
}
}
<div class="relative">
<button class="centered">I'm in the middle</button>
</div>
You are better off changing the image to be a background image on that div and moving the button to be inside of it.
HTML:
<section class="section6">
<div class="img-group"><button>REQUEST AN INTERPRETER</button></div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
z-index: 2;
text-align: right;
margin: 0 auto;
border: 1px solid red;
position: relative;
background: url('http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg') no-repeat;
background-size: cover;
width: 400px;
height: 370px;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
position: relative;
z-index: 5;
top: 100px;
margin-right: 20px;
}
Try this:
HTML:
<section class="section6">
<div class="img-group">
<img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters">
<button>REQUEST AN INTERPRETER</button>
</div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
position: relative;
}
.img-group img {
text-align: center;
max-width: 100%;
margin: 0 auto;
border: 1px solid red;
}
.img-group button {
display: block;
position: absolute;
z-index: 10;
margin-left: -75px;
top: 50%;
left: 50%;
max-width: 100%;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
}
What is the best way to align a fixed div within a relative div to the right, while still keeping an inherited max-width?
Update (Jan 24, 2018): I've answered this question with the solution. See here.
See the following snippet for further reference:
body {
margin: 0;
padding: 0;
border: 0;
}
.container {
width: 100%;
}
.max-width {
margin: 0 auto;
max-width: 500px;
height: 1000px;
position: relative;
box-sizing: border-box;
background-color: lightgrey;
}
.box {
max-width: inherit;
width: 20%;
height: 20px;
position: fixed;
background: blue;
float: right;
color: white;
text-align: center;
right: 0;
}
<div class="container">
<div class="max-width">
<div class="box">fix to right?</div>
</div>
</div>
A fixed element's position is always relative to the viewport/window, never to any other element.
The only thing you can do (with CSS) is to use right: calc(50% - 250px); for its position to have it right aligned to the right border of the 500px wide centered "parent" element, but that will only work if the screen is wider or equal to the max-width of the "parent" element.
Addition after comments: Plus add a media query for screens below 500px width with right: 0 (thanks to #MrLister for that)
body {
margin: 0;
padding: 0;
border: 0;
}
.container {
width: 100%;
}
.max-width {
margin: 0 auto;
max-width: 500px;
height: 1000px;
position: relative;
box-sizing: border-box;
background-color: lightgrey;
}
.box {
max-width: inherit;
width: 20%;
height: 20px;
position: fixed;
top: 0;
right: calc(50% - 250px);
background: blue;
float: right;
color: white;
text-align: center;
}
#media (max-width: 500px) {
.box {
right: 0px;
}
}
<div class="container">
<div class="max-width">
<div class="box">fix to right?</div>
</div>
</div>
What if you did this:
Css
body {
margin: 0;
padding: 0;
border: 0;
}
.container {
width: 100%;
}
.max-width {
margin: 0 auto;
max-width: 500px;
height: 1000px;
position: relative;
box-sizing: border-box;
background-color: lightgrey;
}
.box {
max-width: inherit;
width: 20%;
height: 20px;
position: fixed;
top: 0;
right: calc(50% - 250px);
background: blue;
float: right;
color: white;
text-align: center;
}
#media screen and (max-width: 500px) {
.box {
right: 0;
}
}
#media screen and (min-width: 501px) {
.box {
width: 100px; /* 100px is 20% of the max-width */
}
}
Html
<div class="container">
<div class="max-width">
<div class="box">fix to right?</div>
</div>
</div>
Figured something out. It can be done after all!
body {
margin: 0;
padding: 0;
border: 0;
width: 100%;
}
.max-width {
max-width: 500px;
height: 2000px;
margin: auto;
background-color: lightgrey;
position: relative;
}
.box1 {
position: relative;
width: 20%;
height: 100px;
background-color: yellow;
text-align: center;
}
.container {
position: absolute;
width: 60%;
background-color: purple;
height: 100%;
margin: 0 auto;
left: 0;
right: 0;
top: 0;
text-align: center;
}
.wrap-box {
position: fixed;
max-width: 500px;
width: 100%;
height: 100%;
left: 50%;
transform: translateX(-50%);
top: 0;
}
.wrap-box > div.box2 {
width: 20%;
height: 100px;
background-color: blue;
position: absolute;
top: 0;
right: 0;
text-align: center;
}
.wrap-box > div.box3 {
width: 20%;
height: 100px;
background-color: green;
position: absolute;
bottom: 0;
right: 0;
text-align: center;
}
<div class="max-width">
<div class="box1">position: relative, width: 20%</div>
<div class="container">
position: absolute, width: 60%
<div class="wrap-box">
<div class="box2">position: fixed (top), width: 20%</div>
<div class="box3">position: fixed (bottom), width: 20%</div>
</div>
</div>
</div>
I'm running this JSFIDDLE and I want to set the vertical-align property to bottom for both the float and non-float elements, but it doesn't work :
Here is my html code :
<div class="main">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
CSS :
div {
border: 1px solid;
}
.main {
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
vertical-align: bottom;
width: 20%;
}
.left {
height: 60px;
float: left;
background-color: red;
}
.right {
height: 40px;
float: right;
background-color: blue;
}
.middle {
height: 20px;
background-color: green;
display: inline-block;
}
Unless you're using with a table cells, vertical-align aligns the element with respect to adjacent elements, in particular text.
Read more about vertical alignment here:
http://phrogz.net/css/vertical-align/index.html
What you can do though is give your wrapper a width, height, and set it it to position: relative
After that you can use absolute positioning for your divs, set them to bottom: 0 they will then stick to the bottom of the wrapper.
Here is new CSS:
div {
border: 1px solid;
}
.main {
position: relative;
width: 100%;
height: 100px;
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
width: 20%;
}
.left {
height: 60px;
background-color: red;
position: absolute;
bottom: 0;
left: 0;
}
.right {
height: 40px;
background-color: blue;
position: absolute;
bottom: 0;
right: 0;
}
.middle {
height: 20px;
background-color: green;
position: absolute;
transform: translate(-50%, 0%);
left: 50%;
bottom: 0;
}
CODEPEN DEMO
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;
}