So i'm trying to center my button and my image so when the page gets smaller it should still be aligned in the middle. Also my button is not centering.
i'm trying to make it look like : https://www.unrealengine.com
Try moving the page around and see what happens.
.header {
width: 100%;
height: auto;
}
.header img {
width: 100%;
height: auto;
z-index: -1;
}
.header h1 {
margin: 0;
height: auto;
width: auto;
position: absolute;
top: 20%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
}
.myButton {
display: inline-block;
text-align: center;
position: relative;
height: auto;
background-color: #10b4f5;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 17px;
padding: 16px 31px;
text-decoration: none;
}
.myButton:hover {
background-color: #008fc7;
}
.myButton:active {
position: relative;
top: 1px;
}
<div class="header">
<img src="images/background2.jpg" alt="background">
</div>
EXPLORE
The easiest way to center something vertical and horizontal is using display flex. With align-items and justify-content this can be done quite easily. For more information see: A Complete Guide to Flexbox
header {
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
}
<header>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<button>CTA Here</button>
</header>
EDIT:
This code snippet is based on OP's own code. There is no need to use a div with a header class when you can use the HTML5 header tag.
header {
background-image: url(images/background2.jpg);
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
}
.myButton {
background-color: #10b4f5;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 17px;
padding: 16px 31px;
text-decoration: none;
}
.myButton:hover {
background-color: #008fc7;
}
<header>
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
EXPLORE
</header>
Try this code.
HTML
<div class="header-wrap">
<div class="header">
<img src="images/background2.jpg" alt="background">
</div>
EXPLORE
</div>
CSS
.header-wrap {
text-align: center;
}
.header {
width: 100%;
height: auto;
}
.header img {
width: 100%;
height: auto;
z-index: -1;
}
.header h1 {
margin: 0;
height: auto;
width: auto;
position: absolute;
top: 20%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: white;
}
.myButton {
display: inline-block;
text-align: center;
position: relative;
height: auto;
background-color: #10b4f5;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 17px;
padding: 16px 31px;
text-decoration: none;
}
.myButton:hover {
background-color: #008fc7;
}
.myButton:active {
position: relative;
top: 1px;
}
The alignment of text is specified in the element the text is contained in, so in this case you should specify text-align: center;in the element which contains the button. Like so:
<div class="button-container">
EXPLORE
</div>
and in the CSS
.button-container {
text-align:center;
}
Related
I'm trying to make a card with a title and description but when I set a background image on that image, my card title disappeared but not my description. It happens even if I do the same code for both of them.
I know I don't have to write here animation codes too but I really don't have any idea why this is happening.
Image:
z-index doesn't work I guess.
.index {
width: 400px;
height: 400px;
background-color: white;
float: left;
margin-right: 30px;
border-radius: 10px;
overflow: hidden;
position: relative;
color: white;
cursor: pointer;
}
.index_img {
width: 100%;
height: 100%;
z-index: -1;
}
.index_html {
background-image: url(web_image_html.jpeg);
background-position: center;
background-size: cover;
z-index: -1;
}
.index a {
color: #fff;
text-decoration: none;
}
.index_title {
text-align: center;
font-family: 'Roboto', sans-serif;
font-size: 30px;
}
.index_description {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
font-family: 'Inter', sans-serif;
width: 100%;
font-size: 18px;
}
.index_title,
.index_description {
background-color: rgba(82, 81, 81, 0.56);
height: 80px;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
}
.index_html:hover {
animation: index-animate 3s ease-in-out;
}
#keyframes index-animate {
0% {
transform: scale(1);
}
100% {
transform: scale(1.3);
}
}
<div class="index">
<a href="">
<div class="index_img index_html"></div>
<h2 class="index_title">HTML</h2>
<p class="index_description">Lorem ipsum dolor sit amet.</p>
</a>
</div>
Remove overflow hidden from the below code
.index {
width: 400px;
height: 400px;
background-color: white;
float: left;
margin-right: 30px;
border-radius: 10px;
position: relative;
color: white;
cursor: pointer;
}
You're on the right track! But you're fumbling a little with the usage of display: flex; and position: absolute;. display: flex; should be used on the parent to see the result on the child(ren), you're using it directly on the children (.index_title and .index_description).
For trying to position 2 elements together with position: absolute; I'd recommend wrapping them together, so you only have to position 1 element!
.index {
width: 400px;
height: 400px;
background-color: white;
float: left;
margin-right: 30px;
border-radius: 10px;
overflow: hidden;
position: relative;
color: white;
cursor: pointer;
}
.index_img {
width: 100%;
height: 100%;
z-index: -1;
}
.index_html {
background: url(web_image_html.jpeg) red;
background-position: center;
background-size: cover;
z-index: -1;
}
.index a {
color: #fff;
text-decoration: none;
}
.index_content {
width: 100%;
height: 80px;
position: absolute;
bottom: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: rgba(82, 81, 81, 0.56);
}
.index_title {
font-family: 'Roboto', sans-serif;
font-size: 30px;
color: #000;
}
.index_description {
font-family: 'Inter', sans-serif;
font-size: 18px;
}
.index_title,
.index_description {
width: 100%;
margin: 0;
text-align: center;
}
.index_html:hover {
animation: index-animate 3s ease-in-out;
}
#keyframes index-animate {
0% {
transform: scale(1);
}
100% {
transform: scale(1.3);
}
}
<div class="index">
<a href="">
<div class="index_img index_html"></div>
<div class="index_content">
<h2 class="index_title">HTML</h2>
<p class="index_description">Lorem ipsum dolor sit amet.</p>
</div>
</a>
</div>
i am doing a school project and i'm stuck here. how do i center the image and the button on the page? i need to make 3/4 of these directly underneath each other. how do i do that? I have tried text-align and to position it but it seems to be stuck. also, do you know why my image disappears when the window is resized? thank you!
html:
<div class="container">
<img src="images/parken.jpg" alt="park" width="500px">
<button class="btn">lokationer</button>
</div>
</main><!-- .content -->
.container {
position: relative;
width: 100%;
max-width: 400px;
position: inherit;
}
.container img {
width: 100%;
height: auto;
position: inherit
}
.container .btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.container .btn:hover {
background-color: black;
}
Use margin: 0 auto to center display: block elements
<div class="container">
<img src="images/me.jpg" align="center" alt="park" width="500px">
<button class="btn">lokationer</button>
</div>`
and for CSS, use this:
.container {
border: 1pt solid red;
max-width: 400px;
margin: 0 auto;
}
.container img {
width: 100%;
height: auto;
}
.container .btn {
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
display: block;
margin: 0 auto;
}
.container .btn:hover {
background-color: black;
}
html
<div class="container">
<img src="https://picsum.photos/100" alt="park" width="100px">
<button class="btn">lokationer</button>
</div>
<div class="container">
<img src="https://picsum.photos/100" alt="park" width="100px">
<button class="btn">lokationer</button>
</div>
css
.container {
text-align: center;
padding-bottom: 40px;
}
img{
width: 100%;
height: auto;
}
.btn {
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.btn:hover {
background-color: black;
}
https://jsfiddle.net/hrfadvyn/2/
I have an image that has two buttons inside it. I want these buttons to be responsive and the text to be centered all the time and resize if the image gets resized. The text inside these buttons is not centered and gets even worse when resizing the browser or when browsing on a mobile device. What am I missing? Below is the CSS code for the image and the buttons:
.container {
position: relative;
width: 100%;
max-width: 2400px;
}
.container img {
width: 150%;
opacity: 0.85;
height: auto;
}
.container .btn {
width: 30%;
height: 10%;
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 70%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 1.5vw;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
.container .btns {
width: 30%;
height: 10%;
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 30%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 2vw;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
<div class="container">
<img img="" src="" alt="">
Registracija Kaune
Registracija Vilniuje
</div>
Below are the images of how it looks:
This is how it looks on desktop:
This is how it looks on mobile/resized browser:
I got rid of your width and height on the buttons and changed your padding to a % so it is responsive and the text will always appear centered when resized.
.container {
position: relative;
width: 100%;
max-width: 2400px;
}
.container img {
width: 150%;
opacity: 0.85;
height: auto;
}
.container .btn {
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 70%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 1.5vw;
padding: 1% 5%;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
.container .btns {
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 30%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 2vw;
padding: 1% 5%;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
<div class="container">
<img img="" src="" alt="">
Registracija Kaune
Registracija Vilniuje
</div>
I modified your code. Try this
.container {
position: relative;
width: 100%;
height:100vh;
}
.btn-container{
display:flex;
justify-content: center;
align-items: center;
height:100%;
width:100%;
}
.container img {
width: 150%;
opacity: 0.85;
height: auto;
}
.container .btn {
opacity: 0.8;
filter: alpha(opacity=80);
background-color: #555;
color: white;
font-size: 1.5em;
padding: 12px 24px;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
line-height: 100%;
margin: 0 30px;
}
I added a div with a classname of btn-container that holds your buttons and positioned in the center
<div class="container">
<img img="" src="" alt="">
<div class="btn-container">
Registracija Kaune
Registracija Vilniuje
</div>
</div>
Try remove height: 10%; from .container .btn Because you added padding for that
.container {
position: relative;
width: 100%;
max-width: 2400px;
}
.container img {
width: 150%;
opacity: 0.85;
height: auto;
}
.container .btn {
width: 30%;
/* height: 10%; */ /* Remove this height */
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 70%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 2vw;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
.container .btns {
width: 30%;
/* height: 10%; */ /* Remove this height */
opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
top: 80%;
left: 30%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 2vw;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
text-decoration: none;
vertical-align: middle;
line-height: 100%;
}
<div class="container">
<img img="" src="" alt="">
Registracija Kaune
Registracija Vilniuje
</div>
many solution to this
make the image as background-image for the container, then use flexbox
or
make the image position absolute, add margin to the buttons, and use flaxbox like this esample below
.container {
display: flex;
justify-content: space-evenly;
width: 100%;
max-width: 2400px;
position: relative;
overflow: hidden;
}
.container img {
position: absolute;
width: 100%;
opacity: 0.85;
height: auto;
}
.container .btn, .container .btns{
display: flex;
align-items: center;
justify-content: center;
width: 30%;
height: 10%;
min-height: calc(2vw + 24px);
opacity: .8;
background-color: #555;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
text-decoration: none;
margin: 40% 0;
}
.container .btn {
left: 70%;
font-size: 1.5vw;
}
.container .btns {
left: 30%;
font-size: 2vw;
}
<div class="container">
<img src="https://data.whicdn.com/images/50959200/original.jpg" alt="">
Registracija Vilniuje
Registracija Kaune
</div>
Just Add bootstrap class into button your tag
class="text-center";
I've written this code for a simple html page in which there are three sections. The top(first) and bottom(third) are full screen sections and the middle section has to be shown with both the top and bottom sections i.e., the upper half of the middle section with the top section and the bottom half with the third section.
I was able to show the first part correctly(when scroll position is at the top) but not the second part(which is when scroll position at the bottom).
Can anybody tell me how can I achieve that?
Full page: https://i.imgur.com/YDV1usM.png
When scroll position is at the top: https://i.imgur.com/Mngkqth.png
When scroll position is at the bottom: https://i.imgur.com/wugOxCY.png
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1, .section-3 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 80px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
margin-top: -145px;
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 30px;
}
<div class="section-1">
<h2>SECTION 1</h2>
</div>
<div class="section-2">
<h6>Scroll</h6>
<p>Studio Liana Lalush</p>
</div>
<div class="section-3">
<h2>SECTION 3</h2>
</div>
Are you expecting like this:
Note: check out my answer in full screen mode
Demo:
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1,
.section-3 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 70px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
position: absolute;
top: 98%;
left: 50%;
transform: translate(-50%, -50%);
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 25px;
}
<div class="section-1">
<h2>SECTION 1</h2>
</div>
<div class="section-2">
<h6>Scroll</h6>
<p>Studio Liana Lalush</p>
</div>
<div class="section-3">
<h2>SECTION 3</h2>
</div>
Try with this code. You only need to change in css code.
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-3{
height: 90vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 80px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
margin-top: -145px;
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 30px;
}
http://codepen.io/leongaban/pen/ACJta
^ Updated (I can never get SASS to work right in Codepen)
Can't align to the middle my elements above. Using SASS, I want to avoid using position hacks
<main>
<div class="gradient">
<div class="hero">
<img src="http://placehold.it/350x150" alt=""/>
</div>
</div>
<section class="tagline">
<h1>Hi there Lorem Ipsum <em>My name is Bob</em></h1>
<button>Learn More</button>
</section>
</main>
body {
font-family:Arial
}
.hero {
display: table-cell;
//position: relative;
margin: 0 auto;
// top: 42%;
// left: 10%;
text-align: center;
width: auto;
height: 447px;
vertical-align: bottom;
img {
width: 300px;
height: 280px;
text-align: center;
vertical-align: bottom;
border:0;
}
}
.tagline {
position: relative;
margin: 0 auto;
width: 100%;
height: 300px;
text-align: center;
color: blue;
background: gray;
z-index: 2;
h1 {
margin: 0 auto;
padding-top: 5%;
width: 80%;
font-size: 42px;
text-align: center;
}
em {
font-weight: bold;
}
button {
margin-top: 25px;
padding: 10px 20px;
font-family: Arial;
font-size: em(21);
color: white;
border: 0;
background: orange;
}
}
I was able to center your elements above my doing the following:
.hero {
margin: 0 auto;
text-align: center;
width: 50%;
height: 447px;
vertical-align: bottom;
}
It seems it did not want to center because you had specified width: auto