I have been trying to center this image inside its div, but it keeps aligning to the left.
HTML
<div id="main">
<div id="left">This is a test</div>
<div id="right">
<h2 id="fish">Fishtail</h2>
<img height="150px" width="250px" src="image.jpg">
</div>
</div>
CSS
#left, #right {
width: 40%;
margin:5px;
padding: 1em;
color:#51CBED;
font-size:20px;
padding:15px;
background-color:white;
}
#left {
float:left;
}
#right {
float:right;
}
#fish {
text-align:center;
}
#main {
height:800px;
width:950px;
background-color:black;
opacity:.75;
filter:alpha(opacity=75);
margin-top:10px;
margin-bottom:75px;
padding:20px;
}
I have tried using margin: auto; and align:middle; but neither seem to work.
No need for relative/absolute positioning.
A simple way to solve this is by setting display:block on the image.
jsFiddle demo - it works perfectly.
CSS
#main #right img {
margin: 0px auto;
display: block;
}
Try this:
HTML:
<div id="main">
<div id="left">This is a test</div>
<div id="right">
<h2 id="fish">Fishtail</h2>
<img height="150px" width="250px" src="image.jpg">
</div>
</div>
CSS:
#left, #right {
width: 40%;
margin:5px;
padding: 1em;
color:#51CBED;
font-size:20px;
padding:15px;
background-color:white;
}
#left {
float:left;
}
#right {
float:right;
text-align:center;
}
#fish {
text-align:center;
}
#main {
height:800px;
width:950px;
background-color:black;
opacity:.75;
filter:alpha(opacity=75);
margin-top:10px;
margin-bottom:75px;
padding:20px;
}
Jsfiddle: http://jsfiddle.net/hdMEQ/
I basically just added text-align: center; property in #right id.
Try adding:
#right img {
margin:auto;
display:block;
}
JSfiddle
Will get you the dead center with variable width and height or no need to know widths, heights.
set the position absolute in relative to which div you want the center the image
#main #right img {
position:absolute;
display: block;
top:0;right:0;bottom:0;left:0;
margin:auto;
}
#right {
position:relative;
}
<style type="text/css">
.centerDiv {
margin-right: auto;
margin-left: auto;
width: 80px;
}
</style>
<div id="main">
<div id="left">This is a test</div>
<div id="right" class="centerDiv">
<div class="centerDiv">
<h2 id="fish">Fishtail</h2>
<img height="80px" width="80px" src="http://www.w3schools.com/images/w3html.gif">
</div>
</div>
</div>
</body>
</html>
Related
I am new to programming, I want to arrange div elements as following:
header div 1
--------------------
left 2 | center 3| right 4 |
---------------------
footer div 5
I tried position and display attributes but failed to get desired result.
How can I arrange in this way and how to arrange any div element in simple way?
Try this code for your div structure.
.wrapper {
color: #fff;
float: left;
text-align: center;
width: 100%;
}
.header {
background-color: red;
float: left;
width: 100%;
}
.left {
background-color: orange;
float: left;
width: 25%;
}
.center {
background-color: green;
float: left;
width: 50%;
}
.right {
background-color: orange;
float: left;
width: 25%;
}
.footer {
background-color: blue;
float: left;
width: 100%;
}
.header, .footer {
margin: 1% 0;
}
<div class="wrapper">
<div class="header">
<h1>Header Div</h1>
</div>
<div class="left">
<h1>Left Div</h1>
</div>
<div class="center">
<h1>Center Div</h1>
</div>
<div class="right">
<h1>Right Div</h1>
</div>
<div class="footer">
<h1>Footer Div</h1>
</div>
</div>
Try this code
HTML
<div class="header">Header Div</div>
<div class="left-section"></div>
<div class="center-section"></div>
<div class="right-section"></div>
<div class="footer-section">Footer</div>
CSS
.header{
width:100%;
height:50px;
background:green;
}
.left-section{
height:500px;
width:29%;
display:inline-block;
background:yellow;
padding:0px;
margin:0px;
}
.right-section{
height:500px;
width:29%;
display:inline-block;
background:gold;
padding:0px;
margin:0px;
}
.center-section{
height:500px;
width:40%;
display:block;
display:inline-block;
background:gray;
padding:0px;
margin:0px;
}
.footer-section{
width:100%;
height:50px;
background:orange;
}
Codepen link
http://codepen.io/santoshkhalse/pen/gwWbAV
There is a little white gap on the downside of mainBox, what is it and which cause it?
body {
margin: 0px;
padding: 0px;
}
div {
border: 1px solid black;
box-sizing: border-box;
font-size: 30px;
}
#wrapper {
width: 900px;
margin: 0px auto;
}
#header {
width: 100%;
height: 100px;
background: red;
}
#container {
width: 100%;
height: 100px;
background: black;
}
#mainBox {
width: 75%;
height: 150px;
background: blue;
float: left;
}
#sideBox {
width: 25%;
height: 100px;
background: yellow;
float: left;
}
#footer {
clear: both;
width: 100%;
height: 50px;
background: white;
}
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>
Add border: none to #container
body{
margin:0px;
padding:0px;}
div{
border:1px solid black;
box-sizing:border-box;
font-size:30px;}
#wrapper{
width:900px;
margin:0px auto;}
#header{
width:100%;
height:100px;
background:red;}
#container{
border: none; /**** Add this extra line ****/
width:100%;
height:100px;
background:black;}
#mainBox{
width:75%;
height:150px;
background:blue;
float:left;
}
#sideBox{
width:25%;
height:100px;
background:yellow;
float:left;
}
#footer{
clear:both;
width:100%;
height:50px;
background:white;
}
<body>
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>
You have a white gap here because your blue div (#mainBox) is 150px, which is taller than its container div (#container), set at 100px. This has caused the #mainBox div to extend outside the container with a slight offset at the leftmost side due to the black border applied to the div.
There are a number of ways to correct this gap, such as removing the border or the hardcoded height values.
It is because #sideBox have 100px height and #mainBox have 150px height. Make both same solve your issue.
Here i set #sideBox to height:150px;
And you have given parent container #container to height:100px and child to height:150px.
Edit:
I think i misunderstood your issue before. But as i say above. Your parent height is small then your child. Make it proper solve your issue.
body{
margin:0px;
padding:0px;}
div{
border:1px solid black;
box-sizing:border-box;
font-size:30px;}
#wrapper{
width:900px;
margin:0px auto;}
#header{
width:100%;
height:100px;
background:red;}
#container{
width:100%;
height:150px;
background:black;}
#mainBox{
width:75%;
height:150px;
background:blue;
float:left;
}
#sideBox{
width:25%;
height:100px;
background:yellow;
float:left;
}
#footer{
clear:both;
width:100%;
height:50px;
background:white;
}
<body>
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>
One container with four or more divs inside that line up like a perfect puzzle and the last div fills out the remaining height..
the image illustrates how it should look like
where I am at: http://jsfiddle.net/meb9h3vx/
.wrap {
float: left;
background: #fff;
width: 100%;
}
.get2 {
float: left;
height: auto;
width: 50%;
}
<div class="wrap">
<div class="get2">
<h1>1</h1>
abc
</div>
<div class="get2">
<h1>2</h1>
</div>
<div class="get2">
<h1>3</h1>
d
<br>e
</div>
<div class="get2">
<h1>4</h1>
</div>
</div>
Try using this -
remove float from both .wrap and .get2
remove text-align: center; from body css
add following css to .get2 - text-align: center; display: inline-block;
vertical-align: top;
add margin-left: -4px; css to .get2:nth-child(2) { }
and add margin-left: -4px; margin-top: -17px; css to .get2:nth-child(4)
Flexbox to the rescue with no dirty hacks:
body {
background:#000;
color:#ccc;
text-align:center;
margin:10px;
}
.wrap {
background:#fff;
width:100%;
display:flex;
flex-wrap: wrap;
height:auto;
}
.get2 {
height:auto;
width:50%;
}
.get2:nth-child(1) {
background:red;
}
.get2:nth-child(2) {
background:green;
}
.get2:nth-child(3) {
background:yellow;
}
.get2:nth-child(4) {
background:blue;
}
<div class="wrap">
<div class="get2">
<h1>1</h1>
abc
</div>
<div class="get2">
<h1>2</h1>
</div>
<div class="get2">
<h1>3</h1>
d
<br>e</div>
<div class="get2">
<h1>4</h1>
</div>
</div>
Hey you can try by giving min-height as i am giving below
.get2{
float:left;
height:auto;
width:50%;
min-height:120px
}
Fixed it! So I inline-block the div and nth-child(2n) margin-left:-4px to get rid of the white-space and vertical-align:top and then the last part I added: padding-bottom: 99999px; margin-bottom: -99999px; http://jsfiddle.net/p428b71y/1/
.wrap{
float:left;
background:#fff;
width:100%;
overflow:hidden;
}
.get2:nth-child(2n){
margin-left:-4px;
}
.get2{
vertical-align:top;
display:inline-block;
height:auto;
width:50%;
padding-bottom: 99999px;
margin-bottom: -99999px;
}
I have the following code, which displays chaotic the images . I want to display the images smaller(33.3%) in a row, all with the same width and height and below them a link, but i'm stuck . Any ideas?
<style>
h4 {
direction:ltr;
text-align:center;
}
.line {
position:relative;
width: 33%;
height:33%;
margin-right: 1%;
}
.row {
clear:both;
margin-left: auto;
margin-right: auto;
margin-top: 0;
margin-bottom: 0;
}
</style>
<div class="row">
<div class="line">
<img src="http://www.hdwallpapersmaza.com/wp-content/uploads/2014/03/Download-Space- Background-Images-4.jpg" />
<h4>www.asite.ro</h4>
</div>
<div class="line">
<img src="https://static-secure.guim.co.uk/sys-images/Guardian/Pix/pictures/2013/3/13/1363178656076/Google-talking-shoe-011.jpg" />
<h4>www.asite.ro</h4>
</div>
<div class="line">
<img src="http://www.wired.com/wiredenterprise/wp-content/uploads//2012/10/ff_googleinfrastructure_large.jpg" />
<h4>www.asite.ro</h4>
</div>
</div>
you just need to clean your code a bit
h4{direction:ltr;
text-align:center;}
.line{
position:relative;
width: 32%;
height:33%;
margin-right:1%;
float:left;
}
.row{
width:100%;
overflow:hidden;
}
img
{
width:100%;
height:auto;
}
http://fiddle.jshell.net/prollygeek/GsrFR/
My pen: http://codepen.io/helloworld/pen/IGsoe
I want to center align all divs with the yellow border how can I do that?
I tried margin:0 auto; on the parent container but that did not help.
The "items" with the yellow border should keep their percentage width. Do not change this to fixed pixel values.
HTML code:
<div class="table">
<div id="navigationWrapper">
<div class="table">
<div id="left"><image width="40px" /></div>
<div id="navBar" style="width:100%; height: 100px; background-color: grey;">
<div class="cellContainer">
<div class="alarmTemplate">A</div>
</div>
<div class="cellContainer">
<div class="alarmTemplate">B</div>
</div>
<div class="cellContainer">
<div class="alarmTemplate">C</div>
</div>
<div class="cellContainer">
<div class="alarmTemplate">D</div>
</div>
</div>
<div id="right"><image width="40px" /></div>
</div>
</div>
<div id="navigationWheeler">test</div>
</div>
CSS:
body {
padding: 0;
margin: 0;
width:100%;
height:100%;
}
.cellContainer {
margin:0 auto;
width: 20%;
float: left;
background:black;
}
.alarmTemplate{
height:80px;
margin-top:10px;
margin-bottom:10px;
margin-left:10px;
margin-right:10px;
background:lightgray;
border:yellow solid 2px;
}
#navBar, #right, #left, #navigationWheeler {
height:80px;
background:yellow;
display:table-cell;
vertical-align:middle;
}
.table {
display:table;
min-width:100%;
margin:0 auto;
}
#right, #left, #navigationWheeler {
width:40px;
}
#navigationWheeler{
background:green;
text-align:center;
}
To center the four DIVs set them to
.cellContainer {
float: none;
display: inline-block;
}
and center the navbar contents
#navBar {
text-align: center;
}
Demo Link with corrected css
.cellContainer {
margin:0 auto;
width: 20%;
/*float: left;*/
background:black;
display: inline-block;
}
#navBar{
text-align: center;
}