Center an element overlaying another element - html

I want to make a header with a centered textbox in it but can't seem to center it. I know countless similar questions have been asked but I can't wrap my head around how I would do it with the textbox on top of the image.
Does anyone here have a solution? I prefer using flex.
In the code snippet I'm trying to center the red box.
#container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
background: purple;
width: 100%;
height: 100vh;
}
#back {
background: blue;
height: 80%;
width: 80%;
}
#top {
position: absolute;
background: red;
width: 40%;
height: 40%;
margin-left: 25%;
margin-top: auto;
}
<div id="container">
<div id="top"></div>
<div id="back"></div>
</div>

#container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
background: purple;
width: 100%;
height: 100vh;
}
#back {
background: blue;
height: 80%;
width: 80%;
}
#top {
position: absolute;
background: red;
width: 40%;
height: 40%;
left: 50%; /* center horizontally */
top: 50%; /* center vertically */
transform: translate(-50%,-50%) /* tweak for precise centering; details:
http://stackoverflow.com/q/36817249 */
}
<div id="container">
<div id="top"></div>
<div id="back"></div>
</div>

here is another answer jsfiddle 1
#container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
background: purple;
width: 100%;
height: 100vh;
}
#back{
background: blue;
height: 80%;
width: 80%;
}
#top {
position: absolute;
background: red;
width: 40%;
height: 40%;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
margin-top: 12%;
}
<div id="container">
<div id="top"></div>
<div id="back"></div>
</div>

Related

Trying to fit image inside a flex box in CSS

This is how I want the page to split, so that I can have a fluid layout.
<div class="container">
<div class="top">Top</div>
<div class="image">image</div>
<div class="footer">footer</div>
</div>
.container{
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
.image {
flex: 50%;
width: 100%;
background-color: green;
}
.footer{
flex: 25%;
width: 100%;
background-color:blue;
}
https://jsfiddle.net/9n7zmr60/
In the image section, I wanna insert an image and I want it to fit in the same ratio (25% 50% 25%) as above. However, when I try, its getting stretched like below:
.container{
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
.image {
flex: 50%;
width: 100%;
background-color: green;
max-width: 100%;
object-fit: contain;
}
.footer{
flex: 25%;
width: 100%;
background-color:blue;
}
https://jsfiddle.net/qcnwu7fe/
Please help
Do this changes:
.image {
flex: 50%; /*delete this*/
height: 50%;
width: 100%;
background-color: green;
}
img{
height: 100%;
width: 100%;
}
.container {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: column;
}
.top {
flex: 25%;
width: 100%;
background-color: red;
}
img {
height: 100%;
width: 100%;
}
.image {
width: 100%;
background-color: green;
height: 50%;
}
.footer {
flex: 25%;
width: 100%;
background-color: blue;
}
<div class="container">
<div class="top">Top</div>
<div class="image"><img src="https://static1.momsimage.com/wordpress/wp-content/uploads/2019/08/Kids-cartoons.jpg" alt=""></div>
<div class="footer">footer</div>
</div>

Align child element's center by the paren'ts container right edge

Suppose, we have child element positioned at the top right corner of the parent (think of the badge over the icon button).
My problem is: I need child's center to stick to parent's right edge, but currently when badge content widen, its center shifts to the left:
enter image description here
Live sandbox:
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#parent {
width: 150px;
height: 150px;
background: aquamarine;
position: relative;
}
#child {
width: 150px;
height: 100px;
background: orange;
position: absolute;
right: -50px;
top: -50px;
border-radius: 50px;
}
<div id="wrapper">
<div id="parent">
<div id="child"></div>
</div>
</div>
You can use transform: translate(50%, -50%)
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#parent {
width: 150px;
height: 150px;
background: aquamarine;
position: relative;
}
#child {
width: 150px;
height: 100px;
background: orange;
position: absolute;
right: 0;
top: 0;
border-radius: 50px;
transform: translate(50%, -50%)
}
<div id="wrapper">
<div id="parent">
<div id="child"></div>
</div>
</div>

Centering multiple divs in another div

I want to center 3 divs horizontally and vertically in a div. I can't use flexbox, because later more divs and flexbox is shinks these new divs, but I dont know how to center vertically
<div class="hero">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
.hero {
width: 100vw;
height: 100vh;
text-align: center;
}
.box {
width: 250px;
height: 350px;
background: red;
margin: 50px;
display: inline-block;
}
It's still possible to center it by nudging it up half of it's height after bumping it down halfway:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Update : ( H in Priority ) :
html, body {
height: 100%;
}
.hero {
text-align: center;
align-items: center;
vertical-align: middle;
justify-content: center;
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box {
width: 250px;
height: 350px;
background: red;
margin: 5px;
top: 50%;
position: relative;
display: inline-block;
}
html,body,div,table-cell{
width:100%;
padding:0;
border:0;
}
.hero {
width: 100vw;
height: 100vh;
display:table-cell;
vertical-align:middle;
}
#a{
margin:0 auto;
display:block;
width:750px;
}
.box {
width: 250px;
height: 50vh;
background: red;
display:inline-block;
box-sizing:border-box;
border:3px solid black;
}
<div class="hero">
<div id='a'>
<div class="box"></div
><div class="box"></div
><div class="box"></div>
</div>
</div>

Repeat background with fixed background attachment and rotated container?

Is it possible to get seamless pattern repeat with following conditions?
Idea is to have one rotated background with fixed attachment. I think this is how is supposed to be working, but are there any ways of achieving that?
body {
padding: 50px;
margin: 0;
height: 10000px;
}
.main {
width: 100%;
height: 1300px;
box-sizing: border-box;
overflow: hidden;
border: 5px solid #7cb7b7;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.background {
background-image: url('https://thumbs.dreamstime.com/b/circles-seamless-pattern-10032830.jpg');
background-attachment: fixed;
background-repeat: repeat;
position: absolute;
top: -1000%;
left: -1000%;
right: -1000%;
bottom: -1000%;
transform: rotate(-15deg);
}
.text {
background: white;
border: 2px solid black;
width: 200px;
height: 70px;
display: flex;
justify-content: center;
align-items: center;
z-index: 1;
}
<div class="main">
<div class="background"></div>
<div class="text">
<p>Flex</p>
</div>
</div>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body {
padding: 50px;
margin: 0;
height: 1000px;
}
.main {
width: 100%;
height: 1000px;
box-sizing: border-box;
overflow: hidden;
border: 5px solid #7cb7b7;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.image {
background-image: url('images/attractions01.jpg');
background-attachment: fixed;
background-repeat: repeat;
position: absolute;
top: -1000%;
left: -1000%;
right: -1000%;
bottom: -1000%;
}
.text {
background: white;
border: 2px solid black;
width: 200px;
height: 70px;
display: flex;
justify-content: center;
align-items: center;
z-index: 1;
}
</style>
</head>
<body>
<div class="main">
<div class="image"></div>
<div class="text">
<p>Flex</p>
</div>
</div>
</body>
</html>

trouble creating 100vh page with footer and sidebar

I am trying to make a content slider with a chatbox to the side and a footer stuck to the bottom.
Here is a diagram of what I am trying to achieve:
The problem with below code is that the chatbox is the height of the page. I want the chat box to stop at the footer so that it is the height of the page -60px.
And here is what I have so far:
body {
margin: 0;
}
.wrapper {
background: #95a5a6;
display: table;
height: 100vh;
width: 100%;
}
.wrapper-inner {
display: table-cell;
padding-left: 300px;
padding-bottom: 60px;
vertical-align: middle;
text-align: center;
}
.chatbox {
background: #bdc3c7;
min-height: 100%;
position: absolute;
overflow-x: hidden;
overflow-y: auto;
width: 300px;
z-index: 2;
}
.footer {
background: #2c3e50;
bottom: 0px;
height: 60px;
position: absolute;
width: 100%;
z-index: 1;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner">Content</div>
</div>
<div class="footer"></div>
https://jsfiddle.net/bjxsyve7/4/
Here's a simplified version using only flex and calc():
body {
display: flex;
flex-wrap: wrap;
margin: 0;
}
.chatbox {
flex: 0 0 300px;
height: calc(100vh - 60px);
overflow-y: auto;
background-color: #bdc3c7;
}
.content {
flex: 1;
background-color: #95a5a6;
}
.footer {
flex-basis: 100%;
height: 60px;
background: #2c3e50;
}
<div class="chatbox"></div>
<div class="content">Content</div>
<div class="footer"></div>
jsFiddle
You can use CSS calc() to achieve this. Add this min-height: calc(100% - 60px) to .chatbox. For more info about calc().
body {
margin: 0;
overflow-x:hidden;
}
.wrapper {
background: #95a5a6;
display: table;
height: 100vh;
width: 100%;
}
.wrapper-inner {
display: table-cell;
min-height: 100%;
padding-left:300px;
padding-bottom: 60px;
}
.chatbox {
background: #bdc3c7;
min-height: calc(100% - 60px);
position: absolute;
overflow-x: hidden;
overflow-y: auto;
top:0;
width: 300px;
z-index: 2;
}
.footer {
background: #2c3e50;
bottom: 0px;
height: 60px;
position: absolute;
width: 100%;
z-index: 1;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner"></div>
</div>
<div class="footer"></div>
You need only adding this:
.wrapper{
position: relative;
}
In your code the chatbox div has height 100% of the body. But if you set position: relative; to it's parent(.wrapper) it will have height 100% of it's parent.
This is an easy way to do this with flex:
body {
display: flex;
flex-direction:column;
min-height: 100vh;
}
.wrapper {
background: #95a5a6;
display: flex;
flex: 1;
}
.chatbox {
background: #bdc3c7;
overflow-x: hidden;
overflow-y: auto;
width: 200px;
}
.footer {
background: #2c3e50;
height: 60px;
}
<div class="wrapper">
<div class="chatbox"></div>
<div class="wrapper-inner">Content</div>
</div>
<div class="footer"></div>