Centering all text of <a> as display block, vertically and horizontally - html

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css">
<style>
#metro {
width: 100%
height: auto;
font-size: 16px;
}
#metro .metro_half_row {
width: 50%;
float: left;
height: 100px;
color: white;
background-color: grey;
}
#metro .metro_half_row a {
display: block;
height: inherit;
text-align: center;
}
</style>
<div id="metro">
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
</div>
I've wrapper as #metro, .metro_half_row as each section, using a display: block
Right now the text in <a> is centered horizontally, but not vertically. I need to center the text contained in the links, and each <a> covering the entire sub div block both horizontally and vertically.

display: table and display: table-cell are your friends.
#metro {
width: 100%;
height: auto;
font-size: 16px;
}
#metro .table-block {
width: 100%;
display: table;
}
#metro .table-cell {
width: 50%;
display: table-cell;
vertical-align: middle;
}
#metro .metro_half_row {
width: 100%;
height: 100px;
color: white;
background-color: grey;
display: table;
}
#metro .metro_half_row a {
width: 100%;
display: table-cell;
height: inherit;
text-align: center;
vertical-align: middle;
}
<link href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" rel="stylesheet" />
<div id="metro">
<div class="table-block">
<div class="table-cell">
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
</div>
<div class="table-cell">
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
</div>
</div>
<div class="table-block">
<div class="table-cell">
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
</div>
<div class="table-cell">
<div class="metro_half_row">
<a href="#">
<h1>TEXT1</h1>
<h2>TEXT</h2>
</a>
</div>
</div>
</div>
</div>

If i understand your problem correctly in your case, the CSS may look like this:
a{
margin-top:25px;
}
or
a{
position: relative;
top: 25%;
}

Try:
#metro .metro_half_row a {
display: table-cell;
width: inherit;
vertical-align: middle;
height: inherit;
text-align: center;
}
#metro .metro_half_row {
display: table;
}
view http://jsfiddle.net/alemarch/589pqd62/

Related

Inline-block top align issue; blocks seemingly randomly aligned [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
I'm trying to align multiple inline-blocks at the top of my page, but for reasons that are baffling to me, it's not working. The CSS could hardly be cleaner or less, but the top isn't aligning properly. I thought it could be a floating issue, but even after applying a clear:both it doesn't fix this.
Please see the program here:
https://jsfiddle.net/yezwocta/
#page {
text-align: center;
}
.article {
width: 350px;
height: 150px;
margin: 5px;
display: inline-block;
background-color: #fafafa;
}
.article img {
float: left;
width: 150px;
height: 130px;
margin-top: 10px;
}
.content {
position: relative;
display: inline-block;
width: 170px;
height: 130px;
margin-top: 10px;
margin-left: 10px;
text-align: left;
}
.title {
font-size: 1.2rem;
}
.source {
font-size: 0.8rem;
position: absolute;
bottom: 0;
left: 0;
}
<div id="page">
<div class="article">
<a href="https://cnn.com" target="_blank">
<img alt="News" src="https://loremflickr.com/150/130/news?random=1">
<div class="content">
<span class="title">Cable News Network</span>
<span class="source">CNN</span>
</div>
</a>
</div>
<div class="article">
<a href="https://www.mozilla.org/en-US/firefox/new/" target="_blank">
<img alt="Firefox browser" src="https://loremflickr.com/150/130/browser?random=2">
<div class="content">
<span class="title">Get the Latest Firefox Browser</span>
<span class="source">Mozilla</span>
</div>
</a>
</div>
<div class="article">
<a href="https://www.kproxy.com/" target="_blank">
<img alt="kproxy" src="https://loremflickr.com/150/130/proxy?random=3">
<div class="content">
<span class="title">Surf the web anonymously and bypass filters</span>
<span class="source">kproxy</span>
</div>
</a>
</div>
</div>
Add vertical-align: top; to your .article CSS (the default is baseline):
#page {
text-align: center;
}
.article {
width: 350px;
height: 150px;
margin: 5px;
display: inline-block;
background-color: #fafafa;
vertical-align:top;
}
.article img {
float: left;
width: 150px;
height: 130px;
margin-top: 10px;
}
.content {
position: relative;
display: inline-block;
width: 170px;
height: 130px;
margin-top: 10px;
margin-left: 10px;
text-align: left;
}
.title {
font-size: 1.2rem;
}
.source {
font-size: 0.8rem;
position: absolute;
bottom: 0;
left: 0;
}
<div id="page">
<div class="article">
<a href="https://cnn.com" target="_blank">
<img alt="News" src="https://loremflickr.com/150/130/news?random=1">
<div class="content">
<span class="title">Cable News Network</span>
<span class="source">CNN</span>
</div>
</a>
</div>
<div class="article">
<a href="https://www.mozilla.org/en-US/firefox/new/" target="_blank">
<img alt="Firefox browser" src="https://loremflickr.com/150/130/browser?random=2">
<div class="content">
<span class="title">Get the Latest Firefox Browser</span>
<span class="source">Mozilla</span>
</div>
</a>
</div>
<div class="article">
<a href="https://www.kproxy.com/" target="_blank">
<img alt="kproxy" src="https://loremflickr.com/150/130/proxy?random=3">
<div class="content">
<span class="title">Surf the web anonymously and bypass filters</span>
<span class="source">kproxy</span>
</div>
</a>
</div>
</div>

Image container not aligning bottom to other grid container

Hello,
As you can see from the photo above, I am trying to achieve a grid system. First grid is 3 images, second grid is a column, and third grid is a large image floated to the right of the 2nd grid. You can see this photo on my portfolio website: http://www.irwinlitvak.com
I have three images in the first grid that have a width of 31.33% and the first and second img have a margin-right of 3.005% to full up the container width.
In the next grid (grid-2), I have a two of my images floated left in a column and (grid-2-of-3) is floated right with a width of 65.556%.
I would like the top and bottom of the larger image to take up the full height of the grid, so the bottom of the big image aligns with the self-destructing box.
Here is the HTML & CSS:
.projects-grid {
margin: 100px auto 0;
width: 90%;
}
.projects-grid .title {
margin-bottom: 20px;
text-align: center;
}
.projects-grid h1 {
display: inline-block;
font-family: "Montserrat";
}
.grid-1 {
margin-bottom: 4%;
}
.grid-1-of-3 {
position: relative;
width: 31.33%;
float: left;
overflow: hidden;
}
.grid-2-of-3 {
position: relative;
width: 65.556%;
float: right;
}
.grid-1-of-3:first-child,
.grid-1-of-3:nth-child(2) {
margin-right: 3.005%;
}
.grid-3 {
position: relative;
display: inline-block;
width: 33%;
margin-bottom: 60px;
vertical-align: bottom;
}
.grid-5 {
position: relative;
display: inline-block;
width: 20%;
margin-bottom: 60px;
vertical-align: bottom;
}
.box-1 {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.grid-1-of-3:first-child {
margin-left: 0;
}
.grid-1-of-3:last-child {
margin-right: 0;
}
.big-box {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.grid-2 {
width: 31.33%;
float: left;
}
.grid-2 .box-cont {
width: 100%;
}
.grid-2 .box-cont:first-child {
margin-bottom: 4%;
}
.grid-2 .box-cont {
position: relative;
}
<section class="projects-grid clearfix">
<div class="row title">
<h2>Projects</h2>
</div>
<div class="grid-1 clearfix">
<div class="grid-1-of-3">
<div class="box-1">
<a href="">
<img class="bdgt-app" src="assets/budget-app-x-ps.jpg" alt="budget-app pic">
</a>
<a href="https://budget-app-x.herokuapp.com/" target="_blank">
<div class="box-overlay">
<div class="text-overlay">
<h3>Budget-App-X</h3>
<p>Manage your incomes and expenses in a fun and easy app.</p>
</div>
</div>
</a>
</div>
<h3>
Budget-App-X
</h3>
</div>
<div class="grid-1-of-3">
<div class="box-1">
<a href="">
<img src="/assets/dice-game-x-ps.jpg" alt="dice-game">
</a>
<a href="https://dice-game-x.herokuapp.com/" target="_blank">
<div class="box-overlay">
<div class="text-overlay">
<h3>Dice-Game</h3>
<p>Roll the dice. Test your luck and see who racks the most points.</p>
</div>
</div>
</a>
</div>
<h3>
Dice-Game
</h3>
</div>
<div class="grid-1-of-3">
<div class="box-1">
<a href="">
<img src="/assets/pomodoro-timer-x-ps.jpg" alt="pomodoro-app-timer">
</a>
<a href="http://pomodoro-app-timer.herokuapp.com" target="_blank">
<div class="box-overlay">
<div class="text-overlay">
<h3>Pomodoro-Timer</h3>
<p>A quick and easy solution to being productive. Set the time and get things done.</p>
</div>
</div>
</a>
</div>
<h3>
Pomodoro-Timer
</h3>
</div>
</div>
<div class="grid-2 clearfix">
<div class="box-cont">
<div class="box-1">
<a href="">
<img src="/assets/cucumberme-x-ps.jpg" alt="cucumber me">
</a>
<a href="http://www.cucumberme.com" target="_blank">
<div class="box-overlay">
<div class="text-overlay">
<h3>CucumberMe</h3>
<p>CucumberMe is your way of anonymously sending cucumbers to a friend, ex or anyone you want.<br><br> Go and send one today! </p>
</div>
</div>
</a>
</div>
<h3>
CucumberMe
</h3>
</div>
<div class="box-cont">
<div class="box-1">
<a href="#">
<img src="/assets/self-destruct-x-ps.jpg" alt="to do list">
</a>
<a href="http://todos-irwin.herokuapp.com/" target="_blank">
<div class="box-overlay">
<div class="text-overlay">
<h3>Self Destructing To-Do-List</h3>
<p>A to-do-list that will delete itself within 10 seconds. How many chores can you list within that time? </p>
</div>
</div>
</a>
</div>
<h3>
<a href="http://todos-irwin.herokuapp.com/" target="_blank">
Self Destructing To-Do-List
</a>
</h3>
</div>
</div>
<div class="grid-2-of-3 clearfix">
<div class="box-cont">
<div class="big-box">
<a href="#">
<img src="/assets/omnifood-x-ps.jpg" alt="omnifood">
</a>
<a href="http://con.staging.thegateny.net/con/Omnifood/v1/" target="_blank">
<div class="box-overlay big-overlay">
<div class="text-overlay">
<h3>Omnifood</h3>
<p>My version of the food app Blue Apron. Take a look! </p>
</div>
</div>
</a>
</div>
<h3>
<a href="http://con.staging.thegateny.net/con/Omnifood/v1/" target="_blank">
Omnifood
</a>
</h3>
</div>
</div>
</section>
What you can do is as I show in the snippet below. You have a container for all your divs you want at the same height, which you give a set height. You then give the items on the left a container (with height: 100%) and make a div for the right item (height: 100%).
By doing this, you have a container for the items on the left, so you can make them, say, 50% each, and you have a right item that's the same height as the container on the left.
In order to make an image fit a div, either use background-size: cover or something similar. height:100%; width: auto also works for responsive images.
Hope it helps.
.outer {
background: blue;
width: 600px;
height: 200px;
}
.leftwrap {
width: 30%;
float: left;
height: 100%;
}
.left1 {
background: purple;
width: 100%;
height: 50%;
}
.left2 {
background: orange;
width: 100%;
height: 50%;
}
.right {
background: teal;
height: 100%;
width: 70%;
float: left;
}
/** New code **/
.image {
height: 80%;
width: auto;
border: 1px solid black;
}
.imagetext {
color: white;
text-align: center;
border: 1px solid black;
}
.left {
box-sizing: border-box;
padding-bottom: 30px;
}
<div class='outer'>
<div class='leftwrap'>
<div class='left left1'>
<div class="image">My image here</div>
<div class="imagetext">Some text</div>
</div>
<div class='left left2'>
<div class="image">My image here</div>
<div class="imagetext">Some text</div>
</div>
</div>
<div class='right'>3</div>
</div>

Div tag get down while hover it

While I hover Div tag, Another Div tag will appear in front of the prev Div. When i didn't put any words, it works. but when i put h3 tag, the div goes down.
here is the HTML
<div id="content">
<h1 class="head-content">Biscuits</h1>
<div class="line"></div>
<a href="#">
<div class="list-content">
<div class="detail-content">
<h3>Biscuits 1</h3>
<p>Price: IDR 12000</p>
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
</div>
here is CSS
#content{
width:50%;
}
.line{
border-top: 5px solid black;
}
.list-content{
display:inline-block;
width:25%;
height:200px;
background-color: black;
margin-top: 10px;
}
.detail-content{
display: none;
}
.list-content:hover .detail-content{
display: block;
width:100%;
height:75%;
background-color: rgba(255,255,255,0.6);
}
thank you before
Update below css part
.list-content:hover .detail-content {
display: table;
width: 100%;
height: 75%;
background-color: rgba(255, 255, 255, 0.6);
}
#content {
width: 50%;
}
.line {
border-top: 5px solid black;
}
.list-content {
display: inline-block;
width: 25%;
height: 200px;
background-color: black;
margin-top: 10px;
}
.detail-content {
display: none;
}
.list-content:hover .detail-content {
display: table;
width: 100%;
height: 75%;
background-color: rgba(255, 255, 255, 0.6);
}
<div id="content">
<h1 class="head-content">Biscuits</h1>
<div class="line"></div>
<a href="#">
<div class="list-content">
<div class="detail-content">
<h3>Biscuits 1</h3>
<p>Price: IDR 12000</p>
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
</div>
#James Please find following code. I hope you are expecting the same. Just replaced "display:inline-block;" with "float:left;" and took class "list-content" in anchor tag itself.
#content{
width:50%;
}
.line{
border-top: 5px solid black;
}
.list-content{
float:left;
width:25%;
height:200px;
background-color: black;
margin-top: 10px;
margin-right:10px;
}
.detail-content{
display: none;
}
.list-content:hover .detail-content{
display: block;
width:100%;
height:75%;
background-color: rgba(255,255,255,0.6);
}
.clearfix{
clear:both;
}
<div id="content">
<h1 class="head-content">Biscuits</h1>
<div class="line"></div>
<div class="clearfix">
<a href="#" class="list-content">
<div class="detail-content">
<h3>Biscuits 1</h3>
<p>Price: IDR 12000</p>
</div>
</a>
<a href="#" class="list-content">
<div class="detail-content">
</div>
</a>
<a href="#" class="list-content">
<div class="detail-content">
</div>
</a>
</div>
</div>
Because the .list-content items are inline blocks, when you add a text content you have to vertically align them. Add vertical-align: top to .list-content:
.list-content {
display: inline-block;
width: 25%;
height: 200px;
background-color: black;
margin-top: 10px;
vertical-align: top;
}
And remove the top margin from :
h3 {
margin-top: 0;
}
{
width: 50%;
}
.line {
border-top: 5px solid black;
}
.list-content {
display: inline-block;
width: 25%;
height: 200px;
background-color: black;
margin-top: 10px;
vertical-align: top;
}
.detail-content {
display: none;
width: 100%;
height: 75%;
background-color: rgba(255, 255, 255, 0.6);
}
.list-content:hover .detail-content {
display: block;
}
h3 {
margin-top: 0;
}
<div id="content">
<h1 class="head-content">Biscuits</h1>
<div class="line"></div>
<a href="#">
<div class="list-content">
<div class="detail-content">
<h3>Biscuits 1</h3>
<p>Price: IDR 12000</p>
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
<a href="#">
<div class="list-content">
<div class="detail-content">
</div>
</div>
</a>
</div>

How I align div's elements from a inline-block style when more than one text line are displayed?

I got these elements display on a inline-block, specific width etc.
Preview: https://s13.postimg.org/hk700r45z/solvthis.png
But, when more than one line is displayed, they get disalineadas.
.shop_container,
.list_container {
display: block;
}
.list_container {
max-width: 1170px
}
.element_container,
list_container,
element {
box-sizing: border-box;
}
.element_container {
display: inline;
font-size: 13pt;
line-height: 1.5em;
...
}
.element {
width: 40%;
margin: 25px 2.5%;
height: 100px;
border: 1px solid {
colour
}
;
padding-top:38px;
line-height:17px;
font-size:14px;
display:inline-block;
position:relative;
}
.element > span {
font-size: 14px;
}
<div class="shop_container">
<div class="list_container">
<!-- Element 1 -->
<div class="element_container">
<a href="#" class="element">
<span>Element1</span>
</a>
</div>
<!-- Element 2 -->
<div class="element_container">
<a href="#" class="element">
<span>Element2</span>
</a>
</div>
<!-- Element 3 -->
<div class="element_container">
<a href="#" class="element">
<span>Element3</span>
</a>
</div>
<!-- Long list elements -->
</div>
</div>
Would you know where is the problem? I think that is in the fixed padding-top added to element, but I don't know how to fix it.
You can do it like below:-
.shop_container, .list_container { display: block; }
.list_container { max-width: 1170px }
.element_container, list_container, element { box-sizing: border-box; }
.element {
width: 40%;
margin: 25px 2.5%;
height: 100px;
border: 1px solid {colour};
padding-top: 38px;
line-height: 17px;
font-size: 14px;
display: inline-block;
position: relative;
word-break:break-all;
}
.element_container {
display: inline;
float: left;
font-size: 13pt;
line-height: 1.5em;
width: 50%;
}
.element > span { font-size: 14px; }
<div class="shop_container">
<div class="list_container">
<!-- Element 1 -->
<div class="element_container">
<a href="#" class="element">
<span>Element1</span>
</a>
</div>
<!-- Element 2 -->
<div class="element_container">
<a href="#" class="element">
<span>Element2</span>
</a>
</div>
<!-- Element 3 -->
<div class="element_container">
<a href="#" class="element">
<span>Element3
hello again
we meet here
you have a problem
i solved it
thanks
</span>
</a>
</div>
<div class="element_container">
<a href="#" class="element">
<span>Element4</span>
</a>
</div>
<!-- Long list elements -->
</div>
</div>
A bit more good:-
.shop_container, .list_container { display: block; }
.list_container { max-width: 1170px }
.element_container, list_container, element { box-sizing: border-box; }
.element {
width: 40%;
margin: 25px 2.5%;
height: 100px;
border: 1px solid {colour};
padding-top: 38px;
line-height: 17px;
font-size: 14px;
display: inline-block;
position: relative;
word-break:break-all;
}
.element_container {
display: inline;
float: left;
font-size: 13pt;
line-height: 1.5em;
width: 50%;
}
.element > span {
float: left;
font-size: 14px;
margin-top: 10px;
width: 100%;
}
<div class="shop_container">
<div class="list_container">
<!-- Element 1 -->
<div class="element_container">
<a href="#" class="element">
<span>Element1</span>
</a>
</div>
<!-- Element 2 -->
<div class="element_container">
<a href="#" class="element">
<span>Element2</span>
</a>
</div>
<!-- Element 3 -->
<div class="element_container">
<a href="#" class="element">
<span>Element3</span>
<span>hello again
we meet here
you have a problem
i solved it
thanks
</span>
</a>
</div>
<div class="element_container">
<a href="#" class="element">
<span>Element4</span>
</a>
</div>
<!-- Long list elements -->
</div>
</div>

Allign divs to middle of screen by their middle

Using bootstrap, I need to have a page with divs (4 initially) as buttons in the middle of the screen, as below image:
I succeeded to do this, but, next, I need to enlarge one of them when mouseover as below image:
so, they are aligned by their middle. and all others will be smaller when another on is hover.
Simply increase the width and height on :hover.
html, body,
.container {
height: 100%;
}
.container {
display: table;
vertical-align: middle;
}
.vertical-center-row {
display: table-cell;
vertical-align: middle;
}
.buttons-container {
vertical-align: bottom;
text-align: center;
}
.app-button:hover {
width: 75px;
height: 75px;
}
.app-button {
width: 50px;
height: 50px;
margin: 15px;
background-color: #cccccc;
display: inline-block;
vertical-align: middle;
transition: all 0.5s;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row vertical-center-row">
<div class="col-md-12 buttons-container">
<div class="app-button"></div>
<div class="app-button"></div>
<div class="app-button"></div>
<div class="app-button"></div>
</div>
</div>
</div>
You can try two methods one is to increase the height and width and the other is to use transform
With transform Fiddle
html, body, .container {
height: 100%;
}
.container {
display: table;
vertical-align: middle;
}
.vertical-center-row {
display: table-cell;
vertical-align: middle;
}
.buttons-container {
vertical-align: bottom;
text-align: center;
}
.app-button {
width: 50px;
height: 50px;
margin: 15px;
background-color: #cccccc;
display: inline-block;
}
.app-button:hover {
transform:scale(1.5,1.5);
margin: 15px;
background-color: #cccccc;
display: inline-block;
}
<div class="container">
<div class="row vertical-center-row">
<div class="col-md-12 buttons-container"> <a href="#">
<div class="app-button"></div>
</a>
<a href="#">
<div class="app-button"></div>
</a>
<a href="#">
<div class="app-button">
</div>
</a>
<a href="#">
<div class="app-button">
</div>
</a>
</div>
</div>
</div>
By changing height and width Fiddle
html, body, .container {
height: 100%;
}
.container {
display: table;
vertical-align: middle;
}
.vertical-center-row {
display: table-cell;
vertical-align: middle;
}
.app-button:hover {
height:100px;
width:100px;
}
.buttons-container {
vertical-align: bottom;
text-align: center;
}
.app-button {
width: 50px;
height: 50px;
margin: 15px;
background-color: #cccccc;
display: inline-block;
vertical-align:middle;
}
<div class="container">
<div class="row vertical-center-row">
<div class="col-md-12 buttons-container"> <a href="#">
<div class="app-button"></div>
</a>
<a href="#">
<div class="app-button"></div>
</a>
<a href="#">
<div class="app-button">
</div>
</a>
<a href="#">
<div class="app-button">
</div>
</a>
</div>
</div>
</div>