Is there another way of placing text over an image beside using position: absolute; ?
Working with position: absolute; on different screen sizes doesn't seem like the thing i want to do.
I kept looking for an answer but all i can find about it is the classic : use position absolute.
HTML:
<div class="header-container">
<img src="https://media.sproutsocial.com/uploads/2018/04/Facebook-Cover-Photo-Size.png" alt="" id="header-img" />
<p class="img-text">Make it possible!</p>
</div>
CSS:
#header-container {
max-height: 800px;
display: flex;
align-items: center;
justify-content: center;
}
.text-header {
position: absolute;
font-family: "Gayathri", sans-serif;
font-weight: 1000;
top: 45%;
left: 45%;
}
Try this instead,
add this style to img
#header-img{width:100%;height:auto;}
if you align text in center by
.text-header{
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#header-container {
max-height: 800px;
display: flex;
align-items: center;
justify-content: center;
}
#header-img{width:100%;height:auto;}
.text-header {
position: absolute;
font-family: "Gayathri", sans-serif;
font-weight: 1000;
top: 50%;
left: 50%;
color: white;
font-weight:800;
font-size: 30px;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
<div class="header-container">
<img src="https://media.sproutsocial.com/uploads/2018/04/Facebook-Cover-Photo-Size.png" alt="" id="header-img" />
<p class="text-header">Make it possible!</p>
</div>
Technically, no.
However, you can combine a text element with position: absolute inside of an element with position: relative. This will position the element in an absolute manner (based on pixels/percentages relative to the parent element itself, rather than the entire document.
Expanding on this, you could include an image in said element (either as a background image or as another absolutely positioned div, and use percentages to position your text relative to the container.
#rel{
display: block;
position: relative;
width: 400px;
height: 600px;
}
img{
width: 100%;
height: 100%;
}
h1{
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div id="rel">
<img src="https://picsum.photos/400/600">
<h1>Hello world!</h1>
</div>
Related
I have an svg icon for a website that I would like to make into a css shape so that I can make a custom effect on hover.
I am using pure CSS for this and am not sure I am approaching the problem correctly.
.wrapper {
position: relative;
height: 100vh;
}
.square {
height: 40px;
width: 40px;
background-color: black;
transition: all .2s;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.square:before {
content: "";
width: 100%;
height: 2px;
background-color: #fff;
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.square:after {
content: "";
width: 100%;
height: 2px;
background-color: #fff;
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform-origin: 50%;
transform: translate(-50%, -50%) rotate(90deg);
}
.square:hover {
transform: scale(1.2) translate(-50%, -50%);
}
.square:hover .square:before {
transform: scale(1.2);
}
.square:hover .square:before, .square:hover .square:after {
height: 4px;
}
<div class="wrapper">
<div class="square"></div>
</div>
https://codepen.io/Portismouth/pen/ZEEXeVP
so far I am using a simple square with a cross created using the :before and :after psuedo-selectors. On hover, Im trying to make the square bigger and the lines thicker to give the impression i'm going for.
It's basically a 2 x 2 grid of squares that I want to expand out from the middle of the square on hover. Should I create a square with four separate squares or continue with my approach so far.
Thanks in advance.
Your code works correctly. If you want to hover and it'll expand from middle of square, don't use translate(-50%, -50%); when hover on square
.wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.square {
height: 40px;
width: 40px;
background-color: black;
transition: all .2s;
}
.square:before {
content: "";
width: 100%;
height: 2px;
background-color: #fff;
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.square:after {
content: "";
width: 100%;
height: 2px;
background-color: #fff;
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform-origin: 50%;
transform: translate(-50%, -50%) rotate(90deg);
}
.square:hover {
transform: scale(2);
}
<div class="wrapper">
<div class="square"></div>
</div>
I am trying to put my things in the tag at the center of the screen, but it is not working.
How can I put it in center of screen using HTML and CSS?
Where I am -
Code-
Result -
Assuming you have a .child element you want to center inside a .parent element, You have 2 options:
1) Flexbox - You can use the following css:
.parent {
display: flex;
flex-direction:
flex-wrap: nowrap;
justify-content: center;
align-content: center;
align-items: center;
}
.child {
flex: 0 1 auto;
align-self: auto;
text-align: center; /*optional*/
}
2) Absolute positioning:
.parent {
position:relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center; /*optional*/
}
It would be much easier if you provided some code you have written, but if you want to center a tag you can do this:
CSS:
#parentTag{
position: relative;
}
#myTagIWantToBeCentered{
position: absoulte;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
HTML:
<div id="parentTag">
<div id="myTagIWantToBeCentered">
<!--stuff here-->
</div>
</div>
If you need to center content horizontally and vertically use this:
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box_text {
color: #ffffff;
font-size: 20px;
font-weight: bold;
position: absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
.box {
width: 100px;
height: 100px;
background-color: red;
margin:3px;
position: relative;
}
<div class="box"><span class="box_text">0</span></div>
If you are running on Bootstrap, you can easily add a class text-center. Normally, an (inline)-block element can be centered with css
.center { margin: 0 auto; }
It would be best if you could provide your code so we can suggest base on it.
You can try using CSS Flexbox. If you add
.class {
display: flex;
align-items: center; //Aligns the content vertically
justify-content: center; //Aligns the content horizontally
}
to the parent html element.
For an example, have a look at this js-fiddle https://jsfiddle.net/adamturner93/brjotz2y/1/.
You can try using HTML Tag if you are not using HTML5.
Ex-
<center>This text will be center-aligned.</center>
If you are using HTML5 then you need to use css to modify.
<element>.<class> {
margin-left: auto;
margin-right: auto;
width: 6em
}
I'm trying to center the text in the middle of JQM page, vertically and horizontally. Using 1.4.5, default theme.
In my .css I have:
.splashDiv {
position: absolute;
top: 50%;
height: 50%;
width: 100%;
text-align: center;
padding:0;
}
HTML
<div class="splashDiv" data-role="page">
Please wait...
</div>
The result is:
The text is vertically centered only if I remove top: 0 directive in developer tools(although not perfectly centered).
My question is what is the proper way according to JQM architecture to have what I want? I am not looking for quick/dirty workaround, unless there is no other way.
UPDATE
Put your splash div within a jQM page instead of making it the page:
<div data-role="page" id="page1">
<div id="cont" role="main" class="ui-content">
<div class="splashDiv" >
Please wait...
</div>
</div>
</div>
.splashDiv {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
DEMO
.splashDiv {
position: absolute;
top: 50%;
width: 100%;
text-align: center;
transform: translateY(-50%);
padding:0;
}
Use this it will align your text vertically center.
Please Try it.
Your HTML
<div data-role="page" id="page1">
<div id="cont" role="main" class="ui-content">
<div class="splashDiv" >
<span>Please wait...</span>
</div>
</div>
</div>
Your CSS
.splashDiv {
position: absolute;
top: 50%;
left: 50%;
height: 50%;
width: 100%;
text-align: center;
padding:0;
overflow: hidden;
transform: translate(-50%, -50%)
}
.splashDiv span {
margin: 0;
background: yellow;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
DEMO
You need to use the following css:
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
I need to build a circle shape (in css) which has 2 lines of text that could change in length based on translation selected and always centered.
So far I have this:
h3 {
background-color: #fcd141;
border-radius: 50%;
padding: 12px 5px 5px 5px;
margin-top: 30px;
width: 20%;
height: 20%;
}
<h3>
<span style="vertical-align: middle;">98%</span>
<span style="margin-top: -4px; display: block;">Ratingfasdasfasfsad</span>
</h3>
The circle needs to respond dynamically to the length of the text keeping the aspect ration intact.
You can have a look at the code as in your code it looks like an ellipse to me
.circle-text {
width: 50%;
padding 10px;
}
.circle-text:after {
content: "";
display: block;
width: 100%;
height: 0;
padding-bottom: 100%;
background: #4679BD;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
}
.circle-text div {
float: left;
width: 100%;
padding-top: 50%;
line-height: 1em;
margin-top: -0.5em;
text-align: center;
color: white;
}
<div class="circle-text">
<div>I'm asddddddssssssssssssssssssasdasdashd asfafjsldfashdfisdpf sdjf pe!</div>
</div>
You could use vw (view width units) for this:
note
transform is used for vertical alignment only.
h3 {
background-color: #fcd141;
border-radius: 50%;
padding: 20px;
margin-top: 30px;
width: 20vw;
height: 20vw;
text-align: center;
word-wrap: break-word;
position: relative;
}
h3 span {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>
<span> 98% <br />
Ratingfasdasfasfsad</span>
</h3>
I was also able to remove your inline styling, and combined your two spans into one.
I'm trying to position an image in determined height.
Just like this main image:
http://littlelines.com/
I have this:
HTML
<div class="present100">
<img id="imagem" src="teste.jpg" />
</div>
CSS
.present100 {
width: 100%;
height: 620px;
background-color: #333;
position: absolute;
overflow: hidden;
top: 0;
left: 0;
margin: auto;
}
#imagem {
position: relative;
width: 100%;
min-width: 1300px;
min-height: 100%;
}
I don't know how to center the image on resize just like the http://littlelines.com/
Not sure why you have absolute position on the .present100 element, but you will have to absolutely position the image as well
#imagem {
position: absolute;
width: 100%;
min-width: 1300px;
min-height: 100%;
left:50%;
top:50%;
-ms-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
Demo at http://jsfiddle.net/gaby/vMyvc/