垂直对齐图片和文本

时间:2016-06-16 14:19:48

标签: html css

我试图让我的照片(21x21像素)和我的文字(14像素)在右侧以垂直方向居中,但只要我将照片放入其中,<a>就会变大......我怎么能做得对吗?

班级“aPctr”只是给照片21x21px

继承我的代码:https://jsfiddle.net/vc0h9Ldb/

    <div id="cssmenu">
    <ul class="_mrgA">
        <li>
            <a href="#">Home</a>
        </li>
        <li class="active has-sub">
            <span class="submenu-button"></span>
            <a href="#">Products</a>
            <ul>
                <li class="has-sub">
                    <span class="submenu-button"></span>
                    <a href="#">Product 1</a>
                    <ul>
                        <li>
                            <a href="#">Sub Product</a>
                        </li>
                        <li>
                            <a href="#">Sub Product</a>
                        </li>
                    </ul>
                </li>
                <li class="has-sub">
                    <span class="submenu-button"></span>
                    <a href="#">Product 2</a>
                    <ul>
                        <li>
                            <a href="#">Sub Product</a>
                        </li>
                        <li>
                            <a href="#">Sub Product</a>
                        </li>
                    </ul>
                </li>

            </ul>
        </li>
        <li>
            <a href="#">About</a>
        </li>
        <li>
            <a href="#">Contact</a>
        </li>
        <li class="account-lnk" style="float: right; height: 0;">
            <a href="#">
                <img src="img/autor.jpg" class="_aPctr">
                <span> Name</span>
            </a>
        </li>
    </ul>
</div>

4 个答案:

答案 0 :(得分:0)

只需更改最后一个li中的填充,它就能正常工作

#cssmenu > ul > li > a
{
padding: 15px 20px;
}

代码段也发布在

下面

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#cssmenu:after,
#cssmenu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
#cssmenu #menu-button {
  display: none;
}
#cssmenu {
  font-family: "Lato",sans-serif;
  line-height: 1;
  top: 0;
  background: #ffffff;
  width: auto;
  position:fixed;
  width:100%;
  z-index:100;
}

#cssmenu > ul > li {
  float: left;
}
#cssmenu.align-center > ul {
  font-size: 0;
  text-align: center;
}
#cssmenu.align-center > ul > li {
  display: inline-block;
  float: none;
}
#cssmenu.align-center ul ul {
  text-align: left;
}
#cssmenu.align-right > ul > li {
  float: right;
}
#cssmenu.align-right ul ul {
  text-align: right;
}
#cssmenu > ul > li > a {
  padding: 15px 20px;
  font-size: 12px;
  text-decoration: none;
  text-transform: uppercase;
  color: #000000;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#cssmenu > ul > li:hover > a,
#cssmenu > ul > li.active > a {
  background-color: #00D4EB;
}
#cssmenu > ul > li.has-sub > a {
  padding-right: 25px;
}
#cssmenu > ul > li.has-sub > a::after {
  position: absolute;
  top: 21px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #000000;
  border-right: 1px solid #000000;
  content: "";
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#cssmenu > ul > li.has-sub:hover > a::after {
  border-color: #009ae1;
}
#cssmenu ul ul {
  position: absolute;
  left: -9999px;
}
#cssmenu li:hover > ul {
  left: auto;
}
#cssmenu.align-right li:hover > ul {
  right: 0;
}
#cssmenu ul ul ul {
  margin-left: 100%;
  top: 0;
}
#cssmenu.align-right ul ul ul {
  margin-left: 0;
  margin-right: 100%;
}
#cssmenu ul ul li {
  height: 0;
  -webkit-transition: height .2s ease;
  -moz-transition: height .2s ease;
  -ms-transition: height .2s ease;
  -o-transition: height .2s ease;
  transition: height .2s ease;
}
#cssmenu ul li:hover > ul > li {
  height: 32px;
}
#cssmenu ul ul li a {
  padding: 10px 20px;
  width: 160px;
  font-size: 12px;
  background: #333333;
  text-decoration: none;
  color: #dddddd;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#cssmenu ul ul li:hover > a,
#cssmenu ul ul li a:hover {
  color: #ffffff;
}
#cssmenu ul ul li.has-sub > a::after {
  position: absolute;
  top: 13px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  content: "";
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#cssmenu.align-right ul ul li.has-sub > a::after {
  right: auto;
  left: 10px;
  border-bottom: 0;
  border-right: 0;
  border-top: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
}
<div id="cssmenu">
        <ul class="_mrgA">
            <li>
                <a href="#">Home</a>
            </li>
            <li class="active has-sub">
                <span class="submenu-button"></span>
                <a href="#">Products</a>
                <ul>
                    <li class="has-sub">
                        <span class="submenu-button"></span>
                        <a href="#">Product 1</a>
                        <ul>
                            <li>
                                <a href="#">Sub Product</a>
                            </li>
                            <li>
                                <a href="#">Sub Product</a>
                            </li>
                        </ul>
                    </li>
                    <li class="has-sub">
                        <span class="submenu-button"></span>
                        <a href="#">Product 2</a>
                        <ul>
                            <li>
                                <a href="#">Sub Product</a>
                            </li>
                            <li>
                                <a href="#">Sub Product</a>
                            </li>
                        </ul>
                    </li>
                    
                </ul>
            </li>
            <li>
                <a href="#">About</a>
            </li>
            <li>
                <a href="#">Contact</a>
            </li>
            <li class="account-lnk" style="float: right; height: 0;">
                <a href="#">
                    <img src="img/autor.jpg" class="_aPctr">
                    <span> Name</span>
                </a>
            </li>
        </ul>
    </div>

答案 1 :(得分:0)

请使用此功能,对您有帮助......

06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4165adb8)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA: ACRA caught a NullPointerException for com.ofekagmon.apps.healthchecker
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA: java.lang.NullPointerException
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at com.ofekagmon.apps.healthchecker.ui.activities.HomeActivity.startService(HomeActivity.java:47)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at com.ofekagmon.apps.healthchecker.ui.activities.HomeActivity$$ViewBinder$1.doClick(HomeActivity$$ViewBinder.java:24)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.view.View.performClick(View.java:4445)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.view.View$PerformClick.run(View.java:18446)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.os.Handler.handleCallback(Handler.java:733)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.os.Looper.loop(Looper.java:136)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at android.app.ActivityThread.main(ActivityThread.java:5146)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at java.lang.reflect.Method.invoke(Method.java:515)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
06-23 18:47:06.493 24779-24779/com.ofekagmon.apps.healthchecker E/ACRA:     at dalvik.system.NativeStart.main(Native Method)
06-23 18:47:08.603 24779-25247/? I/Process: Sending signal. PID: 24779 SIG: 9

答案 2 :(得分:0)

问题在于最后一个标签的填充与其他标签的使用相同,并且它的内容超出了高度。

<li class="account-lnk" style="float: right; height: 0;">
    <a href="#">
        <img src="img/autor.jpg" class="_aPctr">
        <span> Name</span>
    </a>
</li>

例如,您可以将其更改为:

<li class="account-lnk" style="float: right; height: 0;">
    <a href="#" style="padding:16px;">
        <img src="img/autor.jpg" class="_aPctr">
        <span> Name</span>
    </a>
</li>

确定CSS取决于你

答案 3 :(得分:0)

您可以在img上应用vertical-align: middle;。然后它看起来像这样:

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul li a,
#cssmenu #menu-button {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  line-height: 1;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#cssmenu:after,
#cssmenu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}
#cssmenu #menu-button {
  display: none;
}
#cssmenu {
  font-family: "Lato",sans-serif;
  line-height: 1;
  top: 0;
  background: #ffffff;
  width: auto;
  position:fixed;
  width:100%;
  z-index:100;
}

#cssmenu > ul > li {
  float: left;
}
#cssmenu.align-center > ul {
  font-size: 0;
  text-align: center;
}
#cssmenu.align-center > ul > li {
  display: inline-block;
  float: none;
}
#cssmenu.align-center ul ul {
  text-align: left;
}
#cssmenu.align-right > ul > li {
  float: right;
}
#cssmenu.align-right ul ul {
  text-align: right;
}
#cssmenu > ul > li > a {
  padding: 20px;
  font-size: 12px;
  text-decoration: none;
  text-transform: uppercase;
  color: #000000;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#cssmenu > ul > li:hover > a,
#cssmenu > ul > li.active > a {
  background-color: #00D4EB;
}
#cssmenu > ul > li.has-sub > a {
  padding-right: 25px;
}
#cssmenu > ul > li.has-sub > a::after {
  position: absolute;
  top: 21px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #000000;
  border-right: 1px solid #000000;
  content: "";
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#cssmenu > ul > li.has-sub:hover > a::after {
  border-color: #009ae1;
}
#cssmenu ul ul {
  position: absolute;
  left: -9999px;
}
#cssmenu li:hover > ul {
  left: auto;
}
#cssmenu.align-right li:hover > ul {
  right: 0;
}
#cssmenu ul ul ul {
  margin-left: 100%;
  top: 0;
}
#cssmenu.align-right ul ul ul {
  margin-left: 0;
  margin-right: 100%;
}
#cssmenu ul ul li {
  height: 0;
  -webkit-transition: height .2s ease;
  -moz-transition: height .2s ease;
  -ms-transition: height .2s ease;
  -o-transition: height .2s ease;
  transition: height .2s ease;
}
#cssmenu ul li:hover > ul > li {
  height: 32px;
}
#cssmenu ul ul li a {
  padding: 10px 20px;
  width: 160px;
  font-size: 12px;
  background: #333333;
  text-decoration: none;
  color: #dddddd;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#cssmenu ul ul li:hover > a,
#cssmenu ul ul li a:hover {
  color: #ffffff;
}
#cssmenu ul ul li.has-sub > a::after {
  position: absolute;
  top: 13px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #dddddd;
  border-right: 1px solid #dddddd;
  content: "";
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#cssmenu.align-right ul ul li.has-sub > a::after {
  right: auto;
  left: 10px;
  border-bottom: 0;
  border-right: 0;
  border-top: 1px solid #dddddd;
  border-left: 1px solid #dddddd;
}
#img-vertical{
  vertical-align:middle;
}
  
	<div id="cssmenu">
		<ul class="_mrgA">
			<li>
				<a href="#">Home</a>
			</li>
			<li class="active has-sub">
				<span class="submenu-button"></span>
				<a href="#">Products</a>
				<ul>
					<li class="has-sub">
						<span class="submenu-button"></span>
						<a href="#">Product 1</a>
						<ul>
							<li>
								<a href="#">Sub Product</a>
							</li>
							<li>
								<a href="#">Sub Product</a>
							</li>
						</ul>
					</li>
					<li class="has-sub">
						<span class="submenu-button"></span>
						<a href="#">Product 2</a>
						<ul>
							<li>
								<a href="#">Sub Product</a>
							</li>
							<li>
								<a href="#">Sub Product</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</li>
			<li>
				<a href="#">About</a>
			</li>
			<li>
				<a href="#">Contact</a>
			</li>
			<li class="account-lnk" style="float: right; height: 0;">
				<a href="#">
					<img id="img-vertical" src="img/autor.jpg" class="_aPctr">
					<span> Name</span>
				</a>
			</li>
		</ul>
	</div>

我从你的小提琴中取出了这个演示的CSS。