Align text horizontally and vertically with CSS inset property [duplicate] - html

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
How to center a "position: absolute" element
(31 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Closed 24 days ago.
I want to my text to be centered both horizontally and vertically within a div.
I'm using the inset property to fill up the entire div with my p tag.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div class="wrapper">
<div class="zone">
<p>Hello World</p>
</div>
</div>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: bordered-box;
}
.wrapper {
background: red;
}
.zone {
min-height:10em;
text-align: center;
position: relative;
}
.zone p {
position: absolute;
inset:0;
}
However I don't get it to work. Can anyone give me a hint on how to achive this with the inset property?

If you require the inset property you can do the following, but there are many better ways to center text in a div.
* {
margin: 0;
padding: 0;
box-sizing: bordered-box;
}
.wrapper {
background: red;
}
.zone {
min-height: 10em;
text-align: center;
position: relative;
}
.zone p {
font-size: 20px;
position: absolute;
inset: calc(50% - 10px) 0 0 0; /* The minus value needs to be half the font-size */
}
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div class="wrapper">
<div class="zone">
<p>Hello World</p>
</div>
</div>
</body>
</html>

Related

Why there is space above p tag in position absolute condition? [duplicate]

This question already has answers here:
Remove space above and below <p> tag HTML
(7 answers)
Closed 9 months ago.
<html>
<head>
<style>
body{
margin: 0px;
}
.outer{
width: 500px;
height: 300px;
position: relative;
}
.innerbg{
position: absolute;
background-image: url('https://images.pexels.com/photos/1762851/pexels-photo-1762851.jpeg?cs=srgb&dl=pexels-ann-h-1762851.jpg&fm=jpg');
background-size: cover;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
}
</style>
</head>
<body>
<div class="outer">
<div class="innerbg">
<p>Some content</p>
</div>
</div>
</body>
</html>
You can notice, there is space above the p tag, however, when the position absolute is removed by from the class innerbg, the p tag has no more top space with the view port, why is that?
The space above the p tag can be removed by using the following CSS:
.p{
margin-top: 0px;
}

CSS Center Button Image In Div [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Center image using text-align center?
(28 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
I want to have a button image centred in a div of fixed size both horizontally and vertically.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#fixed-size {
width: 800px;
height: 600px;
border: 1px solid red;
display: block;
}
button {
padding: 0;
margin: auto;
display: block;
width: 256px;
height: 256px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div id="fixed-size">
<button>
<img src="aqua.png">
</button>
</div>
</body>
</html>
What should I write instead of button { /* ... */ } for the button to be centered in a div and not in a whole page?
ive changed your code to where the button is relative and the parent makes sure the button inside is centered, using flexbox styling
justify-content: center
and
align-items: center
make sure that all the children of the display flex parent are aligned in the middle both horizontally and vertically.
if you want to learn more about it, check this website: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#fixed-size {
width: 800px;
height: 600px;
border: 1px solid red;
display: flex;/* changed */
justify-content: center;/* added */
align-items: center;/* added */
}
button {
padding: 0;
margin: auto;
display: block;
width: 256px;
height: 256px;
position: relative;/* changed */
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div id="fixed-size">
<button>
<img src="aqua.png">
</button>
</div>
</body>
</html>

Using the width of parent element [duplicate]

This question already has answers here:
Setting the width of inline elements
(7 answers)
How to remove the space between inline/inline-block elements?
(41 answers)
How to make an element width: 100% minus padding?
(15 answers)
Closed 2 years ago.
How do i exactly use the parent's element width in css? I know this is a basic question but i'm using width: 50% on css expecting that the width would be 50% of the parent but instead it uses less than 50%.
In this example, i colored the parent as green so i could see the width and i was expecting each tab to be distributed along the green block.
Link to the example: https://jsfiddle.net/zfw81ojq/6/
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BPO Services</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="content" id="main-content" onclick="appendEl()">
<div class="tab-selector">
<div class="tablinks" onclick="openForm()"><h2>1st Tab</h2></div>
<div class="tablinks" onclick="openForm()"><h2>2nd Tab</h2></div>
</div>
</div>
</body>
</html>
.content {
position: absolute;
width: 60%;
padding: 120px auto 120px;
top: 30%;
margin-left: 20%;
}
.content .tab-selector {
height: 25px;
background-color: green;
}
.content .tab-selector .tablinks {
display: inline;
padding: 20px;
width: 50%;
text-align: center;
}
.content .tab-selector .tablinks h2 {
font-size: 20px;
display: inline;
}

Why isn't the png image centered within the parent div? [duplicate]

This question already has answers here:
How to center a "position: absolute" element
(31 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Closed 3 years ago.
I am trying to center a image within a container.
According to what I've understood, setting a container's position as "relative" that has a property of text-align set to center should
center block-level elements vertically that has their position
property set to absolute. However, why isn't this the case with my
code?
.first-container {
background-color: yellow;
height: 100%;
position: relative;
text-align: center;
width: 100%;
}
.mountain {
position: absolute;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Playground</title>
</head>
<body>
<div class="first-container">
<img class="mountain" src="images/mountain.png" alt="">
</div>
<div class="second-container">
</div>
<div class="third-container">
</div>
</body>
</html>
I expected that the mountain image would be at the center of the first container that I set the background color to yellow for ease of distinction.
When you use position: absolute on an image in a container with position: relative, your image is at the center of your container. Or, more specifically, the top-left pixel of your image is as the center.
In order to center the image so that the center of the image is in the center of the containing element, you want to set a margin-left of negative half of the image's width. This can be seen in the following, with an image that's 100px wide, with margin-left: -50px:
.first-container {
background-color: yellow;
height: 100%;
position: relative;
text-align: center;
width: 100%;
}
.mountain {
position: absolute;
margin-left: -50px;
}
<div class="first-container">
<img class="mountain" src="https://placehold.it/100" alt="">
</div>
And assuming you set the width on the image itself, you can actually make use of a combination of CSS variables and calc() in order to determine this margin, with width: var(--image-width) and margin-left: calc(var(--image-width) / -2) on the image.
:root {
--image-width: 100px;
}
.first-container {
background-color: yellow;
height: 100%;
position: relative;
text-align: center;
width: 100%;
}
.mountain {
position: absolute;
width: var(--image-width);
margin-left: calc(var(--image-width) / -2);
}
<div class="first-container">
<img class="mountain" src="https://placehold.it/100" alt="">
</div>
your code is right but has a little bit bug here, you need to set height property to some pixels then you can see the image. For example:
.first-container {
background-color: yellow;
height: 400px;
position: relative;
text-align: center;
width: 100%;
border: 1px solid black;
}
added
left: 50%;
transform: translateX(-50%);
to .mountain. Hope this helps you. Thanks
.first-container {
background-color: yellow;
height: 100%;
position: relative;
width: 100%;
}
.mountain {
left: 50%;
transform: translateX(-50%);
position: absolute;
height: 100px;
width: 100px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Playground</title>
</head>
<body>
<div class="first-container">
<img class="mountain"src="https://upload.wikimedia.org/wikipedia/commons/4/4e/Artesonraju3.jpg" alt="">
</div>
<div class="second-container">
</div>
<div class="third-container">
</div>
</body>
</html>

How am I supposed to move this text inside this div to bottom center? [duplicate]

This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 4 years ago.
CSS:
body {
background-color: #bad455;
}
.basic {
background-color: blue;
color: white;
line-height: 30px;
top: 0px;
left: 0px;
height: 50%;
width: 150px;
position: absolute;
border: 8px solid red;
}
.container {
}
.basic > p {
top: 50%;
position: relative;
}
HTML:
<body>
<div class="container">
<div class="basic">
<p>This text should move</p>
</div>
</div>
<!-- Further code... -->
</body>
Sorry but a stupid question. I'm not sure why this text doesn't go down 50% inside this div? I've been trying to use vertical-align, padding-top, text-center to 50%, but they didn't work that well and it always goes more than 50%. So is there any way to push this text inside to the center? Maybe its a position problem or something? I've been trying to solve this for quite a while but can't figure it out. Thanks in advance.