I am new to using svgs in CSS. My goal is to place the svg that I have referenced below (a battery icon) inside of the divs of item class, centered vertically, in the first case to the left of the bullet (in the item labeled foo), and in the second case to the right of the text (in the item labeled bar).
I have tried using changing the display of the image to inline-block, and inserting
<image xlink:href="https://www.svgrepo.com/show/62382/low-battery.svg" />
within the item (see below), but this does not work. What is the best practice with inserting svg elements inline in divs with other elements? Should span or background-size:contain be used?
#foo { background: #00FF7F; }
#bar { background: #ff0000;}
.items { text-align: center; font-weight: 500; color: #fff; border-width: 2px;
border-color: #fff; padding: 2px;}
.item, .item-selected{ display: inline-block; font-weight: 500;
text-align: center;
vertical-align: middle;
border-width: 2px;
background-color: #484848;
opacity: 0.85;
border-color: green;
border-width: 2px;
border-radius: 3px;
padding: 2px;
color: #fff;
cursor: pointer;
}
.item-selected{
background-color: #fff;
color: #181818;
}
.bullet { display: inline-block; margin: 3px 3px 0px 10px; width: 10px; height: 10px; border-radius: 7px; }
.label { display: inline-block; font-weight: 300;}
image {
width: 50px;
height: 50px;
display: inline-block;
}
<div class='items'>
<div class='item' id= 'wrk'><image xlink:href="https://www.svgrepo.com/show/62382/low-battery.svg" /><div class='bullet' id='foo'></div><div class='label'>foo</div></div>
<div class='item-selected' id = 'brk'><div class='bullet' id='bar'></div><div class='label'>bar</div></div>
</div>
<svg>
<image xlink:href="https://www.svgrepo.com/show/62382/low-battery.svg" />
</svg>
You can use svg as image using img then adjust CSS for alignment:
#foo {
background: #00FF7F;
}
#bar {
background: #ff0000;
}
.items {
text-align: center;
font-weight: 500;
color: #fff;
border-width: 2px;
border-color: #fff;
padding: 2px;
}
img {
vertical-align:middle;
}
.item,
.item-selected {
display: inline-block;
font-weight: 500;
text-align: center;
vertical-align: middle;
border-width: 2px;
background-color: #484848;
opacity: 0.85;
border-color: green;
border-width: 2px;
border-radius: 3px;
padding: 2px;
color: #fff;
cursor: pointer;
}
.item-selected {
background-color: #fff;
color: #181818;
}
.bullet {
display: inline-block;
margin: 3px 3px 0px 10px;
width: 10px;
height: 10px;
border-radius: 7px;
}
.label {
display: inline-block;
font-weight: 300;
}
image {
width: 50px;
height: 50px;
display: inline-block;
}
<div class='items'>
<div class='item' id='wrk'>
<img src="https://www.svgrepo.com/show/62382/low-battery.svg" height="20" width="40" >
<div class='bullet' id='foo'></div>
<div class='label'>foo</div>
</div>
<div class='item-selected' id='brk'>
<div class='bullet' id='bar'></div>
<div class='label'>bar</div>
<img src="https://www.svgrepo.com/show/62382/low-battery.svg" height="20" width="40" >
</div>
</div>
Related
Need to make two buttons align a few pixels left and right of the centre
I need div left to be 1px to the left of the centre. And right to be 1px right of it.
Any ideas how?
.btn {
font-family: helvetica;
font-weight: bolder;
padding: 10px;
border-radius: 2px;
color: black;
border: 2px solid black;
background-color: white;
width: 100px;
text-decoration: none;
}
.btn2 {
font-family: helvetica;
font-weight: bolder;
padding: 10px;
border-radius: 2px;
color: white;
border: 2px solid white;
background-color: black;
width: 100px;
text-decoration: none;
}
.divleft {
margin: auto 0;
text-align: left;
}
.divright {
margin: auto 0;
text-align: right;
}
<div class="divleft"> Memento </div>
<div class="divright"> Mori </div>
Use flexbox:
.box {
display: flex;
width: 100%;
}
.left,
.right {
width: 100%;
}
.left {
text-align: right;
margin-right: 10px;
}
a {
font-family: helvetica;
font-weight: bolder;
padding: 10px;
border-radius: 2px;
border: 2px solid black;
width: 100px;
text-decoration: none;
}
.btn {
color: black;
background-color: white;
}
.btn2 {
color: white;
background-color: black;
}
<div class="box">
<div class="left">
Memento
</div>
<div class="right">
Mori
</div>
</div>
Codepen: https://codepen.io/manaskhandelwal1/pen/jOMgpbd
I created a CodePen that shows how to make it work: https://codepen.io/kaireidcasey/pen/rNMXrde
The parent element needs display: flex set on it. The children each need an auto margin on one side. I am sure there are other ways to solve this, as well.
Code snippets:
HTML:
<div class="container">
<div class="divleft">
Memento
</div>
<div class="divright">
Mori
</div>
</div>
CSS:
.btn {
font-family: helvetica;
font-weight: bolder;
padding: 10px;
border-radius: 2px;
color: black;
border: 2px solid black;
background-color: white;
width: 100px;
text-decoration: none;
}
.btn2 {
font-family: helvetica;
font-weight: bolder;
padding: 10px;
border-radius: 2px;
color: white;
border: 2px solid white;
background-color: black;
width: 100px;
text-decoration: none;
}
.divleft {
margin-right: 1px;
margin-left: auto;
}
.divright {
margin-left: 1px;
margin-right: auto;
}
.container {
display: flex;
}
I have been trying for a while now and cant seem to achive the bellow design:
.exploreItem {
background-color: #353258;
/* rgba(31, 31, 31, 1) */
border: 1px solid #4152F1;
color: white;
/* padding: 20px 24px; */
cursor: pointer;
width: 90%;
height: 45px;
/* font-size: 15px;
font: Arial;
font-family: sans-serif;
display: block; */
}
.exploreImg {
/* background-color: lightblue; */
display: inline-block;
vertical-align: top;
height: 30px;
/* width: 10px; */
padding-left: 10px;
}
.exploreLabel {
/* vertical-align: middle; */
/* background-color: grey; */
display: inline-block;
font-size: 15px;
font: Arial;
font-family: sans-serif;
/* width: 10px; */
margin-top: 0px;
}
<div class="exploreItem" id="exploreItem">
<img class="exploreImg" src="images/defaultProfImg.png">
<label class="exploreLabel">Explore</label>
</div>
How can I create the intended design layout? (An image next to the explore label like in the image)
You even don't need the image just use css to do it. And even if you use image you can use display: flex for .exploreItem and align-items: center can do the magic.
I had put down the simpler css only solution here with :before pseudo element.
.exploreItem {
background-color: #353258;
border: 1px solid #4152F1;
color: white;
cursor: pointer;
padding: 16px;
width: 90%;
border-radius: 32px;
}
.exploreLabel {
display: flex;
align-items: center;
font-size: 15px;
font: Arial;
font-family: sans-serif;
margin-top: 0px;
}
.exploreLabel:before {
content: '';
height: 28px;
width:28px;
background-color: #4152F1;
border: 1px solid #a89dff;
display: block;
margin-right: 16px;
border-radius: 50%;
}
<div class="exploreItem" id="exploreItem">
<label class="exploreLabel">Explore</label>
</div>
.wrapper {
width: 200px;
}
.exploreItem {
width: 100%;
display: flex;
background: transparent;
padding: 20px 30px;
border: 1px solid transparent;
border-radius: 50px;
align-items: center;
cursor: pointer;
}
.exploreItem label {
color: #919191;
}
.exploreItem.active {
display: flex;
background: #2F2D50;
border: 1px solid #483EF1;
}
.exploreItem.active label {
color: #ffffff;
}
.exploreItem i {
display: block;
background: #A3A3A3;
border: 1px solid transparent;
width: 25px;
height: 25px;
border-radius: 50px;
margin-right: 10px;
}
.exploreItem.active i {
background: #483EF1;
border: 1px solid #A5A0FF;
}
<div class="wrapper">
<div class="exploreItem active">
<i></i>
<label class="exploreLabel">Explore</label>
</div>
<div class="exploreItem">
<i></i>
<label class="exploreLabel">Explore</label>
</div>
</div>
Use below code:
HTML:
<div class="outer">
<div class="round"></div>
<label>
<span>Explore</span>
</label>
</div>
CSS:
.outer
{
background: #353258;
border: 1.5px solid #4152F1;
color: white;
padding: 7px 8px;
cursor: pointer;
width: 150px;
height:33px;
border-radius: 30px;
cursor: pointer;
}
.round
{
background: #502ffb;
height: 28px;
width: 28px;
display: inline-block;
border: 1px solid white;
border-radius: 50%;
}
label
{
color: white;
display: inline-block;
cursor: pointer;
}
label span
{
position: absolute;
margin-top: -24px;
margin-left: 3px;
font-size: 17px;
}
The problem is that the elements are not appearing next to each other as I want them to.
all three elements are already on float left and in the right order but they are still not lining up in the right way.
(so probably, the problem is that some elements are position:absolute or relative while they don't need to. The problem is: you can't change that without disrupting the drop-up menu of #Timer. That)
green_button {
background-color: #027fed;
color: white;
border-radius: 2px;
cursor: pointer;
float: right;
position: relative;
}
.green_button:active {
background-color: #eee;
color: black;
}
.keuze {
position: absolute;
width: 100px;
height: 100px;
float: left
}
#timer {
color: black;
background: #eee;
border: none;
padding-left: 10px;
font-size: 12px;
border-radius: 2px;
float: left;
padding: 12px 12px;
cursor: pointer;
list-style-type: none;
position: Relative;
margin-top: -14px;
width: 80px;
}
#timer:hover {
color: white;
background: #027fed;
}
li {
background-color: #eee;
font-size: inherit;
width: 150px;
position: relative;
float: left;
bottom: 31px;
left: 0;
display: block;
font-size: 12px;
text-decoration: none;
font-family: tahoma;
color: black;
width: 50px;
height: auto;
border: 1px solid #;
border-width: 1px 1px 0 0;
background: #eee;
background-color: rgb(238, 238, 238);
padding-left: 10px;
line-height: 38px;
border-radius: 2px;
height: auto;
line-height: 1em;
padding: 5px 10px;
width: 129px;
margin-bottom: 1px;
margin-left: 431px;
}
li:hover {
cursor: pointer;
background-color: #027fed;
color: white
}
.list {
display: none;
list-style-type: none;
position: absolute !important;
}
.keuze:hover .list {
display: block
}
.messages_compose {
padding: 10px;
margin-bottom: auto;
}
.messages_textarea_container {
display: inline-block;
width: 400px;
margin-left: 10px;
}
.messages_textarea {
border: 3px solid lightgray;
margin-top: 0;
margin-bottom: 10px;
padding: 5px;
width: 400px;
height: 40px;
resize: none;
float: left;
border-radius: 2px;
position: absolute;
}
.button {
border: none;
font-size: 12px;
padding: 12px 12px;
height: 40px text-align: center;
}
<div class="messages_compose">
<div class="vraag">CV</div>
<div class="messages_textarea_container">
<textarea class="messages_textarea"></textarea>
<button class="button green_button">Stuur</button>
<ul class="keuze">
<button id="timer">1 Jaar</button>
<div class="list">
<li id="jaar">jaar</li>
<li id="maand">maand</li>
<li id="week">week</li>
<li id="dag">dag</li>
</div>
</ul>
</div>
<script>
document.getElementById("jaar").onclick = function() {
jaar()
};
document.getElementById("maand").onclick = function() {
maand()
};
document.getElementById("week").onclick = function() {
week()
};
document.getElementById("dag").onclick = function() {
dag()
};
</script>
<script src="../scripten.js"></script>
</div>
If you want them side by side (if width allows), to make things simpler, make sure they are inline elements.
textarea and button are inline elements by default and unsorted-list are block element by default
Inline Elements
Block Elements
So basically, all you need is to change your ul to display: inline-block;
* {
padding: 0;
margin: 0;
vertical-align: text-top;
}
green_button {
background-color: #027fed;
color: white;
border-radius: 2px;
cursor: pointer;
}
.green_button:active {
background-color: #eee;
color: black;
}
.keuze {
display: inline-block; /* added */
width: 100px;
list-style: none;
}
.keuze li {
width: 100%;
}
#timer {
color: black;
background: #eee;
border: none;
padding-left: 10px;
font-size: 12px;
border-radius: 2px;
padding: 12px 12px;
cursor: pointer;
list-style-type: none;
width: 80px;
}
<div class="messages_compose">
<div class="vraag">CV</div>
<div class="messages_textarea_container">
<textarea class="messages_textarea"></textarea>
<button class="button green_button">Stuur</button>
<ul class="keuze">
<button id="timer">1 Jaar</button>
<div class="list">
<li id="jaar">jaar</li>
<li id="maand">maand</li>
<li id="week">week</li>
<li id="dag">dag</li>
</div>
</ul>
</div>
</div>
In addition, I have removed all your float and position from your css which I think as #Temani Afif says, you were just trying fix the issue by adding more to it.
I also have added followings just to make it tidier which is irrelevant to your issue. (That is to remove the default margin, padding and vertical align from all html elements to make it tidier and avoid unexpected behavior of different browsers)
* {
padding: 0;
margin: 0;
vertical-align: text-top;
}
I have arranged the anchor tag with text with borders.
I want to make the text to center inside the block.
I have tried padding, margin left, float but nothing is working.
body {
background-color:black;
background-repeat: no-repeat;
background-size: 100% 100%;
}
.title{
height: 17%;
width: 100%;
margin: 0%;
text-align: center;
font-size: 2rem;
color: white;
}
.subjects{
color: white;
width: 28%;
height: 7%;
border: 2px solid white;
font-size: 2rem;
border-radius:20px;
margin-top: 86px;
margin-left: 8px;
text-align:center;
vertical-align: middle;
}
.maths{
margin-left: 24%;
margin-top: 41px;
}
.maths a{
text-decoration: none;
color: white;
border: 2px solid white;
padding: 4px;
padding-right: 140px;
font-size: 2rem;
border-radius: 20px;
cursor: pointer;
vertical-align:middle;
display: inline-block;
}
.physics{
margin-left: 24%;
margin-top: 41px;
}
.physics a{
text-decoration: none;
color: white;
border: 2px solid white;
padding: 4px 103px;
padding-right: 206px;
font-size: 2rem;
border-radius: 20px;
cursor: pointer;
vertical-align:middle;
display: inline-block;
}
.chemistry{
margin-left: 24%;
margin-top: 41px;
}
.chemistry a{
text-decoration: none;
color: white;
border: 2px solid white;
padding: 4px;
padding-right: 169px;
font-size: 2rem;
border-radius: 20px;
cursor: pointer;
vertical-align:middle;
display: inline-block;
}
<div class="title">
<h1>Syllabus</h1>
</div>
<div class="subjectsList">
<div class="subjects">
Syllabus
<span class="triangle"></span>
</div>
<div class="maths">
<a href="mathsinfo.html" target="_blank">
<!--<img src="images/maths.jpg" height="50px" width="50px" alt="Mathematics Icon">-->
Mathematics</a>
</div>
<div class="physics">
Physics
</div>
<div class="chemistry">
Chemistry
</div>
</div>
that physics text must be at middle.Can anyone help me out to solve this problem.
Just change the padding (left and right padding to half of what you had for right padding):
.physics{
margin-left: 24%;
margin-top: 41px;
}
.physics a{
text-decoration: none;
color: black;
border: 2px solid black;
padding: 4px 103px;
font-size: 2rem;
border-radius: 20px;
cursor: pointer;
vertical-align:middle;
display: inline-block;
}
<div class="physics">
Physics
</div>
remove padding-right:206px; add text-align:center
.physics a{
text-decoration: none;
color: black;
border: 2px solid black;
padding: 4px 0px;
font-size: 2rem;
border-radius: 20px;
cursor: pointer;
vertical-align:middle;
display: inline-block;
text-align:center;
}
remove margin-left from .physicsc and padding-right from .physics a and set
.physics {
text-align: center;
}
.physics a {
width:30%; // or anything you need
}
Also check out centering guide.
how to make html button like this picture?
two line text.vertical centered.
here is my code
.btn {
display: block;
background: #ffffff;
box-shadow: none;
border: 1px solid #CECECE;
border-radius: 4px;
margin-bottom: 5px;
color: #19d197;
}
.bz-fa-icon {
margin-right: 5px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<button class="btn"><i class="fa fa-check bz-fa-icon"></i>
Confirm<br>
Return
</button>
Change some CSS
.btn {
background: #ffffff none repeat scroll 0 0;
border: 1px solid #cecece;
border-radius: 4px;
box-shadow: none;
color: #19d197;
display: block;
margin-bottom: 5px;
padding: 0 10px 0 30px;
position: relative;
}
.bz-fa-icon {
left: 5px;
margin-right: 5px;
position: absolute;
top: 50%;
transform: translateY(-50%);
vertical-align: middle;
}
https://jsfiddle.net/8kwyjsho/
Try this:
You can also wrap the text and span inside a div then wrap that div and img inside another div then add these classes:
also add display: inline-block; on your img
.center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.btnText {
vertical-align: middle;
display: inline-block;
}
body {
padding: 20px;
}
.button {
background: #000;
color: #fff;
text-decoration: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
height: 120px;
padding: 30px 50px;
display: inline-block;
}
span {
font-size: 11px;
color: #ccc;;
display: block;
}
img {
vertical-align: middle;
display: inline-block;
}
.center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.btnText {
vertical-align: middle;
display: inline-block;
}
<a class="button" href="">
<div class="center">
<img src="http://dummyimage.com/30x30/cf97cf/fff">
<div class="btnText">
Button
<span>Button alt</span>
</div>
</div>
</a>
Change to this code.
.btn {
width:80px;
height:40px;
display: block;
background: #ffffff;
box-shadow: none;
border: 1px solid #CECECE;
border-radius: 4px;
margin-bottom: 5px;
color: #19d197;
}
.bz-fa-icon {
display: block;
float:left;
margin-top:9px;
margin-right: 5px;
}
A flexbox option:
.btn {
display: inline-flex;
align-items: center; /* vertical alignment of items */
line-height: 16px;
padding: 5px 13px;
background: none;
border: 1px solid #CECECE;
border-radius: 3px;
color: #19d197;
}
.inner {
text-align: left;
padding-left: 7px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<button class="btn"><i class="fa fa-check bz-fa-icon"></i>
<div class="inner">Confirm <br /> Return</div>
</button>