Why wont this css code center the two elements on the page? - html

I want to centre the content on my page. I have a button and a paragraph of text on my page.
I'm new to css so I don't know what I should try.
body {
margin: 0 auto;
}
button {
top: 921px;
left: 400px;
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
p.text {
top: 2639px;
left: 1014px;
width: 398px;
height: 88px;
text-align: left;
font: Regular 20px/27px Segoe UI;
letter-spacing: 0;
color: #39316c;
opacity: 1;
}
<button>Click Me</button>
<p class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis
necessitatibus sed officiis repellat illo ratione libero recusandae
tempora dolorum excepturi. Velit odio mollitia nam vel, nulla nostrum
officiis exercitationem esse!
</p>

You can use display: flex;
body,
html {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
p {
text-align: center;
}
button {
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
<div class="container">
<button>Click Me</button>
<p class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis necessitatibus sed officiis repellat illo ratione libero recusandae tempora dolorum excepturi. Velit odio mollitia nam vel, nulla nostrum officiis exercitationem esse!
</p>
</div>

Used flexbox for that:
body {
margin: 0 auto;
}
.wrapper {
display: flex;
align-items:center;
flex-direction: column;
justify-content: center;
height: 100vh;
}
button {
top: 921px;
left: 400px;
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
p.text {
top: 2639px;
left: 1014px;
width: 398px;
height: 88px;
text-align: left;
font: Regular 20px/27px Segoe UI;
letter-spacing: 0;
color: #39316c;
opacity: 1;
}
<div class="wrapper">
<button>Click Me</button>
<p class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis
necessitatibus sed officiis repellat illo ratione libero recusandae
tempora dolorum excepturi. Velit odio mollitia nam vel, nulla nostrum
officiis exercitationem esse!
</p>
</div>

You can use flexbox. Take a look at this https://css-tricks.com/snippets/css/a-guide-to-flexbox/.
Here is the solution for your current problem.
html,body, .wrapper {
margin: 0;
height: 100%;
}
.wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
button {
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
p.text {
width: 398px;
height: 88px;
text-align: left;
font: Regular 20px/27px Segoe UI;
letter-spacing: 0;
color: #39316c;
opacity: 1;
}
<div class="wrapper">
<button>Click Me</button>
<p class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis necessitatibus sed officiis repellat illo ratione libero recusandae tempora dolorum excepturi. Velit odio mollitia nam vel, nulla nostrum officiis exercitationem esse!
</p>
</div>

You can use flexbox technique as they say in other comments or you can add some lines to you code.
Add to body text-align: center; and add to p.text margin: 0 auto;
Your CSS code will become like that:
body {
margin: 0 auto;
text-align: center;
}
button {
top: 921px;
left: 400px;
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
p.text {
margin: 0 auto;
top: 2639px;
left: 1014px;
width: 398px;
height: 88px;
text-align: left;
font: Regular 20px/27px Segoe UI;
letter-spacing: 0;
color: #39316c;
opacity: 1;
}
See demo here

Just add
text-align: center;
and remember to remove the
text-align: left;
from your p.text

Some things to consider;
You are using top and left rules without giving a position rule value other than static (the default value). In your case you should use position: absolute
Don't use positioning with absolute values, if you don't need to. Absolute values (px unit) are not flexible enough to adjust to window size. Use relative units like % or vw/vh
For your contents to act as a group of elements, you should wrap it with a container element, which you then position accordingly (I used <div class="center"></div>)
I positioned this center container element absolutely using top: 50vh and left: 50vw. 100vh is 100 per cent of the view height, i.e. the inner window height and 100vw is 100 per cent of the view width likewise
This positioning is not enough, because it will position the top left corner of the .center element at the center of the window. We need to have a correction. I use transform: translate(-50%, -50%). This will shift an element 50% of its width to the left and 50% of its height to the top, making it perfectly centered.
There are alternatives like flexbox mentioned in the other posts, but this one is a little bit more straightforward for a beginner like you. Nonetheless it is recommended to learn flexbox. You might want to read A complete guide to flexbox on css-tricks.com
body {
margin: 0 auto;
}
.center {
position: absolute;
top: 50vh;
left: 50vw;
transform: translate(-50%, -50%);
}
button {
width: 180px;
height: 60px;
background: #ff9b52 0% 0% no-repeat padding-box;
border-radius: 30px;
opacity: 1;
}
p.text {
width: 398px;
height: 88px;
text-align: left;
font: Regular 20px/27px Segoe UI;
letter-spacing: 0;
color: #39316c;
opacity: 1;
}
<div class="center">
<button>Click Me</button>
<p class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis necessitatibus sed officiis repellat illo ratione libero recusandae tempora dolorum excepturi. Velit odio mollitia nam vel, nulla nostrum officiis exercitationem esse!
</p>
</div>

Related

How to make top and left border of a div slanted

.container{
max-width: 80%;
margin:auto;
}
.section1{
background: #541A81;
padding: 60px 0;
}
.content{
background:#ffffff;
/* clip-path: polygon(0 90px, 100% 0, 100% 100%, 0 100%); */
border: 7px dashed #FFFD54;
border-radius: 50px;
padding: 168px 60px 92px;
transform: skew(10deg, 0);
}
<div class="section1">
<div class="container">
<div class="content">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit ad hic distinctio laboriosam iste neque quibusdam, adipisci sed magni explicabo nemo delectus nesciunt numquam non ducimus enim voluptatem ipsam! Esse!
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Possimus dolorum unde debitis quos velit repudiandae. Explicabo, veniam, totam? Consectetur eum ab veniam, consequatur nemo, beatae soluta blanditiis quas quos dicta.</p>
</div>
</div>
</div>
I tried skewing it, but it seems to be skewing the whole axis, I have also tried the clip-path approach (code is commented there), however, this clip method is also removing the border as well.
I certainly wouldn't recommend this, however you theoretically could achieve something similar using pseudo elements - albeit it could get a bit messy with the perspective css property to skew in 3D.
Using pseudo elements, you could stop the actual text becoming skewed as you only apply these to the pseudo elements rather than the whole element.
body {
background: #541A81;
}
.wrap,
.light {
perspective: 300px;
}
.light {
position: absolute;
top: 50px;
left: 50px;
display: inline-block;
min-height: 200px;
width: 500px;
border-radius: 30px;
padding: 30px;
}
.light:before,
.light:after {
content: "";
position: absolute;
border-radius: inherit;
}
.light:after {
top: 30px;
left: 10px;
height: calc(100% - 20px);
width: 90%;
background: rgba(0, 0, 0, 0.8);
transform: rotateY(10deg) rotate(5deg);
z-index: -5;
}
.light:before {
top: 0;
left: -50px;
height: 100%;
width: calc(100% + 55px);
background: white;
border: 7px dashed #FFFD54;
box-sizing: border-box;
transform: rotateY(-5deg) rotateX(2deg);
z-index: -2;
}
<div class="wrap">
<div class="light">
hello! This text won't be skewed or messed up.
</div>
</div>

How to remove space between div elements? [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 2 years ago.
I have two div elements and I want to remove the space between them, which is filled with the background color at the moment. This is what it looks like. I want to remove the space between the green section and where the background image ends for the first div element. Here is the HTML for the page:
<body style="background-color: #c5ffff">
<div class="main-search hero-image">
Log Out
<div class="welcome-text">
<div class="title">Welcome to Ripple.</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A aliquam commodi doloremque esse itaque iusto, labore laborum maxime odio, quidem quos, repellat rerum? Ab, asperiores aspernatur assumenda atque distinctio dolor dolore eveniet facilis, id illo ipsa ipsam minus nemo nobis porro quam quia quibusdam quis ratione rerum soluta suscipit temporibus vel vitae voluptate. Accusamus dignissimos ea esse expedita itaque mollitia nobis, numquam odio, quaerat qui vel voluptatibus?</div>
<button class="search_button" >Search for a location</button>
<button class="search_button">Search for a song</button>
</div>
</div>
<div class="main-left">
<div class="title">Collections Near Me</div>
<div>
</div>
</body>
And the CSS:
.main-left {
vertical-align: top;
margin-top: 0;
margin-left: 0;
position: relative;
background-color: #85dcba;
text-align: left;
width: 100%;
float: top;
left: 50%;
transform: translate(-50%);
}
.main-search {
vertical-align: top;
background-color: #d2fdff;
text-align: left;
margin: 0;
padding-top: 2em;
position: relative;
top: 0;
width: 100%;
left: 50%;
transform: translate(-50%);
}
.hero-image {
background-image: url("main_background.jpg");
background-size: cover;
background-repeat: no-repeat;
}
.title {
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0.5em;
margin-left: 10px;
margin-bottom: 0.5em;
}
.content {
color: black;
font-family: Roboto, sans-serif;
font-size: 1em;
margin: 1em;
}
.welcome-text {
top: 5%;
left: 5%;
max-width: 35%;
}
There is a gap because div is block element, if you want to remove the gap between div use display: inline-block to remove it.
body > div { display: inline-block; }
This will display an element as an inline-level block container. Please refer to CSS Display
You could try setting the margin values of the elements manually. I see you've set the padding- Which refers to the internal distance of contents to edge, but not the margin- which refers to the distance between seperate elements.
Also, nice looking design so far!
Display flex has a nifty way of removing undesirable whitespace from the html
So place flex on the wrapper of these elements, in this case the body tag
css
body {
display: flex;
flex-direction: column;
}
Set margin-top of the .title to zero and for space between the .main-search and .title, give padding-bottom to .main-search as well. Below is how it will look like:
body{
background-color: #c5ffff;
}
.main-left{
vertical-align: top;
margin-top: 0;
margin-left: 0;
position: relative;
background-color: #85dcba;
text-align: left;
width: 100%;
float: top;
left: 50%;
transform: translate(-50%);
}
.main-search{
vertical-align: top;
background-color: #d2fdff;
text-align: left;
margin: 0;
padding: 2em 0;
position: relative;
top: 0;
width: 100%;
left: 50%;
transform: translate(-50%);
}
.hero-image{
background-image: url("https://picsum.photos/500/500");
background-size: cover;
background-repeat: no-repeat;
}
.title{
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0;
margin-left: 10px;
margin-bottom: 0.5em;
}
.content{
color: black;
font-family: Roboto, sans-serif;
font-size: 1em;
margin: 1em;
}
.welcome-text{
top: 5%;
left: 5%;
max-width: 35%;
}
<div class="main-search hero-image">
Log Out
<div class="welcome-text">
<div class="title">Welcome to Ripple.</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A aliquam commodi doloremque esse itaque iusto, labore laborum maxime odio, quidem quos, repellat rerum? Ab, asperiores aspernatur assumenda atque distinctio dolor dolore eveniet facilis, id illo ipsa ipsam minus nemo nobis porro quam quia quibusdam quis ratione rerum soluta suscipit temporibus vel vitae voluptate. Accusamus dignissimos ea esse expedita itaque mollitia nobis, numquam odio, quaerat qui vel voluptatibus?</div>
<button class="search_button" >Search for a location</button>
<button class="search_button">Search for a song</button>
</div>
</div>
<div class="main-left">
<div class="title">Collections Near Me</div>
<div>
remove margin-top:0.5em; on your title class.
.title {
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0.5em; <-- REMOVE
margin-left: 10px;
margin-bottom: 0.5em;

CSS - Unable to align image and text together inside div

I'm trying to show message within a div with icon on the left.
Expected result is icon should always adjacent to text and together they need to be aligned at bottom-center of div.
I'm using :after pseudo element. Keeping position: absolute of icon didn't help since that needs manually adjusting the icon position relative to text.
Here is the CSS.
.parent{
font-weight: 500;
height: 65px;
text-align: center;
padding: 15px 0 10px;
margin: auto;
display: block;
width: 80%;
font-size: 12px;
overflow: hidden;
position: relative;
}
.parent > div {
float: none;
/* display: table-cell; */
vertical-align: middle;
position: absolute;
bottom: 0;
width: 100%;
}
.msg:after {
content: '';
background: url(data:image/...);
height: 16px;
width: 16px;
display: block;
position: absolute;
top: 0;
padding-right: 5px;
left: 108px;
}
And markup:
<div class="parent">
<div class="msg">text goes here</div>
</div>
Flexbox can do that:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
.parent {
font-weight: 500;
margin: auto;
padding: 1em;
width: 80%;
font-size: 12px;
overflow: hidden;
position: relative;
border: 1px solid red;
}
.msg {
display: flex;
}
.msg p {
padding-left: 1em;
}
.msg:before {
content: "";
height: 16px;
flex: 0 0 16px;
background: red;
border-radius: 100%;
}
<div class="parent">
<div class="msg">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae numquam unde, eum sequi expedita fugiat ipsa exercitationem nesciunt libero repellendus aperiam excepturi, dolorem repudiandae eveniet alias perspiciatis, vero veniam tempora natus magnam
itaque quos. Nemo sit nisi, veniam mollitia fugit eaque reiciendis ex doloribus rem et suscipit debitis commodi sapiente.</p>
</div>
</div>

How to properly format a bleeding image so that it appears in proper ratio

I have a container that is keeping the content to a max-width of 1200px and want to have a main image that is placed as the background of the .main_prize_section and can expand on bigger screens while staying within the proper aspect ratio. the main wrapper image is like a container for the main content, so it cant appear distorted.
here is the html code
<section class="main_prize_section">
<div class="container">
<div>
<div class="row">
<div class="flex_row">
<div class="col_1_2">
<div>
<h1>TEST HEADER</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos quae quo fugit asperiores, aperiam perspiciatis dolores consectetur quam nemo, laudantium et doloribus officia voluptates eveniet optio ad ab quaerat natus!</p>
</div>
<div>
<h2>sub header</h2>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Corrupti accusantium molestiae id a quae pariatur? Sequi ipsum quos libero aspernatur tempore molestiae facere, porro, autem perferendis, atque aut earum reiciendis.</p>
</div>
</div>
<div class="col_1_2 main_prize_product"><img src="images/english/prize_images.png" alt="" width="100%"/></div>
</div>
</div>
</div>
</div>
</section>
here is the css code
.container {
max-width: 1200px;
margin: 0 auto;
}
.flex_col {
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
flex-direction: column;
display: -webkit-flex;
display: -moz-box;
display: flex;
}
.flex_row {
display: -webkit-flex;
display: -moz-box;
display: flex;
}
.col_1_2 {
width: 50%;
}
.main_prize_section {
text-align: center;
background: url("../images/prize_background.png");
background-repeat: no-repeat;
background-size: 100%;
width: 100%;
background-position: 50% 50%;
text-align: center;
}
.main_prize_section img {
width: 50%;
margin: 0 auto;
}
.main_prize_section .flex_row {
padding: 2%;
}
.main_prize_section h1 {
color: pink;
font-size: 4rem;
font-weight: 700;
}
.main_prize_section h2 {
color: pink;
font-size: 2.5rem;
font-weight: 700;
}
.main_prize_section .col_1_2 div:nth-child(2) {
padding: 2%;
}
.main_prize_section p:nth-child(2) {
padding: 2%;
}
.main_prize_section p span:nth-child(1) {
color: gold;
}
.main_prize_section p span:nth-child(2) {
color: pink;
}
.main_prize_section p span:nth-child(3) {
color: red;
}
.main_prize_section p span:nth-child(4) {
color: blue;
}
the main wrapper image that bleeds out is 1366x800.
See if this helps you: https://codepen.io/panchroma/pen/jxxoqO
The important bit is the CSS lines 36 - 39
.main_prize_section {
...
background:url("../images/prize_background.png");
background-repeat: no-repeat;
background-size:cover; /* could also be background-size:contain; */
background-position:50% 50%; /* adjust to fit your design and the image */
}
For the background size, use either 'cover' or 'contain', and in combination with the background-position values, find a combination of settings that work for your specific design and image.
Good luck!

How to display the page part after fixing the header and the footer

I want to display my website by three parts: header, page and footer. Now I can fixed my header and my footer, but the header shields a part of page and the page dont put down my footer(some content of my page displays behind my footer )
My template code:
<div class="fixed-header">
<...>
</div>
<div class="page">
<...>
</div>
<div class="fixed-footer">
<...>
</div>
My Css is :
.fixed-header {
position: fixed;
top: 0;
left: 0;
z-index: 2;
width: 100% !important;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
color: white;
text-align: center;
}
Anyone knows how to fix this problem?
Add padding on the top and bottom of the .page
.page {
padding-top: x // height of header
padding-bottom: x // height of footer
}
.fixed-header {
position: fixed;
top: 0;
left: 0;
z-index: 2;
background: red;
width: 100% !important;
height: 20px;
}
.fixed-footer {
background: blue;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
color: white;
text-align: center;
height: 20px;
}
.page {
margin: 20px 0;
background: pink;
width: 100px;
}
#media (max-width: 767px) {
.fixed-header, .fixed-footer {
height: 40px;
}
.page {
margin: 40px 0;
}
}
<div class="fixed-header">
<...>
</div>
<div class="page">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi odio, est rerum quod nisi ipsam laboriosam quam eius doloremque exercitationem. Laboriosam aut consequatur atque natus beatae explicabo sunt. Quam, labore.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi odio, est rerum quod nisi ipsam laboriosam quam eius doloremque exercitationem. Laboriosam aut consequatur atque natus beatae explicabo sunt. Quam, labore.
</div>
<div class="fixed-footer">
<...>
</div>