Center text horisontaly and verticaly in an image - html

I want to change the text on the photos to be in the center, both horisontal and vertical. It's currently centered horisontally, but not vertically. Remember it's a "hover-effect background image"-thing.
HTML
<a href="portrait">
<div id="imagebox1" class="imagebox columns">
<div id="image1">
<span id="plus">Portrett</span>
</div>
</div>
</a>
<a href="nature">
<div id="imagebox3" class="imagebox columns">
<div id="image3">
<span id="plus">Natur</span>
</div>
</div>
</a>
<a href="various">
<div id="imagebox2" class="imagebox columns">
<div id="image2">
<span id="plus">Annet</span>
</div>
</div>
</a>
CSS
#imagebox1, #imagebox2, #imagebox3 {
float: left;
background-repeat: no-repeat;
margin-right: 0px;
}
#imagebox1 {
background:url(../images/sample_image400.jpg);
background-size: cover;
margin-left: 0px;
}
#imagebox2 {
background:url(../images/sample_image.jpg);
background-size: cover;
}
#imagebox3 {
background:url(../images/sample_image.jpg);
background-size: cover;
}
#image1, #image2, #image3 {
background:rgba(255,255,255,.75);
text-align:center;
padding-top: 100%;
opacity:0;
-webkit-transition: all 0.3s ease-in-out;
}
#imagebox1:hover #image1 {
opacity:1;
}
#imagebox2:hover #image2 {
opacity:1;
}
#imagebox3:hover #image3 {
opacity:1;
}
.images img {
margin-left: 0px;
padding-bottom:30px;
padding-top:35px;
}
#imagebox1, #imagebox2, #imagebox3 {
margin-bottom: 30px;
}
#plus {
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 200;
color: #282828;
font-size: 20px;
text-transform: uppercase;
}
JSFIDDLE
http://jsfiddle.net/8abCY/

Ok...I think your HTML structure is too complex for what you are trying to do.
So I've simplified it.
JSfiddle DEmo
The image is content and so it should be in the HTML not a background-image in the CSS which gives us, simply
HTML
<a href="portrait" class="imagebox">
<img src="http://www.ginakorslund.no/images/sample_image400.jpg" alt=""/>
<span class="plus">Portrett</span>
</a>
Note I'be changed a lot of the ID out for a more generic class. you can still give each anchor link an individual ID if you want but the classes mean that the CSS is re-usable.
CSS
.imagebox {
position: relative; /* required */
text-align: center;
display: block;
}
.plus {
position: absolute;
top:50%; /* top and left values put the span's top left corner exactly */
left:50%; /* halfway down and across the screen but do **not** center it */
-webkit-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
/* move the span back exactly half it's own width and height */
/* and now it's centered regardless of width & height */
display: none; /* hide it until hovered */
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 200;
color: #282828;
font-size: 20px;
text-transform: uppercase;
color:white;
}
.imagebox:hover .plus {
display: block; /* show it when the link is hovered */
}

try...
#plus {
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 200;
color: #282828;
font-size: 20px;
text-transform: uppercase;
position: relative;
margin-top: auto;
margin-bottom: auto;
}

Related

Unable to make image responsive using viewport width

I am trying to make a image responsive by using viewport width instead of pixels (vh) but when i resize the page the text also moves along with the image i tried to fix this by changing the font size of the text that's moving from pixels to vh but it did not work.
I have also the following meta tag in the html head:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
HTML
<body>
<div id="top">
<div class="kygo">Kygo</div>
<div class="img"> <img src="maskGroup1.png" alt="Profile Picture"></div>
<div class="tag">#000</div>
</div>
<div id="bottom"></div>
</body>
CSS
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden
}
#top{
float:bottom;
background-color:#1A1C1E;
width:100%;
height:200px;
}
#bottom{
position: relative;
float: top;
background-color:#2F3136;
width:100%;
height:1000px;
}
.kygo{
font-family: Segoe UI;
font-weight: 700;
padding-top: 24px;
padding-left: 103px;
color: white;
font-size: 20px;
}
.tag{
font-family: Segoe UI;
font-weight: 400;
margin-top: -40px;
padding-left: 103px;
color: #A2A4A8;
font-size: 13px;
}
img{
max-width: 11.333vw;
max-width: 20vw;
padding-left: 27px;
margin-top: -27px;
}
Thank you for reading.
for responsive image use width in %. And made some changes as follows
html, body {
margin: 0;
padding: 0;
}
#top{
background-color:#1A1C1E;
width:100%;
height:200px;
display: flex;
flex-direction: row;
align-items: center;
}
#bottom{
position: relative;
float: top;
background-color:#2F3136;
width:100%;
height:1000px;
}
.kygo{
font-family: Segoe UI;
font-weight: 700;
color: white;
font-size: 20px;
}
.tag{
font-family: Segoe UI;
font-weight: 400;
color: #A2A4A8;
font-size: 13px;
}
.name {
width:75%;
padding-left:5px;
}
.img{
width:25%;
padding-left:5px;
}
img {
width:100%;
border-radius: 300px;
}
<body>
<div id="top">
<div class="img"> <img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" alt="Profile Picture">
</div>
<div class="name">
<div class="kygo">Kygo</div>
<div class="tag">#000</div>
</div>
</div>
<div id="bottom"></div>
</body>
There are a few bugs in the css. As far as I can see, without knowing the idea behind the layout, this is the css with some cleanup.
Note that the size of the image is unkown so it is not clear to me how to give proper margins and paddings.
* {
box-sizing: border-box;
}
html, body {
margin: 0;
height: 100%;
overflow: hidden
}
#top {
width:100%;
height:200px;
background-color:#1A1C1E;
}
#bottom {
position: relative;
width:100%;
height:1000px;
background-color:#2F3136;
}
.kygo {
font-family: "Segoe UI", sans-serif;
font-weight: 700;
padding-top: 24px;
padding-left: 103px;
color: white;
font-size: 20px;
}
.img {
max-width: 20%;
padding-left: 27px;
margin-top: -27px;
}
.img img {
/* responsive image */
width: 100%;
height: auto;
border: 0;
}
.tag {
font-family: "Segoe UI", sans-serif;
font-weight: 400;
margin-top: -40px; /* this will partial overlay the top */
padding-left: 103px;
color: #a2a4a8;
font-size: 13px;
}
<!-- this in your <head> section -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- this in your <body> section -->
<div id="top">
<div class="kygo">Kygo</div>
<div class="img">
<img src="https://picsum.photos/id/206/150/100" alt="Profile Picture">
</div>
<div class="tag">#000</div>
</div>
<div id="bottom"></div>

How to align divs to the right

I have a cityHeader(in this pic it is Los Angeles).
Underneath cityHeader there is div called weatherMain which contains three smaller divs(id=temperature,id=weatherDescriptionHeader and id=documentIconImg
#cityHeader {
float: right;
display: block;
margin: 5px;
font-size: 42px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#weatherMain {
display: block;
margin-bottom: 20px;
text-align: right;
}
#weatherMain div {
display: inline-block;
}
#weatherDescriptionHeader {
font-size: 28px;
vertical-align: 50%;
}
#temperature {
font-size: 38px;
vertical-align: 25%;
}
<h1 id="cityHeader"></h1>
<div id="weatherMain">
<div id="temperature"></div>
<div id="weatherDescriptionHeader"></div>
<div><img id="documentIconImg" /></div>
</div>
enter image description heredocumentIconImg).
Three smaller divs should be under cityHeader, positioned next to each other and all aligned to the right.
I tried floating them to the right,adding text-align:right to their parent element(weatherMain), nothing works.
In the pic temperature div is 9 degrees,weatherDescriptionHeader is Clear sky and documentIconImg is icon.
Is this what you're looking for: https://codepen.io/dillonbrannick/pen/XOoNjp
I added in the background colours just so it's easy to identify the elements. Just put your background image into the #weatherMain and remove the other background colours and it should work fine. I added in a margin to #weathermain just to display it within the middle of the page.
Also I added a margin:0 to the h1 tag as h1 automatically adds some css style to it that was interfering.
HTML:
<div id="weatherMain">
<h1 id="cityHeader">Hello World</h1>
<div id="temperature">Howya</div>
<div id="weatherDescriptionHeader">Hola</div>
</div>
CSS:
#weatherMain {
background-color:red;
text-align: right;
margin: 10% 20% 10% 20%
}
#cityHeader {
background-color:yellow;
}
h1{
margin:0;
font-size:45px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; }
#temperature {
background-color:green;
font-size: 38px;
}
#weatherDescriptionHeader {
background-color:lightblue;
font-size: 28px;
}
Try this:
#weatherMain {
display: block;
margin-bottom: 20px;
text-align: right;
float: right
}

How I can add centered horizontal line behind the link using css?

I need to add centered horizontal line behind the link, so when I change size of the screen this line stays centered behind the link and moves with it. Also I need this line to be on the full width of screen.
On first image is what I have right now:
On the second image is what I need, as yo can see there thin gray line centered behind "V" link:
My html code of this link:
<div class="welcome-content">
<h1 class="welcome-text">Welcome To Kramerica Industries</h1>
<div class="explore-container">
<a class="v-explore-button" href="#about"><i class="fa fa-angle-down fa-lg" aria-hidden="true"></i></a>
<p class="explore-text">Explore</p>
</div>
</div>
My Sass:
.welcome-text {
text-align: center;
font-family: "Droid Sans Bold", "sans-serif";
color: #ffffff;
}
.explore-container {
position: absolute;
top: 147%;
left: 38%;
.v-explore-button {
text-decoration: none;
color: #ffffff;
background-color: #2ecc71;
padding: 15px 20px;
border-radius: 100%;
}
.explore-text {
margin-top: 20px;
color: #ffffff;
}
}
How about to use ::before inline block pseudoelement for button class in pair with transform:translateY(50% of the button width);and 100% width?
body{
background-color: black;
}
.welcome-text {
text-align: center;
font-family: "Droid Sans Bold", "sans-serif";
color: #ffffff;
}
.explore-container {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.v-explore-button {
display: flex;
align-items: center;
position: relative;
text-decoration: none;
color: #ffffff;
background-color: #2ecc71;
padding: 15px 20px;
border-radius: 100%;
}
.explore-text {
margin-top: 20px;
color: #ffffff;
}
.explore-container:before {
content: "";
position: absolute;
top: 1em;
left: 0;
width: 100%;
height: .15em;
background-color: #00bade;
}
.inner-wrap{
margin-right: 38%;
}
<div class="welcome-content">
<h1 class="welcome-text">Welcome To Kramerica Industries</h1>
<div class="explore-container">
<div class="inner-wrap">
<a class="v-explore-button" href="#about"><i class="fa fa-angle-
down fa-lg" aria-hidden="true"></i></a>
<p class="explore-text">Explore</p>
</div>
</div>
</div>
My recommendation would be to use a pseudo-element such as before:
See example for the solution. Let me know if this helps.
You can use the translate property to align elements inside a
container in the middle as demonstrated below without needing to
calculate the approximate % value to align it.
Instead of giving left:38% to your explore-container, set its width to 100% and align the elements using text-align:center as shown below.
I hope this helps.
.welcome-text {
text-align: center;
font-family: "Droid Sans Bold", "sans-serif";
}
.explore-container {
position: absolute;
top: 147%;
margin:0 auto;
width:100%;
text-align:center;
}
.v-explore{
position:relative;
background:yellow;
}
.v-explore-button {
text-decoration: none;
background-color: #2ecc71;
color:#ffffff;
padding: 15px 20px;
border-radius: 100px;
position:absolute;
top:50%;
transform: translate(-50%, -50%);
}
.explore-text {
margin-top: 20px;
}
<div class="welcome-content">
<h1 class="welcome-text">Welcome To Kramerica Industries</h1>
<div class="explore-container">
<div class="v-explore">
<a class="v-explore-button" href="#about">v</a>
<hr>
</div>
<p class="explore-text">Explore</p>
</div>
</div>

Align Select box and images

I have a div with a select box and 3 images.
I want to make this:
I have this:
So basically I am having trouble aligning the 3 images at the same height as the select box. I have tried several things and have no clue why it does not work.
Here is my HTML and CSS:
#lang_social {
width: 100%;
height: 60px;
background-color: #3CC;
display: table;
}
#language select {
height: 40px;
width: 200px;
background-color: #3FF;
border-color: #3FF;
display: inline-block;
vertical-align: middle;
color: #FFF;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
font-size: 16px;
margin-left: 10%;
margin-top: 10px;
;
}
#twitter_conn img {
float: right;
display: inline-block;
vertical-align: middle;
position: absolute;
float: right;
}
#fb_conn {
float: right;
margin-top: 10px;
margin-right: 20%;
display: inline-block;
vertical-align: middle;
}
#linkedin_conn {
float: right;
margin-top: 10px;
margin-right: 10%;
display: inline-block;
vertical-align: middle;
}
<div id="lang_social">
<div id="language">
<select>
<option value="swe">SWEDISH</option>
<option value="eng">ENGLISH</option>
</select>
</div>
<div id="twitter_conn">
<img src="http://www.placehold.it/32" onmouseover="this.src='http://www.placehold.it/32'" onmouseout="this.src='http://www.placehold.it/32'">
</div>
<div id="fb_conn">
<img src="http://www.placehold.it/32" onmouseover="this.src='http://www.placehold.it/32'" onmouseout="this.src='http://www.placehold.it/32'">
</div>
<div id="linked_conn">
<img src="http://www.placehold.it/32" onmouseover="this.src='http://www.placehold.it/32'" onmouseout="this.src='http://www.placehold.it/32'">
</div>
</div>
The general layout
To simplify things, let's create two separate divs that are each given 50% width on one line.
They can be marked up like this:
<div id="lang_social">
<div id="language"><!-- Select goes here --></div>
<div id="social"><!-- Icons go here --></div>
</div>
and positioned like this, each child div is given 50% width and will line up next to the other:
#lang_social > div {
display: inline-block;
width: 50%;
}
We now have two divs on one line:
Right align the icons
We want the social icons to be aligned from the right, that's easy now:
#social {
text-align: right;
}
Apply the colours and we get the finished result:
If you want, we can end the answer here, have a snippet:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#lang_social {
width: 100%;
background-color: #3CC;
line-height: 60px;
/*This Line height is the same as the height and will vertically center a single line*/
min-width: 340px;
}
#lang_social > div {
display: inline-block;
width: 50%;
padding: 0 10px;
}
img {
vertical-align: middle;/*Important to align in the middle*/
}
#social {
text-align: right;
}
#language select {
height: 40px;
width: 200px;
background-color: #3FF;
border-color: #3FF;
color: #FFF;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
font-size: 16px;
}
<div id="lang_social">
<div id="language">
<select>
<option value="swe">SWEDISH</option>
<option value="eng">ENGLISH</option>
</select>
</div><div id="social">
<img src="http://www.placehold.it/32/FF0000" />
<img src="http://www.placehold.it/32/FF9900" />
<img src="http://www.placehold.it/32/FF99CC" />
</div>
</div>
or we can go a little further below.
Note:
Each inline elements closing tag has no whitespace between it and the next inline elements opening tag, this prevents the gaps between inline elements. Example:
<div id="language">
Contents
</div><div id="social">
Contents
</div>
box-sizing: border-box incorporates padding and borders into the width and height.
Hover states for the social icons using CSS Sprites
If you want to, we can create a simple image hover state for each social image without javascript.
Instead of using <img>, we can use <a>, like so:
<div id="social">
<a class="icon facebook"></a>
<a class="icon twitter"></a>
<a class="icon linked"></a>
</div>
This allows us to create the normal and hover state with a CSS sprite. We can apply the background to the icon class:
.icon {
display: inline-block;
vertical-align: middle;
background: url(http://i.stack.imgur.com/1eIYU.png);
height: 32px;
width: 32px;
}
The background image looks like this:
Each coloured section should be changed with the social logo on a transparent background.
the top row of colours is used for the unhovered icon state. The bottom row is used for the hovered state.
Red = Facebook icon
Orange = Twitter icon
Pink = LinkedIn icon
Each icon section is 32px x 32px and each icon is positioned with background-position:
.facebook {
background-position: 0 0; /*x-axis | y-axis*/
}
.twitter {
background-position: -32px 0;
}
.linked {
background-position: -64px 0;
}
On hover, the background-position is changed to show a hover state:
.facebook:hover {
background-position: 0 -32px;
}
.twitter:hover {
background-position: -32px -32px;
}
.linked:hover {
background-position: -64px -32px;
}
Completed example
Note:
Each inline elements closing tag has no whitespace between it and the next inline elements opening tag, this prevents the gaps between inline elements.
box-sizing: border-box incorporates padding and borders into the width and height.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#lang_social {
width: 100%;
background-color: #3CC;
line-height: 60px;
/*This Line height is the same as the height and will vertically center a single line*/
min-width: 340px;
}
#lang_social > div {
display: inline-block;
width: 50%;
padding: 0 10px;
}
#social {
text-align: right;
}
#language select {
height: 40px;
width: 200px;
background-color: #3FF;
border-color: #3FF;
color: #FFF;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
font-size: 16px;
}
.icon {
display: inline-block;
vertical-align: middle;
margin: 0 0 0 10px;
background: url(http://i.stack.imgur.com/1eIYU.png);
height: 32px;
width: 32px;
transition: background-position .3s
}
.facebook {
background-position: 0 0;
}
.twitter {
background-position: -32px 0;
}
.linked {
background-position: -64px 0;
}
.facebook:hover {
background-position: 0 -32px;
}
.twitter:hover {
background-position: -32px -32px;
}
.linked:hover {
background-position: -64px -32px;
}
<div id="lang_social">
<div id="language">
<select>
<option value="swe">SWEDISH</option>
<option value="eng">ENGLISH</option>
</select>
</div><div id="social">
<a class="icon facebook"></a><a class="icon twitter"></a><a class="icon linked"></a>
</div>
</div>
I couldn't see your icons so i have used my own. you can substitute. I made your ids into classes and gave the lang_social an overall attr of vertical-align:middle to affect everything. I also floated the language left. Here is a fiddle
#lang_social {
width: 100%;
height: 60px;
background-color: #3CC;
display: table;
vertical-align:middle;
}
.language select {
height: 40px;
width: 200px;
background-color: #3FF;
border-color: #3FF;
display: inline-block;
vertical-align: middle;
color: #FFF;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
font-size: 16px;
margin-left: 10%;
margin-top: 10px;
float:left;
}
.twitter_conn {
display:inline-block;
margin-top: 10px;
margin-right: 20px;
width:30px;
float:right;
}
.fb_conn {
margin-top: 10px;
margin-right: 20px;
display:inline-block;
width:30px;
float:right;
}
.linkedin_conn {
width:30px;
float:right;
margin-top: 10px;
margin-right: 20px;
display:inline-block;
}
img {
width:32px;
height:32px;
padding:4px;
}
<div id="lang_social">
<div class="language">
<select>
<option value="swe">SWEDISH</option>
<option value="eng">ENGLISH</option>
</select>
</div>
<div class="linkedin_conn">
<img src="http://www.teratology.org/images/linkedin.png" onmouseover="this.src='http://www.teratology.org/images/linkedin.png'" onmouseout="this.src='http://www.teratology.org/images/linkedin.png">
</div>
<div class="fb_conn">
<img src="http://www.musicmatters.ie/images/fb3.png" onmouseover="this.src='http://www.musicmatters.ie/images/fb3.png'" onmouseout="this.src='http://www.musicmatters.ie/images/fb3.png'">
</div>
<div class="twitter_conn">
<img src="http://www.musicmatters.ie/images/twitter3.png" onmouseover="this.src='http://www.musicmatters.ie/images/twitter3.png" onmouseout="this.src='http://www.musicmatters.ie/images/twitter3.png'">
</div>
</div>

Why won't my text center here?

I have the following CSS:
.me-header {
display: inline-block;
position: relative;
top: 50px;
left: 10px;
}
.me-header a {
color: #333;
font: 10px "Lucida Grande", Helvetica, sans-serif;
text-decoration: none
}
.resume-header {
display: inline-block;
position: relative;
top: 50px;
left: 100px;
}
.resume-header a {
color: #333;
font: 10px "Lucida Grande", Helvetica, sans-serif;
text-align: center;
text-decoration: none
}
.center-text {
text-align: center;
}
And the following HTML:
<div class="me-header">
<img src="media/me-icon.png" alt="Picture of Christian Selig"><br> <span class="center-text">Contact Me</span>
</div>
<div class="resume-header">
<img src="media/resume-icon.png" alt="Resume icon" title="Click to download resume (PDF)"><br> <span class="center-text">Resume (PDF)</span>
</div>
But my text still comes out left-aligned. Why?
Both a and span are inline elements - so they are only as wide as their contents. So when you center the text, it is centred within the inline element, but the inline element appears on the left.
If you set text-align: center; on the parent block element it will work.
If you want to center the whole block, use an automatic margin.
You could do
.center-text
{
display: table;
margin: 0 auto;
}