Vertically align text next to checkbox - html

I'm trying to put a checkbox with some text next to it to explain what the checkbox does. I want to make both their position absolute since their wrapper div has other things inside.
Although I put both of their top to the same value the checkbox appears to be higher up than the text.
I made a jsfiddle to explain what I mean: https://jsfiddle.net/9jx5t4xL/.
Should I just set the top of the checkbox a bit bigger than the text or is there a better way to align them vertically?

Using position and with % values wont be a good option instead of that you can use awesome flexbox layouts to easily achieve that as:
Code Snippet:
#wrapper {
height: 200px;
width: 250px;
border: 1px solid red;
position: relative;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
min-height: 24em;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
}
label {
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
text-align: center;
}
input {
margin: 0;
vertical-align: middle;
}
<div id="wrapper">
<label>Caption for input
<input type="checkbox" />
</label>
</div>

change your css of
p{
position: absolute;
top: 40%;
display: inline-block;
left: 5%;
}
#wrapper {
height: 200px;
width: 250px;
border: 1px solid red;
position: relative;
}
p{
position: absolute;
top: 40%;
display: inline-block;
left: 5%;
}
input{
position: absolute;
top: 50%;
display: inline-block;
left: 70%;
}
<div id="wrapper">
<p>Caption for input</p>
<input type="checkbox"/>
</div>

Related

How to align any element in the midle of x and y axes?

I just want to put the numbers in the midle of the flex items but I just can't figure out how to do it. I tried many ways but none of them worked with flexbox... It is just a learning material where I experiment with flexbox so I want to stick with this display.
As I said I tried many ways so there might be some things that are not needed to be there, sorry about that.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flex</title>
<style>
* {
margin: 0px;
}
.con {
display: flex;
background-color: aquamarine;
border: 4px solid wheat;
padding: 16px;
height: 511px;
width: 95%;
align-items: center;
justify-content: center;
gap: 8px;
margin: 0px;
}
.i {
height: 60px;
width: 400px;
background-color: red;
border: 4px solid darkslategray;
flex: 1 1 auto;
}
#i2 {
flex: 2 4 auto;
}
p {
position: relative;
vertical-align: middle;
text-align: center;
margin: 0 auto;
width: 20px;
height: 20px;
}
</style>
</head>
<body>
<div class="con">
<div id="i1" class="i"><p>1</p></div>
<div id="i2" class="i"><p>2</p></div>
<div id="i3" class="i"><p>3</p></div>
</div>
</body>
</html>
You can do this by display: flex or or position: absolute
/* can used display flex */
.con > div {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
/* or used position */
.con > div {
position: relative;
}
.con > div p {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This is happening because your <div class='i'></div> elements have height of 60px in css. So they are the ones that make the whole height of the block they are inside of.
Give each of the <div class='i'></div> elements display: flex; and align-items: center;. This will center the numbers both on x and y axis.
Here is the CodePen: https://codepen.io/Juka99/pen/qBVJdNv
Maybe you can use something like this
I just add some extra flex inside con class:
.con > div {
display: flex;
align-items: center;
}
you can use display: flex; align-items:center; for .i class

Css flex layout not aligning aligning items as desired

I am trying to position Author designation under Author name, i tried few thing since theme is using flex i find it hard to make it work.
This them is using flex all over the place and if change one thing it breaks other thing.
How can i place Author Designation under the Author Name with minimal css changes
https://codepen.io/KGuide/pen/OJJBzmp
.article-container .article-thumbnail-wrapper {
height: 480px;
height: auto;
}
.article-thumbnail-info {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
position: absolute;
width: 100%;
left: 0;
bottom: 20px;
padding: 0 15px;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
}
.article-author {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
text-decoration: none !important;
}
.article-author figure {
margin: 0;
width: 50px;
height: 50px;
margin-right: 18px;
}
.article-author figure img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
Just wrapped two spans to div and aligned it to column with flex property:
https://codepen.io/Nevados/pen/mddzpYw
If the width of the image is static you can consider some margin trick. The 68px I am using is the width+margin for the image.
I removed some CSS to keep only the relevant one
.article-author {
display: flex;
flex-wrap: wrap; /* added */
/*align-items:center; removed */
text-decoration: none !important;
}
.article-author figure {
margin: 0;
width: 50px;
height: 50px;
margin-right: 18px;
}
.article-author figure img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
/* Added */
.blog-detail-author {
flex-basis: calc(100% - 68px);
margin-top: 5px;
}
.blog-detail-designation {
margin-left: 68px;
margin-top: -25px; /* This one is a bit hacky, you may need to change it based on the font or other CSS*/
}
<div class="article-thumbnail-wrapper blog-thumbnail-wrapper text-center">
<div class="article-author">
<figure class="article-author-avatar"><img alt="" src="http://themeflex.com/strucflex/en/structures/assets/img/avatar_2.jpg"></figure>
<span class="blog-detail-author">Author Name</span>
<span class="blog-detail-designation">Author Designation</span>
</div>
</div>
Try With this :
HTML
<div class="article-thumbnail-wrapper blog-thumbnail-wrapper text-center">
<div class="article-author">
<figure class="article-author-avatar"><img alt="" src="http://themeflex.com/strucflex/en/structures/assets/img/avatar_2.jpg"></figure>
<span>
<span class="blog-detail-author">Author Name</span>
<span class="blog-detail-designation">Author Designation</span>
</span>
</div>
</div>
CSS:
figure + span {
display:flex; flex-direction:column;
}

Want to make my text on custom loader responsive

I want to make my centered div text responsive on my website loader. The clue is I don't know how I am be able to do this. Im a learning coder so I hope someone can help me with my problem :)
NOTE! Load the snippet in full page so you can see the text :P
Here is the source code:
body {
overflow: hidden
}
#preloader {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url(https://www.publicdomainpictures.net/pictures/200000/velka/plain-red-background.jpg);
z-index: 99;
/* makes sure it stays on top */
}
#camera {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
/* centers the loading animation horizontally one the screen */
top: 50%;
/* centers the loading animation vertically one the screen */
background-image: url(https://svgshare.com/i/6kD.svg);
/* path to your loading animation */
background-repeat: no-repeat;
background-position: center;
margin: -150px 0 0 -150px/* is width and height divided by two */
}
#text {
line-height: 890px;
margin: auto;
text-align: center;
color: white;
font-size: 20px;
font-weight: bold;
}
<div id="preloader">
<div id="text">Website loading...</div>
<div id="camera"></div>
</div>
What you want to be using here is display: flex; as that allows you to center elements both horizontally and vertially. This is also known as a flexbox.
Some other notes regarding the code, you should have the image as an image rather than a background image in this case, and it is much easier to use background-color: red; than using an image as a background color.
body,
html {
margin: 0;
padding: 0;
}
body {
overflow: hidden
}
#preloader {
position: fixed;
background-color: red;
z-index: 99;
/* makes sure it stays on top */
width: 100%;
height: 100%;
display: flex;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
#camera {
height: 150px;
/* path to your loading animation */
background-repeat: no-repeat;
background-position: center;
background-size: contain;
display: flex;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
flex-direction: row;
align-items: center;
justify-content: center;
text-align: center;
}
#text {
height: 100px;
line-height: 890px;
text-align: center;
color: white;
font-size: 20px;
font-weight: bold;
display: flex;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
flex-direction: row;
align-items: center;
justify-content: center;
text-align: center;
}
<body>
<div id="preloader">
<img src="https://svgshare.com/i/6kD.svg" id="camera">
<div id="text">Website loading...</div>
</div>
</body>
Cheers!
Should note:
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
This just makes sure that the centring works on all browsers.
I think using flexbox here would be better idea
I have added flexbox styles for #preloader
#preloader {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url(https://www.publicdomainpictures.net/pictures/200000/velka/plain-red-background.jpg);
z-index: 99;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* makes sure it stays on top */
}
Here is the fiddle with other changes to make everything correct

Stacking Context: Putting one child div above another

I'm not quite understanding what rule of stacking context I am not understanding here. I have a 'divider' line ('divider-line') that I want to put behind a box div ('block').
Here is the HTML:
<div class="report-title">
<div class="divider-line"></div>
<div class="block">
<div class="icon">0</div>
<h1 class="text">FOO BAR</h1>
</div>
</div>
Here is the CSS(w/ scss nesting):
.report-title {
display: flex;
align-items: center;
flex-direction: column;
position: relative;
width: 100%;
margin: 100px 0;
.block {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: -100px;
z-index: 10;
height: 200px;
width: 475px;
.icon {
font-size: 9rem;
}
.text {
display: block;
}
}
.divider-line {
width: 100%;
height: 1px;
background-color: gray;
}
}
The HTML context, having 'divider-line' child come before it's sibling 'block' should put it behind no? The z-index of 10 on 'block' doesn't do anything, and I've tried putting a z-index of -1 on 'divider-line' as well (to no avail).
Any advice or direction would be great,
Without the z-index in you example, adding a background colour to the block shows that the divider is behind the block.
.report-title {
display: flex;
align-items: center;
flex-direction: column;
position: relative;
width: 100%;
margin: 100px 0;
}
.block {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
background-color: #FFF;
top: -100px;
height: 200px;
width: 475px;
}
.icon {
font-size: 9rem;
}
.text {
display: block;
}
.divider-line {
width: 100%;
height: 1px;
background-color: gray;
}
<div class="report-title">
<div class="divider-line"></div>
<div class="block">
<div class="icon">0</div>
<h1 class="text">FOO BAR</h1>
</div>
</div>

Centered responsive text

I have a jsfiddle here - http://jsfiddle.net/kw83kLmo/
It's justs a block of text that I need to center in the window.
The text needs a set width because I don't want it to be the full width of the container.
When the window gets smaller the text needs to stay in the center but get narrower.
In the example here it stays centered and responds until it get's to 600px then just stays that width.
I know I have set that width but I did that to center it
<div class="container-fluid ">
<div class="hero">
<div class="hero_heading text-center">
<h1>This is a heading This is a heading This is a heading </h1>
</div>
</div>
</div>
Update your h1 style like below.
.hero_heading h1 {
border: 1px solid red;
color: white;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
max-width: 600px;
}
DEMO
Edit on your code
.hero_heading h1{
border: 1px solid red;
color: white;
//top: 0; left: 0; bottom: 0; right: 0;
width: 600px;/*added*/
max-width:80%;/*to leave spaces around your text on responsive*/
margin:auto;/*changed*/
}
You no need to position your element for making it unless if you need
NOTE: Remove your position:relative; from .hero
DEMO
Edit your code like this :
.hero_heading h1{
border: 1px solid red;
color: white;
margin: 0 auto;
max-width: 600px;
position: absolute;
bottom: 0;
}
Demo
JSfiddle Demo
I took the positioning off the h1 and put it on the wrapping div.
CSS
.hero{
background-image: url(http://placehold.it/800x300);
background-size: cover;
position: relative;
height: 400px;
}
.hero_heading {
border: 1px solid red;
color: white;
position: absolute;
left: 50%;
bottom: 0;
-webkit-transform:translateX(-50%);
transform:translateX(-50%);
max-width: 600px;
}
I'd go with:
.hero_heading{
border: 1px solid red;
color: white;
position: absolute;
width:50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
CSS flex can do the magic for you in a compatible way across most popular browsers including IE. Take a look at this JSFiddle
.hero{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-box-pack: center;
-moz-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-box-align: center;
-moz-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-image: url(http://placehold.it/800x300);
background-size: cover;
height: 400px;
}
.hero_heading h1{
border: 1px solid red;
color: white;
max-width: 600px;
}