Is it possible to anchor a textarea to an image that is centered in the middle of the page, so that it doesn't move out of the image when the screen size changes?
Example on jsfiddle: https://jsfiddle.net/rxg7t2ca/1/
.container {
width: 60%;
margin: 0 auto;
/* border: 2px solid blue; */
}
#cat {
display: block;
margin: 0 auto;
}
.box1 {
position: relative;
top: -250px;
left: 30px;
width: 100px;
height: 100px;
}
<div class="container">
<img src="http://i.imgur.com/a2Wd9D2.jpg" height=300px id="cat" />
<textarea class="box1"> This is a text box </textarea>
</div>
.container {
position: relative;
width: 60%;
margin: 0 auto;
border: 2px solid blue;
}
#cat {
width: 100%;
object-fit: cover; /* 1 */
vertical-align: bottom; /* 2 */
}
.box1 {
position: absolute; /* 3 */
top: 50%; /* 3 */
left: 50%; /* 3 */
transform: translate(-50%, -50%); /* 3 */
width: 100px;
height: 100px;
}
<div class="container">
<img src="http://i.imgur.com/a2Wd9D2.jpg" height=300px id="cat" />
<textarea class="box1"> This is a text box </textarea>
</div>
Explanations:
Why isn't object-fit working in flexbox?
Mystery white space underneath image tag
Element will not stay centered, especially when re-sizing screen
In the container use 'position:relative', and in the textarea and in the image use 'position: absolute'.
CSS absolute property: The element is positioned relative to its first positioned (not static) ancestor element.
.container{
width: 60%;
margin: 0 auto;
position:relative;
/* border: 2px solid blue; */
}
#cat{
position: absolute;
top: 0px;
left: 0px;
display: block;
margin: 0 auto;
}
.box1{
position: absolute;
top: 25px;
left: 30px;
width: 100px;
height: 100px;
}
Related
I want to make vertical line in DIV.
then I want to layer img on vertical line.
(the pic shows the result I want)
For my source code is like this .
<div style="background-color:gray;width:1px;height:100%;"></div>
<img src="circle.png">
<img src="triangle.png">
How can I layer these elements???
You will need to do some math to adjust it in the center.
.outer-flex {
display: flex;
width: 40px;
align-items: center;
position: absolute;
}
.line {
background-color: gray;
width: 1px;
height: 100vh;
margin-left: auto;
margin-right: auto;
}
.circle {
position: absolute;
left: calc(50% - 15px);
top: 20px;
border: 5px solid white;
border-radius: 20px;
}
.arrow {
position: absolute;
top: 70vh;
left: calc(50% - 15px)
}
<div class="outer-flex">
<div class="line"></div>
<img src="https://www.marylandeyeassociates.com/wp-content/uploads/2015/03/red-dot-hi.png" width="21px" class="circle">
<img src="https://image.flaticon.com/icons/png/512/60/60995.png" width="31px" class="arrow">
</div>
The images are inside the div this way:
div {
background-color: gray;
width: 1px;
height: 200px;
}
img:first-of-type {
margin-left: -10px;
top: 30px;
position: relative;
}
img:last-of-type {
margin-left: -10px;
top: 85px;
position: relative;
}
<div>
<img src="https://picsum.photos/20/20" />
<img src="https://picsum.photos/20/20" />
</div>
If you know the width of the images, move them to the left with a negative margin of half their width.
I have a div with some elements inside.
I would like to center the start button exactly in the middle of the div. I've tried making it fixed like this:
Board.css
.board{
width: 400px;
height: 400px;
margin: 150px auto 0;
border: 5px solid black;
border-radius: 5px;
background-color: darkgray;
display: flex;
flex-wrap: wrap;
}
.startButton {
position: fixed;
left: 50%;
top:50%;
}
#media(max-width: 650px) {
.board {
width: 300px;
height: 300px;
margin: 150px auto 0;
border: 5px solid black;
border-radius: 5px;
}
}
But as you can see, it is not perfectly aligned in the middle. How do I go about this? I've also made a media-query that makes the div smaller on smaller devices, so it needs to be responsive as well.
Board.js
render() {
return (
<div className="board">
<button className="startButton">Start Game!</button>
<Square clicked={this.squareClickedHandler} val={this.state.squareOne} />
<Square clicked={this.squareClickedHandler} val={this.state.squareTwo} />
<Square clicked={this.squareClickedHandler} val={this.state.squareThree} />
<Square clicked={this.squareClickedHandler} val={this.state.squareFour} />
</div>
)
}
Add position: relative; in .board class and remove all css styles from .startButton and add following code in .startButton
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Try following code
.board{
position: relative;
width: 400px;
height: 400px;
margin: 150px auto 0;
border: 5px solid black;
border-radius: 5px;
background-color: darkgray;
display: flex;
flex-wrap: wrap;
}
.startButton {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
#media(max-width: 650px) {
.board {
width: 300px;
height: 300px;
margin: 150px auto 0;
border: 5px solid black;
border-radius: 5px;
}
}
<div class="board">
<button class="startButton">Start Game!</button>
</div>
left: 50%
makes the element's x-coördinate be in the middle of your screen.
The value you actually want is
´left: (50% - [element width / 2]) ´
so your the x-coördinate will be a little to the left of the middle so your element can be in the middle
So I have two <div> next to each other and I want to make it so when you have little space (Phone for example) it puts the second <div> under the first one with some space. When you're on a 16:9 ratio computer it has them next to each other.
body {
background: #FFFFFF;
}
.box {
float: left;
margin: 10px;
padding: 25px;
max-width: 300px;
height: 300px;
border: 1px solid black;
}
div {
max-width: 2480px;
z-index: 2;
left: 0;
position: absolute;
width: 100%;
height: 50px;
top: 0px;
color: #2D2E32;
background: #2D2E32;
}
/*Box1*/
div2 {
position: absolute;
color: #2D2E32;
background: #2D2E32;
width: 700px;
height: 950px;
top: 700;
left: 200;
}
div3
/*Box2*/
{
position: absolute;
color: #2D2E32;
background: #2D2E32;
width: 700px;
height: 950px;
top: 700;
right: 10%;
}
img {
max-height: 800;
max-width: 2480;
z-index: 1;
width: 100%;
height: 63%;
left: 10%;
}
div4 {
max-height: 59%;
z-index: -1;
position: absolute;
width: 100%;
height: 59%;
top: 5%;
color: #17181A;
background: #17181A;
left: 0;
}
div5 {
max-width: 2480;
max-height: 25;
z-index: 2;
left: 0;
position: absolute;
width: 100%;
height: 25px;
color: #2D2E32;
background: #2D2E32;
}
<body>
<div id="page1">
<!--Task-->
<a id="Task" class="smooth"></a>
</div>
<div2 id="page2">
<!--Box1-->
<a id="Info1" class="smooth" class="box"></a>
</div2>
<div3>
<!--Box2-->
<a id="Info1" class="smooth" class="box"></a>
</div3>
</body>
CSS Media Queries will solve this problem by allowing you to create styles that will be conditionally applied based on a query that you specify. Here's an example:
/* Develop "mobile-first, meaning that your normal styles should reflect how you want
the content to look on a mobile device
div elements will normally appear on their own line, but let's add a little space between
the lines
*/
div { margin:1em; }
/* When the viewport is not bigger than 760px and it is rotated to be wide
put divs next to each other and only move them down when the full width
of the viewport is used up */
#media screen and (max-width:760px) and (orientation:landscape) {
div {
float:left;
margin:auto; /* reset margins back to normal */
}
}
<div>Some div content</div>
<div>Some div content</div>
The following code shows a logo and it's background. I can't get the logo centered vertically. Is it because the background div has no height defined? How can I get the background to be always 100% of the screen size with a centered logo?
body {
margin: 0;
}
#header {
min-height: 100%;
width: 100%;
position: absolute;
background-color: #CCC;
}
.logo {
max-width: 100%;
margin: 0 auto;
display: block;
position: relative;
}
<div id="header">
<img class="logo" src="http://lorempixel.com/100/50/abstract/3/">
</div>
Using absolute position for image, positing to center of div using top and left and them move it back for half using transform:translate
There is code
html,body {
margin: 0;
height:100%;
}
#header {
height: 100%;
width: 100%;
position: relative;
background-color: #CCC;
}
.logo {
display: block;
position: absolute;
top:50%; left:50%; /*positing top left corner of image to center*/
transform: translate(-50%, -50%); /* move left and up for 1/2 of image */
}
<div id="header">
<img class="logo" src="http://lorempixel.com/100/50/abstract/3/">
</div>
There is fiddle example with position:absolute;
html, body {
margin: 0;
height: 100%;
}
#header {
min-height: 100%;
width: 100%;
position: relative;
background-color: #CCC;
}
.logo {
margin: 0 auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
border: 1px solid blue;
}
<div id="header">
<img class="logo" src="http://lorempixel.com/100/50/abstract/3/">
</div>
You can vertical center using this method inside your .logo class. But you most define your parent height for it to be effective.
.logo {
max-width: 100%;
margin: 0 auto;
display: block;
position: relative;
top: 50%; /* Added Code */
transform: translateY(-50%); /* Added Code */
}
I have got the following structure in HTML:
<div id="a">
<div id="b">
<div id="c">
<img src="http://public.media.smithsonianmag.com/legacy_blog/npg_portraits_nicholson_jack_2002.jpg"/>
</div>
</div>
</div>
And in CSS:
#a{
height: 300px;
background-color: red;
padding: 20px;
width: 100%;
text-align:center;
}
#b {
width: auto;
height: auto;
display: inline-block;
max-height:100%; /*should be 300px - 2*20px = 260px */
/*no height shall be set here*/
}
#c {
background-color:green;
max-width: 300px;
max-height: inherit;
}
img {
opacity: 0.8;
max-width: 100%;
}
I want the image to be scaled into the red container. The structure is kind of fixed that way. I also uploaded the fiddle for you:
Demo Fiddle
I hope someone is able to help!
Scaled with position: absolute on the image.
#a has position: relative and the position: absolute image will scale accordingly.
Centered with the combination of top, right, bottom, left and margin: auto
box-sizing: border-box incorporates the padding and any borders into your width and helps prevent that pesky scrollbar
Example
* {
box-sizing: border-box;
}
#a {
height: 300px;
background-color: red;
padding: 20px;
width: 100%;
text-align: center;
position: relative
}
#b {
width: auto;
height: auto;
display: inline-block;
max-height: 100%;
/*should be 300px - 2*20px = 260px */
/*no height shall be set here*/
}
#c {
background-color: green;
max-width: 300px;
max-height: inherit;
}
img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
opacity: 0.8;
height: 90%;
height: calc(100% - 40px);
}
<div id="a">
<div id="b">
<div id="c">
<img src="http://public.media.smithsonianmag.com/legacy_blog/npg_portraits_nicholson_jack_2002.jpg" />
</div>
</div>
</div>