this is what i created:
This is what i want like:
this is my code:
<div id="OR"><span style=" vertical-align: middle;background:blue;">OR</span></div>
this is css:
div #OR { border-radius:50%;border-style:1px solid black;background:red;width:42px;height:42px;float:right; background:red;vertical-align: middle;}
div #OR span{ vertical-align: middle; }
so please help me to bring thart OR in a center of the div.
I don't think you will need an extra element for the OR text, what you need is the line-height property
Demo
div {
height: 50px;
width: 50px;
border: 1px solid #ddd;
border-radius: 50%;
font-weight: bold;
line-height: 50px; /* Equals elements height */
text-align: center;
}
This solution is perfect if you want to vertical-align single word, if you want to perfectly center an element horizontally and vertically, other two approaches are to use display: table-cell; with vertical-align: middle; or use CSS Positioning.
CSS Positioning way..
Explanation: Here am using position: relative; on the wrapper/parent element and than am assigning position: absolute; for the child element. Though, here's a catch, you need to assign fixed width to the child element you are trying to center.
Demo 2
div {
height: 50px;
width: 50px;
border: 1px solid #ddd;
border-radius: 50%;
font-weight: bold;
position: relative;
}
div span {
position: absolute;
border: 1px solid #f00;
left: 50%;
top: 50%;
height: 20px;
width: 24px;
margin-top: -10px; /* Half of the elements height */
margin-left: -12px; /* Half of the elements width */
}
Related
This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
How can I horizontally center an element?
(133 answers)
Closed 4 years ago.
This is what I am trying to achieve. I tried using absolute position and it seemed to work on one screen size but doesn't work for others.
.countdown {
width: 200px;
background: #b02d1b;
margin: 0;
border-radius: 5px;
color: #fff;
text-align: center;
padding: 5px;
position: absolute;
z-index: 1;
}
.countdown-bg {
width: 100%;
border-top: solid 1px #ccc;
position: absolute;
margin: 12px;
}
<div class="countdown"> Final Hours </div>
<div class="countdown-bg"></div>
I think I have a solution for your question:
.divOuter {
background-color: #cccccc;
width: 100%;
height: 2px;
position: absolute;
}
.divInner {
background-color: #cc0000;
width: 130px;
height: 20px;
position: relative;
top: -12px;
margin: 0 auto;
color: #ffffff;
text-align: center;
font-family: Verdana;
font-size: 0.8em;
font-weight: bold;
border-radius: 10px;
border: 4px solid #ffffff;
}
<div class="divOuter">
<div class="divInner">FINAL HOURS!</div>
</div>
Brief explanation:
We have two divs, the second one in the html is on top of the first one. This placement in CSS is called a float.
When we need this effect we use the "position" property of the CSS with values like Absotule and Relative.
In this case, the first Div makes the thin line and the second Div makes the red rectangle.
The "top", "left", "right" and "button" property of the css causes to align the second Div relative to the first. And the property "margin: auto" causes the internal div to be aligned to the center of the external div.
I hope I've helped!
Without flexbox
Wrapp one div inside another div. Make the outer div position: relative and the inner div position: absolute. Center the inner div with left and transform
.countdown {
position: relative;
border-top: solid 1px #ccc;
margin: 15px 0;
}
.countdown>.content {
width: 200px;
text-align: center;
background: #b02d1b;
border-radius: 5px;
color: #fff;
padding: 5px;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1;
text-transform: uppercase;
}
<div class="countdown">
<div class="content">Final Hours!</div>
</div>
With flexbox
The advantage of using flexbox is that you don't have to set positions and that you can center with justify-content.
.countdown {
border-top: solid 1px #ccc;
margin: 15px 0;
display: flex;
justify-content: center;
}
.countdown>.content {
width: 200px;
text-align: center;
background: #b02d1b;
border-radius: 5px;
color: #fff;
padding: 5px;
transform: translateY(-50%);
z-index: 1;
text-transform: uppercase;
}
<div class="countdown">
<div class="content">Final Hours!</div>
</div>
I am having issues getting my viewproductbutton to go overtop of it's parent div. I have added a z-index to the element and have tried to position it with position: relative; and position: absolute;. Nothing is getting it to position in the top right corner as I want it.
Here is a fiddle https://jsfiddle.net/Lrdswa5a/
What am I not doing right
Here is the working jsfiddle: https://jsfiddle.net/Lrdswa5a/5/
If you replace your '.featuredproductspic' and '.viewproductbutton' with the following:
.featuredproductspic {
/*width: 100%;*/
border: 1px solid #D9D9D9;
min-height: 350px;
max-height: 350px;
float: left;
position: relative;
}
and
.viewproductbutton {
width: 150px;
padding: 15px 15px;
background-color: #909090;
color: #FFFFFF;
font-size: 1em;
font-weight: bold;
cursor: pointer;
text-align: center;
position:absolute;
top:0;
right:0;
}
Then the button is on the top right of the image.
Cheers!
I have a set of divs that vary in size depending on an image inside it. Inside each div I would like two more divs, one is floated left and the other is floated right, like so:
I sort of accomplished it this way ... html:
<div class="image-wrap">
<img src="{{ img }}">
<div class="lookbook-title"><h5 >{{ title }}</h5></div>
<div class="item-buy">{{ theme:partial src="_buynow" }}</div>
</div>
and css:
div.image-wrap {
max-height: 1000px;
max-width: 100%;
border: 0;
display: inline-block;
height: auto;
box-sizing: border-box;
}
.lookbook-title {
position: relative;
top: -36px;
float: left;
padding-left: 10px;
padding-right: 10px;
color: #f7f7f7;
}
.item-buy {
position: relative;
top: -56px;
float: right;
padding-right: 20px;
pointer-events: none;
-webkit-font-smoothing: antialiased;
fill: #f7f7f7;
}
The reason I say "sort of" is because it initially was working just fine, but now the floated divs are appearing on above and outside their parent divs. What is interesting is that if I inspect the problem with dev tools and uncheck and recheck the "float" on either div both go back to where I want them to go...
You need to clear your floats.
Here is a interesting article that explains it in detail: http://css-tricks.com/snippets/css/clear-fix/
Hope this helps.
You should use position: absolute; for your 'floating' elements instead of float.
You'll need to add position: relative; to the parent wrap element - this will tell the children to respect the bounds of this element instead of floating somewhere outside of it. Then you can add position: absolute; to each of the children that you want to float and use top, bottom, left, right to control where the box is positioned. Experiment with different values to get the hang of it.
div.image-wrap {
height: 300px;
width: 400px;
position: relative;
border: 1px solid red;
}
.lookbook-title,
.item-buy {
background: white;
position: absolute;
bottom: 10px;
height: 100px;
width: 100px;
}
.lookbook-title {
border: 1px solid lime;
left: 10px;
}
.item-buy {
border: 1px solid blue;
right: 10px;
}
<div class="image-wrap">
<img src="http://www.placehold.it/400x300.jpg">
<div class="lookbook-title"><h5>Div 1</h5></div>
<div class="item-buy">Div 2</div>
</div>
I am trying to get this done in HTML and CSS. I am able to get the box done using the border and padding. But how do I get the line above?
Here is what I have so far:
.november {
padding: 1%;
border: 2px solid #000;
}
<div class="november">November 2014</div>
Pseudo element goodness
The HTML
It's a one liner:
<div>November 2014</div>
The CSS
The vertical line is created with a :before pseudo element:
The :before pseudo element is given position: absolute
left: 50% shifts the line to the middle and bottom: 100% pops the line above the div
The line is created by the 2px width
margin-left: -2px shifts the line 2px to the left to correctly offset its position (this is equal to the width)
The div is made position: relative and the position: absolute :before will position itself in relation to it. Space above the div is created with the top margin.
Complete Example
In this example, display: inline-block allows the div to expand and retract with its contents.
div {
padding: 10px;
border: solid 2px #000;
display: inline-block;
position: relative;
margin-top: 50px;
}
div:before {
content: '';
width: 2px;
height: 50px;
background: #000;
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -2px;
}
<div>November 2014</div>
I tried this and got it right:
body {
background: #EEE;
}
.november {
margin: 0;
padding: 1%;
border: 2px solid white;
clear: both;
}
<div class="col-sm-2">
<hr style="width: 2px; border-top: 50px solid white; padding: 0; text-align: center; margin: auto;" />
<div class="november">November 2014</div>
</div>
tried text-align center and margin auto, both doesn't work and I do not want to used to use the 'margin hack' for centering..
http://jsfiddle.net/st9AM/1/
.circle{
float: left;
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 2px solid #DDD;
}
.inner{
float: left;
position: relative;
width: 60px;
height: 60px;
border-radius: 50%;
border: 2px solid #DDD;
}
First of all, using margin: auto; is not a hack
And to center your circle inside the circle, you can use positioning techniques, like position: absolute;. Here, I am using position: absolute; on the inner circle, than am assigning top and left properties with a value of 50% and than am using margin-top and margin-left and deducting 1/2 of the height and width.
Why am deducting 32px? As I already said am deducting exactly half the total width and height so this also includes the border of your element which is set to 2px which makes your element 64px in height and width respectively.
To vertical-align the + symbol, am using line-height property as I can only see a single character to be vertically aligned(you didn't said but technically I can assume what shape are you looking for), alternatively you can also use vertical-align: middle; but you need to set the container element to display: table-cell;
Demo
Last but not the least, you should nest the span tag inside the inner circle div.
.circle{
float: left;
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 2px solid #DDD;
}
.inner{
text-align: center;
line-height: 60px;
position: absolute;
top: 50%;
margin-top: -31px;
left: 50%;
margin-left: -31px;
width: 60px;
height: 60px;
border-radius: 50%;
border: 2px solid #DDD;
}
Here's a cleaner solution.
with one HTML element only.
HTML:
<div class='circle'></div>
CSS:
*
{
margin: 0;
padding: 0;
}
.circle, .circle:after
{
border-radius: 50%;
border: 2px solid #DDD;
text-align: center;
}
.circle
{
width: 120px;
height: 120px;
font-size: 0;
}
.circle:before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.circle:after
{
content:'+';
font-size: 20px;
padding: 20px 0; /* 2*padding + font size = innerCircle height*/
display: inline-block;
vertical-align: middle;
width: 50%;
}
You had "float: left" in the inner circle, which you didn't need
//float: left;
Working fiddle
remove float left and use margin: 0 auto;
.circle{
position: relative;
width: 120px;
height: 120px;
border-radius: 50%;
border: 2px solid #DDD;
margin:0 auto;
}
Have a look at this fiddle. You wrote float:left; and wanted to center the image. Remove the float:left; and it works fine.
Current browsers (May-22) work with this (replace 261px and 165x by 50% of your image size... mine is 522px x 330px ):
{
position:absolute;
left: calc( 50% - 261px );
top: calc( 50% - 165px );
}