I have a container thats 900px wide with 2 floating divs inside. I need to make column 2 background full height and dependent to the image to the left. The text in column 2 also needs to be vertically centered, again based on image height.
https://jsfiddle.net/rj5o6n79/1/
<div class="wrapper">
<div class="col span_2_of_3 content">
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
</div>
<div class="col span_1_of_3 box2">
This is column 2
</div>
<div style="clear:both;"></div>
</div>
You can use jQuery to get the height of the left div
var leftDivHeight = $('.span_2_of_3').height();
$('.span_1_of_3').css('height',leftDivHeight);
then wrap the content of your inner div to another div
<div class="col span_1_of_3 box2">
<div class='innerContent'> This is column 2 </div> <!-- added div -->
</div>
then add this css to vertically center your inner div
.innerContent{
position: relative;
top: 50%;
transform: translateY(-50%);
}
JSFIDDLE DEMO
Is this what you want?
body { margin:0; padding:0; }
.wrapper { border:1px solid green; width:900px; margin:0px auto; }
.content {
width: 100%; /* or whatever is required */
text-align: center; /* ensures the image is always in the h-middle */
overflow: hidden; /* hide the cropped portion */
}
img {
position: relative; /* allows repositioning */
/*left: 100%; move the whole width of the image to the right */
/* margin-left: -200%; magic! */
float:left;
}
/* COLUMN SETUP */
.col {
display: block;
float:left;
margin:0;
}
.col:first-child { margin-left: 0; }
.box { background-color: #ccc; }
.box2 { background-color: red; }
/* GROUPING */
.group:before,
.group:after {
content:"";
display:table;
}
.group:after {
clear:both;
}
.group {
zoom:1; /* For IE 6/7 */
}
/* GRID OF THREE */
.span_3_of_3 {
width: 100%;
}
.span_2_of_3 {
width: 66.1%;
}
.span_1_of_3 {
width: 33.8%;
height: 400px;
}
.content {
position: relative;
top: 50%;
text-align:left;
}
<div class="wrapper">
<div class="col span_2_of_3 content">
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
</div>
<div class="col span_1_of_3 box2">
<div class="content">
This is column 2
</div>
</div>
<div style="clear:both;"></div>
</div>
I think, I have more or less found what you are looking for. Though the second column isn't full height it is vertically centered and just as big as it needs to be.
.wrapper {
width: 900px;
border: 1px solid #000000;
display: inline-block;
}
.col img{
display: block;
}
.content {
vertical-align: middle;
display: inline-block;
}
.box2 {
display: inline-block;
vertical-align: middle;
}
<div class="wrapper">
<div class="col span_2_of_3 content">
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg"/>
</div>
<div class="col span_1_of_3 box2">
This is column 2
</div>
<div style="clear:both;"></div>
</div>
I have a solution of your problem checkout this demo, I think it's working for you. Here is the code:
body { margin:0; padding:0; }
.wrapper {
border: 1px solid green;
width: 900px;
margin: 0px auto;
/* background: #f00; */
display: table;
table-layout: fixed;}
.content {
width: 100%; /* or whatever is required */
text-align: center; /* ensures the image is always in the h-middle */
overflow: hidden; /* hide the cropped portion */
}
img {
position: relative; /* allows repositioning */
left: 100%; /* move the whole width of the image to the right */
margin-left: -200%; /* magic! */
}
/* COLUMN SETUP */
.col {
display: table-cell;
vertical-align: middle;
}
.col:first-child { margin-left: 0; }
.box { background-color: #ccc; }
.box2 { background-color: red; }
/* GROUPING */
.group:before,
.group:after {
content:"";
display:table;
}
.group:after {
clear:both;
}
.group {
zoom:1; /* For IE 6/7 */
}
/* GRID OF THREE */
.span_3_of_3 {
width: 100%;
}
.span_2_of_3 {
width: 66.1%;
}
.span_1_of_3 {
width: 33.8%;
}
td{ background:#000; color:#fff;}
<div class="wrapper">
<div class="col span_2_of_3 content">
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
</div>
<div class="col span_1_of_3 box2">
This is column 2
</div>
<div style="clear:both;"></div>
</div>
Related
I am new to CSS and HTML and i am trying to get the text to appear in the middle of the image, I am not sure what i am doing wrong. I removed text-align center because it was appearing in the middle of the whole page. I have two containers dividing the homepage into two sections, I want to make the text appear in the middle of each container which has an image
.column {
float: left;
width: 50%;
padding: 10px;
height:800px;
text-align: center;
color: white;
}
.centered {
left: 90;
position:absolute;
top: 260px;
width: 100%
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
img {
width:100%;
height:100%;
}
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg"alt="Snow">
<div class="centered">I want This Text in the center</div>
</div>
<div class="column" style="background-color:#bbb;">
<img src="5.jpg"alt="Snow">
.centered has position: absolute; which will reference the nearest parent whose position has been set, or if there are none set then it will reference the document body (the window or view port). In this case, by setting .column to position: relative; the absolute position of .centered will refer to the constraints of .column instead of the window.
.column {
float: left;
width: 50%;
padding: 10px;
height:800px;
text-align: center;
color: white;
position: relative;
}
.centered {
position:absolute;
top: 260px;
width: 100%
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
img {
width:100%;
height:100%;
}
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg"alt="Snow">
<div class="centered">I want This Text in the center</div>
</div>
<div class="column" style="background-color:#bbb;">
<img src="5.jpg"alt="Snow">
Try this, it should be responsive too.
.column {
width: 50%;
padding: 10px;
height:400px;
position:relative;
color: white;
}
.centered {
top: calc(50% - 24px); /*24px is the font size of the text */
position:absolute;
text-align:center;
margin: 0 auto;
width: 100%
}
img {
width:100%;
height:100%;
}
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg">
<div class="centered">I want This Text in the center</div>
</div>
</div>
I have just started of with HTML so this is a new realm for me. I am trying to get a rectangular container in the middle of the pafe,Overlaying two other containers. This is the script i wrote and this how i am trying to make it look like
<style>
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 10px;
height:800px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
img {
width:100%;
height:100%;
}
.footer {
clear:both;
font-family: 'McLaren', cursive;
background-color:black;
text-align:center;
height:50px;
padding-top: 10px;
box-sizing: border-box;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Two Equal Columns</h2>
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg"alt="Snow">
</div>
<div class="column" style="background-color:#bbb;">
<img src="5.jpg"alt="Snow">
</div>
<div class="footer">
© These are random text
</div>
</div>
</body>
</html>
This is what i am trying to get it to look like
Add position:relative to your row Class. This way the orange block can be positioned in the middle of the two .column divs
And then add this css class to the div you want to center
.middleScreen {
position:absolute;
top: 50%;
left: 50%;
width : 100px ;
height : 100px ;
background-color : orange ;
margin-top: -50px; /* = - 1/2 of the height*/
margin-left: -50px; /* = - 1/2 of the width*/
}
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 10px;
height:800px;
}
.row {
position :relative;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
img {
width:100%;
height:100%;
}
.footer {
clear:both;
font-family: 'McLaren', cursive;
background-color:black;
text-align:center;
height:50px;
padding-top: 10px;
box-sizing: border-box;
}
.middleScreen {
position:absolute;
top: 50%;
left: 50%;
width : 100px ;
height : 100px ;
background-color : orange ;
margin-top: -50px; /* = - 1/2 of the height*/
margin-left: -50px; /* = - 1/2 of the width*/
}
<h2>Two Equal Columns</h2>
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg"alt="Snow">
</div>
<div class="column" style="background-color:#bbb;">
<img src="5.jpg"alt="Snow">
</div>
<div class="footer">
© These are random text
</div>
<div class="middleScreen">
aaaaaaaaaaa
</div>
</div>
You can try using absolute position.
Example : if you’re div is 50% of the screen width and height etc.
.div{
position: absolute;
top: 25%;
left: 25%;
}
<style>
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.row{
display: flex;
justify-content: center;
align-items: center;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
img {
width:100%;
height:100%;
}
.footer {
position: fixed;
font-family: 'McLaren', cursive;
background-color: black;
text-align: center;
height: 50px;
padding-top: 10px;
box-sizing: border-box;
width: 100%;
bottom: 0;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Two Equal Columns</h2>
<div class="row">
<div class="column" style="background-color:#aaa;">
<img src="4.jpg"alt="Snow">
</div>
<div class="column" style="background-color:#bbb;">
<img src="5.jpg"alt="Snow">
</div>
</div>
<div class="footer">
© These are random text
</div>
</body>
</html>
Hope this code will help you:
body
{
padding:0;
margin:0;
}
.row
{
width: 100%;
height: 100vh;
display: flex;
position: relative;
}
.column
{
width: 50%;
height: 100%;
}
.column img
{
width: 100%;
height: 100%;
object-fit: cover;
}
.yellow
{
background-color: #ff0;
}
.red
{
background-color: #f00;
}
.overlay
{
width: 30%;
height: 30%;
box-sizing: border-box;
padding: 2rem;
background-color: #00f;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
}
<section class="row">
<div class="column yellow">
<img src="" alt="">
</div>
<div class="column red">
<img src="" alt="">
</div>
<div class="overlay">
<h1>Hello word!</h1>
</div>
</section>
I've been searching online for an answer and can't really find a solution. I'm trying to display an image and then directly below have a colored block (that is attached to the bottom of the image) for a caption on the image.
Below are my CSS code and HTML for the page.
I have this css code:
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}
/* COLUMN SETUP */
.col {
display: block;
float:left;
margin: 1% 0 1% 5.0%;
}
.col:first-child { margin-left: 0; }
/* GROUPING */
.group:before,
.group:after { content:""; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }
/* GRID OF FOUR */
.span_4_of_4 {
width: 100%;
}
.span_3_of_4 {
width: 73.75%;
}
.span_2_of_4 {
width: 47.5%;
}
.span_1_of_4 {
width: 21.25%;
}
/* GO FULL WIDTH BELOW 480 PIXELS */
#media only screen and (max-width: 480px) {
.col { margin: 1% 0 1% 0%; }
.span_1_of_4, .span_2_of_4, .span_3_of_4, .span_4_of_4 { width: 100%; }
}
/***** HTML CODE: *****/
<div class="section group">
<div class="col span_1_of_4">
<img src="XXX" style="border-width: 1px; border-style: solid; width: 190px; height: 255px;" /></a>
<br />
TEXT HERE
</div>
</div>
.img-wrapper {
background: #888;
border-width: 1px;
border-style: solid;
}
.img-wrapper img {
width: 100%;
padding-bottom: 50px;
}
<div class="section group">
<div class="col span_1_of_4">
<div class="img-wrapper">
<img src="xxx">
<br>
TEXT HERE
</div>
</div>
</div>
Wrap the image and the caption in the same div, and give that div a background color.
Here's a simple example:
<div id="wrapper">
<img src="XXX" style="width: 190px; height: 255px;" />
Text
</div>
#wrapper {
background: #ccc;
display: inline-block;
}
#wrapper a {
text-align: center;
display: block;
padding: 10px;
}
http://jsfiddle.net/eo9wfx5c/1/
I have HTML markup that consists of three divs:
<div class="gallery">Gallery</div>
<div class="content">Content</div>
<div class="sidebar">Sidebar</div>
I want to present it in two different layouts using CSS (would be nicer if I could control whether the sidebar appears on left or right):
+------------------+
| Gallery |
+------+-----------+
| Side | Content |
| | |
+------+-----------+
+------+-----------+
| Side | Gallery |
+ +-----------+
| | Content |
| | |
+------+-----------+
In fact, it would be nicer if I could control whether the sidebar appears on left or right.
I can add additional divs and/or change the source order of divs as long as content appears before sidebar. But the HTML cannot be changed on per-layout basis.
Here is my incomplete attempt to solve the problem using flexbox + order property.
/* for demonstration */
.gallery { height: 100px; background-color: #CCC; }
.content { height: 200px; background-color: #EEE; }
.sidebar { height: 150px; background-color: #AAA; }
/* common */
.middle { display: flex; flex-direction: row; flex-wrap: wrap; }
/* layout-1 */
.middle.layout-1 .gallery { order: 1; width: 100%; }
.middle.layout-1 .content { order: 3; width: 75%; }
.middle.layout-1 .sidebar { order: 2; width: 25%; }
/* layout-2 */
.middle.layout-2 .gallery { order: 2; width: 75%; }
.middle.layout-2 .content { order: 3; width: 75%; }
.middle.layout-2 .sidebar { order: 1; width: 25%; }
<div class="middle layout-1">
<div class="gallery">Gallery</div>
<div class="content">Content (this layout works perfectly)</div>
<div class="sidebar">Sidebar</div>
</div>
<hr>
<div class="middle layout-2">
<div class="gallery">Gallery</div>
<div class="content">Content (should go below gallery)</div>
<div class="sidebar">Sidebar</div>
</div>
Check this demo if you have containing div like .middle.layout-2.
HTML:
<div class="middle layout-1">
<div class="gallery">Gallery</div>
<div class="content">Content (this layout works perfectly)</div>
<div class="sidebar">Sidebar</div>
</div>
<hr>
<div class="middle layout-2">
<div class="gallery">Gallery</div>
<div class="content">Content (should go below gallery)</div>
<div class="sidebar">Sidebar</div>
</div>
CSS:
/* for demonstration */
.gallery { height: 100px; background-color: #CCC; }
.content { height: 200px; background-color: #EEE; }
.sidebar { height: 200px; background-color: #AAA; }
/* common */
.middle { display: flex; flex-direction: row; flex-wrap: wrap; }
/* layout-1 */
.middle.layout-1 .gallery { order: 1; width: 100%; }
.middle.layout-1 .content { order: 3; width: 75%; }
.middle.layout-1 .sidebar { order: 2; width: 25%; }
/* layout-2 */
.middle.layout-2 { position: relative; }
.middle.layout-2 .gallery { width: 75%; margin-left: 25%; }
.middle.layout-2 .content { width: 75%; margin-left: 25%; }
.middle.layout-2 .sidebar { position: absolute; top: 0; left: 0; width: 25%; height: 100%;}
Normally I avoid floats like the plague, but for this case they do what you need.
If you want to switch the side the sidebar is on, just swap the lefts and rights.
This will keep your layout with dynamic sized content:
.gallery {
background: red;
}
.content {
background: green;
}
.sidebar {
background: blue;
}
/* layout1 */
.layout1 .gallery {
width: 100%;
}
.layout1 .content {
width: 75%;
float: right;
}
.layout1 .sidebar {
width: 25%;
float: left;
}
/* layout2 */
.layout2 .gallery {
width: 75%;
float: right;
}
.layout2 .content {
width: 75%;
float: right;
}
.layout2 .sidebar {
width: 25%;
}
<div class="layout1">
<div class="gallery">Gallery</div>
<div class="content">Content</div>
<div class="sidebar">Sidebar</div>
</div>
<br>
<br>
<br>
<div class="layout2">
<div class="gallery">Gallery</div>
<div class="content">Content</div>
<div class="sidebar">Sidebar</div>
</div>
Let me know if this is what you want:
HTML:
First Layout
<div class="container one">
<div class="gallery">Gallery</div>
<div class="sidebar">Sidebar</div>
<div class="content">Content</div>
</div>
<br/>Second Layout
<div class="container two">
<div class="gallery">Gallery</div>
<div class="sidebar">Sidebar</div>
<div class="content">Content</div>
</div>
<br/>Third Layout
<div class="container three">
<div class="gallery">Gallery</div>
<div class="sidebar">Sidebar</div>
<div class="content">Content</div>
</div>
<br/>
CSS:
html, body {
width:100%:height:100%:
}
* {
box-sizing:border-box;
white-space:nowrap;
}
.container {
width:500px;
height:500px;
position:relative;
}
.container div {
border:1px solid black;
padding:20px;
}
/*One*/
.one.container .sidebar, .one.container .content {
float:left;
width:50%;
}
.one.container:after {
clear:both;
display:block;
content:"";
}
/*Two*/
.two.container .sidebar {
float:right;
width:50%;
}
.two.container .content {
float:left;
width:50%;
}
.two.container:after {
clear:both;
display:block;
content:"";
}
/*Three*/
.three.container>div {
position:absolute;
}
.three.container .gallery {
top:0;
left:50%;
right:0;
height:50%;
}
.three.container .sidebar {
top:0;
left:0;
bottom:0;
width:50%;
}
.three.container .content {
top:50%;
left:50%;
right:0;
height:50%;
bottom:0;
}
Demo: http://jsfiddle.net/GCu2D/665/
This doesn't have additional div or markup. You need to just toggle the classe in the container.
I want the Content A, Content B, and Content C columns to be horizontally centered. I have this code been trying to add
http://jsfiddle.net/hsX5q/24/
HTML:margin: 0 auto to .columns-container and it doesn't work. Could anyone help?
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width: 300px;
margin: 0 auto;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>
If you add text-align: center to the declarations for .columns-container then they align centrally:
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>
This does, though, require that you reset the .column elements to text-align: left (assuming you want them left-aligned, obviously (JS Fiddle demo).
Sometimes you have things other than text inside a table cell that you'd like to be horizontally centered. In order to do this, first set up some css...
<style>
div.centered {
margin: auto;
width: 100%;
display: flex;
justify-content: center;
}
</style>
Then declare a div with class="centered" inside each table cell you want centered.
<td>
<div class="centered">
Anything: text, controls, etc... will be horizontally centered.
</div>
</td>
Short snippet for future visitors - how to center horizontal table-cell (+ vertically)
html, body {
width: 100%;
height: 100%;
}
.tab {
display: table;
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
text-align: center; /* the key */
background-color: #EEEEEE;
}
.content {
display: inline-block; /* important !! */
width: 100px;
background-color: #00FF00;
}
<div class="tab">
<div class="cell">
<div class="content" id="a">
<p>Content</p>
</div>
</div>
</div>
Just add this class in your css
.column p{
text-align:center;
}