I'm trying to follow this tutorial:
https://css-tricks.com/full-browser-width-bars/#article-header-id-0
What I need, is my <header> element to be a max-width of 800px, centered in the middle of the page. Then inside that header bar, I will be having a #filtersBar div that will go across the whole body of the page (edge to edge). This is what my code comes out with so far:
Here is a fiddle:
https://jsfiddle.net/us2jsmLy/3/
html, body {
overflow-x: hidden;
}
header {
position: relative;
width: 100%;
margin: 0 auto;
max-width: 1200px;
z-index: 250;
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
background:red;
justify-content: space-between;
}
#filtersBar {
position: relative;
background: green;
width: 100%;
max-width: 800px;
margin: 0 -9999rem;
padding: 0.25rem 9999rem;
z-index: 1;
}
#filtersBarInner {
position: relative;
width: 100%;
max-width: 800px;
background-color: blue;
z-index: 2;
}
<header>
foo bar some contents
<div id="filtersBar">
<div id="filtersBarInner">
dddd
</div>
</div>
</header>
I must be missing something stupid :/
As I understand, you can use :before pseudo for the full background edge to edge and also apply margin:0 to body
Updated Fiddle
html,
body {
overflow-x: hidden;
margin: 0;
}
div#filtersBar:before {
content: "";
position: absolute;
background: green;
top: 0;
bottom: 0;
left: -1000px;
right: -1000px;
z-index: -1;
}
header {
position: relative;
width: 100%;
margin: 0 auto;
max-width: 800px;
z-index: 250;
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
background: red;
justify-content: space-between;
}
#filtersBar {
position: relative;
background: green;
width: 100%;
max-width: 800px;
z-index: 1;
padding: 15px 0;
}
#filtersBarInner {
position: relative;
width: 100%;
max-width: 800px;
/*margin: 0 auto;*/
background-color: blue;
z-index: 2;
}
<header>
foo bar some contents
<div id="filtersBar">
<div id="filtersBarInner">
<p>
ddddd
</p>
<p>
ddddd
</p>
<p>
ddddd
</p>
<p>
ddddd
</p>
</div>
</div>
</header>
Related
I'm looking for an alternative to this css code, which is working as expected on chrome, but not on firefox.
The pins are not keeping their position when the viewport size is different. You can resize (height and width) the viewport on jsfiddle on chrome to see what I expect and on firefox to see what I don't.
I guess there is something between margin: auto and object-fit: contain which is not calculated the same way...
Chrome margin from devtools
Firefox margin from devtools
I know that I can do it in javascript but i'd like to know if it's possible with css !
<div class="site">
<div class="wrapper">
<div class="title">TITLE</div>
<div class="subtitle">Subtitle</div>
<div class="container">
<div class="img-wrapper">
<img src="./img.jpg" alt="">
</div>
<div class="pin-container">
<div class="pin pin1">?</div>
<div class="pin pin2">?</div>
<div class="pin pin3">?</div>
<div class="pin pin4">?</div>
</div>
</div>
</div>
</div>
.site{
position: relative;
width: 100%;
height: 80vh;
display: flex;
flex-direction: column;
margin: 0 auto;
max-width: 960px;
border: 1px solid blue;
}
.wrapper{
display: flex;
flex-direction: column;
}
.container{
position: relative;
border-radius: 6px;
margin: auto;
}
.img-wrapper{
height: 100%;
}
.img-wrapper img{
width: 100%;
height: 100%;
object-fit: contain;
}
.pin-container{
position: absolute;
color: red;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.pin{
position: absolute;
background-color: white;
width: 20px;
height: 20px;
border-radius: 50%;
text-align: center;
transform: translate(-50%, -50%);
}
.pin1 {
top: 50%;
left: 24%;
}
.pin2 {
top: 30%;
left: 52%;
}
.pin3 {
top: 87%;
left: 79%;
}
.pin4 {
top: 100%;
left: 100%;
}
https://jsfiddle.net/MathieuSP/8u3gLz4q/
Thank you ! :)
I think it's because of display: flex in the .wrapper.
The flex-box itself will always keep the content to fit inside so it will shrink your image down or scale it up.
You can use flex: 1 to keep the image from being change (you can read more about it here) and set the aspect-ratio to keep width and height of the image consistent. Just set it in the .container
.container {
position: relative;
border-radius: 6px;
margin: auto;
flex: 1;
aspect-ratio: 1.5;
}
Here is the snippet, hope this can help you
* {
margin: 0;
overflow: hidden;
box-sizing: border-box;
}
body{
height: 100vh;
width: 100vw;
color: white;
background-color: black;
}
.title{
text-align: center;
padding: 2rem 0;
font-size: 2rem;
}
.subtitle{
text-align: center;
padding: 1rem 0;
font-size: 1rem;
}
.site{
position: relative;
width: 100%;
height: 80vh;
display: flex;
flex-direction: column;
margin: 0 auto;
max-width: 960px;
border: 1px solid blue;
}
.wrapper{
display: flex;
flex-direction: column;
}
.container{
position: relative;
border-radius: 6px;
margin: auto;
flex: 1;
aspect-ratio: 1.5;
}
.img-wrapper{
height: 100%;
}
.img-wrapper img{
width: 100%;
height: 100%;
object-fit: contain;
}
.pin-container{
position: absolute;
color: red;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.pin{
position: absolute;
background-color: white;
width: 20px;
height: 20px;
border-radius: 50%;
text-align: center;
transform: translate(-50%, -50%);
}
.pin1 {
top: 50%;
left: 24%;
}
.pin2 {
top: 30%;
left: 52%;
}
.pin3 {
top: 87%;
left: 79%;
}
.pin4 {
top: 100%;
left: 100%;
}
<div class="site">
<div class="wrapper">
<div class="title">TITLE</div>
<div class="subtitle">Subtitle</div>
<div class="container">
<div class="img-wrapper">
<img src="https://images.pexels.com/photos/1534057/pexels-photo-1534057.jpeg" alt="">
<div class="pin-container">
<div class="pin pin1">?</div>
<div class="pin pin2">?</div>
<div class="pin pin3">?</div>
<div class="pin pin4">?</div>
</div>
</div>
</div>
</div>
</div>
They are moving because their container size changes bigger than the picture. For such positioning, always put the image and absolute elements in one container with fit-content width and height. and of course relative position If you do so, they will stay in the same spot.
* {
margin: 0;
overflow: hidden;
box-sizing: border-box;
}
body{
height: 100vh;
width: 100vw;
color: white;
background-color: black;
}
.title{
text-align: center;
padding: 2rem 0;
font-size: 2rem;
}
.subtitle{
text-align: center;
padding: 1rem 0;
font-size: 1rem;
}
.site{
position: relative;
width: 100%;
height: 80vh;
display: flex;
flex-direction: column;
margin: 0 auto;
max-width: 960px;
border: 1px solid blue;
}
.wrapper{
display: flex;
flex-direction: column;
}
.container{
position: relative;
border-radius: 6px;
margin: auto;
}
.img-wrapper{
height: 100%;
}
.img-wrapper img{
width: 100%;
height: 100%;
object-fit: contain;
}
.pin-container{
position: absolute;
color: red;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 1px solid red;
}
.pos-container {
border: 3px solid orange;
position: relative;
width: fit-content;
height: fit-content;
}
.pin{
position: absolute;
background-color: white;
color: red;
width: 20px;
height: 20px;
border-radius: 50%;
text-align: center;
transform: translate(-50%, -50%);
}
.pin1 {
top: 50%;
left: 24%;
}
.pin2 {
top: 30%;
left: 52%;
}
.pin3 {
top: 87%;
left: 79%;
}
.pin4 {
top: 100%;
left: 100%;
}
<body>
<div class="site">
<div class="wrapper">
<div class="title">TITLE</div>
<div class="subtitle">Subtitle</div>
<div class="container">
<div class="img-wrapper">
<div class="pos-container">
<img src="https://images.pexels.com/photos/1534057/pexels-photo-1534057.jpeg" alt="">
<div class="pin pin1">?</div>
<div class="pin pin2">?</div>
<div class="pin pin3">?</div>
<div class="pin pin4">?</div>
</div>
</div>
</div>
</div>
</div>
</body>
i have two divs, i want to put the child on top of the parent top, i was trying but the child div got stuck outside, what is the wrong?
.content {
align-items: center;
border-radius: 4px;
display: flex;
flex-direction: column;
height: 200px;
min-height: 140px;
min-width: 140px;
border: 1px solid;
}
.topcorner {
width: 42px;
height: 42px;
background: red;
position: absolute;
top: 0;
right: 0;
z-index: 1;
}
<div class="content">
<div class="topcorner">top</div>
</div>
Parent element needs to have position: relative.
Add position: relative to the parent class .content to make the child div inside the parent div.
.content {
align-items: center;
border-radius: 4px;
display: flex;
flex-direction: column;
height: 200px;
min-height: 140px;
min-width: 140px;
border: 1px solid;
position: relative; //Add this line
}
.topcorner {
width: 42px;
height: 42px;
background: red;
position: absolute;
top: 0;
right: 0;
z-index: 1;
}
<div class="content">
<div class="topcorner">top</div>
</div>
I have a box with three elements, and I also need a full-width of viewport element which place inside item 2
This is pen which show this problem https://codepen.io/in-in/pen/Nwxoar
I’m talking about the purple element (after), and I expect it to occupy the full-width of viewport but it has a strange left margin (41px)
.container {
width: 800px;
height: 100vh;
margin-left: auto;
margin-right: auto;
background-color: tan;
}
.box {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
width: 100%;
height: 80px;
background-color: deeppink;
}
.box__item1 {
flex-basis: 200px;
background-color: dodgerblue;
}
.box__item2 {
position: relative;
flex-basis: 500px;
background-color: coral;
}
.box__item2::after {
position: absolute;
top: 100%;
left: 0;
right: 0;
width: 100vw;
height: 100%;
margin-left: calc(50% - 50vw);
content: "after";
background-color: blueviolet;
outline: 1px solid red;
}
.box__item3 {
flex-basis: 100px;
background-color: seagreen;
}
<div class="container">
<div class="box">
<div class="box__item1"><span>item 1</span></div>
<div class="box__item2"><span>item 2</span></div>
<div class="box__item3"><span>item 3</span></div>
</div>
</div>
The .box__item2 element has a relative position. I moved the position property to the .container element and added a top: 80px and set the height: 80px forafter.
.container {
width: 800px;
height: 100vh;
margin-left: auto;
margin-right: auto;
background-color: tan;
position: relative;
}
.box {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
width: 100%;
height: 80px;
background-color: deeppink;
}
.box__item1 {
-ms-flex-preferred-size: 200px;
flex-basis: 200px;
background-color: dodgerblue;
}
.box__item2 {
-ms-flex-preferred-size: 500px;
flex-basis: 500px;
background-color: coral;
}
.box__item2::after {
position: absolute;
top: 80px;
left: 0;
width: 100vw;
height: 80px;
margin-left: calc(50% - 50vw);
content: "after";
background-color: blueviolet;
outline: 1px solid red;
}
.box__item3 {
-ms-flex-preferred-size: 100px;
flex-basis: 100px;
background-color: seagreen;
}
<div class="container">
<div class="box">
<div class="box__item1"><span>item 1</span></div>
<div class="box__item2"><span>item 2</span></div>
<div class="box__item3"><span>item 3</span></div>
</div>
</div>
link: https://codepen.io/greensleeves/pen/gXrOQx
I am trying to produce this effect with CSS. I have tried creating a box with a triangle and then using negative margin to overlap it onto the circle, but cannot get it right.
Many thanks for any help.
fiddle
https://jsfiddle.net/Hastig/n3w0tztv/
Getting the circle to stay vertically centered and have the text container min-height the height of circle is tricky and is not worked out in this example. A cheap fix is adding align-items: center to .container at a breakpoint with #media.
body {
display: flex;
justify-content: center;
align-items: center;
margin: 0;
background-color: white;
height: 100vh;
}
.container {
display: flex;
width: 100%;
padding: 10px;
overflow: hidden;
}
.left {
position: relative;
display: flex;
width: 150px;
height: 150px;
margin-top: -4px;
margin-bottom: -4px;
margin-right: -17px;
background-color: #ec847c;;
border-style: solid;
border-color: white;
border-width: 4px;
border-radius: 100%;
z-index: 10;
}
.right {
position: relative;
display: flex;
flex: 2;
font-size: 20px;
color: white;
background-color: #4ca132;
}
.square {
position: absolute;
left: 0;
width: 50px;
height: 50px;
background-color: white;
overflow: hidden;
}
.square-top { top: 0; }
.square-btm { bottom: 0; }
.square::before {
content: "";
position: absolute;
display: flex;
width: 100%;
height: 100%;
transform: rotate(45deg) scale(2);
background-color: #4ca132;
z-index: 1;
}
.square-top::before { top: 50%; left: 50%; }
.square-btm::before { bottom: 50%; left: 50%; }
.text {
position: relative;
z-index: 2;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
padding: 20px 20px 20px 40px;
}
<div class="container">
<div class="left">
</div>
<div class="right">
<div class="square square-top"></div>
<div class="square square-btm"></div>
<div class="text">
Roles play an extremely important part in family funtion.
</div>
</div>
</div>
I have 3 divs in a container.
I want the left one (green) to be anchored (with some offset) to the left-bottom corner, the middle element (red) docked to the left element and centered vertically, and the right one (blue) to be docked to the right but centered vertically.
Here's a fiddle I'm working on.
I tried using right and margin-right etc. but it didn't work, here are some of my attempts.
This is the initial setup:
<div class="container">
<div class="left">
</div>
<div class="middle">
</div>
<div class="right">
</div>
</div>
.container {
background: gray;
height: 300px;
width: 100%;
}
.container > div {
height: 100px;
width: 100px;
}
div.left {
background: green;
height: 250px;
}
div.middle {
background: red;
}
div.right {
background: blue;
}
Result:
I've changed the fiddle based on your comments. Is this what you desire? Fiddle
*I've updated the fiddle
.container {
position: relative;
background: gray;
height: 300px;
width: 100%;
}
.container > div {
height: 100px;
width: 100px;
}
.left {
position: absolute;
left: 0;
bottom: 0;
height: 250px !important;
background: green;
}
.middle {
position: absolute;
left: 100px;
bottom: calc(50% - 50px);
background: red;
}
.right {
position: absolute;
right: 0;
bottom: calc(50% - 50px);
background: blue;
}
Fiddle
If you still want to retain the float layout (i.e. left and middle will not overlap each other), the solution is to wrap the inner content of each div with another <div> element, and position them absolutely with respect to their floated parents: http://jsfiddle.net/teddyrised/drrz6/2/
<div class="container">
<div class="left"><div></div>
</div>
<div class="middle"><div></div>
</div>
<div class="right"><div></div>
</div>
</div>
For your CSS:
.container {
background: gray;
height: 300px;
width: 100%;
}
.container > div {
height: 300px;
width: 100px;
position: relative;
}
.container > div > div {
width: 100px;
height: 100px;
position: absolute;
}
.left {
float: left;
}
.left > div {
background: green;
bottom: 0;
}
.middle {
float: left;
}
.middle > div {
background: red;
top: 50%;
margin-top: -50px; /* Half of height */
}
.right {
float: right;
}
.right > div {
background: blue;
bottom: 0;
}
Simply use flex boxes:
Demo http://jsfiddle.net/fr9U5/
HTML:
<div class="box">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
CSS:
.box {
width: 270px;
height:210px;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-direction: reverse;
-moz-box-direction: reverse;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-webkit-flex-direction: column-reverse;
-ms-flex-direction: column-reverse;
flex-direction: column-reverse;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-box-pack: justify;
-moz-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-align-content: flex-start;
-ms-flex-line-pack: start;
align-content: flex-start;
-webkit-box-align: center;
-moz-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-color: gray;
}
.left {
-webkit-box-ordinal-group: 1;
-moz-box-ordinal-group: 1;
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-box-flex: 0;
-moz-box-flex: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: flex-start;
-ms-flex-item-align: start;
align-self: flex-start;
background-color: green;
}
.middle {
-webkit-box-ordinal-group: 1;
-moz-box-ordinal-group: 1;
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-box-flex: 0;
-moz-box-flex: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: center;
-ms-flex-item-align: center;
align-self: center;
background-color: red;
}
.right {
-webkit-box-ordinal-group: 1;
-moz-box-ordinal-group: 1;
-webkit-order: 0;
-ms-flex-order: 0;
order: 0;
-webkit-box-flex: 0;
-moz-box-flex: 0;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
-webkit-align-self: flex-end;
-ms-flex-item-align: end;
align-self: flex-end;
background-color: blue;
}
/*
Legacy Firefox implementation treats all flex containers
as inline-block elements.
*/
#-moz-document url-prefix() {
.flex-container {
width: 100%;
-moz-box-sizing: border-box;
}
}
.box > div {
border:1px solid #000;
width: 33%;
height:33%;
}
OK. I guess the position:absolute is what works best for me, although I'd prefer the middle (red) element to depend on the left (green) one.
Here is the solution, and thanks Terry for the tip on deducting half-size of self to center vertically.
.container {
background: gray;
height: 300px;
width: 100%;
position: relative;
}
.container > div {
height: 100px;
width: 100px;
position: absolute;
}
div.left {
background: green;
left: 0;
bottom: 0;
height: 250px;
}
div.middle {
background: red;
left: 100px;
bottom: 0;
top: 50%;
margin-top: -50px; //deduct lalfsize of self
}
div.right {
background: blue;
right: 0;
top: 50%;
margin-top: -50px; //deduct lalfsize of self
}